Skip to content

Commit 8c16ddf

Browse files
authored
Merge pull request #154 from ryanwelcher/feature/add-context-to-components
Add current post ID and type to context
2 parents 7fee57b + 729b857 commit 8c16ddf

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

CLAUDE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Each trait in the Traits directory handles a specific query modification:
9494
- Conditionally renders different control sets based on `query.inherit` attribute
9595
- When `inherit: false` - Shows all advanced controls in the "Advanced Query Settings" panel
9696
- When `inherit: true` - Shows limited controls (only PostOrderControls and inherited query slot)
97+
- Builds `propsWithControls` passed to every component; extends the original block props with:
98+
- `allowedControls` — array of permitted control keys
99+
- `context.currentPostId` — numeric ID of the currently edited post/page (`0` in template context)
100+
- `context.currentPostType` — post type string, e.g. `'post'`, `'page'`, `'wp_template'`
97101

98102
**UI Components** (`src/components/`):
99103
Each component corresponds to a query feature:

extending-aql.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,36 @@ For example, a control that makes changes to the content types being displayed m
1414

1515
Both SlotFills are passed all `props` from the main block and are available on the `window.aql` object for use.
1616

17-
The example below adds a new control to only show content from the from currently logged in user regardless of the status of `Inherit query from template`.
17+
##### Available props
18+
19+
In addition to the standard block props (`attributes`, `setAttributes`, `clientId`, etc.), every component receives an enriched `context` object with the following values:
20+
21+
| Property | Type | Description |
22+
|---|---|---|
23+
| `context.currentPostId` | `number` | The ID of the post or page currently open in the editor. Returns `0` when editing a template. |
24+
| `context.currentPostType` | `string` | The post type of the currently edited entity, e.g. `'post'`, `'page'`, or `'wp_template'`. |
25+
26+
Any other context values that `core/query` already receives from its parent are also preserved.
27+
28+
```js
29+
const MyControl = ( { attributes, setAttributes, context } ) => {
30+
const { currentPostId, currentPostType } = context;
31+
// currentPostId: e.g. 42
32+
// currentPostType: e.g. 'post'
33+
};
34+
```
35+
36+
The example below adds a new control to only show content from the currently logged in user regardless of the status of `Inherit query from template`.
1837

1938
```js
2039
const { AQLControls, AQLControlsInheritedQuery } = window.aql;
2140
import { registerPlugin } from '@wordpress/plugins';
2241
import { ToggleControl } from '@wordpress/components';
2342
import { __ } from '@wordpress/i18n';
2443

25-
const LoggedInUserControl = ( { attributes, setAttributes } ) => {
44+
const LoggedInUserControl = ( { attributes, setAttributes, context } ) => {
2645
const { query: { authorContent = false } = {} } = attributes;
46+
const { currentPostId, currentPostType } = context;
2747
return (
2848
<>
2949
<ToggleControl
@@ -137,8 +157,9 @@ import { registerPlugin } from '@wordpress/plugins';
137157
import { ToggleControl } from '@wordpress/components';
138158
import { __ } from '@wordpress/i18n';
139159
140-
const LoggedInUserControl = ( { attributes, setAttributes } ) => {
160+
const LoggedInUserControl = ( { attributes, setAttributes, context } ) => {
141161
const { query: { authorContent = false } = {} } = attributes;
162+
const { currentPostId, currentPostType } = context;
142163
return (
143164
<>
144165
<ToggleControl

src/variations/controls.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*/
55
import { addFilter } from '@wordpress/hooks';
66
import { InspectorControls } from '@wordpress/block-editor';
7+
import { useSelect } from '@wordpress/data';
78
import {
89
PanelBody,
910
__experimentalVStack as VStack,
1011
BaseControl,
1112
} from '@wordpress/components';
1213
import { __ } from '@wordpress/i18n';
1314
import { createBlock } from '@wordpress/blocks';
15+
import { store as editorStore } from '@wordpress/editor';
1416

1517
/**
1618
* Internal dependencies
@@ -50,6 +52,23 @@ const isAdvancedQueryLoop = ( props ) => {
5052
* @return {Element} BlockEdit instance
5153
*/
5254
const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => {
55+
const { currentPostId, currentPostType } = useSelect(
56+
( select ) => {
57+
if ( ! isAdvancedQueryLoop( props ) ) {
58+
return {
59+
currentPostId: undefined,
60+
currentPostType: undefined,
61+
};
62+
}
63+
const editor = select( editorStore );
64+
return {
65+
currentPostId: editor.getCurrentPostId(),
66+
currentPostType: editor.getCurrentPostType(),
67+
};
68+
},
69+
[ props?.attributes?.namespace ]
70+
);
71+
5372
// If the is the correct variation, add the custom controls.
5473
if ( isAdvancedQueryLoop( props ) ) {
5574
const { allowedControls } = window?.aql;
@@ -58,6 +77,11 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => {
5877
const propsWithControls = {
5978
...props,
6079
allowedControls: allowedControlsArray,
80+
context: {
81+
...props.context,
82+
currentPostId,
83+
currentPostType,
84+
},
6185
};
6286
// If the inherit prop is false or undefined, add all the controls.
6387
if ( ! attributes.query.inherit ) {

0 commit comments

Comments
 (0)