__( 'is', 'adpresso' ), 'is_not' => __( 'is not', 'adpresso' ), ]; } /** * Returns the default UI configuration for a simple text value input. * Child classes MUST override this to define their specific value fields. * * @param array $stored_node_data * * @return array */ public function get_value_config( array $stored_node_data = [] ): array { $authors = get_users( [ 'who' => 'authors', 'fields' => [ 'ID', 'display_name' ] ] ); $options = []; foreach ( $authors as $author ) { $options[] = [ 'value' => $author->ID, 'label' => $author->display_name, ]; } return [ 'type' => 'token_select', 'multiple' => true, 'options' => $options, 'placeholder' => __( 'Select authors...', 'adpresso' ), 'description' => __( 'Show ads based on selected authors.', 'adpresso' ), 'link' => 'https://wpadpresso.com/docs/conditions/author/?utm_source=plugin-backend&utm_medium=manual-link&utm_campaign=cond-author&utm_content=modal', ]; } /** * @return string */ public function get_description(): string { return __( 'Show ads only for specific post authors.', 'adpresso' ); } /** * Provides a default formatter for simple values. * Child classes can override this for complex values (like Post IDs -> Titles). */ public function format_value( mixed $raw ): string { if ( is_array( $raw ) ) { $names = array_map( fn( $id ) => get_userdata( (int) $id )?->display_name ?? "ID $id", $raw ); return implode( ', ', $names ); } if ( is_numeric( $raw ) ) { return get_userdata( (int) $raw )?->display_name ?? "ID $raw"; } return (string) $raw; } /** * The core logic to determine whether the condition is met. * MUST be implemented by the child class. * * @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 { global $post; $context = Plugin::get()->context; if ( isset( $context['is_singular'] ) && ! $context['is_singular'] ) { // A list page has no single author, except an author archive. $post is just its first post. if ( empty( $context['is_author'] ) ) { return 'is_not' === $operator; } $current_author_id = (int) get_queried_object_id(); } elseif ( $post instanceof WP_Post ) { $current_author_id = (int) $post->post_author; } else { return false; } $selected_author_ids = array_map( 'intval', (array) $value ); if ( empty( $selected_author_ids ) ) { return true; // No authors selected, condition is always true. } $is_match = in_array( $current_author_id, $selected_author_ids, true ); if ( $operator === 'is' ) { return $is_match; } if ( $operator === 'is_not' ) { return ! $is_match; } return false; // Unknown operator, default to false. } }