acewpcf_google_fonts_are_enabled(), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'previewNonce' => wp_create_nonce( 'acewpcf_preview_filter' ), 'contentTaxonomies' => acewpcf_get_admin_content_taxonomy_map(), 'i18n' => array( 'previewUpdating' => __( 'Updating preview...', 'ace-content-filters' ), 'previewUpdated' => __( 'Preview updated.', 'ace-content-filters' ), 'previewFailed' => __( 'Preview could not be refreshed. Try again, or save the filter if the problem persists.', 'ace-content-filters' ), 'noTaxonomies' => __( 'No taxonomies found', 'ace-content-filters' ), ), ) ) . ';', 'before' ); } add_action( 'admin_enqueue_scripts', 'acewpcf_enqueue_admin_assets' ); function acewpcf_get_admin_page_url( $args = array() ) { $args = wp_parse_args( $args, array( 'page' => 'acewpcf-settings', ) ); return add_query_arg( $args, admin_url( 'admin.php' ) ); } function acewpcf_get_admin_logo_url() { return ACEWPCF_PLUGIN_URL . 'assets/images/awp-logo.svg'; } function acewpcf_get_admin_taxonomy_label( $taxonomy ) { $taxonomy = sanitize_key( (string) $taxonomy ); $taxonomy_object = get_taxonomy( $taxonomy ); if ( $taxonomy_object instanceof WP_Taxonomy && ! empty( $taxonomy_object->labels->singular_name ) ) { return (string) $taxonomy_object->labels->singular_name; } return ucwords( str_replace( array( 'pa_', '-', '_' ), array( '', ' ', ' ' ), $taxonomy ) ); } function acewpcf_get_admin_terms_for_taxonomy( $taxonomy ) { static $terms_cache = array(); $taxonomy = sanitize_key( (string) $taxonomy ); if ( '' === $taxonomy || ! taxonomy_exists( $taxonomy ) ) { return array(); } if ( isset( $terms_cache[ $taxonomy ] ) ) { return $terms_cache[ $taxonomy ]; } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', ) ); if ( is_wp_error( $terms ) ) { $terms_cache[ $taxonomy ] = array(); return array(); } $terms_cache[ $taxonomy ] = $terms; return $terms; } function acewpcf_get_admin_term_map( $terms ) { $term_map = array(); foreach ( $terms as $term ) { $term_map[ (int) $term->term_id ] = $term; } return $term_map; } function acewpcf_get_admin_term_path( $term, $term_map, $taxonomy ) { $taxonomy = sanitize_key( (string) $taxonomy ); if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { return ''; } $ancestor_ids = array_reverse( get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ) ); $ancestor_names = array(); foreach ( $ancestor_ids as $ancestor_id ) { if ( isset( $term_map[ $ancestor_id ] ) ) { $ancestor_names[] = $term_map[ $ancestor_id ]->name; } } return implode( ' / ', $ancestor_names ); } function acewpcf_get_admin_term_options( $taxonomy, $terms ) { $taxonomy = sanitize_key( (string) $taxonomy ); $terms = is_array( $terms ) ? $terms : array(); if ( empty( $terms ) || ! taxonomy_exists( $taxonomy ) ) { return array(); } $term_map = acewpcf_get_admin_term_map( $terms ); $taxonomy_label = acewpcf_get_admin_taxonomy_label( $taxonomy ); $options = array(); if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { usort( $terms, static function ( $left, $right ) { return strcasecmp( $left->name, $right->name ); } ); foreach ( $terms as $term ) { $options[] = array( 'taxonomy' => $taxonomy, 'taxonomy_label' => $taxonomy_label, 'term' => $term, 'depth' => 0, 'path' => '', 'search_text' => strtolower( trim( $term->name . ' ' . $term->slug . ' ' . $taxonomy_label ) ), ); } return $options; } $children = array(); $seen_ids = array(); foreach ( $terms as $term ) { $parent_id = isset( $term_map[ (int) $term->parent ] ) ? (int) $term->parent : 0; if ( ! isset( $children[ $parent_id ] ) ) { $children[ $parent_id ] = array(); } $children[ $parent_id ][] = $term; } foreach ( $children as $parent_id => $child_terms ) { usort( $child_terms, static function ( $left, $right ) { return strcasecmp( $left->name, $right->name ); } ); $children[ $parent_id ] = $child_terms; } $walk_terms = static function ( $parent_id, $depth ) use ( &$walk_terms, &$options, &$children, &$seen_ids, $taxonomy, $taxonomy_label, $term_map ) { if ( empty( $children[ $parent_id ] ) ) { return; } foreach ( $children[ $parent_id ] as $term ) { $seen_ids[ (int) $term->term_id ] = true; $path = acewpcf_get_admin_term_path( $term, $term_map, $taxonomy ); $options[] = array( 'taxonomy' => $taxonomy, 'taxonomy_label' => $taxonomy_label, 'term' => $term, 'depth' => $depth, 'path' => $path, 'search_text' => strtolower( trim( $term->name . ' ' . $term->slug . ' ' . $path . ' ' . $taxonomy_label ) ), ); $walk_terms( (int) $term->term_id, $depth + 1 ); } }; $walk_terms( 0, 0 ); foreach ( $terms as $term ) { if ( isset( $seen_ids[ (int) $term->term_id ] ) ) { continue; } $path = acewpcf_get_admin_term_path( $term, $term_map, $taxonomy ); $options[] = array( 'taxonomy' => $taxonomy, 'taxonomy_label' => $taxonomy_label, 'term' => $term, 'depth' => 0, 'path' => $path, 'search_text' => strtolower( trim( $term->name . ' ' . $term->slug . ' ' . $path . ' ' . $taxonomy_label ) ), ); } return $options; } function acewpcf_get_admin_content_taxonomy_map() { $map = array(); foreach ( acewpcf_get_content_type_choices() as $content_type => $label ) { unset( $label ); $map[ $content_type ] = acewpcf_get_content_taxonomy_choices( $content_type ); } return $map; } function acewpcf_get_admin_filter_taxonomies( $filter = array() ) { $filter = is_array( $filter ) ? $filter : array(); $content_type = acewpcf_get_filter_content_type( $filter ); return acewpcf_get_content_taxonomy_choices( $content_type ); } function acewpcf_get_admin_term_option_groups( $taxonomies ) { $taxonomies = is_array( $taxonomies ) ? $taxonomies : array(); $groups = array(); foreach ( $taxonomies as $taxonomy => $label ) { $groups[ $taxonomy ] = acewpcf_get_admin_term_options( $taxonomy, acewpcf_get_admin_terms_for_taxonomy( $taxonomy ) ); } return $groups; } function acewpcf_get_filter_mode_label( $selection_mode, $filter = array() ) { unset( $filter ); return 'multiple' === $selection_mode ? __( 'Multiple / Checkboxes', 'ace-content-filters' ) : __( 'Single / Buttons', 'ace-content-filters' ); } function acewpcf_get_filter_source_label( $filter ) { $filter = is_array( $filter ) ? $filter : array(); $source_type = isset( $filter['source_type'] ) ? acewpcf_sanitize_filter_source_type( $filter['source_type'] ) : 'category'; $choices = acewpcf_get_filter_source_choices(); if ( isset( $choices[ $source_type ] ) && ! acewpcf_is_taxonomy_filter_source_type( $source_type ) ) { return $choices[ $source_type ]; } $taxonomy = acewpcf_get_filter_taxonomy( $filter ); if ( '' !== $taxonomy ) { return acewpcf_get_admin_taxonomy_label( $taxonomy ); } return isset( $choices[ $source_type ] ) ? $choices[ $source_type ] : $source_type; } function acewpcf_get_filter_display_summary_label( $filter ) { $filter = is_array( $filter ) ? $filter : array(); $show_label = ! empty( $filter['show_label'] ); $show_count = ! empty( $filter['show_count'] ); if ( function_exists( 'acewpcf_filter_uses_swatches' ) && acewpcf_filter_uses_swatches( $filter ) ) { if ( $show_label && $show_count ) { return __( 'Swatches + label + count', 'ace-content-filters' ); } if ( $show_label ) { return __( 'Swatches + label', 'ace-content-filters' ); } if ( $show_count ) { return __( 'Swatches + count', 'ace-content-filters' ); } return __( 'Colour swatches', 'ace-content-filters' ); } if ( $show_label && $show_count ) { return __( 'Title + count', 'ace-content-filters' ); } if ( $show_count ) { return __( 'Count only', 'ace-content-filters' ); } return __( 'Title only', 'ace-content-filters' ); } function acewpcf_get_filter_orientation_label( $orientation ) { $orientation = acewpcf_sanitize_filter_orientation( $orientation ); return 'vertical' === $orientation ? __( 'Vertical / Left bar', 'ace-content-filters' ) : __( 'Horizontal / Top bar', 'ace-content-filters' ); } function acewpcf_get_filter_item_summary_label( $filter, $term_ids = array() ) { $term_ids = is_array( $term_ids ) ? $term_ids : array(); if ( acewpcf_is_search_filter( $filter ) ) { return __( 'Search box', 'ace-content-filters' ); } if ( acewpcf_is_author_filter( $filter ) || acewpcf_is_date_filter( $filter ) ) { $option_count = count( $term_ids ); /* translators: %d: number of options included in the filter. */ $option_count_text = _n( '%d option', '%d options', $option_count, 'ace-content-filters' ); return sprintf( $option_count_text, $option_count ); } /* translators: %d: number of terms included in the filter. */ $term_count_text = _n( '%d term', '%d terms', count( $term_ids ), 'ace-content-filters' ); return sprintf( $term_count_text, count( $term_ids ) ); } function acewpcf_get_filter_term_preview_labels( $filter ) { $labels = array(); $filter = is_array( $filter ) ? $filter : array(); $terms = acewpcf_get_filter_terms( isset( $filter['id'] ) ? $filter['id'] : '' ); $taxonomy = acewpcf_get_filter_taxonomy( $filter ); if ( empty( $terms ) ) { return $labels; } $term_map = acewpcf_get_admin_term_map( $terms ); $taxonomy_label = acewpcf_get_admin_taxonomy_label( $taxonomy ); foreach ( $terms as $term ) { $path = acewpcf_get_admin_term_path( $term, $term_map, $taxonomy ); $label = $term->name; if ( '' !== $path ) { $label = $path . ' / ' . $label; } $labels[] = $label; if ( count( $labels ) >= 3 ) { break; } } return $labels; } function acewpcf_get_filter_term_preview_label_map( $filter_sets ) { $filter_sets = is_array( $filter_sets ) ? $filter_sets : array(); $preview_map = array(); $terms_by_filter = array(); $term_ids_by_tax = array(); $term_maps = array(); foreach ( $filter_sets as $filter ) { $filter_id = sanitize_key( isset( $filter['id'] ) ? $filter['id'] : '' ); $preview_map[ $filter_id ] = array(); if ( '' === $filter_id || ! acewpcf_filter_uses_terms( $filter ) ) { continue; } $taxonomy = acewpcf_get_filter_taxonomy( $filter ); if ( ! taxonomy_exists( $taxonomy ) ) { continue; } $term_ids = isset( $filter['term_ids'] ) && is_array( $filter['term_ids'] ) ? $filter['term_ids'] : array(); if ( empty( $term_ids ) ) { continue; } $term_order = isset( $filter['term_order'] ) && is_array( $filter['term_order'] ) ? $filter['term_order'] : $term_ids; $term_ids = acewpcf_sort_term_ids_by_order( $term_ids, $term_order ); $term_ids = array_values( array_filter( array_map( 'absint', $term_ids ) ) ); if ( empty( $term_ids ) ) { continue; } $terms_by_filter[ $filter_id ] = array( 'taxonomy' => $taxonomy, 'term_ids' => $term_ids, ); if ( ! isset( $term_ids_by_tax[ $taxonomy ] ) ) { $term_ids_by_tax[ $taxonomy ] = array(); } $term_ids_by_tax[ $taxonomy ] = array_merge( $term_ids_by_tax[ $taxonomy ], $term_ids ); } foreach ( $term_ids_by_tax as $taxonomy => $term_ids ) { $term_ids = array_values( array_unique( array_filter( array_map( 'absint', $term_ids ) ) ) ); if ( empty( $term_ids ) ) { $term_maps[ $taxonomy ] = array(); continue; } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'include' => $term_ids, 'orderby' => 'include', ) ); $term_maps[ $taxonomy ] = array(); if ( is_wp_error( $terms ) ) { continue; } foreach ( $terms as $term ) { if ( $term instanceof WP_Term ) { $term_maps[ $taxonomy ][ (int) $term->term_id ] = $term; } } } foreach ( $terms_by_filter as $filter_id => $filter_config ) { $taxonomy = $filter_config['taxonomy']; $term_map = isset( $term_maps[ $taxonomy ] ) ? $term_maps[ $taxonomy ] : array(); $labels = array(); foreach ( $filter_config['term_ids'] as $term_id ) { if ( ! isset( $term_map[ $term_id ] ) ) { continue; } $term = $term_map[ $term_id ]; $path = acewpcf_get_admin_term_path( $term, $term_map, $taxonomy ); $label = $term->name; if ( '' !== $path ) { $label = $path . ' / ' . $label; } $labels[] = $label; if ( count( $labels ) >= 3 ) { break; } } $preview_map[ $filter_id ] = $labels; } return $preview_map; } function acewpcf_get_filter_panel_preview_labels( $panel, $filter_sets ) { $panel = is_array( $panel ) ? $panel : array(); $filter_sets = is_array( $filter_sets ) ? $filter_sets : array(); $labels = array(); $filter_ids = isset( $panel['filter_ids'] ) ? acewpcf_parse_filter_id_list( $panel['filter_ids'] ) : array(); foreach ( $filter_ids as $filter_id ) { if ( ! isset( $filter_sets[ $filter_id ]['name'] ) ) { continue; } $labels[] = $filter_sets[ $filter_id ]['name']; if ( count( $labels ) >= 4 ) { break; } } return $labels; } function acewpcf_render_color_style_field( $field_id, $field_name, $label, $value, $placeholder, $description = '' ) { ?>