| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 7 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 8 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Watcher for the enable_schema_aggregation_endpoint option. |
| 12 |
* |
| 13 |
* Monitors the enable_schema_aggregation_endpoint option and sets a timestamp when first enabled. |
| 14 |
*/ |
| 15 |
class Schema_Aggregator_Watcher implements Integration_Interface { |
| 16 |
|
| 17 |
use No_Conditionals; |
| 18 |
|
| 19 |
/** |
| 20 |
* The options helper. |
| 21 |
* |
| 22 |
* @var Options_Helper |
| 23 |
*/ |
| 24 |
protected $options_helper; |
| 25 |
|
| 26 |
/** |
| 27 |
* Schema_Aggregator_Watcher constructor. |
| 28 |
* |
| 29 |
* @param Options_Helper $options_helper The options helper. |
| 30 |
*/ |
| 31 |
public function __construct( Options_Helper $options_helper ) { |
| 32 |
$this->options_helper = $options_helper; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Initializes the integration. |
| 37 |
* |
| 38 |
* This is the place to register hooks and filters. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function register_hooks() { |
| 43 |
\add_action( 'update_option_wpseo', [ $this, 'check_schema_aggregator_enabled' ], 10, 2 ); |
| 44 |
} |
| 45 |
|
| 46 |
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification -- They can really be anything. |
| 47 |
|
| 48 |
/** |
| 49 |
* Checks if the enable_schema_aggregation_endpoint option has been enabled for the first time. |
| 50 |
* |
| 51 |
* @param array $old_value The old value of the option. |
| 52 |
* @param array $new_value The new value of the option. |
| 53 |
* |
| 54 |
* @return bool Whether the schema_aggregator_enabled_on timestamp was set. |
| 55 |
*/ |
| 56 |
public function check_schema_aggregator_enabled( $old_value, $new_value ): bool { |
| 57 |
if ( $old_value === false ) { |
| 58 |
$old_value = []; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! \is_array( $old_value ) || ! \is_array( $new_value ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
$option_key = 'enable_schema_aggregation_endpoint'; |
| 66 |
$timestamp_key = 'schema_aggregation_endpoint_enabled_on'; |
| 67 |
|
| 68 |
$old_enabled = isset( $old_value[ $option_key ] ) && (bool) $old_value[ $option_key ]; |
| 69 |
$new_enabled = isset( $new_value[ $option_key ] ) && (bool) $new_value[ $option_key ]; |
| 70 |
|
| 71 |
if ( ! $old_enabled && $new_enabled ) { |
| 72 |
$current_timestamp = $this->options_helper->get( $timestamp_key ); |
| 73 |
|
| 74 |
if ( empty( $current_timestamp ) ) { |
| 75 |
$this->options_helper->set( $timestamp_key, \time() ); |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
// phpcs:enable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification |
| 84 |
} |
| 85 |
|