__( 'is', 'adpresso' ), 'is_not' => __( 'is not', 'adpresso' ), ]; } /** * Configures the select field for potential parent pages in the admin UI. * Filter the post-types to add other hierarchical post-types if needed. * * @param array $stored_node_data * * @return array */ public function get_value_config( array $stored_node_data = [] ): array { /** * Filters the post-types to add other hierarchical post-types if needed. * * By default, only get pages and posts. * * @param array $post_types The default post types for the condition. * * @since 1.0.0 * */ $parent_post_types = apply_filters( 'adpresso_condition_parent_page_post_types', [ 'page' ] ); $posts = get_posts( [ 'post_type' => $parent_post_types, 'post_status' => 'publish', 'posts_per_page' => (int) apply_filters( 'adpresso_condition_parent_page_select_limit', 200 ), 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ] ); $options = []; $options[] = [ 'value' => 0, 'label' => __( '— No Parent (Top-Level Page) —', 'adpresso' ), ]; if ( ! empty( $posts ) ) { foreach ( $posts as $post ) { $options[] = [ 'value' => $post->ID, 'label' => sprintf( '%s (#%d)', esc_html( $post->post_title ), $post->ID ), ]; } } // Add already saved values that aren't in the current list, e.g., not published anymore. $current_values = isset( $stored_node_data['value'] ) ? (array) $stored_node_data['value'] : []; foreach ( $current_values as $post_id ) { if ( is_numeric( $post_id ) && ! isset( $options[ $post_id ] ) ) { if ( (int) $post_id === 0 ) { continue; } // Option "No Parent" is already there. $post = get_post( (int) $post_id ); if ( $post ) { $options[] = [ 'value' => $post->ID, 'label' => sprintf( '%s (#%d) - [%s]', esc_html( $post->post_title ), $post->ID, __( 'selected', 'adpresso' ) ), ]; } } } return [ 'type' => 'token_select', 'multiple' => true, // Allow multiple parent pages. 'options' => $options, 'placeholder' => __( 'Select parent pages...', 'adpresso' ), 'description' => __( 'Show ads based on selected parent pages, including child pages.', 'adpresso' ), ]; } /** * Formats the selected parent-page-IDs for display. * * @param mixed $raw * * @return string */ public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { $raw = [ $raw ]; } $names = array_map( function( $id ) { if ( ! is_numeric( $id ) ) { return __( 'Invalid ID', 'adpresso' ); } $id = (int) $id; if ( $id === 0 ) { return __( 'No Parent (Top-Level)', 'adpresso' ); } $post = get_post( $id ); return $post ? sprintf( '%s (#%d)', esc_html( $post->post_title ), $id ) : "ID $id"; }, $raw ); return implode( ', ', $names ); } /** * Evaluates the condition. Is the current page a child of one of the selected parents? * * @param string $operator 'is' or 'is_not'. * @param mixed $value The selected parent IDs (Array with integers) * @param Entity $entity The entity object (Ad/Group/Placement), that is going to be checked.. * * @return bool True, if condition is fulfilled. */ public function evaluate( string $operator, mixed $value, Entity $entity ): bool { // Get the current post. $current_post = null; if ( ( Plugin::get()->context['is_singular'] ?? false ) || ( Plugin::get()->context['is_page'] ?? false ) ) { $current_post = get_queried_object(); } // If we aren't on a single page or do not have a post object the condition 'is child of' cannot be true, except we check for 'top-level'. $current_post_parent_id = 0; // Standard: Top-Level if ( $current_post instanceof WP_Post ) { $current_post_parent_id = $current_post->post_parent; } elseif ( ! ( Plugin::get()->context['is_singular'] ?? false ) && ! ( Plugin::get()->context['is_page'] ?? false ) ) { // If we are not on a single page, // the condition can only be met if ‘0’ (No Parent) // has not been selected and the operator is ‘is_not’. // Or if ‘0’ has been selected and the operator is ‘is’ (which makes no sense). // For simplicity: If not singular, it is never a child of a specific page. // It *could* be considered “no parent” if $selected_parent_ids contains [0]. } // Validate and check the selected parent IDs $selected_parent_ids = array_map( 'intval', (array) $value ); // 0 is a valid selection. // Check if there are parent IDs at all. if ( empty( $selected_parent_ids ) && ! in_array( 0, $selected_parent_ids, true ) ) { // No specific parent selected and "no parent" isn't selected too. return true; // Condition is not restricting. } $is_match = in_array( $current_post_parent_id, $selected_parent_ids, true ); if ( $operator === 'is' ) { // "is child of" return $is_match; } if ( $operator === 'is_not' ) { // "is not child of" return ! $is_match; } // Unknown operator. return false; } }