__( 'is between', 'adpresso' ), ]; } public function get_value_config( array $stored_node_data = [] ): array { return [ 'type' => 'date_range', 'description' => __( 'Show ads based on the post\'s publish date. Leave a field empty for no lower/upper bound.', 'adpresso' ), 'link' => '', ]; } public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { return ''; } $start = $raw['start'] ?? ''; $end = $raw['end'] ?? ''; if ( $start && $end ) { /* translators: 1: start date, 2: end date. */ return sprintf( __( '%1$s to %2$s', 'adpresso' ), $start, $end ); } if ( $start ) { /* translators: %s: start date. */ return sprintf( __( 'from %s', 'adpresso' ), $start ); } if ( $end ) { /* translators: %s: end date. */ return sprintf( __( 'until %s', 'adpresso' ), $end ); } return ''; } /** * Evaluates whether the currently displayed post's publish date falls within the selected range. * * @param string $operator 'is_between'. * @param mixed $value ['start' => 'Y-m-d'|'', 'end' => 'Y-m-d'|'']. * @param Entity $entity The object (Ad/Group/Placement) being checked. * * @return bool */ public function evaluate( string $operator, mixed $value, Entity $entity ): bool { if ( ! is_array( $value ) ) { return true; } $start_date = $value['start'] ?? ''; $end_date = $value['end'] ?? ''; if ( ! $start_date && ! $end_date ) { return true; } $start_timestamp = $start_date ? strtotime( $start_date . ' 00:00:00' ) : null; $end_timestamp = $end_date ? strtotime( $end_date . ' 23:59:59' ) : null; $context = Plugin::get()->context; if ( isset( $context['is_singular'] ) && ! $context['is_singular'] ) { return false; } $post = get_queried_object(); if ( ! $post instanceof WP_Post || $post->post_status !== 'publish' ) { RuntimeDebugger::add_server_error_log_entry( 'PublishedDateCondition: No valid, published post is currently displayed.' ); return false; } $publish_timestamp = get_the_time( 'U', $post->ID ); if ( $publish_timestamp === false ) { return false; } if ( $start_timestamp !== null && $publish_timestamp < $start_timestamp ) { return false; } if ( $end_timestamp !== null && $publish_timestamp > $end_timestamp ) { return false; } return true; } }