Skip to content

Commit 9568dba

Browse files
authored
Merge pull request #1 from humanmade/claude/fix-pages-per-post-override-011CUti1Vgq8uT4uGcAoVPCy
Fix pages per post override display
2 parents 13ff452 + 5e73569 commit 9568dba

File tree

15 files changed

+5264
-2046
lines changed

15 files changed

+5264
-2046
lines changed

.github/workflows/build-and-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ jobs:
3030
release_branch: release
3131
built_asset_paths: build
3232
build_script: |
33-
npm ci
33+
npm i
3434
npm run build
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Playwright Tests
2+
3+
on:
4+
push:
5+
branches: [ main, master, develop ]
6+
pull_request:
7+
branches: [ main, master, develop ]
8+
9+
jobs:
10+
test:
11+
timeout-minutes: 60
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
pull-requests: write
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: 24
25+
cache: 'npm'
26+
27+
- name: Install dependencies
28+
run: npm i
29+
30+
- name: Build plugin
31+
run: npm run build
32+
33+
- name: Install Playwright Browsers
34+
run: npx playwright install --with-deps chromium
35+
36+
- name: Start WordPress environment
37+
run: npm run wp-env start
38+
39+
- name: Wait for WordPress to be ready
40+
run: |
41+
timeout 60 bash -c 'until curl -sSf http://localhost:8889 > /dev/null 2>&1; do sleep 2; done' || echo "WordPress may not be fully ready"
42+
sleep 5
43+
44+
- name: Debug - Check WordPress status
45+
run: |
46+
curl -I http://localhost:8889 || echo "WordPress not responding"
47+
docker ps
48+
49+
- name: Run Playwright tests
50+
id: tests
51+
continue-on-error: true
52+
run: npm run test:e2e
53+
env:
54+
CI: true
55+
56+
- name: Stop WordPress environment
57+
if: always()
58+
run: npm run wp-env stop
59+
60+
- name: Upload test results
61+
uses: actions/upload-artifact@v4
62+
if: always()
63+
with:
64+
name: playwright-report
65+
path: playwright-report/
66+
retention-days: 30
67+
if-no-files-found: ignore
68+
69+
- name: Upload test videos and traces
70+
uses: actions/upload-artifact@v4
71+
if: failure()
72+
with:
73+
name: test-results
74+
path: test-results/
75+
retention-days: 7
76+
if-no-files-found: ignore
77+
78+
- name: Comment PR with test results
79+
uses: daun/playwright-report-comment@v3
80+
if: always() && github.event_name == 'pull_request'
81+
with:
82+
report-file: test-results/results.json
83+
84+
- name: Fail if tests failed
85+
if: steps.tests.outcome == 'failure'
86+
run: exit 1

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ Thumbs.db
2020
*.swo
2121
*~
2222

23+
# WordPress environment
24+
.wp-env/
25+
26+
# Test artifacts
27+
.auth/
28+
test-results/
29+
playwright-report/
30+
playwright/.cache/
31+
2332
/vendor/

.wp-env.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"core": "WordPress/WordPress#6.7.1",
3+
"plugins": [
4+
"."
5+
],
6+
"themes": [
7+
"WordPress/twentytwentyfour",
8+
"WordPress/twentytwentyfive"
9+
],
10+
"port": 8888,
11+
"testsPort": 8889,
12+
"config": {
13+
"WP_DEBUG": true,
14+
"SCRIPT_DEBUG": true
15+
},
16+
"env": {
17+
"tests": {
18+
"themes": [
19+
"WordPress/twentytwentyfour",
20+
"WordPress/twentytwentyfive"
21+
]
22+
}
23+
}
24+
}

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ A WordPress plugin that extends the core Query Loop block with advanced controls
88

99
When a Query Loop block is set to "Inherit query from template", you can now override the number of posts to display. This is useful when you want to show a different number of posts than the main query. Leave the field empty to use the default number of posts.
1010

11+
**Editor Preview**: The posts per page override is now reflected in the editor preview, making it easier to see how your content will appear without needing to preview or publish the page.
12+
1113
### 2. Hide on Paginated Pages
1214

