label, also the keys evaluate() may call (category/tag stay valid for old trees). * * @var array */ private array $labels; public function __construct() { $this->page_type_options = [ 'is_front_page' => __( 'Home Page (Front Page)', 'adpresso' ), // is_front_page() 'is_home' => __( 'Blog Posts Index (Home)', 'adpresso' ), // is_home() 'is_singular' => __( 'Singular Pages (Posts, Pages, CPTs)', 'adpresso' ), // is_singular() 'is_single' => __( 'Single Post', 'adpresso' ), // is_single() 'is_page' => __( 'Page (WP Page)', 'adpresso' ), // is_page() 'is_archive' => __( 'Archive Pages (Category, Tag, Date, Author, CPT Archive)', 'adpresso' ), // is_archive() 'is_category' => __( 'Category Archive', 'adpresso' ), // is_category() 'is_tag' => __( 'Tag Archive', 'adpresso' ), // is_tag() 'is_tax' => __( 'Taxonomy Archive (any custom taxonomy)', 'adpresso' ), // is_tax() 'is_author' => __( 'Author Archive', 'adpresso' ), // is_author() 'is_search' => __( 'Search Results Page', 'adpresso' ), // is_search() 'is_404' => __( '404 Error Page', 'adpresso' ), // is_404() 'is_attachment' => __( 'Attachment Page', 'adpresso' ), // is_attachment() 'is_paged' => __( 'Paginated Pages (page 2 and later)', 'adpresso' ), // is_paged(), or a split post / static front page past page 1. ]; $this->page_type_options = apply_filters( 'adpresso_condition_general_page_type_options', $this->page_type_options ); $this->labels = $this->page_type_options; // Superseded by the dedicated "Taxonomy Archive" condition $selectable_options = array_diff_key( $this->page_type_options, array_flip( [ 'is_category', 'is_tag' ] ) ); $this->page_type_options = array_map( fn( $label, $value ) => [ 'value' => $value, 'label' => $label, ], $selectable_options, array_keys( $selectable_options ) ); } public function get_id(): string { return 'general_page_type'; } public function get_label(): string { return __( 'General Pages', 'adpresso' ); } public function get_group(): string { return __( 'Meta', 'adpresso' ); } public function get_execution_context(): string { return ConditionInterface::CONTEXT_BACKEND; } public function get_supported_operators(): array { return [ 'is_on_any_selected' => __( 'Is on ANY of the selected page types', 'adpresso' ), 'is_not_on_any_selected' => __( 'Is NOT on ANY of the selected page types', 'adpresso' ), ]; } public function get_value_config( array $stored_node_data = [] ): array { return [ 'type' => 'token_select', 'options' => $this->page_type_options, 'description' => __( 'Show ads based on selected general pages (e.g., Search, 404).', 'adpresso' ), 'multiple' => true, 'link' => 'https://wpadpresso.com/docs/conditions/general-pages/?utm_source=plugin-backend&utm_medium=manual-link&utm_campaign=cond-general&utm_content=modal' ]; } public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { return ''; } $labels = []; foreach ( $raw as $type_key ) { $labels[] = $this->labels[ $type_key ] ?? ucfirst( str_replace( '_', ' ', (string) $type_key ) ); } return implode( ', ', $labels ); } public function evaluate( string $operator, mixed $value, Entity $entity ): bool { // $value is an array of the selected page-type-keys (e.g. ['is_front_page', 'is_singular']) $selected_page_types = (array) $value; // None selected, always true. if ( empty( $selected_page_types ) ) { return true; } $match_found = false; foreach ( $selected_page_types as $type_key ) { // is_paged() misses split posts () and a paginated static front page: both use the "page" query var. if ( 'is_paged' === $type_key ) { if ( is_paged() || (int) get_query_var( 'page' ) > 1 ) { $match_found = true; break; } continue; } // Stored values are data: only offered page types may be called, never any other function. if ( is_string( $type_key ) && isset( $this->labels[ $type_key ] ) && function_exists( $type_key ) ) { if ( call_user_func( $type_key ) ) { $match_found = true; break; // One hit is enough for the 'any of' logic } } elseif ( $type_key === 'is_rest_api' ) { // Special cases if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { $match_found = true; break; } } } if ( $operator === 'is_on_any_selected' ) { return $match_found; } if ( $operator === 'is_not_on_any_selected' ) { return ! $match_found; // True, if NONE of the selected types is true } return false; // Unknown operator. } }