Skip to content

Commit 74de623

Browse files
committed
Merge branch 'trunk' into try/vibe-code-ui-for-post-meta
2 parents 92528e0 + e41b2cf commit 74de623

22 files changed

+516
-257
lines changed

includes/Query_Params_Generator.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,23 @@ class Query_Params_Generator {
2121
use Traits\Tax_Query;
2222
use Traits\Post_Parent;
2323

24-
2524
/**
26-
* The list of all custom params
25+
* The list of allowed controls and their associated params in the query.
2726
*/
28-
const KNOWN_PARAMS = array(
29-
'multiple_posts',
30-
'exclude_current',
31-
'include_posts',
32-
'meta_query',
33-
'date_query',
34-
'disable_pagination',
35-
'tax_query',
36-
'post_parent',
27+
const ALLOWED_CONTROLS = array(
28+
'additional_post_types' => 'multiple_posts',
29+
'taxonomy_query_builder' => 'tax_query',
30+
'post_meta_query' => 'meta_query',
31+
'post_order' => 'post_order',
32+
'exclude_current_post' => 'exclude_current',
33+
'include_posts' => 'include_posts',
34+
'child_items_only' => 'child_items_only',
35+
'date_query_dynamic_range' => 'date_query',
36+
'date_query_relationship' => 'date_query',
37+
'pagination' => 'disable_pagination',
3738
);
3839

40+
3941
/**
4042
* Default values from the default block.
4143
*
@@ -68,7 +70,6 @@ public function __construct( $default_params, $custom_params ) {
6870
$this->custom_params = is_array( $custom_params ) ? $custom_params : array();
6971
}
7072

71-
7273
/**
7374
* Checks to see if the item that is passed is a post ID.
7475
*
@@ -106,12 +107,30 @@ public function get_custom_param( string $name ) {
106107
}
107108
return false;
108109
}
110+
/**
111+
* Static function to return the list of allowed controls and their associated params in the query.
112+
*
113+
* @return array
114+
*/
115+
public static function get_allowed_controls() {
116+
return \apply_filters( 'aql_allowed_controls', array_keys( self::ALLOWED_CONTROLS ) );
117+
}
118+
119+
protected function get_params_to_process() {
120+
$params = array();
121+
foreach ( self::get_allowed_controls() as $control ) {
122+
$params[] = self::ALLOWED_CONTROLS[ $control ];
123+
}
124+
return $params;
125+
}
109126

110127
/**
111128
* Process all params at once.
112129
*/
113130
public function process_all(): void {
114-
foreach ( self::KNOWN_PARAMS as $param_name ) {
131+
// Get the params from the allowed controls and remove any duplicates.
132+
$params = array_unique( $this->get_params_to_process() );
133+
foreach ( $params as $param_name ) {
115134
if ( $this->has_custom_param( $param_name ) ) {
116135
call_user_func( array( $this, 'process_' . $param_name ) );
117136
}
@@ -124,5 +143,4 @@ public function process_all(): void {
124143
public function get_query_args(): array {
125144
return $this->custom_args;
126145
}
127-
128146
}

includes/Traits/Exclude_Current.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public function get_exclude_ids( $to_exclude ) {
3333
} else {
3434
// This is usually when this was set on a template.
3535
global $post;
36-
array_push( $exclude_ids, $post->ID );
36+
if ( $post ) {
37+
array_push( $exclude_ids, $post->ID );
38+
}
3739
}
3840
return $exclude_ids;
3941
}

includes/Traits/Meta_Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function parse_meta_query( $meta_query_data ) {
2323
$meta_queries[] = array_filter(
2424
array(
2525
'key' => $query['meta_key'] ?? '',
26-
'value' => $query['meta_value'],
27-
'compare' => $query['meta_compare'],
26+
'value' => $query['meta_value'] ?? '',
27+
'compare' => $query['meta_compare'] ?? '',
2828
)
2929
);
3030
}

includes/Traits/Multiple_Posts.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ trait Multiple_Posts {
1313
* Main processing function.
1414
*/
1515
public function process_multiple_posts(): void {
16-
$this->custom_args['post_type'] = array_unique( array_merge( array( $this->default_params['post_type'] ), $this->custom_params['multiple_posts'] ), SORT_REGULAR );
16+
$this->custom_args['post_type'] = array_unique(
17+
array_merge(
18+
(array) $this->default_params['post_type'],
19+
(array) $this->custom_params['multiple_posts']
20+
),
21+
SORT_REGULAR
22+
);
1723
}
1824
}

includes/enqueues.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace AdvancedQueryLoop;
99

10-
use function AdvancedQueryLoop\Utils\{ is_gutenberg_plugin_version_or_higher,is_core_version_or_higher };
10+
use function AdvancedQueryLoop\Utils\{ is_gutenberg_plugin_version_or_higher, is_core_version_or_higher };
1111

1212

1313
// Bail on unit tests.
@@ -35,9 +35,14 @@ function () {
3535
);
3636
// Allow for translation.
3737
wp_set_script_translations( 'advanced-query-loop', 'advanced-query-loop' );
38+
// Add inline script.
39+
wp_add_inline_script(
40+
'advanced-query-loop',
41+
'aql.allowedControls = "' . implode( ',', Query_Params_Generator::get_allowed_controls() ) . '";'
42+
);
3843
}
3944

40-
// Per Page, Offset, and Max count controls where merged into GB 19.
45+
// Per Page, Offset, and Max count controls were merged into GB 19.
4146
if ( ! is_gutenberg_plugin_version_or_higher( '19' ) && ! is_core_version_or_higher( '6.7' ) ) {
4247
// Enqueue the legacy controls.
4348
$pre_gb_19_assets_file = BUILD_DIR_PATH . 'legacy-pre-gb-19.asset.php';

includes/query-loop.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function ( $default_query, $block ) {
9898
\add_action(
9999
'init',
100100
function () {
101-
$registered_post_types = \get_post_types( array( 'public' => true ) );
101+
$registered_post_types = \get_post_types( array( 'show_in_rest' => true, 'publicly_queryable' => true ) );
102102
foreach ( $registered_post_types as $registered_post_type ) {
103103
\add_filter( 'rest_' . $registered_post_type . '_query', __NAMESPACE__ . '\add_custom_query_params', 10, 2 );
104104

@@ -128,7 +128,6 @@ function add_more_sort_by( $query_params ) {
128128
$query_params['orderby']['enum'][] = 'post__in';
129129
$query_params['orderby']['enum'][] = 'comment_count';
130130
$query_params['orderby']['enum'][] = 'name';
131-
// die( '<pre>' .print_r( $query_params , 1 ) .'</pre>' );
132131
return $query_params;
133132
}
134133

@@ -158,7 +157,5 @@ function add_custom_query_params( $args, $request ) {
158157
array_filter( $filtered_query_args )
159158
);
160159

161-
// die( var_dump( $request->get_params() ) );
162-
163160
return $merged;
164161
}

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Advanced Query Loop
44
* Description: Query loop block variations to create custom queries.
55
* Plugin URI: https://github.com/ryanwelcher/advanced-query-loop/
6-
* Version: 4.1.0
6+
* Version: 4.2.0
77
* Requires at least: 6.2
88
* Requires PHP: 7.4
99
* Author: Ryan Welcher

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "advanced-query-loop",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "Query loop block variations to create custom queries.",
55
"main": "index.js",
66
"scripts": {
@@ -31,7 +31,6 @@
3131
"@wordpress/scripts": "^24.6.0"
3232
},
3333
"dependencies": {
34-
"19": "^0.0.0",
3534
"uuid": "^9.0.0"
3635
}
3736
}

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="tests/unit/bootstrap.php"
55
cacheResultFile=".phpunit.cache/test-results"
66
executionOrder="depends,defects"
77
forceCoversAnnotation="true"

readme.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ Sort in ascending or descending order by:
6161

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

64+
## Filtering the available controls
65+
66+
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.
67+
68+
```php
69+
add_filter(
70+
'aql_allowed_controls',
71+
function( $controls ) {
72+
// Exclude the additional_post_types and taxonomy_query_builder controls.
73+
$to_exclude = array( 'additional_post_types', 'taxonomy_query_builder' );
74+
$filtered_controls = array_filter(
75+
$controls,
76+
function( $control ) use ( $to_exclude ) {
77+
if ( ! in_array( $control, $to_exclude, true ) ) {
78+
return $control;
79+
}
80+
},
81+
);
82+
return $filtered_controls;
83+
}
84+
);
85+
```
86+
87+
### List of control identifiers
88+
89+
- `'additional_post_types'`
90+
- `'taxonomy_query_builder'`
91+
- `'post_meta_query'`
92+
- `'post_order'`
93+
- `'exclude_current_post'`
94+
- `'include_posts'`
95+
- `'child_items_only'`
96+
- `'date_query_dynamic_range'`
97+
- `'date_query_relationship'`
98+
- `'pagination'`
99+
64100
## Extending AQL
65101

66102
Detailed instructions on how to extend AQL as well as an example are available [here](./extending-aql.md)

0 commit comments

Comments
 (0)