rescue_missed_expirations(); } /** * Create Action Scheduler job and save into independent meta * * @param Ad $ad Ad instance. * * @return void */ public function save_expiration_date( Ad $ad ): void { $post_id = $ad->get_id(); if ( ! $post_id || ! function_exists( 'as_unschedule_all_actions' ) ) { return; } $args = [ $post_id ]; $group = 'advanced_ads'; $expiry = (int) $ad->get_expiry_date( 'edit' ); as_unschedule_all_actions( Constants::CRON_JOB_AD_EXPIRATION, $args, $group ); if ( $expiry <= 0 ) { delete_post_meta( $post_id, Constants::AD_META_EXPIRATION_TIME ); return; } $datetime = ( new DateTimeImmutable() )->setTimestamp( $expiry ); update_post_meta( $post_id, Constants::AD_META_EXPIRATION_TIME, $datetime->format( 'Y-m-d H:i:s' ) ); if ( $expiry <= time() ) { $this->update_ad_status( $post_id ); return; } as_schedule_single_action( $expiry, Constants::CRON_JOB_AD_EXPIRATION, $args, $group, true ); } /** * Expire any published ads whose expiry timestamp is in the past but whose * post_status was never updated (e.g. the Action Scheduler job failed). * * Runs once per request on `init`, guarded by a transient so it only * queries the database at most once every 15 minutes. * * @return void */ public function rescue_missed_expirations(): void { // Early bail. if ( ! is_admin() || ! function_exists( 'as_unschedule_all_actions' ) ) { return; } $transient_key = 'advanced_ads_expiry_rescue_lock'; if ( get_transient( $transient_key ) ) { return; } // Set the lock first to prevent parallel requests from running the same query. set_transient( $transient_key, 1, 15 * MINUTE_IN_SECONDS ); $now = current_time( 'mysql', true ); // UTC. /** * Find every published ad that has an expiration meta key whose stored * datetime is in the past. The meta value is stored as 'Y-m-d H:i:s' * in UTC by save_expiration_date(). */ $expired_ids = get_posts( [ 'post_type' => Constants::POST_TYPE_AD, 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query [ 'key' => Constants::AD_META_EXPIRATION_TIME, 'value' => $now, 'compare' => '<', 'type' => 'DATETIME', ], ], ] ); foreach ( $expired_ids as $post_id ) { $this->update_ad_status( (int) $post_id ); as_unschedule_all_actions( Constants::CRON_JOB_AD_EXPIRATION, [ $post_id ], Constants::CRON_GROUP_ADVANCED_ADS ); } } /** * Update post status to expired * * @param int $post_id Post ID. * * @return void */ public function update_ad_status( $post_id ): void { $post_id = absint( $post_id ); if ( ! $post_id ) { return; } $post = get_post( $post_id ); if ( ! $post instanceof WP_Post || Constants::POST_TYPE_AD !== $post->post_type ) { return; } if ( Constants::AD_STATUS_EXPIRED === $post->post_status || 'trash' === $post->post_status ) { return; } $this->bypass_cap_post_id = $post_id; add_filter( 'user_has_cap', [ $this, 'grant_edit_cap_for_expiration' ], 10, 3 ); kses_remove_filters(); wp_update_post( [ 'ID' => $post_id, 'post_status' => Constants::AD_STATUS_EXPIRED, ] ); kses_init_filters(); remove_filter( 'user_has_cap', [ $this, 'grant_edit_cap_for_expiration' ], 10 ); $this->bypass_cap_post_id = 0; } /** * Allow Action Scheduler to expire ads when no user is logged in. * * @param array $allcaps All capabilities. * @param array $caps Required capabilities. * @param array $args Capability check args. * * @return array */ public function grant_edit_cap_for_expiration( array $allcaps, array $caps, array $args ): array { if ( ! $this->bypass_cap_post_id || empty( $args[0] ) || 'edit_post' !== $args[0] ) { return $allcaps; } if ( isset( $args[2] ) && (int) $args[2] === $this->bypass_cap_post_id ) { $allcaps['advanced_ads_edit_ads'] = true; } return $allcaps; } }