taxonomy = $taxonomy; $this->id = $id; $this->label = $label; $this->group = $group; } public function get_execution_context(): string { return ConditionInterface::CONTEXT_BACKEND; } public function get_supported_operators(): array { return [ 'is' => __( 'is', 'adpresso' ), 'is_not' => __( 'is not', 'adpresso' ), ]; } public function get_value_config( array $stored_node_data = [] ): array { $terms = get_terms( [ 'taxonomy' => $this->taxonomy, 'hide_empty' => false, ] ); $options = []; if ( ! is_wp_error( $terms ) && is_array( $terms ) ) { foreach ( $terms as $term ) { $options[] = [ 'value' => $term->term_id, 'label' => esc_html( $term->name ), ]; } } return [ 'type' => 'token_select', 'multiple' => true, 'options' => $options, /* translators: %s: taxonomy label, e.g. "Category". */ 'placeholder' => sprintf( __( 'Select %s...', 'adpresso' ), $this->label ), /* translators: %s: taxonomy label, e.g. "Category". */ 'description' => sprintf( __( 'Show ads based on selected %s.', 'adpresso' ), $this->label ), ]; } public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { $raw = [ $raw ]; } $names = array_map( function ( $term_id ) { if ( ! is_numeric( $term_id ) ) { return __( 'Invalid Term', 'adpresso' ); } $term = get_term( (int) $term_id, $this->taxonomy ); return ( $term && ! is_wp_error( $term ) ) ? esc_html( $term->name ) : "ID $term_id"; }, $raw ); return implode( ', ', $names ); } /** * Evaluates the condition against the currently displayed post. * * @param string $operator 'is' or 'is_not'. * @param mixed $value The selected term IDs. * @param Entity $entity The object (Ad/Group/Placement) being checked. * * @return bool True, if the condition is met. */ public function evaluate( string $operator, mixed $value, Entity $entity ): bool { $context = Plugin::get()->context; if ( isset( $context['is_singular'] ) && ! $context['is_singular'] ) { // A list page shows none of the selected items. return 'is_not' === $operator; } $current_id = get_queried_object_id(); if ( $current_id <= 0 ) { return false; } $selected_ids = array_map( 'intval', (array) $value ); $selected_ids = array_filter( $selected_ids, fn( $id ) => $id > 0 ); if ( empty( $selected_ids ) ) { return true; } $is_match = has_term( $selected_ids, $this->taxonomy, $current_id ); if ( $operator === 'is' ) { return $is_match; } if ( $operator === 'is_not' ) { return ! $is_match; } return false; } }