__( 'older than', 'adpresso' ), 'younger_than' => __( 'younger than', 'adpresso' ), ]; } /** * Konfiguriert das Eingabefeld für das Alter (in Tagen) im Admin UI. */ public function get_value_config( array $stored_node_data = [] ): array { return [ 'type' => 'number', 'placeholder' => __( 'Days', 'adpresso' ), 'min' => 0, 'step' => 0.1, 'suffix' => __( 'days', 'adpresso' ), 'description' => __( 'Show ads based on selected content age (in days).', 'adpresso' ), 'link' => 'https://wpadpresso.com/docs/conditions/content-age/?utm_source=plugin-backend&utm_medium=manual-link&utm_campaign=cond-age&utm_content=modal', ]; } /** * Formatiert den Wert für die Anzeige. */ public function format_value( mixed $raw ): string { $days = is_numeric( $raw ) ? (float) $raw : 0; // Translators: %s is the number of days. return sprintf( _n( '%s day', '%s days', $days, 'adpresso' ), number_format_i18n( $days, 1 ) ); } /** * Wertet die Bedingung aus: Ist der angezeigte Inhalt älter/jünger als X Tage? * * This checks the age of the post/page the ad is being displayed on - not the age of the ad/group/placement entity's own post * * @param string $operator 'older_than' oder 'younger_than'. * @param mixed $value Die Anzahl der Tage (sollte numerisch sein). * @param Entity $entity Das Objekt (Ad/Group/Placement), für das geprüft wird. * * @return bool True, wenn die Bedingung erfüllt ist. */ public function evaluate( string $operator, mixed $value, Entity $entity ): bool { if ( ! is_numeric( $value ) ) { return true; } $days_value = (float) $value; if ( $days_value <= 0 ) { return true; } $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( 'ContentAgeCondition: No valid, published post is currently displayed.' ); return false; } $publish_timestamp = get_the_time( 'U', $post->ID ); if ( $publish_timestamp === false ) { return false; } $current_timestamp = time(); $age_in_seconds = $current_timestamp - $publish_timestamp; $threshold_seconds = $days_value * DAY_IN_SECONDS; if ( $operator === 'younger_than' ) { return $age_in_seconds < $threshold_seconds; } elseif ( $operator === 'older_than' ) { return $age_in_seconds > $threshold_seconds; } RuntimeDebugger::add_doing_it_wrong_entry( esc_html( 'ContentAgeCondition: Invalid operator: ' . $operator ) ); return false; } }