| 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 |
|