Skip to content
46 changes: 32 additions & 14 deletions includes/Query_Params_Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ class Query_Params_Generator {
use Traits\Tax_Query;
use Traits\Post_Parent;


/**
* The list of all custom params
* The list of allowed controls and their associated params in the query.
*/
const KNOWN_PARAMS = array(
'multiple_posts',
'exclude_current',
'include_posts',
'meta_query',
'date_query',
'disable_pagination',
'tax_query',
'post_parent',
const ALLOWED_CONTROLS = array(
'additional_post_types' => 'multiple_posts',
'taxonomy_query_builder' => 'tax_query',
'post_meta_query' => 'meta_query',
'post_order' => 'post_order',
'exclude_current_post' => 'exclude_current',
'include_posts' => 'include_posts',
'child_items_only' => 'child_items_only',
'date_query_dynamic_range' => 'date_query',
'date_query_relationship' => 'date_query',
'pagination' => 'disable_pagination',
);


/**
* Default values from the default block.
*
Expand Down Expand Up @@ -68,7 +70,6 @@ public function __construct( $default_params, $custom_params ) {
$this->custom_params = is_array( $custom_params ) ? $custom_params : array();
}


/**
* Checks to see if the item that is passed is a post ID.
*
Expand Down Expand Up @@ -106,12 +107,30 @@ public function get_custom_param( string $name ) {
}
return false;
}
/**
* Static function to return the list of allowed controls and their associated params in the query.
*
* @return array
*/
public static function get_allowed_controls() {
return \apply_filters( 'aql_allowed_controls', array_keys( self::ALLOWED_CONTROLS ) );
}

protected function get_params_to_process() {
$params = array();
foreach ( self::get_allowed_controls() as $control ) {
$params[] = self::ALLOWED_CONTROLS[ $control ];
}
return $params;
}

/**
* Process all params at once.
*/
public function process_all(): void {
foreach ( self::KNOWN_PARAMS as $param_name ) {
// Get the params from the allowed controls and remove any duplicates.
$params = array_unique( $this->get_params_to_process() );
foreach ( $params as $param_name ) {
if ( $this->has_custom_param( $param_name ) ) {
call_user_func( array( $this, 'process_' . $param_name ) );
}
Expand All @@ -124,5 +143,4 @@ public function process_all(): void {
public function get_query_args(): array {
return $this->custom_args;
}

}
9 changes: 7 additions & 2 deletions includes/enqueues.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace AdvancedQueryLoop;

use function AdvancedQueryLoop\Utils\{ is_gutenberg_plugin_version_or_higher,is_core_version_or_higher };
use function AdvancedQueryLoop\Utils\{ is_gutenberg_plugin_version_or_higher, is_core_version_or_higher };


// Bail on unit tests.
Expand Down Expand Up @@ -35,9 +35,14 @@ function () {
);
// Allow for translation.
wp_set_script_translations( 'advanced-query-loop', 'advanced-query-loop' );
// Add inline script.
wp_add_inline_script(
'advanced-query-loop',
'aql.allowedControls = "' . implode( ',', Query_Params_Generator::get_allowed_controls() ) . '";'
);
}

// Per Page, Offset, and Max count controls where merged into GB 19.
// Per Page, Offset, and Max count controls were merged into GB 19.
if ( ! is_gutenberg_plugin_version_or_higher( '19' ) && ! is_core_version_or_higher( '6.7' ) ) {
// Enqueue the legacy controls.
$pre_gb_19_assets_file = BUILD_DIR_PATH . 'legacy-pre-gb-19.asset.php';
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="tests/unit/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
Expand Down
36 changes: 36 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ Sort in ascending or descending order by:

Improve the performance of the query by disabling pagination. This is done automatically when there is now Pagination block in teh Post Template.

## Filtering the available controls

It is possible to remove controls from AQL using the `aql_allowed_controls` filter. The filter receives a single parameter containing an array of allowed controls. This can be modified to remove the control from the UI and stop processing the associated query param.

```php
add_filter(
'aql_allowed_controls',
function( $controls ) {
// Exclude the additional_post_types and taxonomy_query_builder controls.
$to_exclude = array( 'additional_post_types', 'taxonomy_query_builder' );
$filtered_controls = array_filter(
$controls,
function( $control ) use ( $to_exclude ) {
if ( ! in_array( $control, $to_exclude, true ) ) {
return $control;
}
},
);
return $filtered_controls;
}
);
```

### List of control identifiers

- `'additional_post_types'`
- `'taxonomy_query_builder'`
- `'post_meta_query'`
- `'post_order'`
- `'exclude_current_post'`
- `'include_posts'`
- `'child_items_only'`
- `'date_query_dynamic_range'`
- `'date_query_relationship'`
- `'pagination'`

## Extending AQL

Detailed instructions on how to extend AQL as well as an example are available [here](./extending-aql.md)
11 changes: 10 additions & 1 deletion src/components/child-items-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';

export const ChildItemsToggle = ( { attributes, setAttributes } ) => {
export const ChildItemsToggle = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
const { query: { post_parent: postParent } = {} } = attributes;

const { isHierarchial, postTypeName, postID } = useSelect( ( select ) => {
Expand All @@ -22,6 +26,11 @@ export const ChildItemsToggle = ( { attributes, setAttributes } ) => {
};
}, [] );

// If the control is not allowed, return null.
if ( ! allowedControls.includes( 'child_items_only' ) ) {
return null;
}

return (
<ToggleControl
__nextHasNoMarginBottom
Expand Down
23 changes: 17 additions & 6 deletions src/components/multiple-post-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

export const MultiplePostSelect = ( { attributes, setAttributes } ) => {
export const MultiplePostSelect = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
const { query: { multiple_posts: multiplePosts = [], postType } = {} } =
attributes;

const postTypes = useSelect( ( select ) =>
select( coreStore )
.getPostTypes( { per_page: 50 } )
?.filter( ( { viewable } ) => viewable )
?.map( ( { slug } ) => slug )
const postTypes = useSelect(
( select ) =>
select( coreStore )
.getPostTypes( { per_page: 50 } )
?.filter( ( { viewable } ) => viewable )
?.map( ( { slug } ) => slug ),
[]
);

// If the control is not allowed, return null.
if ( ! allowedControls.includes( 'additional_post_types' ) ) {
return null;
}

if ( ! postTypes ) {
return <div>{ __( 'Loading…', 'advanced-query-loop' ) }</div>;
}
Expand Down
10 changes: 9 additions & 1 deletion src/components/pagination-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export const PaginationToggle = ( { attributes, setAttributes } ) => {
export const PaginationToggle = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
const { query: { disable_pagination: disablePagination } = {} } =
attributes;
// If the control is not allowed, return null.
if ( ! allowedControls.includes( 'pagination' ) ) {
return null;
}

return (
<ToggleControl
Expand Down
Loading
Loading