$content_type, 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => 1, 'paged' => 1, 'acewpcf_skip_request_filters' => true, 'ignore_sticky_posts' => true, 'no_found_rows' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ); } function acewpcf_get_author_option_definitions( $content_type = 'post' ) { static $cache = array(); $content_type = acewpcf_sanitize_content_type( $content_type ); if ( isset( $cache[ $content_type ] ) ) { return $cache[ $content_type ]; } $definitions = array(); $users = get_users( array( 'has_published_posts' => array( $content_type ), 'fields' => array( 'ID', 'display_name', 'user_nicename' ), 'orderby' => 'display_name', 'order' => 'ASC', ) ); foreach ( $users as $user ) { $user_id = isset( $user->ID ) ? absint( $user->ID ) : 0; if ( $user_id <= 0 ) { continue; } $label = isset( $user->display_name ) && '' !== (string) $user->display_name ? (string) $user->display_name : ( isset( $user->user_nicename ) ? (string) $user->user_nicename : (string) $user_id ); $definitions[ 'author-' . $user_id ] = $label; } $cache[ $content_type ] = $definitions; return $definitions; } function acewpcf_get_date_option_definitions( $content_type = 'post', $granularity = 'month' ) { static $cache = array(); $content_type = acewpcf_sanitize_content_type( $content_type ); $granularity = acewpcf_sanitize_date_granularity( $granularity ); $cache_key = $content_type . ':' . $granularity; if ( isset( $cache[ $cache_key ] ) ) { return $cache[ $cache_key ]; } global $wpdb; $dates = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Distinct publish months for the date filter; results are statically cached per content type and granularity. $wpdb->prepare( "SELECT DISTINCT DATE_FORMAT(post_date, '%%Y-%%m') FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = %s ORDER BY post_date DESC", $content_type ) ); $definitions = array(); $seen_years = array(); if ( ! is_array( $dates ) ) { $cache[ $cache_key ] = $definitions; return $definitions; } foreach ( $dates as $year_month ) { $year_month = (string) $year_month; if ( ! preg_match( '/^\d{4}-\d{2}$/', $year_month ) ) { continue; } $year = substr( $year_month, 0, 4 ); $month = (int) substr( $year_month, 5, 2 ); if ( 'year' === $granularity ) { if ( isset( $seen_years[ $year ] ) ) { continue; } $seen_years[ $year ] = true; $definitions[ 'y-' . $year ] = $year; continue; } $timestamp = strtotime( $year_month . '-01 00:00:00' ); $label = $timestamp ? wp_date( 'F Y', $timestamp ) : $year_month; $definitions[ 'ym-' . $year_month ] = $label; } $cache[ $cache_key ] = $definitions; return $definitions; } function acewpcf_get_virtual_option_definitions( $filter ) { $filter = is_array( $filter ) ? $filter : array(); $source_type = isset( $filter['source_type'] ) ? acewpcf_sanitize_filter_source_type( $filter['source_type'] ) : ''; $content_type = acewpcf_get_filter_content_type( $filter ); if ( 'author' === $source_type ) { return acewpcf_get_author_option_definitions( $content_type ); } if ( 'date' === $source_type ) { return acewpcf_get_date_option_definitions( $content_type, isset( $filter['date_granularity'] ) ? $filter['date_granularity'] : 'month' ); } return array(); } function acewpcf_sort_virtual_option_keys( $option_keys, $option_order ) { $option_keys = array_values( array_filter( array_unique( array_map( 'sanitize_title', is_array( $option_keys ) ? $option_keys : array() ) ) ) ); $option_order = is_array( $option_order ) ? $option_order : array(); if ( ! empty( $option_order ) && array_keys( $option_order ) === range( 0, count( $option_order ) - 1 ) ) { $normalized_option_order = array(); foreach ( $option_order as $index => $option_key ) { $option_key = sanitize_title( (string) $option_key ); if ( '' !== $option_key ) { $normalized_option_order[ $option_key ] = $index + 1; } } $option_order = $normalized_option_order; } if ( empty( $option_keys ) || empty( $option_order ) ) { return $option_keys; } $order_map = array(); foreach ( $option_keys as $index => $option_key ) { $raw_order = isset( $option_order[ $option_key ] ) ? trim( (string) $option_order[ $option_key ] ) : ''; $order_map[ $option_key ] = array( 'order' => '' === $raw_order ? 1000 + $index : max( 1, absint( $raw_order ) ), 'index' => $index, ); } usort( $option_keys, static function ( $left, $right ) use ( $order_map ) { $left_position = isset( $order_map[ $left ] ) ? $order_map[ $left ] : array( 'order' => 1000, 'index' => 0, ); $right_position = isset( $order_map[ $right ] ) ? $order_map[ $right ] : array( 'order' => 1000, 'index' => 0, ); if ( $left_position['order'] === $right_position['order'] ) { return $left_position['index'] <=> $right_position['index']; } return $left_position['order'] <=> $right_position['order']; } ); return array_values( $option_keys ); } function acewpcf_sanitize_virtual_option_keys( $source_type, $option_keys, $option_order = array(), $filter = array() ) { $source_type = sanitize_key( (string) $source_type ); $filter = is_array( $filter ) ? $filter : array(); if ( empty( $filter['source_type'] ) ) { $filter['source_type'] = $source_type; } $definitions = acewpcf_get_virtual_option_definitions( $filter ); $allowed = array_keys( $definitions ); $option_keys = is_array( $option_keys ) ? $option_keys : array(); $sanitized = array(); foreach ( $option_keys as $option_key ) { $option_key = sanitize_title( (string) $option_key ); if ( '' === $option_key || ! in_array( $option_key, $allowed, true ) || in_array( $option_key, $sanitized, true ) ) { continue; } $sanitized[] = $option_key; } if ( empty( $sanitized ) ) { $sanitized = $allowed; } return acewpcf_sort_virtual_option_keys( $sanitized, $option_order ); } function acewpcf_get_filter_virtual_option_keys( $filter ) { $filter = is_array( $filter ) ? $filter : array(); $source_type = isset( $filter['source_type'] ) ? sanitize_key( (string) $filter['source_type'] ) : ''; return acewpcf_sanitize_virtual_option_keys( $source_type, isset( $filter['option_keys'] ) ? $filter['option_keys'] : array(), isset( $filter['option_order'] ) ? $filter['option_order'] : array(), $filter ); } function acewpcf_get_virtual_option_term_id( $source_type, $slug ) { $hash = hexdec( substr( md5( 'acewpcf|' . sanitize_key( (string) $source_type ) . '|' . sanitize_title( (string) $slug ) ), 0, 7 ) ); return (int) max( 1, $hash ); } function acewpcf_get_virtual_taxonomy( $source_type ) { $source_type = sanitize_key( (string) $source_type ); return in_array( $source_type, array( 'author', 'date' ), true ) ? 'acewpcf_' . $source_type : ''; } function acewpcf_make_virtual_term( $source_type, $slug, $label, $count = 0 ) { $term_id = acewpcf_get_virtual_option_term_id( $source_type, $slug ); $taxonomy = acewpcf_get_virtual_taxonomy( $source_type ); return new WP_Term( (object) array( 'term_id' => $term_id, 'name' => (string) $label, 'slug' => sanitize_title( (string) $slug ), 'term_group' => 0, 'term_taxonomy_id' => $term_id, 'taxonomy' => $taxonomy, 'description' => '', 'parent' => 0, 'count' => (int) $count, ) ); } function acewpcf_get_virtual_filter_terms( $filter ) { $filter = is_array( $filter ) ? $filter : array(); $source_type = isset( $filter['source_type'] ) ? sanitize_key( (string) $filter['source_type'] ) : ''; $definitions = acewpcf_get_virtual_option_definitions( $filter ); $option_keys = acewpcf_get_filter_virtual_option_keys( $filter ); $terms = array(); foreach ( $option_keys as $slug ) { if ( ! isset( $definitions[ $slug ] ) ) { continue; } $terms[] = acewpcf_make_virtual_term( $source_type, $slug, $definitions[ $slug ] ); } return $terms; } function acewpcf_parse_filter_request_search( $filter_id = '', $raw_value = null ) { $filter = acewpcf_get_filter_set( $filter_id ); if ( empty( $filter ) || ! acewpcf_is_search_filter( $filter ) ) { return ''; } if ( null === $raw_value ) { $query_var = acewpcf_get_filter_query_var( $filter['id'] ); $raw_value = acewpcf_get_request_query_value( $query_var, '' ); } $raw_value = sanitize_text_field( (string) $raw_value ); return trim( $raw_value ); } function acewpcf_parse_author_slug_to_id( $slug ) { $slug = sanitize_title( (string) $slug ); if ( 0 !== strpos( $slug, 'author-' ) ) { return 0; } return absint( substr( $slug, 7 ) ); } function acewpcf_parse_date_slug_to_query( $slug ) { $slug = sanitize_title( (string) $slug ); if ( 0 === strpos( $slug, 'ym-' ) && preg_match( '/^ym-(\d{4})-(\d{2})$/', $slug, $matches ) ) { return array( 'year' => (int) $matches[1], 'month' => (int) $matches[2], ); } if ( 0 === strpos( $slug, 'y-' ) && preg_match( '/^y-(\d{4})$/', $slug, $matches ) ) { return array( 'year' => (int) $matches[1], ); } return array(); } function acewpcf_get_all_active_search_queries( $exclude_filter_id = '' ) { $exclude_filter_id = sanitize_key( (string) $exclude_filter_id ); $searches = array(); foreach ( acewpcf_get_filter_sets() as $filter ) { $filter_id = isset( $filter['id'] ) ? sanitize_key( (string) $filter['id'] ) : ''; if ( '' === $filter_id || $filter_id === $exclude_filter_id || ! acewpcf_is_search_filter( $filter ) ) { continue; } $search = acewpcf_parse_filter_request_search( $filter_id ); if ( '' === $search ) { continue; } $searches[ $filter_id ] = array( 'search' => $search, 'content_type' => acewpcf_get_filter_content_type( $filter ), ); } return $searches; } function acewpcf_get_all_active_author_filters( $exclude_filter_id = '' ) { $exclude_filter_id = sanitize_key( (string) $exclude_filter_id ); $authors = array(); foreach ( acewpcf_get_filter_sets() as $filter ) { $filter_id = isset( $filter['id'] ) ? sanitize_key( (string) $filter['id'] ) : ''; if ( '' === $filter_id || $filter_id === $exclude_filter_id || ! acewpcf_is_author_filter( $filter ) ) { continue; } $slugs = acewpcf_parse_filter_request_slugs( $filter_id ); $author_ids = array(); foreach ( $slugs as $slug ) { $author_id = acewpcf_parse_author_slug_to_id( $slug ); if ( $author_id > 0 ) { $author_ids[] = $author_id; } } $author_ids = array_values( array_unique( array_filter( array_map( 'absint', $author_ids ) ) ) ); if ( empty( $author_ids ) ) { continue; } $authors[ $filter_id ] = array( 'author_ids' => $author_ids, 'content_type' => acewpcf_get_filter_content_type( $filter ), ); } return $authors; } function acewpcf_get_all_active_date_filters( $exclude_filter_id = '' ) { $exclude_filter_id = sanitize_key( (string) $exclude_filter_id ); $dates = array(); foreach ( acewpcf_get_filter_sets() as $filter ) { $filter_id = isset( $filter['id'] ) ? sanitize_key( (string) $filter['id'] ) : ''; if ( '' === $filter_id || $filter_id === $exclude_filter_id || ! acewpcf_is_date_filter( $filter ) ) { continue; } $slugs = acewpcf_parse_filter_request_slugs( $filter_id ); $date_clauses = array(); foreach ( $slugs as $slug ) { $clause = acewpcf_parse_date_slug_to_query( $slug ); if ( ! empty( $clause ) ) { $date_clauses[] = $clause; } } if ( empty( $date_clauses ) ) { continue; } $dates[ $filter_id ] = array( 'clauses' => $date_clauses, 'content_type' => acewpcf_get_filter_content_type( $filter ), ); } return $dates; } function acewpcf_apply_virtual_filters_to_query_args( &$query_args, $content_type, $exclude_filter_id = '' ) { $query_args = is_array( $query_args ) ? $query_args : array(); $content_type = acewpcf_sanitize_content_type( $content_type ); $search_parts = array(); foreach ( acewpcf_get_all_active_search_queries( $exclude_filter_id ) as $search_data ) { if ( $content_type !== $search_data['content_type'] ) { continue; } $search_parts[] = $search_data['search']; } if ( ! empty( $search_parts ) ) { $existing_search = isset( $query_args['s'] ) ? trim( (string) $query_args['s'] ) : ''; $query_args['s'] = trim( $existing_search . ' ' . implode( ' ', $search_parts ) ); } $author_ids = array(); foreach ( acewpcf_get_all_active_author_filters( $exclude_filter_id ) as $author_data ) { if ( $content_type !== $author_data['content_type'] ) { continue; } $author_ids = array_merge( $author_ids, $author_data['author_ids'] ); } $author_ids = array_values( array_unique( array_filter( array_map( 'absint', $author_ids ) ) ) ); if ( ! empty( $author_ids ) ) { $query_args['author__in'] = $author_ids; } $date_query = isset( $query_args['date_query'] ) && is_array( $query_args['date_query'] ) ? $query_args['date_query'] : array(); foreach ( acewpcf_get_all_active_date_filters( $exclude_filter_id ) as $date_data ) { if ( $content_type !== $date_data['content_type'] ) { continue; } $group = $date_data['clauses']; if ( count( $group ) > 1 ) { $group['relation'] = 'OR'; } $date_query[] = $group; } if ( ! empty( $date_query ) ) { $query_args['date_query'] = $date_query; } } function acewpcf_get_filter_count_context_virtual_state( $filter ) { $filter = is_array( $filter ) ? $filter : array(); $filter_id = isset( $filter['id'] ) ? sanitize_key( (string) $filter['id'] ) : ''; $content_type = acewpcf_get_filter_content_type( $filter ); return array( 'search' => acewpcf_get_all_active_search_queries( $filter_id ), 'authors' => acewpcf_get_all_active_author_filters( $filter_id ), 'dates' => acewpcf_get_all_active_date_filters( $filter_id ), 'content_type' => $content_type, ); } function acewpcf_get_virtual_filter_count_map( $filter, $active_tax_clauses = array() ) { $filter = is_array( $filter ) ? $filter : array(); $content_type = acewpcf_get_filter_content_type( $filter ); $filter_id = isset( $filter['id'] ) ? sanitize_key( (string) $filter['id'] ) : ''; $source_type = isset( $filter['source_type'] ) ? sanitize_key( (string) $filter['source_type'] ) : ''; $terms = acewpcf_get_virtual_filter_terms( $filter ); $count_map = array(); $query_args = acewpcf_get_content_count_query_base_args( $content_type ); $tax_query = array(); foreach ( $terms as $term ) { if ( $term instanceof WP_Term ) { $count_map[ (int) $term->term_id ] = 0; } } if ( empty( $count_map ) ) { return $count_map; } foreach ( acewpcf_normalize_active_filter_tax_clauses( $active_tax_clauses ) as $filter_clause ) { if ( empty( $filter_clause['taxonomy'] ) || empty( $filter_clause['slugs'] ) ) { continue; } $tax_query[] = array( 'taxonomy' => $filter_clause['taxonomy'], 'field' => 'slug', 'terms' => $filter_clause['slugs'], 'operator' => ( isset( $filter_clause['operator'] ) && 'and' === $filter_clause['operator'] && count( $filter_clause['slugs'] ) > 1 ) ? 'AND' : 'IN', ); } if ( ! empty( $tax_query ) ) { $query_args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Contextual filter counts require taxonomy constraints. } acewpcf_apply_virtual_filters_to_query_args( $query_args, $content_type, $filter_id ); $query_args['posts_per_page'] = -1; $query_args['no_found_rows'] = true; $query_args['fields'] = 'all'; $query_args['orderby'] = 'none'; $query_args['cache_results'] = false; $query = new WP_Query( $query_args ); $posts = ! empty( $query->posts ) ? $query->posts : array(); wp_reset_postdata(); $slug_counts = array(); foreach ( $posts as $post ) { if ( ! ( $post instanceof WP_Post ) ) { continue; } if ( 'author' === $source_type ) { $slug = 'author-' . absint( $post->post_author ); $slug_counts[ $slug ] = isset( $slug_counts[ $slug ] ) ? $slug_counts[ $slug ] + 1 : 1; continue; } if ( 'date' === $source_type ) { $year = mysql2date( 'Y', $post->post_date, false ); $year_month = mysql2date( 'Y-m', $post->post_date, false ); $year_slug = 'y-' . $year; $month_slug = 'ym-' . $year_month; $slug_counts[ $year_slug ] = isset( $slug_counts[ $year_slug ] ) ? $slug_counts[ $year_slug ] + 1 : 1; $slug_counts[ $month_slug ] = isset( $slug_counts[ $month_slug ] ) ? $slug_counts[ $month_slug ] + 1 : 1; } } foreach ( $terms as $term ) { if ( ! ( $term instanceof WP_Term ) ) { continue; } $count_map[ (int) $term->term_id ] = isset( $slug_counts[ $term->slug ] ) ? (int) $slug_counts[ $term->slug ] : 0; } return $count_map; }