__( 'Is on ANY of the selected taxonomy archives', 'adpresso' ), 'is_not_on_any_selected' => __( 'Is NOT on ANY of the selected taxonomy archives', 'adpresso' ), ]; } /** * Every public, queryable taxonomy (built-in and custom) as select options. * * @return array */ private function get_taxonomy_options(): array { $taxonomies = get_taxonomies( [ 'public' => true, 'publicly_queryable' => true ], 'objects' ); $options = []; foreach ( $taxonomies as $taxonomy ) { $options[] = [ 'value' => $taxonomy->name, 'label' => $taxonomy->labels->name, ]; } return apply_filters( 'adpresso_condition_taxonomy_archive_options', $options ); } public function get_value_config( array $stored_node_data = [] ): array { return [ 'type' => 'token_select', 'multiple' => true, 'options' => $this->get_taxonomy_options(), 'placeholder' => __( 'Select taxonomy...', 'adpresso' ), 'description' => __( 'Show ads on the archive page of any of the selected taxonomies (e.g. any Category or Genre archive).', 'adpresso' ), ]; } public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { return ''; } $labels = []; foreach ( $raw as $taxonomy_name ) { $taxonomy = get_taxonomy( $taxonomy_name ); $labels[] = $taxonomy ? $taxonomy->labels->name : $taxonomy_name; } return implode( ', ', $labels ); } /** * Evaluates whether the currently displayed page is the archive of one of * the selected taxonomies. * * @param string $operator 'is_on_any_selected' or 'is_not_on_any_selected'. * @param mixed $value The selected taxonomy slugs. * @param Entity $entity The object (Ad/Group/Placement) being checked. * * @return bool */ public function evaluate( string $operator, mixed $value, Entity $entity ): bool { $selected_taxonomies = (array) $value; if ( empty( $selected_taxonomies ) ) { return true; } $match_found = false; foreach ( $selected_taxonomies as $taxonomy ) { if ( $taxonomy === 'category' ) { $is_match = is_category(); } elseif ( $taxonomy === 'post_tag' ) { $is_match = is_tag(); } else { $is_match = is_tax( $taxonomy ); } if ( $is_match ) { $match_found = true; break; } } if ( $operator === 'is_on_any_selected' ) { return $match_found; } if ( $operator === 'is_not_on_any_selected' ) { return ! $match_found; } return false; } }