From ae754157edc412f9fd9210dd6ec70baba7300465 Mon Sep 17 00:00:00 2001 From: Matt Scheurich Date: Mon, 25 Sep 2017 22:44:52 +0200 Subject: [PATCH 1/4] wp_count_terms relies on string so breaks when using array to set multiple taxonomies to select from. Additionally, I added in a way to differentiate terms by slug and taxonomy --- inc/fields/class-shortcode-ui-field-term-select.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/inc/fields/class-shortcode-ui-field-term-select.php b/inc/fields/class-shortcode-ui-field-term-select.php index a6cb13d3..9631173d 100644 --- a/inc/fields/class-shortcode-ui-field-term-select.php +++ b/inc/fields/class-shortcode-ui-field-term-select.php @@ -148,7 +148,10 @@ public function action_wp_ajax_shortcode_ui_term_field() { $response['terms_per_page'] = 10; } - $num_results = wp_count_terms( $taxonomy_type, $args ); + // wp_count_terms relies on a string and uses the get_terms method anyway, so let's just cut to the chase + $num_results = get_terms( $taxonomy_type, array_merge( $args, [ + 'fields' => 'count', + ] ) ); if ( empty( $num_results ) ) { wp_send_json_error(); @@ -167,7 +170,7 @@ public function action_wp_ajax_shortcode_ui_term_field() { array_push( $response['items'], array( 'id' => $result->term_id, - 'text' => html_entity_decode( $result->name ), + 'text' => html_entity_decode( $result->name . ' - ' . $result->slug . ' (' . $result->taxonomy . ')' ), ) ); } From a7bf4294730ae6b8d149fa946b8dfab38f2c5188 Mon Sep 17 00:00:00 2001 From: Matt Scheurich Date: Wed, 27 Sep 2017 14:02:49 +0200 Subject: [PATCH 2/4] added post_name/slug to post select output to help differentiate between posts with same names --- inc/fields/class-shortcode-ui-field-post-select.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/fields/class-shortcode-ui-field-post-select.php b/inc/fields/class-shortcode-ui-field-post-select.php index 0a62a26f..8e7e3fd1 100644 --- a/inc/fields/class-shortcode-ui-field-post-select.php +++ b/inc/fields/class-shortcode-ui-field-post-select.php @@ -143,7 +143,7 @@ public function action_wp_ajax_shortcode_ui_post_field() { $post_type = get_post_type( $post_id ); $post_type_obj = get_post_type_object( $post_type ); - $text = html_entity_decode( get_the_title( $post_id ) ); + $text = html_entity_decode( get_the_title( $post_id ) . ' - ' . get_post_field( 'post_name', $post_id ) ); if ( $is_multiple_post_types && $post_type_obj ) { $text .= sprintf( ' (%1$s)', $post_type_obj->labels->singular_name ); From 222d20b1363829999a0f28951316929be3b799dc Mon Sep 17 00:00:00 2001 From: Matt Scheurich Date: Wed, 27 Sep 2017 17:26:37 +0200 Subject: [PATCH 3/4] modified the page slug to show the relative URL instead. Did same for term slug and also made these only visible if multiple post types or taxonomies are enabled --- .../class-shortcode-ui-field-post-select.php | 5 +++-- .../class-shortcode-ui-field-term-select.php | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/inc/fields/class-shortcode-ui-field-post-select.php b/inc/fields/class-shortcode-ui-field-post-select.php index 8e7e3fd1..3655d3a5 100644 --- a/inc/fields/class-shortcode-ui-field-post-select.php +++ b/inc/fields/class-shortcode-ui-field-post-select.php @@ -143,11 +143,12 @@ public function action_wp_ajax_shortcode_ui_post_field() { $post_type = get_post_type( $post_id ); $post_type_obj = get_post_type_object( $post_type ); - $text = html_entity_decode( get_the_title( $post_id ) . ' - ' . get_post_field( 'post_name', $post_id ) ); + $text = html_entity_decode( get_the_title( $post_id ) ); if ( $is_multiple_post_types && $post_type_obj ) { - $text .= sprintf( ' (%1$s)', $post_type_obj->labels->singular_name ); + $text .= sprintf( '- %1$s (%2$s)', get_post_field( 'post_name', $post_id ), $post_type_obj->labels->singular_name ); } + array_push( $response['items'], array( 'id' => $post_id, diff --git a/inc/fields/class-shortcode-ui-field-term-select.php b/inc/fields/class-shortcode-ui-field-term-select.php index 9631173d..0caf3288 100644 --- a/inc/fields/class-shortcode-ui-field-term-select.php +++ b/inc/fields/class-shortcode-ui-field-term-select.php @@ -164,13 +164,29 @@ public function action_wp_ajax_shortcode_ui_term_field() { $args['offset'] = ( $page - 1 ) * $response['items_per_page']; } + $is_multiple_taxonomies = count( $args['taxonomy'] ) > 1; + $taxonomies = []; + if ( $is_multiple_taxonomies ) + { + foreach ( $args['taxonomy'] as $tax_slug ) + { + $taxonomies[ $tax_slug ] = get_taxonomy( $tax_slug ); + } + } + $results = get_terms( $args ); foreach ( $results as $result ) { + $text = html_entity_decode( $result->name ); + + if ( $is_multiple_taxonomies ) { + $text .= sprintf( ' - %1%s (%2$s)', $result->slug, $taxonomies[ $result->taxonomy ]->labels->singular_name ); + } + array_push( $response['items'], array( 'id' => $result->term_id, - 'text' => html_entity_decode( $result->name . ' - ' . $result->slug . ' (' . $result->taxonomy . ')' ), + 'text' => $text, ) ); } From c041c7ab9e378bcdcbfa0f96e9a6c1f3088191c9 Mon Sep 17 00:00:00 2001 From: Matt Scheurich Date: Thu, 28 Sep 2017 10:05:35 +0200 Subject: [PATCH 4/4] attempting to address code style issues for CI --- .../class-shortcode-ui-field-post-select.php | 18 ++++---- .../class-shortcode-ui-field-term-select.php | 46 +++++++++---------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/inc/fields/class-shortcode-ui-field-post-select.php b/inc/fields/class-shortcode-ui-field-post-select.php index 3655d3a5..b2efc0a0 100644 --- a/inc/fields/class-shortcode-ui-field-post-select.php +++ b/inc/fields/class-shortcode-ui-field-post-select.php @@ -58,15 +58,15 @@ public function action_shortcode_ui_loaded_editor() { ?> - + - + 'count', - ] ) ); + // wp_count_terms relies on a string and uses the get_terms method anyway, so let's just cut to the chase + $num_results = get_terms( $taxonomy_type, array_merge( $args, [ + 'fields' => 'count', + ] ) ); if ( empty( $num_results ) ) { wp_send_json_error(); @@ -164,29 +164,29 @@ public function action_wp_ajax_shortcode_ui_term_field() { $args['offset'] = ( $page - 1 ) * $response['items_per_page']; } - $is_multiple_taxonomies = count( $args['taxonomy'] ) > 1; + $is_multiple_taxonomies = count( $args['taxonomy'] ) > 1; $taxonomies = []; if ( $is_multiple_taxonomies ) - { - foreach ( $args['taxonomy'] as $tax_slug ) - { - $taxonomies[ $tax_slug ] = get_taxonomy( $tax_slug ); - } - } + { + foreach ( $args['taxonomy'] as $tax_slug ) + { + $taxonomies[ $tax_slug ] = get_taxonomy( $tax_slug ); + } + } $results = get_terms( $args ); foreach ( $results as $result ) { - $text = html_entity_decode( $result->name ); + $text = html_entity_decode( $result->name ); - if ( $is_multiple_taxonomies ) { + if ( $is_multiple_taxonomies ) { $text .= sprintf( ' - %1%s (%2$s)', $result->slug, $taxonomies[ $result->taxonomy ]->labels->singular_name ); } array_push( $response['items'], array( 'id' => $result->term_id, - 'text' => $text, + 'text' => $text, ) ); }