class-restai-cron.php
1 <?php 2 /** 3 * Schedules background jobs — daily knowledge sync, weekly SEO audit. 4 * 5 * @package RESTai 6 */ 7 8 namespace RESTai; 9 10 if ( ! defined( 'ABSPATH' ) ) { 11 exit; 12 } 13 14 class Cron { 15 16 /** @var Client */ 17 private $client; 18 19 public function __construct( Client $client ) { 20 $this->client = $client; 21 add_action( 'init', array( $this, 'maybe_schedule' ) ); 22 add_action( 'restai_seo_audit', array( $this, 'run_seo_audit' ) ); 23 } 24 25 public function maybe_schedule() { 26 if ( ! wp_next_scheduled( 'restai_knowledge_sync' ) ) { 27 wp_schedule_event( time() + 600, 'daily', 'restai_knowledge_sync' ); 28 } 29 if ( ! wp_next_scheduled( 'restai_seo_audit' ) ) { 30 wp_schedule_event( time() + 3600, 'weekly', 'restai_seo_audit' ); 31 } 32 } 33 34 /** 35 * Sweep recent posts and (re)generate SEO meta if missing. 36 */ 37 public function run_seo_audit() { 38 $plugin = Plugin::instance(); 39 $project_id = $plugin->provisioner->project_for( 'seo_assistant' ); 40 if ( $project_id <= 0 ) { 41 return; 42 } 43 44 $posts = get_posts( array( 45 'post_type' => array( 'post', 'page' ), 46 'post_status' => 'publish', 47 'posts_per_page' => 50, 48 'meta_query' => array( 49 'relation' => 'OR', 50 array( 'key' => '_yoast_wpseo_metadesc', 'compare' => 'NOT EXISTS' ), 51 array( 'key' => '_yoast_wpseo_metadesc', 'value' => '', 'compare' => '=' ), 52 ), 53 ) ); 54 55 $seo = new SEO( $this->client ); 56 foreach ( $posts as $post ) { 57 $seo->generate_for_post( $post->ID ); 58 } 59 } 60 }