| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Internals\Options |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Option: wpseo_tracking_only. |
| 10 |
* |
| 11 |
* For stuff that their only purpose is tracking. |
| 12 |
*/ |
| 13 |
class WPSEO_Option_Tracking_Only extends WPSEO_Option { |
| 14 |
|
| 15 |
/** |
| 16 |
* Option name. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public $option_name = 'wpseo_tracking_only'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Array of defaults for the option. |
| 24 |
* |
| 25 |
* Shouldn't be requested directly, use $this->get_defaults(); |
| 26 |
* |
| 27 |
* @var array<string, int|string|array<int>> |
| 28 |
*/ |
| 29 |
protected $defaults = [ |
| 30 |
'task_list_first_opened_on' => '', |
| 31 |
'task_first_actioned_on' => '', |
| 32 |
'frontend_inspector_first_actioned_on' => '', |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the singleton instance of this class. |
| 37 |
* |
| 38 |
* @return object |
| 39 |
*/ |
| 40 |
public static function get_instance() { |
| 41 |
if ( ! ( self::$instance instanceof self ) ) { |
| 42 |
self::$instance = new self(); |
| 43 |
} |
| 44 |
|
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* All concrete classes must contain a validate_option() method which validates all |
| 50 |
* values within the option. |
| 51 |
* |
| 52 |
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Needed because the function is called with the parameter $old. |
| 53 |
* |
| 54 |
* @param array<string, string> $dirty New value for the option. |
| 55 |
* @param array<string, string> $clean Clean value for the option, normally the defaults. |
| 56 |
* @param array<string, string> $old Old value of the option. |
| 57 |
* |
| 58 |
* @return array<string, string> The clean option with the saved value. |
| 59 |
*/ |
| 60 |
protected function validate_option( $dirty, $clean, $old ) { |
| 61 |
|
| 62 |
foreach ( $clean as $key => $value ) { |
| 63 |
switch ( $key ) { |
| 64 |
case 'task_list_first_opened_on': |
| 65 |
case 'task_first_actioned_on': |
| 66 |
case 'frontend_inspector_first_actioned_on': |
| 67 |
// These should be set only once and never changed again (unless completely reset to default). |
| 68 |
|
| 69 |
if ( isset( $dirty[ $key ] ) && $old[ $key ] === $this->get_defaults()[ $key ] ) { |
| 70 |
// Allow setting it for the first time. |
| 71 |
$clean[ $key ] = sanitize_text_field( $dirty[ $key ] ); |
| 72 |
} |
| 73 |
elseif ( isset( $dirty[ $key ] ) && $dirty[ $key ] === $this->get_defaults()[ $key ] ) { |
| 74 |
// Allow resetting to default. |
| 75 |
$clean[ $key ] = $dirty[ $key ]; |
| 76 |
} |
| 77 |
else { |
| 78 |
// Otherwise keep old value. |
| 79 |
$clean[ $key ] = $old[ $key ]; |
| 80 |
} |
| 81 |
|
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $clean; |
| 87 |
} |
| 88 |
} |
| 89 |
|