__( 'is', 'adpresso' ), 'is_not' => __( 'is not', 'adpresso' ), ]; } /** * Configures the value selection field in the Admin UI. */ public function get_value_config( array $stored_node_data = [] ): array { $post_types = get_post_types( [ 'public' => true, ], 'objects' ); $options = []; if ( ! empty( $post_types ) ) { uasort( $post_types, fn( $a, $b ) => $a->labels->singular_name <=> $b->labels->singular_name ); foreach ( $post_types as $type => $obj ) { $options[] = [ 'value' => $type, 'label' => esc_html( $obj->labels->singular_name ), ]; } } return [ 'type' => 'token_select', 'multiple' => true, 'options' => $options, 'placeholder' => __( 'Select post types...', 'adpresso' ), 'description' => __( 'Show ads based on selected post types.', 'adpresso' ), ]; } /** * Formats the value for display in the admin UI. */ public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { $raw = [ $raw ]; } $labels = array_map( function( $type_slug ) { if ( ! is_string( $type_slug ) ) { return 'Invalid Type'; } $obj = get_post_type_object( $type_slug ); return $obj ? esc_html( $obj->labels->singular_name ) : esc_html( $type_slug ); }, $raw ); return implode( ', ', $labels ); } /** * Evaluates the condition. * * @param string $operator 'is' or 'is_not'. * @param mixed $value The selected post types. * @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 { $current_post_type = Plugin::get()->context['post_type'] ?? null; if ( ! $current_post_type ) { $queried_object = get_queried_object(); if ( $queried_object instanceof WP_Post_Type ) { $current_post_type = $queried_object->name; } elseif ( $queried_object instanceof WP_Taxonomy ) { return false; } else { return false; } } $selected_types = array_map( 'sanitize_key', (array) $value ); $selected_types = array_filter( $selected_types ); if ( empty( $selected_types ) ) { return true; } $is_match = in_array( $current_post_type, $selected_types, true ); if ( $operator === 'is' ) { return $is_match; } if ( $operator === 'is_not' ) { return ! $is_match; } return false; } }