get_defaults();
*
* @var array>
*/
protected $defaults = [
'task_list_first_opened_on' => '',
'task_first_actioned_on' => '',
'frontend_inspector_first_actioned_on' => '',
];
/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* All concrete classes must contain a validate_option() method which validates all
* values within the option.
*
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Needed because the function is called with the parameter $old.
*
* @param array $dirty New value for the option.
* @param array $clean Clean value for the option, normally the defaults.
* @param array $old Old value of the option.
*
* @return array The clean option with the saved value.
*/
protected function validate_option( $dirty, $clean, $old ) {
foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'task_list_first_opened_on':
case 'task_first_actioned_on':
case 'frontend_inspector_first_actioned_on':
// These should be set only once and never changed again (unless completely reset to default).
if ( isset( $dirty[ $key ] ) && $old[ $key ] === $this->get_defaults()[ $key ] ) {
// Allow setting it for the first time.
$clean[ $key ] = sanitize_text_field( $dirty[ $key ] );
}
elseif ( isset( $dirty[ $key ] ) && $dirty[ $key ] === $this->get_defaults()[ $key ] ) {
// Allow resetting to default.
$clean[ $key ] = $dirty[ $key ];
}
else {
// Otherwise keep old value.
$clean[ $key ] = $old[ $key ];
}
break;
}
}
return $clean;
}
}