get_type() : null; } /** * Saves the fields for this box. * * @param int $post_id * @param ParamsHandler $params_handler * * @internal This method should not be called directly. Security checks (nonce verification, * capability checks) are handled by the calling orchestrator class. */ public function save( int $post_id, ParamsHandler $params_handler ): void { static $is_saving = false; if ( $is_saving ) { return; } $is_saving = true; $this->save_scheduling( $post_id, $params_handler ); $this->save_impression_limits( $post_id, $params_handler ); $this->in_generic_save = true; parent::save( $post_id, $params_handler ); $this->in_generic_save = false; $is_saving = false; } /** * Schedule_days is owned by save_scheduling() (weekday whitelist, delete when empty); the generic loop must not rewrite it. * * @param Entity $entity The entity. * * @return array */ public function get_fields( Entity $entity ): array { $fields = parent::get_fields( $entity ); if ( ! $this->in_generic_save ) { return $fields; } return array_values( array_filter( $fields, static fn( $field ) => 'schedule_days' !== ( $field['id'] ?? '' ) ) ); } /** * Saves the active-period scheduling fields. Ads only - unrelated to * premium status, so this always runs regardless of tier. * * @param int $post_id * @param ParamsHandler $params_handler * * @return void */ private function save_scheduling( int $post_id, ParamsHandler $params_handler ): void { if ( get_post_type( $post_id ) !== Constants::POST_TYPE_AD ) { return; } // DATE (Start & End) // We expect strings in the format "2025-07-04 07:37" or "2025-07-04". // We save them as Y-m-d H:i strings for SQL comparison. $date_fields = [ 'start_date', 'end_date' ]; foreach ( $date_fields as $field ) { $raw_date = $params_handler->getString( $field ); $meta_key = Constants::META_PREFIX . $field; if ( ! empty( $raw_date ) ) { // Try to validate and format the date with strtotime $timestamp = strtotime( $raw_date ); if ( $timestamp ) { // Save in standardized format for DB comparison update_post_meta( $post_id, $meta_key, gmdate( 'Y-m-d H:i:s', $timestamp ) ); } else { // Unknown date format, delete the meta field delete_post_meta( $post_id, $meta_key ); } } else { // Delete the meta field if the date is empty delete_post_meta( $post_id, $meta_key ); } } // Days of the week, e.g. ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] $days_raw = Util::get_checked_keys( $params_handler->getArray( 'schedule_days' ) ); $meta_key_days = Constants::META_PREFIX . 'schedule_days'; if ( ! empty( $days_raw ) ) { // Whitelist-Validation $allowed_days = [ 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun' ]; $clean_days = array_values( array_intersect( $days_raw, $allowed_days ) ); if ( ! empty( $clean_days ) ) { update_post_meta( $post_id, $meta_key_days, $clean_days ); } else { delete_post_meta( $post_id, $meta_key_days ); } } else { // If the array is empty, delete the key (default = all days) delete_post_meta( $post_id, $meta_key_days ); } // Times $time_mode = $params_handler->getKey( 'schedule_time_mode', 'all_day' ); update_post_meta( $post_id, Constants::META_PREFIX . 'schedule_time_mode', $time_mode ); if ( $time_mode === 'scheduled' ) { // Only save start/end times if the mode is 'scheduled' // we validate the format of the time fields to be HH:MM $time_fields = [ 'schedule_time_start', 'schedule_time_end' ]; foreach ( $time_fields as $field ) { $val = $params_handler->getString( $field ); // Simple regex for time format if ( preg_match( '/^([01][0-9]|2[0-3]):([0-5][0-9])$/', $val ) ) { update_post_meta( $post_id, Constants::META_PREFIX . $field, $val ); } else { delete_post_meta( $post_id, Constants::META_PREFIX . $field ); } } } else { // Cleanup: If "All day", delete the start/end time meta fields delete_post_meta( $post_id, Constants::META_PREFIX . 'schedule_time_start' ); delete_post_meta( $post_id, Constants::META_PREFIX . 'schedule_time_end' ); } } /** * Saves the shortcut fields and merges/removes the corresponding * 'max_impressions' condition node in the shared conditions_tree. * * @param int $post_id * @param ParamsHandler $params_handler * * @return void */ private function save_impression_limits( int $post_id, ParamsHandler $params_handler ): void { // Like every other Pro field: left untouched on Free, not deleted. if ( ! Util::is_pro_active() ) { return; } $raw = $params_handler->getUnsanitized( 'max_impressions_shortcut', [] ); $raw = is_array( $raw ) ? $raw : []; $enabled = ! empty( $raw['enabled'] ); $count = max( 1, absint( $raw['count'] ?? 0 ) ); $period_amount = max( 1, absint( $raw['period_amount'] ?? 0 ) ); $period_unit = in_array( $raw['period_unit'] ?? '', [ 'hour', 'day', 'week', 'month' ], true ) ? $raw['period_unit'] : 'day'; $cooldown_minutes = absint( $raw['cooldown_minutes'] ?? 0 ); if ( $enabled ) { update_post_meta( $post_id, self::META_KEY_SHORTCUT, [ 'enabled' => true, 'count' => $count, 'period_amount' => $period_amount, 'period_unit' => $period_unit, 'cooldown_minutes' => $cooldown_minutes, ] ); } else { delete_post_meta( $post_id, self::META_KEY_SHORTCUT ); } $this->sync_condition_tree( $params_handler, $enabled, $count, $period_amount, $period_unit, $cooldown_minutes ); } /** * Finds the first 'max_impressions' leaf node in the current conditions_tree * (as submitted this request) and creates/updates/removes it, then hands the * merged tree back to the ParamsHandler for Conditions_Box::save() to persist. * * @param ParamsHandler $params_handler * @param bool $enabled * @param int $count * @param int $period_amount * @param string $period_unit * @param int $cooldown_minutes * * @return void */ private function sync_condition_tree( ParamsHandler $params_handler, bool $enabled, int $count, int $period_amount, string $period_unit, int $cooldown_minutes ): void { $tree = $params_handler->getDecodedJson( 'conditions_tree', [ 'outer_connector' => 'AND', 'groups' => [], ] ); $groups = is_array( $tree['groups'] ?? null ) ? array_values( $tree['groups'] ) : []; $is_or = strtoupper( (string) ( $tree['outer_connector'] ?? 'AND' ) ) === 'OR'; // The shortcut's node is the first max_impressions node; its copies (outer OR) carry the same id plus a suffix. $node_id = null; foreach ( $groups as $group ) { foreach ( ( $group['conditions'] ?? [] ) as $condition ) { if ( ( $condition['type'] ?? '' ) === 'max_impressions' ) { $node_id = preg_replace( '/__g\d+$/', '', (string) ( $condition['id'] ?? '' ) ); break 2; } } } // Take the shortcut's nodes out; a group that held nothing else goes with them. if ( $node_id !== null ) { foreach ( $groups as $index => $group ) { $conditions = $group['conditions'] ?? []; $kept = array_values( array_filter( $conditions, static function ( $condition ) use ( $node_id ) { $id = (string) ( $condition['id'] ?? '' ); return ( $condition['type'] ?? '' ) !== 'max_impressions' || ( $id !== $node_id && ! str_starts_with( $id, $node_id . '__g' ) ); } ) ); if ( count( $kept ) !== count( $conditions ) && empty( $kept ) ) { unset( $groups[ $index ] ); continue; } $groups[ $index ]['conditions'] = $kept; } $groups = array_values( $groups ); } if ( $enabled ) { $node_id = $node_id ?: uniqid( 'c_' ); $node = [ 'type' => 'max_impressions', 'operator' => 'is', 'value' => [ 'count' => $count, 'period_amount' => $period_amount, 'period_unit' => $period_unit, 'cooldown_minutes' => $cooldown_minutes, ], ]; // The cap is ANDed with the existing conditions. Groups are joined by the outer connector and the // conditions inside a group by its opposite: outer AND -> own group, outer OR -> a copy in every group. if ( $is_or && ! empty( $groups ) ) { foreach ( $groups as $index => $group ) { $groups[ $index ]['conditions'][] = [ 'id' => $index === 0 ? $node_id : $node_id . '__g' . $index ] + $node; } } else { $groups[] = [ 'id' => uniqid( 'g_' ), 'inner_connector' => $is_or ? 'AND' : 'OR', 'conditions' => [ [ 'id' => $node_id ] + $node ], ]; } } $tree['groups'] = $groups; $params_handler->override( 'conditions_tree', wp_json_encode( $tree ) ); } }