Skip to content

Commit

Permalink
Merge pull request #1596 from tomusborne/tweak/dynamic-tags-filter-re…
Browse files Browse the repository at this point in the history
…placeTags

Elements preview support, Query improvements
  • Loading branch information
tomusborne authored Dec 17, 2024
2 parents c77dbdf + 6550650 commit b98717c
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 29 deletions.
31 changes: 27 additions & 4 deletions includes/class-query-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,40 @@ public function get_wp_query( $request ) {
$attributes = $request->get_param( 'attributes' ) ?? [];
$current_post = $request->get_param( 'postId' ) ?? null;
$current_author = $request->get_param( 'authorId' ) ?? null;
$context = $request->get_param( 'context' ) ?? [];
$query_type = $request->get_param( 'queryType' ) ?? GenerateBlocks_Block_Query::TYPE_WP_QUERY;
$current = [
'post_id' => $current_post,
'author_id' => $current_author,
];

/**
* Filter the args for get-wp-query calls before they're passed to get_wp_query_args.
*
* @param array $args The WP_Query args to parse.
* @param array $props Additional filter properties.
*
* @return array $args The optimized WP_Query args array.
*/
$args = apply_filters(
'generateblocks_rest_get_wp_query_args',
$args,
[
'page' => $page,
'attributes' => $attributes,
'context' => $context,
'current' => $current,
'query_type' => $query_type,
]
);

$query = new WP_Query(
self::get_wp_query_args(
$args,
$page,
$attributes,
null,
[
'post_id' => $current_post,
'author_id' => $current_author,
]
$current
)
);

Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@edge22/block-styles": "^1.1.33",
"@edge22/components": "^1.1.48",
"@edge22/components": "^1.1.49",
"@edge22/styles-builder": "^1.2.48",
"@wordpress/block-editor": "^12.12.0",
"@wordpress/blocks": "^12.21.0",
Expand Down
17 changes: 13 additions & 4 deletions src/blocks/looper/components/LoopInnerBlocksRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function BlockPreview( { blocks, isHidden } ) {

const MemoizedBlockPreview = memo( BlockPreview );

function useWpQuery( shouldRequest = true, query, attributes, block ) {
function useWpQuery( shouldRequest = true, { query, attributes, selectedBlock, context, queryType } ) {
const currentPost = useSelect( ( select ) => {
const { getCurrentPost } = select( 'core/editor' );

Expand Down Expand Up @@ -70,6 +70,12 @@ function useWpQuery( shouldRequest = true, query, attributes, block ) {
}
}

const { queryLoopEditorPostsCap = 50 } = generateBlocksEditor;

if ( args.posts_per_page > queryLoopEditorPostsCap ) {
args.posts_per_page = queryLoopEditorPostsCap;
}

async function fetchPosts() {
setIsLoading( true );

Expand All @@ -80,7 +86,9 @@ function useWpQuery( shouldRequest = true, query, attributes, block ) {
data: {
args,
attributes,
block,
context,
queryType,
block: selectedBlock,
postId: currentPost?.id,
authorId: currentPost?.author,
},
Expand All @@ -106,11 +114,12 @@ function useWpQuery( shouldRequest = true, query, attributes, block ) {
export function LoopInnerBlocksRenderer( props ) {
const {
clientId,
context,
attributes,
isSelected,
} = props;

const context = applyFilters( 'generateblocks.editor.preview.context', props.context, { props } );

const {
'generateblocks/query': query = {},
'generateblocks/queryType': queryType = 'WP_Query',
Expand All @@ -123,7 +132,7 @@ export function LoopInnerBlocksRenderer( props ) {
};
const { getSelectedBlock } = useSelect( blockEditorStore );
const selectedBlock = getSelectedBlock();
const wpQuery = useWpQuery( 'WP_Query' === queryType, query, attributes, selectedBlock );
const wpQuery = useWpQuery( 'WP_Query' === queryType, { query, context, queryType, attributes, selectedBlock } );

const otherQuery = applyFilters( 'generateblocks.editor.looper.query', null, {
query,
Expand Down
2 changes: 2 additions & 0 deletions src/blocks/query/components/BlockSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function BlockSettings( {
name,
attributes,
setAttributes,
context,
} ) {
const panelProps = {
name,
Expand All @@ -38,6 +39,7 @@ export function BlockSettings( {
<QueryInspectorControls
attributes={ attributes }
setAttributes={ setAttributes }
context={ context }
/>
</OpenPanel>
<OpenPanel
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/query/components/ControlBuilder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function ControlComponent( props ) {
/>
);
case 'postTypeSelect':
return <SelectPostType { ...standardProps } />;
return <SelectPostType multiple={ true } { ...standardProps } />;
case 'select':
return <ComboboxControl { ...standardProps } />;
case 'multiSelect':
Expand Down
12 changes: 10 additions & 2 deletions src/blocks/query/components/QueryInspectorControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParameterList } from './ParameterList';
import useQueryReducer from '@hooks/useQueryReducer';
import { getParameters } from '../query-parameters';

export function QueryInspectorControls( { attributes, setAttributes } ) {
export function QueryInspectorControls( { attributes, setAttributes, context } ) {
const { queryState, setParameter, removeParameter } = useQueryReducer( attributes.query );
const [ displayParameterSelect, setDisplayParameterSelect ] = useState( false );

Expand Down Expand Up @@ -121,7 +121,15 @@ export function QueryInspectorControls( { attributes, setAttributes } ) {
applyFilters(
'generateblocks.editor.query.inspectorControls',
null,
{ queryType: attributes.queryType, attributes, setAttributes, queryState, setParameter, removeParameter }
{
queryType: attributes.queryType,
attributes,
setAttributes,
queryState,
setParameter,
removeParameter,
context,
}
)
}
</>
Expand Down
3 changes: 2 additions & 1 deletion src/dynamic-tags/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import apiFetch from '@wordpress/api-fetch';
import { applyFilters } from '@wordpress/hooks';

export async function replaceTags( content, context = {} ) {
// Define an async function to fetch data
Expand All @@ -8,7 +9,7 @@ export async function replaceTags( content, context = {} ) {
method: 'POST',
data: {
content,
context,
context: applyFilters( 'generateblocks.editor.preview.context', context, { content } ),
},
} );

Expand Down
4 changes: 3 additions & 1 deletion src/hoc/withDynamicTag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { replaceTags } from '../dynamic-tags/utils';

export function withDynamicTag( WrappedComponent ) {
Expand All @@ -16,6 +17,7 @@ export function withDynamicTag( WrappedComponent ) {

const [ dynamicTagValue, setDynamicTagValue ] = useState( '' );
const [ contentMode, setContentMode ] = useState( 'edit' );
const isSavingPost = useSelect( ( select ) => select( 'core/editor' ).isSavingPost() );

const getContentValue = () => {
if ( 'img' === tagName ) {
Expand Down Expand Up @@ -48,7 +50,7 @@ export function withDynamicTag( WrappedComponent ) {
}

fetchData();
}, [ contentValue, contentMode, context ] );
}, [ contentValue, contentMode, context, tagName, isSavingPost ] );

return (
<WrappedComponent
Expand Down
4 changes: 3 additions & 1 deletion src/hoc/withHtmlAttributes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useMemo, useState } from '@wordpress/element';
import { InspectorAdvancedControls } from '@wordpress/block-editor';
import { TextControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { applyFilters } from '@wordpress/hooks';

import { useUpdateEffect } from 'react-use';
Expand Down Expand Up @@ -74,6 +75,7 @@ export function withHtmlAttributes( WrappedComponent ) {
align,
} = attributes;

const isSavingPost = useSelect( ( select ) => select( 'core/editor' ).isSavingPost() );
const { style = '', href, ...otherAttributes } = htmlAttributes;
const [ processedStyle, setProcessedStyle ] = useState( style );

Expand All @@ -89,7 +91,7 @@ export function withHtmlAttributes( WrappedComponent ) {
}

fetchProcessedStyle();
}, [ style, props ] );
}, [ style, context, isSavingPost ] );

useUpdateEffect( () => {
const layoutClasses = [ 'alignwide', 'alignfull' ];
Expand Down

0 comments on commit b98717c

Please sign in to comment.