'boolean', 'default' => false, 'show_in_rest' => true, 'sanitize_callback' => array( __CLASS__, 'sanitize_enabled' ), 'description' => __( 'Whether the Premium Analytics dashboard is enabled for this site.', 'jetpack-premium-analytics-pkg' ), ) ); add_action( 'update_option_' . self::ENABLED_OPTION, array( __CLASS__, 'reset_onboarding_on_reactivation' ), 10, 2 ); } /** * Store the opt-in as 0 or 1. * * Without this, switching the dashboard off stores `''`: core's settings controller hands * `update_option()` a bare `false`, which reaches the options table as an empty string. The * REST schema does not accept that as a boolean, so the next read reports `null` rather than * `false`, and a later `null` write answers 500 `rest_invalid_stored_value`. * * @since 0.6.0 * * @param mixed $value Value being written. * @return int */ public static function sanitize_enabled( $value ) { return (int) rest_sanitize_boolean( $value ); } /** * Forget that the reader switching the dashboard back on has seen the onboarding. * * Someone who switched off and came back gets the tour again; a first activation, a * switch-off, and everyone else's preference are left alone. Riding on the option write means * only writes made while the setting is registered count, which is every product path: the * classic Stats banner and menu, and the dashboard's own switch-off, all go through REST. * * The preference lives in core's persisted preferences user meta, whose client-side layer * keeps a localStorage copy and takes whichever is newer, so `_modified` moves with the change. * * @since 0.6.0 * * @param mixed $old_value Previous option value. * @param mixed $value New option value. * @return void */ public static function reset_onboarding_on_reactivation( $old_value, $value ) { if ( (bool) $old_value || ! (bool) $value ) { return; } $user_id = get_current_user_id(); if ( ! $user_id ) { return; } global $wpdb; $meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences'; $preferences = get_user_meta( $user_id, $meta_key, true ); if ( ! is_array( $preferences ) || ! isset( $preferences[ self::PREFERENCES_SCOPE ][ self::ONBOARDING_KEY ] ) ) { return; } unset( $preferences[ self::PREFERENCES_SCOPE ][ self::ONBOARDING_KEY ] ); $preferences['_modified'] = gmdate( 'Y-m-d\TH:i:s\Z' ); update_user_meta( $user_id, $meta_key, $preferences ); } }