Skip to content

Commit 7cb0a37

Browse files
authored
Merge pull request #153 from ryanwelcher/fix/exclude-current-post
Fix Exclude current post for synced patterns and template.
2 parents 8c16ddf + 8e07cca commit 7cb0a37

File tree

6 files changed

+382
-18
lines changed

6 files changed

+382
-18
lines changed

includes/Traits/Exclude_Current.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,35 @@ public function process_exclude_current(): void {
2020
/**
2121
* Helper to generate the array
2222
*
23-
* @param mixed $to_exclude The value to be excluded.
23+
* @param int $exclude_current_post The value to be excluded.
2424
*
2525
* @return array The ids to exclude
2626
*/
27-
public function get_exclude_ids( $to_exclude ) {
27+
public function get_exclude_ids( $exclude_current_post ) {
2828
// If there are already posts to be excluded, we need to add to them.
29-
$exclude_ids = $this->custom_args['post__not_in'] ?? array();
30-
31-
if ( $this->is_post_id( $to_exclude ) ) {
32-
array_push( $exclude_ids, intval( $to_exclude ) );
29+
$exclude_ids = $this->custom_args['post__not_in'] ?? array();
30+
$post_to_exclude = 0;
31+
if ( true !== $exclude_current_post && is_numeric( $exclude_current_post ) && $exclude_current_post >= 1 ) {
32+
$post_to_exclude = intval( $exclude_current_post );
3333
} else {
34-
// This is usually when this was set on a template.
35-
global $post;
36-
if ( $post ) {
37-
array_push( $exclude_ids, $post->ID );
34+
// Try to get the queried object ID (frontend context)
35+
if ( function_exists( 'get_queried_object_id' ) ) {
36+
$post_to_exclude = get_queried_object_id();
37+
}
38+
39+
// Fallback to global $post (editor, unit tests, etc.)
40+
if ( ! $post_to_exclude ) {
41+
global $post;
42+
if ( $post && isset( $post->ID ) ) {
43+
$post_to_exclude = $post->ID;
44+
}
3845
}
3946
}
47+
48+
if ( $post_to_exclude > 0 ) {
49+
array_push( $exclude_ids, intval( $post_to_exclude ) );
50+
}
51+
4052
return $exclude_ids;
4153
}
4254
}

src/components/post-exclude-controls.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ const ExcludeCurrentPostControl = ( {
9191

9292
const isDisabled = () => {
9393
// If the user is not an admin, they cannot edit template anyway
94-
if ( ! isAdmin ) {
94+
if ( ! isAdmin || ! currentPost ) {
9595
return false;
9696
}
97+
98+
// Only disable if we're editing a template AND it's in the list
99+
if ( currentPost.type !== 'wp_template' ) {
100+
return false;
101+
}
102+
97103
const templatesToExclude = [ 'archive', 'search' ];
98104
const {
99105
show_on_front: showOnFront, // What is the front page set to show? Options: 'posts' or 'page'
@@ -102,10 +108,7 @@ const ExcludeCurrentPostControl = ( {
102108
...templatesToExclude,
103109
...( showOnFront === 'posts' ? [ 'home', 'front-page' ] : [] ),
104110
];
105-
return (
106-
currentPost.type === 'wp_template' &&
107-
disabledTemplates.includes( currentPost.slug )
108-
);
111+
return disabledTemplates.includes( currentPost.slug );
109112
};
110113

111114
return (
@@ -118,7 +121,7 @@ const ExcludeCurrentPostControl = ( {
118121
setAttributes( {
119122
query: {
120123
...attributes.query,
121-
exclude_current: value ? currentPost.id : 0,
124+
exclude_current: value,
122125
},
123126
} );
124127
} }

tests/e2e/Playground.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class Playground {
3030
},
3131
],
3232
blueprint,
33-
port: 8889,
33+
port: 9876,
3434
quiet: true,
3535
} );
3636
this.handler = this.cliServer.requestHandler;

tests/e2e/playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineConfig( {
1818
use: {
1919
/* Collect trace when retrying the failed test. */
2020
trace: 'on-first-retry',
21-
baseURL: 'http://127.0.0.1:8889/',
21+
baseURL: 'http://127.0.0.1:9876/',
2222
},
2323

2424
/* Configure projects for major browsers */
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Exclude Current Post E2E Tests
2+
3+
This test suite covers the "Exclude Current Post" control functionality in the Advanced Query Loop block.
4+
5+
## Test Coverage
6+
7+
### ✅ Tests for Regular Posts
8+
9+
1. **Initial state** - Verifies the control is visible and unchecked by default
10+
2. **Toggle on** - Verifies toggling the control on stores `true` in block attributes
11+
3. **Toggle off** - Verifies toggling the control off stores `false` in block attributes
12+
4. **Not disabled** - Verifies the control is enabled in regular posts
13+
14+
## Running the Tests
15+
16+
```bash
17+
# Run all exclude current post tests
18+
npm run test:e2e -- tests/exclude-current-post.spec.ts
19+
20+
# Run with UI
21+
npm run test:e2e:ui -- tests/exclude-current-post.spec.ts
22+
```
23+
24+
## Future Test Additions
25+
26+
The following test scenarios were planned but require additional setup/configuration:
27+
28+
### Templates
29+
- Should be disabled in archive template
30+
- Should be disabled in search template
31+
- Should be disabled in home/front-page templates (when show_on_front is 'posts')
32+
- Should be enabled in single template
33+
- Should work in single template and store boolean value
34+
35+
### Synced Patterns
36+
- Should be visible and functional in a synced pattern
37+
- Synced pattern with exclude current should work when inserted in a post
38+
39+
These tests require:
40+
- Proper theme configuration in the test environment
41+
- Site Editor navigation that works reliably with Playground
42+
- Pattern creation workflow that's compatible with the test environment
43+
44+
## Test Structure
45+
46+
All tests follow the same pattern:
47+
1. Initialize Playground with blueprint
48+
2. Visit post editor
49+
3. Insert AQL block with custom query
50+
4. Interact with "Exclude Current Post" control
51+
5. Assert expected behavior
52+
6. Clean up Playground instance
53+
54+
## Notes
55+
56+
- Tests use WordPress Playground via `@wp-playground/cli` for isolated testing
57+
- Each test gets a fresh WordPress instance
58+
- The `insertAQL` utility handles block insertion and variation selection
59+
- Tests verify both UI state and block attributes

0 commit comments

Comments
 (0)