Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate selected terms by slug and taxonomy #774

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions inc/fields/class-shortcode-ui-field-post-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public function action_shortcode_ui_loaded_editor() {

?>

<script type="text/html" id="tmpl-shortcode-ui-field-post-select">
<div class="field-block shortcode-ui-field-post-select shortcode-ui-attribute-{{ data.attr }}">
<label for="{{ data.id }}">{{{ data.label }}}</label>
<select id="{{ data.id }}" name="{{ data.name }}" class="shortcode-ui-post-select" ></select>
<# if ( typeof data.description == 'string' && data.description.length ) { #>
<p class="description">{{{ data.description }}}</p>
<# } #>
</div>
</script>
<script type="text/html" id="tmpl-shortcode-ui-field-post-select">
<div class="field-block shortcode-ui-field-post-select shortcode-ui-attribute-{{ data.attr }}">
<label for="{{ data.id }}">{{{ data.label }}}</label>
<select id="{{ data.id }}" name="{{ data.name }}" class="shortcode-ui-post-select" ></select>
<# if ( typeof data.description == 'string' && data.description.length ) { #>
<p class="description">{{{ data.description }}}</p>
<# } #>
</div>
</script>

<?php
}
Expand Down Expand Up @@ -146,8 +146,9 @@ public function action_wp_ajax_shortcode_ui_post_field() {
$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,
Expand Down
41 changes: 30 additions & 11 deletions inc/fields/class-shortcode-ui-field-term-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public function action_shortcode_ui_loaded_editor() {

?>

<script type="text/html" id="tmpl-shortcode-ui-field-term-select">
<div class="field-block shortcode-ui-field-term-select shortcode-ui-attribute-{{ data.attr }}">
<label for="{{ data.id }}">{{{ data.label }}}</label>
<select name="{{ data.attr }}" id="{{ data.id }}" class="shortcode-ui-term-select"></select>
<# if ( typeof data.description == 'string' && data.description.length ) { #>
<p class="description">{{{ data.description }}}</p>
<# } #>
</div>
</script>
<script type="text/html" id="tmpl-shortcode-ui-field-term-select">
<div class="field-block shortcode-ui-field-term-select shortcode-ui-attribute-{{ data.attr }}">
<label for="{{ data.id }}">{{{ data.label }}}</label>
<select name="{{ data.attr }}" id="{{ data.id }}" class="shortcode-ui-term-select"></select>
<# if ( typeof data.description == 'string' && data.description.length ) { #>
<p class="description">{{{ data.description }}}</p>
<# } #>
</div>
</script>

<?php
}
Expand Down Expand Up @@ -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();
Expand All @@ -161,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 ),
'text' => $text,
)
);
}
Expand Down