Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions includes/Traits/Exclude_Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,35 @@ public function process_exclude_current(): void {
/**
* Helper to generate the array
*
* @param mixed $to_exclude The value to be excluded.
* @param int $exclude_current_post The value to be excluded.
*
* @return array The ids to exclude
*/
public function get_exclude_ids( $to_exclude ) {
public function get_exclude_ids( $exclude_current_post ) {
// If there are already posts to be excluded, we need to add to them.
$exclude_ids = $this->custom_args['post__not_in'] ?? array();

if ( $this->is_post_id( $to_exclude ) ) {
array_push( $exclude_ids, intval( $to_exclude ) );
$exclude_ids = $this->custom_args['post__not_in'] ?? array();
$post_to_exclude = 0;
if ( true !== $exclude_current_post && is_numeric( $exclude_current_post ) && $exclude_current_post >= 1 ) {
$post_to_exclude = intval( $exclude_current_post );
} else {
// This is usually when this was set on a template.
global $post;
if ( $post ) {
array_push( $exclude_ids, $post->ID );
// Try to get the queried object ID (frontend context)
if ( function_exists( 'get_queried_object_id' ) ) {
$post_to_exclude = get_queried_object_id();
}

// Fallback to global $post (editor, unit tests, etc.)
if ( ! $post_to_exclude ) {
global $post;
if ( $post && isset( $post->ID ) ) {
$post_to_exclude = $post->ID;
}
}
}

if ( $post_to_exclude > 0 ) {
array_push( $exclude_ids, intval( $post_to_exclude ) );
}

return $exclude_ids;
}
}
15 changes: 9 additions & 6 deletions src/components/post-exclude-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ const ExcludeCurrentPostControl = ( {

const isDisabled = () => {
// If the user is not an admin, they cannot edit template anyway
if ( ! isAdmin ) {
if ( ! isAdmin || ! currentPost ) {
return false;
}

// Only disable if we're editing a template AND it's in the list
if ( currentPost.type !== 'wp_template' ) {
return false;
}

const templatesToExclude = [ 'archive', 'search' ];
const {
show_on_front: showOnFront, // What is the front page set to show? Options: 'posts' or 'page'
Expand All @@ -102,10 +108,7 @@ const ExcludeCurrentPostControl = ( {
...templatesToExclude,
...( showOnFront === 'posts' ? [ 'home', 'front-page' ] : [] ),
];
return (
currentPost.type === 'wp_template' &&
disabledTemplates.includes( currentPost.slug )
);
return disabledTemplates.includes( currentPost.slug );
};

return (
Expand All @@ -118,7 +121,7 @@ const ExcludeCurrentPostControl = ( {
setAttributes( {
query: {
...attributes.query,
exclude_current: value ? currentPost.id : 0,
exclude_current: value,
},
} );
} }
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/Playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Playground {
},
],
blueprint,
port: 8889,
port: 9876,
quiet: true,
} );
this.handler = this.cliServer.requestHandler;
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig( {
use: {
/* Collect trace when retrying the failed test. */
trace: 'on-first-retry',
baseURL: 'http://127.0.0.1:8889/',
baseURL: 'http://127.0.0.1:9876/',
},

/* Configure projects for major browsers */
Expand Down
59 changes: 59 additions & 0 deletions tests/e2e/tests/README-exclude-current-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Exclude Current Post E2E Tests

This test suite covers the "Exclude Current Post" control functionality in the Advanced Query Loop block.

## Test Coverage

### ✅ Tests for Regular Posts

1. **Initial state** - Verifies the control is visible and unchecked by default
2. **Toggle on** - Verifies toggling the control on stores `true` in block attributes
3. **Toggle off** - Verifies toggling the control off stores `false` in block attributes
4. **Not disabled** - Verifies the control is enabled in regular posts

## Running the Tests

```bash
# Run all exclude current post tests
npm run test:e2e -- tests/exclude-current-post.spec.ts

# Run with UI
npm run test:e2e:ui -- tests/exclude-current-post.spec.ts
```

## Future Test Additions

The following test scenarios were planned but require additional setup/configuration:

### Templates
- Should be disabled in archive template
- Should be disabled in search template
- Should be disabled in home/front-page templates (when show_on_front is 'posts')
- Should be enabled in single template
- Should work in single template and store boolean value

### Synced Patterns
- Should be visible and functional in a synced pattern
- Synced pattern with exclude current should work when inserted in a post

These tests require:
- Proper theme configuration in the test environment
- Site Editor navigation that works reliably with Playground
- Pattern creation workflow that's compatible with the test environment

## Test Structure

All tests follow the same pattern:
1. Initialize Playground with blueprint
2. Visit post editor
3. Insert AQL block with custom query
4. Interact with "Exclude Current Post" control
5. Assert expected behavior
6. Clean up Playground instance

## Notes

- Tests use WordPress Playground via `@wp-playground/cli` for isolated testing
- Each test gets a fresh WordPress instance
- The `insertAQL` utility handles block insertion and variation selection
- Tests verify both UI state and block attributes
Loading
Loading