| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 6 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Watcher for the wpseo option. |
| 10 |
* |
| 11 |
* Represents the option wpseo watcher. |
| 12 |
*/ |
| 13 |
class Option_Wpseo_Watcher implements Integration_Interface { |
| 14 |
|
| 15 |
use No_Conditionals; |
| 16 |
|
| 17 |
/** |
| 18 |
* Initializes the integration. |
| 19 |
* |
| 20 |
* This is the place to register hooks and filters. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function register_hooks() { |
| 25 |
\add_action( 'update_option_wpseo', [ $this, 'check_semrush_option_disabled' ], 10, 2 ); |
| 26 |
\add_action( 'update_option_wpseo', [ $this, 'check_wincher_option_disabled' ], 10, 2 ); |
| 27 |
\add_action( 'update_option_wpseo', [ $this, 'check_toggle_usage_tracking' ], 10, 2 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Checks if the SEMrush integration is disabled; if so, deletes the tokens. |
| 32 |
* |
| 33 |
* We delete the tokens if the SEMrush integration is disabled, no matter if |
| 34 |
* the value has actually changed or not. |
| 35 |
* |
| 36 |
* @param array $old_value The old value of the option. |
| 37 |
* @param array $new_value The new value of the option. |
| 38 |
* |
| 39 |
* @return bool Whether the SEMrush tokens have been deleted or not. |
| 40 |
*/ |
| 41 |
public function check_semrush_option_disabled( $old_value, $new_value ) { |
| 42 |
return $this->check_token_option_disabled( 'semrush_integration_active', 'semrush_tokens', $new_value ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Checks if the Wincher integration is disabled; if so, deletes the tokens |
| 47 |
* and website id. |
| 48 |
* |
| 49 |
* We delete them if the Wincher integration is disabled, no matter if the |
| 50 |
* value has actually changed or not. |
| 51 |
* |
| 52 |
* @param array $old_value The old value of the option. |
| 53 |
* @param array $new_value The new value of the option. |
| 54 |
* |
| 55 |
* @return bool Whether the Wincher tokens have been deleted or not. |
| 56 |
*/ |
| 57 |
public function check_wincher_option_disabled( $old_value, $new_value ) { |
| 58 |
$disabled = $this->check_token_option_disabled( 'wincher_integration_active', 'wincher_tokens', $new_value ); |
| 59 |
if ( $disabled ) { |
| 60 |
\YoastSEO()->helpers->options->set( 'wincher_website_id', '' ); |
| 61 |
} |
| 62 |
|
| 63 |
return $disabled; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Checks if the WordProof integration is disabled; if so, deletes the tokens |
| 68 |
* |
| 69 |
* We delete them if the WordProof integration is disabled, no matter if the |
| 70 |
* value has actually changed or not. |
| 71 |
* |
| 72 |
* @deprecated 22.10 |
| 73 |
* @codeCoverageIgnore |
| 74 |
* |
| 75 |
* @param array $old_value The old value of the option. |
| 76 |
* @param array $new_value The new value of the option. |
| 77 |
* |
| 78 |
* @return bool Whether the WordProof tokens have been deleted or not. |
| 79 |
*/ |
| 80 |
public function check_wordproof_option_disabled( $old_value, $new_value ) { |
| 81 |
\_deprecated_function( __METHOD__, 'Yoast SEO 22.10' ); |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Checks if the usage tracking feature is toggled; if so, set an option to stop us from messing with it. |
| 88 |
* |
| 89 |
* @param array $old_value The old value of the option. |
| 90 |
* @param array $new_value The new value of the option. |
| 91 |
* |
| 92 |
* @return bool Whether the option is set. |
| 93 |
*/ |
| 94 |
public function check_toggle_usage_tracking( $old_value, $new_value ) { |
| 95 |
$option_name = 'tracking'; |
| 96 |
|
| 97 |
if ( \array_key_exists( $option_name, $old_value ) && \array_key_exists( $option_name, $new_value ) && $old_value[ $option_name ] !== $new_value[ $option_name ] && $old_value['toggled_tracking'] === false ) { |
| 98 |
\YoastSEO()->helpers->options->set( 'toggled_tracking', true ); |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Checks if the passed integration is disabled; if so, deletes the tokens. |
| 108 |
* |
| 109 |
* We delete the tokens if the integration is disabled, no matter if |
| 110 |
* the value has actually changed or not. |
| 111 |
* |
| 112 |
* @param string $integration_option The intergration option name. |
| 113 |
* @param string $target_option The target option to remove the tokens from. |
| 114 |
* @param array $new_value The new value of the option. |
| 115 |
* |
| 116 |
* @return bool Whether the tokens have been deleted or not. |
| 117 |
*/ |
| 118 |
protected function check_token_option_disabled( $integration_option, $target_option, $new_value ) { |
| 119 |
if ( \array_key_exists( $integration_option, $new_value ) && $new_value[ $integration_option ] === false ) { |
| 120 |
\YoastSEO()->helpers->options->set( $target_option, [] ); |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
} |
| 128 |
|