\WP_Stager_Integration\Calendar::get_filter_taxonomy_terms(string $taxonomy, array $events = array()): array

Description

Get the filter taxonomy terms.

Parameters:

ParameterTypeDescription
$taxonomy string The taxonomy to get the terms for.
$events array The events to get the terms for.

Returns:

array The terms for the taxonomy.

Information

Fileclass-calendar.php line 2314

Full Code

/**
 * Get the filter taxonomy terms.
 *
 * @param string $taxonomy The taxonomy to get the terms for.
 * @param array $events The events to get the terms for.
 *
 * @return array The terms for the taxonomy.
 */
public static function get_filter_taxonomy_terms( $taxonomy, $events = array() ) {

    // Type casting
    $taxonomy = (string) $taxonomy;
    $events   = (array) $events;

    if ( ! $events ) {
        $events = (array) (Event::get_all_events() ?: array());
    }

    // Flatten events (may be flat list or nested hierarchy: year → month → day → events)
    $flat_events = self::flatten_events_for_filter( $events );

    // Get terms
    $terms = (array) (self::extract_nested_taxonomy_terms( $taxonomy, $flat_events ) ?: array());
    $terms = (array) (self::parse_raw_taxonomy_terms( $terms, $taxonomy ) ?: array());

    // Build term map
    $terms = array_map( function ( $term ) use ( $taxonomy ) {

        // Init
        $label = (string) $term;

        if ( 'location.country' === $taxonomy ) {

            // Map ISO codes to country names
            $country_names = (array) (\WP_Stager_Integration\Admin::get_countries() ?: array());
            $label         = (string) ($country_names[ $term ] ?? $term);
        }

        return array(
    'taxonomy' => $taxonomy,
    'label'    => $label,
    'value'    => $term,
        );
    }, $terms );

    // Remove values excluded in global calendar settings (per filter field).
    $exclude_rows = (array) ( Options::get_calendar_filter_exclude_values() ?: array() );

    // Init
    $excluded_set = array();

    // Loop over exclude rows
    foreach ( $exclude_rows as $row ) {

        // Continue if row is not an array
        if ( ! is_array( $row ) ) {
            continue;
        }

        // Continue if field does not match taxonomy
        if ( (string) ( $row[ 'field' ] ?? '' ) !== $taxonomy ) {
            continue;
        }

        // Add exclude values to excluded set
        $excluded_set = array_merge( $excluded_set, (array) ( $row[ 'exclude_values' ] ?? array() ) );
    }

    // Remove duplicates and empty values
    $excluded_set = array_values( array_unique( $excluded_set ) );

    // Check if terms should be excluded
    if ( ! empty( $excluded_set ) ) {

        // Remove any terms whose value is in the excluded set
        $terms = array_values( array_filter( $terms, function ( $t ) use ( $excluded_set ) {
                    return ! in_array( (string) ( $t[ 'value' ] ?? '' ), $excluded_set, true );
                } ) );
    }

    /**
     * Filters the terms displayed in the calendar filter
     *
     * @param array[] $terms {
     *  An array of terms
     *
     *  @type string $taxonomy  The taxonomy of the term
     *  @type string $label     The label of the term
     *  @type string $value     The filter value
     * }
     * @param string $taxonomy The taxonomy to filter the terms for
     *
     * @return array[]    Must return an array of terms (arrays with the above sub keys)
     *
     * @example
     * // Only include 'Amsterdam' and 'Rotterdam' as values to filter on
     * add_filter( 'wpstagerintegration_calendar_filter_taxonomy_terms', function ( $terms, $taxonomy ) {
     *
     *     // Return original values if we're not filtering values for the 'location.city' field
     *     if ( $taxonomy !== 'location.city' ) {
     *         return $terms;
     *     }
     *
     *     // Init
     *     $subset = array();
     *
     *     // Loop over terms
     *     foreach ( $terms as $term ) {
     *
     *         // Skip terms not matching specific values
     *         if ( ! in_array( $term[ 'value' ], array( 'Amsterdam', 'Rotterdam' ) ) ) {
     *             continue;
     *         }
     *
     *         // Add $term to $subset
     *         $subset[] = $term;
     *     }
     *
     *     // Return only a subset of terms
     *     return $subset;
     * }, 10, 2 );
     */
    $terms = (array) (apply_filters( 'wpstagerintegration_calendar_filter_taxonomy_terms', $terms, $taxonomy ) ?: array());

    return $terms;
}

šŸ’” If you ever get stuck or have a question, please check our FAQs, our Free Integration Service, our paid Full Integration Service, or reach out to us!

Get WP Stager Integration

šŸŽ Limited offer: Use code WELCOME26 to get your first month for free!