wordpress-seo
/
src
/
alerts
/
user-interface
/
default-seo-data
/
default-seo-data-cron-callback-integration.php
default-seo-data-cron-callback-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/alerts/user-interface/default-seo-data/default-seo-data-cron-callback-integration.php
| 1 | <?php |
| 2 | |
| 3 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 | namespace Yoast\WP\SEO\Alerts\User_Interface\Default_SEO_Data; |
| 5 | |
| 6 | use Yoast\WP\SEO\Alerts\User_Interface\Default_Seo_Data\Default_SEO_Data_Cron_Scheduler; |
| 7 | use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 8 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 | use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 11 | |
| 12 | /** |
| 13 | * Cron Callback integration. This handles the actual process of detecting default SEO data in recent posts and updating the relevant options. |
| 14 | * |
| 15 | * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 16 | */ |
| 17 | class Default_SEO_Data_Cron_Callback_Integration implements Integration_Interface { |
| 18 | |
| 19 | use No_Conditionals; |
| 20 | |
| 21 | /** |
| 22 | * The options helper. |
| 23 | * |
| 24 | * @var Options_Helper |
| 25 | */ |
| 26 | private $options_helper; |
| 27 | |
| 28 | /** |
| 29 | * The scheduler. |
| 30 | * |
| 31 | * @var Default_SEO_Data_Cron_Scheduler |
| 32 | */ |
| 33 | private $scheduler; |
| 34 | |
| 35 | /** |
| 36 | * The indexable repository. |
| 37 | * |
| 38 | * @var Indexable_Repository |
| 39 | */ |
| 40 | private $indexable_repository; |
| 41 | |
| 42 | /** |
| 43 | * Constructor. |
| 44 | * |
| 45 | * @param Options_Helper $options_helper The options helper. |
| 46 | * @param Default_SEO_Data_Cron_Scheduler $scheduler The scheduler. |
| 47 | * @param Indexable_Repository $indexable_repository The indexable repository. |
| 48 | */ |
| 49 | public function __construct( |
| 50 | Options_Helper $options_helper, |
| 51 | Default_SEO_Data_Cron_Scheduler $scheduler, |
| 52 | Indexable_Repository $indexable_repository |
| 53 | ) { |
| 54 | $this->options_helper = $options_helper; |
| 55 | $this->scheduler = $scheduler; |
| 56 | $this->indexable_repository = $indexable_repository; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Registers the hooks. |
| 61 | * |
| 62 | * @return void |
| 63 | */ |
| 64 | public function register_hooks() { |
| 65 | \add_action( |
| 66 | Default_SEO_Data_Cron_Scheduler::CRON_HOOK, |
| 67 | [ |
| 68 | $this, |
| 69 | 'detect_default_seo_data_in_recent', |
| 70 | ], |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Detects default SEO data in recent posts and updates the relevant options. |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function detect_default_seo_data_in_recent(): void { |
| 80 | if ( ! \wp_doing_cron() ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | $recent_posts = $this->indexable_repository->get_recently_modified_posts( 'post', 5, false ); |
| 85 | |
| 86 | $recent_default_seo_title = []; |
| 87 | $recent_default_seo_meta_desc = []; |
| 88 | foreach ( $recent_posts as $post ) { |
| 89 | if ( $post->title === null ) { |
| 90 | $recent_default_seo_title[] = $post->object_id; |
| 91 | } |
| 92 | |
| 93 | if ( $post->description === null ) { |
| 94 | $recent_default_seo_meta_desc[] = $post->object_id; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $this->options_helper->set( 'default_seo_title', $recent_default_seo_title ); |
| 99 | $this->options_helper->set( 'default_seo_meta_desc', $recent_default_seo_meta_desc ); |
| 100 | } |
| 101 | } |
| 102 |