options = $options; // admin_init, not init: this only re-verifies a recurring schedule that // has no front-end contract, so it never needs to run for anonymous // front-end traffic — an admin visiting the dashboard is enough to // keep it in sync. add_action( 'admin_init', [ $this, 'schedule' ] ); add_action( 'adminoptim_republish_cleanup', [ $this, 'cleanup' ] ); } /** * Schedule the recurring cleanup action if it isn't already scheduled. * * @return void */ public function schedule(): void { if ( function_exists( 'as_has_scheduled_action' ) && ! as_has_scheduled_action( 'adminoptim_republish_cleanup', [], 'admin-optimizer' ) ) { as_schedule_recurring_action( time(), DAY_IN_SECONDS, 'adminoptim_republish_cleanup', [], 'admin-optimizer', true ); } } /** * Delete or trash refresh copies stuck in the pending-cleanup status. * * @return void */ public function cleanup(): void { $post_ids = get_posts( [ 'post_status' => 'republish-trash', 'posts_per_page' => -1, 'fields' => 'ids', 'post_type' => 'any', ] ); foreach ( $post_ids as $post_id ) { if ( 'delete' === ( $this->options['after_republish_action'] ?? 'trash' ) ) { wp_delete_post( $post_id, true ); } else { wp_trash_post( $post_id ); } } } }