Skip to content

Commit

Permalink
More thorough fixes for MPG and MTG field queries
Browse files Browse the repository at this point in the history
For #93
  • Loading branch information
JiveDig committed Dec 13, 2024
1 parent 0987944 commit 6c88db6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 107 deletions.
72 changes: 58 additions & 14 deletions lib/fields/wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@
// Prevent direct file access.
defined( 'ABSPATH' ) || die;

add_filter( 'acf/prepare_field/key=mai_grid_block_post_type', 'mai_grid_prepare_post_type_field' );
/**
* Load post type choices based on existing saved field value.
* Since we're using ajax to load choices, we need to load the saved value as an initial choice.
*
* @link https://github.com/maithemewp/mai-engine/issues/93
*
* @since TBD
*
* @param array $field The existing field array.
*
* @return array
*/
function mai_grid_prepare_post_type_field( $field ) {
// Bail if not in admin. No AJAX check here because we need this on page load.
if ( ! is_admin() ) {
return $field;
}

if ( $field['value'] ) {
foreach ( $field['value'] as $post_type ) {
$object = get_post_type_object( $post_type );

if ( $object ) {
$field['choices'] = [ $object->name => $object->label ];
}
}
}

return $field;
}

add_filter( 'acf/load_field/key=mai_grid_block_post_type', 'mai_grid_load_post_type_field' );
/**
* Loads post type choices.
Expand All @@ -25,6 +57,7 @@
* @return array
*/
function mai_grid_load_post_type_field( $field ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $field;
}
Expand Down Expand Up @@ -77,11 +110,14 @@ function mai_grid_prepare_tax_taxonomy_field( $field ) {
* @return array
*/
function mai_grid_load_tax_taxonomy_field( $field ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $field;
}

if ( ! isset( $_REQUEST['post_type'] ) ) {
$post_types = (array) mai_get_acf_request( 'post_type' );

if ( ! $post_types ) {
return $field;
}

Expand All @@ -93,42 +129,41 @@ function mai_grid_load_tax_taxonomy_field( $field ) {
add_filter( 'acf/prepare_field/key=mai_grid_block_tax_terms', 'mai_acf_prepare_terms', 10, 1 );
/**
* Load term choices based on existing saved field value.
* Ajax loading terms was working, but if a term was already saved
* it was not loading correctly when editing a post.
* Since we're using ajax to load choices, we need to load the saved value as an initial choice.
*
* @link https://github.com/maithemewp/mai-engine/issues/93
*
* @since 0.3.3
* @since 2.25.6 Added admin check.
* @since 2.35.0 Added ajax check.
* @since TBD Limited choices to only the current term.
*
* @param array $field The ACF field array.
*
* @return mixed
*/
function mai_acf_prepare_terms( $field ) {
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
// Bail if not in admin. No AJAX check here because we need this on page load.
if ( ! is_admin() ) {
return $field;
}

if ( ! $field['value'] ) {
return $field;
}

$term_id = reset( $field['value'] );
$field['choices'] = isset( $field['choices'] ) ? (array) $field['choices'] : [];

if ( ! $term_id ) {
return $field;
}
foreach ( $field['value'] as $term_id ) {
$term = get_term( $term_id );

$term = get_term( $term_id );
if ( ! $term || is_wp_error( $term ) ) {
continue;
}

if ( ! $term || is_wp_error( $term ) ) {
return $field;
$field['choices'][ $term->term_id ] = $term->name;
}

$field['choices'] = mai_get_term_choices_from_taxonomy( $term->taxonomy );

return $field;
}

Expand All @@ -146,6 +181,7 @@ function mai_acf_prepare_terms( $field ) {
* @return mixed
*/
function mai_acf_load_terms( $field ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $field;
}
Expand Down Expand Up @@ -174,6 +210,7 @@ function mai_acf_load_terms( $field ) {
* @return mixed
*/
function mai_acf_get_post_parents( $args ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $args;
}
Expand Down Expand Up @@ -212,6 +249,7 @@ function mai_acf_get_post_parents( $args ) {
* @return array
*/
function mai_acf_get_posts_by_id( $args, $field, $post_id ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $args;
}
Expand Down Expand Up @@ -245,6 +283,12 @@ function mai_acf_get_posts_by_id( $args, $field, $post_id ) {
* @return array
*/
function mai_get_post_type_choices() {
static $choices = null;

if ( ! is_null( $choices ) ) {
return $choices;
}

$choices = [];
$post_types = get_post_types( [ 'public' => true ] );

Expand Down Expand Up @@ -406,7 +450,7 @@ function mai_get_wp_query_fields() {
'choices' => [], // Added later via load_field.
'multiple' => 1,
'ui' => 1,
'ajax' => 0,
'ajax' => 1,
],
[
'key' => 'mai_grid_block_query_by',
Expand Down
138 changes: 45 additions & 93 deletions lib/fields/wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@
// Prevent direct file access.
defined( 'ABSPATH' ) || die;

add_filter( 'acf/load_field/key=mai_grid_block_taxonomy', 'mai_grid_load_taxonomy_field' );
/**
* Loads taxonomy choices.
*
* @since 2.21.0
* @since 2.25.6 Added admin check.
* @since 2.35.0 Added ajax check.
*
* @param array $field The existing field array.
*
* @return array
*/
function mai_grid_load_taxonomy_field( $field ) {
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $field;
}

$field['choices'] = mai_get_taxonomy_choices();

return $field;
}

add_filter( 'acf/prepare_field/key=mai_grid_block_taxonomy', 'mai_grid_prepare_taxonomy_field' );
/**
* Load taxonomy choices based on existing saved field value.
Expand All @@ -55,59 +33,79 @@ function mai_grid_prepare_taxonomy_field( $field ) {

if ( $field['value'] ) {
foreach ( $field['value'] as $taxonomy ) {
$taxonomy = get_taxonomy( $taxonomy );
$object = get_taxonomy( $taxonomy );

if ( $taxonomy ) {
$field['choices'] = [ $taxonomy->name => $taxonomy->label ];
if ( $object ) {
$field['choices'] = [ $object->name => $object->label ];
}
}
}

return $field;
}

add_filter( 'acf/load_field/key=mai_grid_block_tax_include', 'mai_grid_load_include_terms_field' );
add_filter( 'acf/load_field/key=mai_grid_block_tax_exclude', 'mai_grid_load_include_terms_field' );
add_filter( 'acf/load_field/key=mai_grid_block_taxonomy', 'mai_grid_load_taxonomy_field' );
/**
* Sets taxonomy based on the block taxonomies field, when the include/exclude fields are initially loaded.
* Loads taxonomy choices.
*
* @since 2.27.0
* @since 2.21.0
* @since 2.25.6 Added admin check.
* @since 2.35.0 Added ajax check.
*
* @param array $field
* @param array $field The existing field array.
*
* @return array
*/
function mai_grid_load_include_terms_field( $field ) {
function mai_grid_load_taxonomy_field( $field ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $field;
}

$taxonomies = mai_get_acf_request( 'taxonomy' );
$field['choices'] = mai_get_taxonomy_choices();

$action = mai_get_acf_request( 'action' );
$block = mai_get_acf_request( 'block' );
return $field;
}

if ( ! ( $action && $block && 'acf/ajax/fetch-block' === $action ) ) {
add_filter( 'acf/prepare_field/key=mai_grid_block_tax_include', 'mai_grid_prepare_include_terms_field' );
add_filter( 'acf/prepare_field/key=mai_grid_block_tax_exclude', 'mai_grid_prepare_include_terms_field' );
add_filter( 'acf/prepare_field/key=mai_grid_block_tax_parent', 'mai_grid_prepare_include_terms_field' );
/**
* Sets taxonomy based on the block taxonomies field, when the include/exclude fields are initially loaded.
* The ACF taxonomy field can only load terms from a single taxonomy, so we use the first and assume
* all terms are from the same taxonomy.
*
* @since TBD
*
* @param array $field
*
* @return array
*/
function mai_grid_prepare_include_terms_field( $field ) {
// Bail if not in admin. No AJAX check here because we need this on page load.
if ( ! is_admin() ) {
return $field;
}

$block = json_decode( wp_unslash( $block ), true );
$term_ids = (array) $field['value'];

if ( ! isset( $block['name'] ) || 'acf/mai-term-grid' !== $block['name'] ) {
return $field;
}
if ( $term_ids ) {
foreach( $term_ids as $term_id ) {
$term = get_term( $term_id );

if ( isset( $block['data']['taxonomy'] ) ) {
$taxonomies = (array) $block['data']['taxonomy'];
$field['taxonomy'] = reset( $taxonomies );
if ( $term ) {
$field['taxonomy'] = $term->taxonomy;
break;
}
}
}

return $field;
}

add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_include', 'mai_acf_get_terms', 10, 3 );
add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_exclude', 'mai_acf_get_terms', 10, 3 );
add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_parent', 'mai_acf_get_terms', 10, 3 );
/**
* Get terms from an ajax query.
* The taxonomy is passed via JS on select2_query_args filter.
Expand All @@ -122,64 +120,17 @@ function mai_grid_load_include_terms_field( $field ) {
* @return mixed
*/
function mai_acf_get_terms( $args, $field, $post_id ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $args;
}

$args['taxonomy'] = [];
$taxonomies = mai_get_acf_request( 'taxonomy' );

if ( ! $taxonomies ) {
return $args;
}

// Forces first taxonomy, since ACF's Taxonomy field type does not allow multiple taxonomies.
$taxonomies = wp_unslash( (array) $taxonomies );
$taxonomies = (array) mai_get_acf_request( 'taxonomy' );
$args['taxonomy'] = reset( $taxonomies );

return $args;
}

add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_parent', 'mai_acf_get_term_parents', 10, 1 );
/**
* Get taxonomies from an ajax query.
* The taxonomy is passed via JS on select2_query_args filter.
*
* @since 0.1.0
* @since 2.25.6 Added admin check.
* @since 2.35.0 Added ajax check.
*
* @param array $args Field args.
*
* @return mixed
*/
function mai_acf_get_term_parents( $args ) {
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $args;
}

$args['taxonomy'] = [];
$taxonomies = mai_get_acf_request( 'taxonomy' );

if ( ! $taxonomies ) {
return $args;
}

foreach ( (array) $taxonomies as $taxonomy ) {
$args['taxonomy'][] = sanitize_text_field( wp_unslash( $taxonomy ) );
}

foreach ( $args['taxonomy'] as $index => $taxonomy ) {
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
unset( $args['taxonomy'][ $index ] );
}

continue;
}

return $args;
}

add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_include', 'mai_acf_get_terms_by_id', 10, 3 );
add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_exclude', 'mai_acf_get_terms_by_id', 10, 3 );
add_filter( 'acf/fields/taxonomy/query/key=mai_grid_block_tax_parent', 'mai_acf_get_terms_by_id', 10, 3 );
Expand All @@ -195,11 +146,12 @@ function mai_acf_get_term_parents( $args ) {
* @return array
*/
function mai_acf_get_terms_by_id( $args, $field, $post_id ) {
// Bail if not in admin and doing ajax.
if ( ! ( is_admin() && wp_doing_ajax() ) ) {
return $args;
}

$query = ! empty( $args['search'] ) ? $args['search'] : false;
$query = isset( $args['search'] ) && ! empty( $args['search'] ) ? $args['search'] : false;

if ( ! $query ) {
return $args;
Expand Down Expand Up @@ -414,7 +366,7 @@ function mai_get_wp_term_query_fields() {
'choices' => '', // Loaded in filter later.
'multiple' => 1,
'ui' => 1,
'ajax' => 0,
'ajax' => 1,
],
[
'key' => 'mai_grid_block_tax_query_by',
Expand Down

0 comments on commit 6c88db6

Please sign in to comment.