1315
Toggle whether the query loop should be hidden when viewing page 2 or higher (when the `paged` query var is greater than 1). This is useful for creating layouts where different query loops appear on the first page vs subsequent pages.
@@ -35,6 +37,31 @@ Enable this option to automatically exclude posts that have been displayed by pr
3537
- `npm run lint:css` - Lint CSS/SCSS files
3638
- `npm run format` - Format all files
3739

40+
### Testing
41+
42+
The plugin includes end-to-end tests using Playwright and `@wordpress/scripts`.
43+
44+
#### Running Tests
45+
46+
1. Start the WordPress test environment:
47+
```bash
48+
npm run wp-env start
49+
```
50+
51+
2. Run the tests:
52+
```bash
53+
npm run test:e2e
54+
```
55+
56+
#### Additional Test Commands
57+
58+
- `npm run test:e2e:debug` - Run tests in debug mode
59+
- `npm run test:e2e:watch` - Run tests in watch mode (reruns on changes)
60+
- `npm run wp-env stop` - Stop the WordPress environment
61+
- `npm run wp-env` - Access wp-env commands directly
62+
63+
See [tests/e2e/README.md](tests/e2e/README.md) for more details on the test setup and writing tests.
64+
3865
## Usage
3966

4067
1. Add a Query Loop block to your page or template

hm-query-loop.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
function init() {
3131
// Enqueue block editor assets.
32-
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets' );
32+
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets', 9 );
3333

3434
// Register block filters.
3535
add_filter( 'pre_render_block', __NAMESPACE__ . '\\pre_render_block', 10, 3 );
@@ -41,8 +41,8 @@ function init() {
4141
// Hook into the_posts to track displayed posts.
4242
add_filter( 'the_posts', __NAMESPACE__ . '\\track_displayed_posts', 10, 2 );
4343

44-
// Add context to post-template block.
45-
add_filter( 'block_type_metadata', __NAMESPACE__ . '\\filter_post_template_metadata' );
44+
// Add contexts to query and post-template block.
45+
add_filter( 'block_type_metadata', __NAMESPACE__ . '\\filter_block_metadata' );
4646
}
4747

4848
add_action( 'init', __NAMESPACE__ . '\\init' );
@@ -61,6 +61,15 @@ function enqueue_block_editor_assets() {
6161
true
6262
);
6363

64+
// Pass the posts_per_page setting to JavaScript
65+
wp_localize_script(
66+
'hm-query-loop-editor',
67+
'hmQueryLoopSettings',
68+
[
69+
'postsPerPage' => (int) get_option( 'posts_per_page', 10 ),
70+
]
71+
);
72+
6473
wp_enqueue_style(
6574
'hm-query-loop-editor',
6675
HM_QUERY_LOOP_URL . 'build/index.css',
@@ -70,20 +79,25 @@ function enqueue_block_editor_assets() {
7079
}
7180

7281
/**
73-
* Filter post-template block metadata to add usesContext.
82+
* Filter block metadata to add context.
7483
*
7584
* @param array $metadata Block metadata.
7685
* @return array Modified metadata.
7786
*/
78-
function filter_post_template_metadata( $metadata ) {
79-
if ( $metadata['name'] !== 'core/post-template' ) {
80-
return $metadata;
87+
function filter_block_metadata( $metadata ) {
88+
if ( $metadata['name'] === 'core/query' ) {
89+
$metadata['providesContext'] = array_merge(
90+
$metadata['providesContext'] ?? [],
91+
[ 'hm-query-loop/settings' ]
92+
);
8193
}
8294

83-
$metadata['usesContext'] = array_merge(
84-
$metadata['usesContext'] ?? [],
85-
[ 'hm-query-loop/settings' ]
86-
);
95+
if ( $metadata['name'] === 'core/post-template' ) {
96+
$metadata['usesContext'] = array_merge(
97+
$metadata['usesContext'] ?? [],
98+
[ 'hm-query-loop/settings' ]
99+
);
100+
}
87101

88102
return $metadata;
89103
}

0 commit comments

Comments
 (0)