__( 'is', 'adpresso' ), 'is_not' => __( 'is not', 'adpresso' ), ]; } /** * Returns the UI configuration for the post condition. * * @param array $stored_node_data * * @return array */ public function get_value_config( array $stored_node_data = [] ): array { $post_types = apply_filters( 'adpresso_condition_post_id_post_types', [ 'post', 'page' ] ); $posts = get_posts( [ 'post_type' => $post_types, 'post_status' => 'any', 'posts_per_page' => (int) apply_filters( 'adpresso_condition_post_id_select_limit', 100 ), 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ] ); $options = []; if ( ! empty( $posts ) ) { foreach ( $posts as $post ) { // Add post-title and type to the label for better clarity. $options[] = [ 'value' => $post->ID, 'label' => sprintf( '%s (%s)%s', esc_html( $post->post_title ), esc_html( $post->post_type ), $post->post_status === 'draft' ? ' (Draft)' : '' ), ]; } } // Add already stored values that aren't in the list. $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 ] ) ) { $post = get_post( (int) $post_id ); if ( $post ) { $options[] = [ 'value' => $post->ID, 'label' => sprintf( '%s (%s) - [%s]', esc_html( $post->post_title ), esc_html( $post->post_type ), __( 'selected', 'adpresso' ) ), ]; } } } return [ 'type' => 'token_select', 'multiple' => true, 'options' => $options, 'placeholder' => __( 'Select posts or pages...', 'adpresso' ), 'description' => __( 'Show ads based on selected posts and pages.', 'adpresso' ), ]; } /** * Provides a default formatter for simple values. * Child classes can override this for complex values (like Post IDs -> Titles). * * @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' ); } $post = get_post( (int) $id ); // Show title and type. return $post ? sprintf( '%s (%s)', esc_html( $post->post_title ), esc_html( $post->post_type ) ) : "ID $id"; }, $raw ); return implode( ', ', $names ); } /** * Evaluates the post condition. * * @param string $operator The selected operator (e.g., 'is', 'contains'). * @param mixed $value The stored value (can be scalar, array, or object). * @param Entity $entity The entity (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 ); // Remove invalid IDs if ( empty( $selected_ids ) ) { return true; } $is_match = in_array( $current_id, $selected_ids, true ); if ( $operator === 'is' ) { return $is_match; } if ( $operator === 'is_not' ) { return ! $is_match; } // Unknown operator. return false; } }