esult = []; tribe_cache()->set( self::CACHE_KEY, $result, 3600 ); return $result; } $categories_with_meta = array_map( fn( $category ) => $this->get_category_meta( $category ), $categories ); $filtered_categories = $this->filter_categories( $categories_with_meta ); /** * Filters the final list of categories shown in the dropdown. * * @since 6.14.0 * * @param array $filtered_categories The final processed categories. */ $result = (array) apply_filters( 'tec_events_category_color_dropdown_categories', $this->sort_by_priority( $filtered_categories ) ); // Cache the result for 1 hour (3600 seconds). tribe_cache()->set( self::CACHE_KEY, $result, 3600 ); return $result; } /** * Busts the dropdown categories cache. * * @since 6.14.0 */ public function bust_dropdown_categories_cache(): void { tribe_cache()->delete( self::CACHE_KEY ); } /** * Checks if there are categories with colors available. * * @since 6.14.0 * * @return bool True if there are categories with colors, false otherwise. */ public function has_dropdown_categories(): bool { $categories = $this->get_dropdown_categories(); return ! empty( $categories ); } /** * Fetches all event categories. * * @since 6.14.0 * * @return array Array of WordPress term objects representing event categories. */ protected function get_categories(): array { $terms = get_terms( [ 'taxonomy' => Tribe__Events__Main::TAXONOMY, 'hide_empty' => false, ] ); // Handle potential WP_Error. if ( is_wp_error( $terms ) ) { return []; } /** * Filters the raw list of event categories before processing. * * @since 6.14.0 * * @param array $categories The retrieved categories. */ return (array) apply_filters( 'tec_events_category_color_raw_categories', $terms ); } /** * Retrieves category metadata (color, priority, hidden status). * * @since 6.14.0 * * @param WP_Term $category The category term object. * * @return array{ * slug: string, * name: string, * priority: int, * primary: string, * hidden: bool * } Array containing the category's metadata. */ protected function get_category_meta( WP_Term $category ): array { $meta_instance = tribe( Event_Category_Meta::class )->set_term( $category->term_id ); $priority = $meta_instance->get( $this->get_key( 'priority' ) ); $category_meta = [ 'slug' => $category->slug ?? '', 'name' => $category->name ?? '', 'priority' => is_numeric( $priority ) ? (int) $priority : -1, 'primary' => $meta_instance->get( $this->get_key( 'primary' ) ) ?? '', 'hidden' => (bool) $meta_instance->get( $this->get_key( 'hide_from_legend' ) ), ]; /** * Filters metadata of a single category. * * @since 6.14.0 * * @param array{ * slug: string, * name: string, * priority: int, * primary: string, * hidden: bool * } $category_meta The metadata of the category. * @param WP_Term $category The category term object. */ return (array) apply_filters( 'tec_events_category_color_category_meta', $category_meta, $category ); } /** * Filters categories based on their primary color and visibility settings. * * @since 6.14.0 * * @param array $categories { * slug: string, * name: string, * priority: int, * primary: string, * hidden: bool * }> $categories The list of categories to filter. * * @return array Filtered array of categories. */ protected function filter_categories( array $categories ): array { if ( empty( $categories ) ) { return []; } $show_hidden_categories = tribe_get_option( 'category-color-show-hidden-categories', false ); if ( ! is_bool( $show_hidden_categories ) ) { $show_hidden_categories = false; } $filtered = array_values( array_filter( $categories, fn( $category ) => ! empty( $category['primary'] ) && ( $show_hidden_categories || ! $category['hidden'] ) ) ); /** * Filters the categories after visibility filtering. * * @since 6.14.0 * * @param array $filtered The filtered categories list. */ return (array) apply_filters( 'tec_events_category_color_filtered_categories', $filtered ); } /** * Sorts categories by priority (highest first). * * @since 6.14.0 * * @param array $categories { * slug: string, * name: string, * priority: int, * primary: string, * hidden: bool * }> $categories The list of categories to sort. * * @return array Sorted array of categories. */ protected function sort_by_priority( array $categories ): array { if ( empty( $categories ) ) { return []; } // Validate that all categories have a priority key and it's numeric. $valid_categories = array_filter( $categories, fn( $category ) => isset( $category['priority'] ) && is_numeric( $category['priority'] ) ); // Only sort if we have valid categories. if ( ! empty( $valid_categories ) ) { usort( $valid_categories, fn( $a, $b ) => $b['priority'] <=> $a['priority'] ); } /** * Filters the sorted list of categories. * * @since 6.14.0 * * @param array $categories The sorted categories list. */ return (array) apply_filters( 'tec_events_category_color_sorted_categories', $valid_categories ); } }