PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.7
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / myyoast-client / user-interface / myyoast-client-integration.php

myyoast-client-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.7, at src/myyoast-client/user-interface/myyoast-client-integration.php

117 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\MyYoast_Client\User_Interface;
4
5 use Exception;
6 use Yoast\WP\SEO\Conditionals\MyYoast_Connection_Conditional;
7 use Yoast\WP\SEO\Integrations\Integration_Interface;
8 use Yoast\WP\SEO\MyYoast_Client\Application\Ports\Client_Registration_Interface;
9 use YoastSEO_Vendor\Psr\Log\LoggerAwareInterface;
10 use YoastSEO_Vendor\Psr\Log\LoggerAwareTrait;
11 use YoastSEO_Vendor\Psr\Log\NullLogger;
12
13 /**
14 * Registers wp-cron for registration key rotation.
15 */
16 class MyYoast_Client_Integration implements Integration_Interface, LoggerAwareInterface {
17 use LoggerAwareTrait;
18
19 private const CRON_HOOK = 'wpseo_myyoast_key_rotation';
20 private const ROTATION_INTERVAL = 'wpseo_myyoast_90days';
21 private const ROTATION_INTERVAL_IN_DAYS = 90;
22
23 /**
24 * The registration manager.
25 *
26 * @var Client_Registration_Interface
27 */
28 private $client_registration;
29
30 /**
31 * MyYoast_Client_Integration constructor.
32 *
33 * @param Client_Registration_Interface $client_registration The registration manager.
34 */
35 public function __construct( Client_Registration_Interface $client_registration ) {
36 $this->client_registration = $client_registration;
37 $this->logger = new NullLogger();
38 }
39
40 /**
41 * Returns the conditionals based on which this integration should be loaded.
42 *
43 * @return array<string> The array of conditionals.
44 */
45 public static function get_conditionals() {
46 return [ MyYoast_Connection_Conditional::class ];
47 }
48
49 /**
50 * Registers hooks.
51 *
52 * @return void
53 */
54 public function register_hooks() {
55 \add_action( 'admin_init', [ $this, 'schedule_key_rotation' ] );
56 \add_action( self::CRON_HOOK, [ $this, 'handle_key_rotation' ] );
57 \add_action( 'wpseo_deactivate', [ $this, 'unschedule_key_rotation' ] );
58 // phpcs:ignore WordPress.WP.CronInterval -- The sniff doesn't understand the self::ROTATION_INTERVAL_IN_DAYS reference.
59 \add_filter( 'cron_schedules', [ $this, 'add_cron_schedule' ] );
60 }
61
62 /**
63 * Adds a 90-day cron schedule.
64 *
65 * @param array<string, array<string, string|int>> $schedules Existing cron schedules.
66 *
67 * @return array<string, array<string, string|int>> Modified schedules.
68 */
69 public function add_cron_schedule( $schedules ) {
70 if ( ! \is_array( $schedules ) ) {
71 $schedules = [];
72 }
73
74 $schedules[ self::ROTATION_INTERVAL ] = [
75 'interval' => ( self::ROTATION_INTERVAL_IN_DAYS * \DAY_IN_SECONDS ),
76 'display' => \esc_html__( 'Every 90 days', 'wordpress-seo' ),
77 ];
78
79 return $schedules;
80 }
81
82 /**
83 * Schedules the key rotation cron job if not already scheduled.
84 *
85 * @return void
86 */
87 public function schedule_key_rotation(): void {
88 if ( ! \wp_next_scheduled( self::CRON_HOOK ) ) {
89 \wp_schedule_event( ( \time() + ( self::ROTATION_INTERVAL_IN_DAYS * \DAY_IN_SECONDS ) ), self::ROTATION_INTERVAL, self::CRON_HOOK );
90 }
91 }
92
93 /**
94 * Clears all scheduled key rotation events.
95 *
96 * @return void
97 */
98 public function unschedule_key_rotation(): void {
99 \wp_clear_scheduled_hook( self::CRON_HOOK );
100 }
101
102 /**
103 * Handles the key rotation cron event.
104 *
105 * @return void
106 */
107 public function handle_key_rotation(): void {
108 try {
109 $this->client_registration->rotate_registration_keys();
110 }
111 catch ( Exception $e ) {
112 // Best-effort — log but don't crash cron.
113 $this->logger->warning( 'Yoast MyYoast key rotation failed: {error}', [ 'error' => $e->getMessage() ] );
114 }
115 }
116 }
117