Jetpack::state PHP Method

state() public static method

SET: state( $key, $value ); GET: $value = state( $key );
public static state ( string $key = null, string $value = null, boolean $restate = false )
$key string
$value string
$restate boolean private
    public static function state($key = null, $value = null, $restate = false)
    {
        static $state = array();
        static $path, $domain;
        if (!isset($path)) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
            $admin_url = Jetpack::admin_url();
            $bits = parse_url($admin_url);
            if (is_array($bits)) {
                $path = isset($bits['path']) ? dirname($bits['path']) : null;
                $domain = isset($bits['host']) ? $bits['host'] : null;
            } else {
                $path = $domain = null;
            }
        }
        // Extract state from cookies and delete cookies
        if (isset($_COOKIE['jetpackState']) && is_array($_COOKIE['jetpackState'])) {
            $yum = $_COOKIE['jetpackState'];
            unset($_COOKIE['jetpackState']);
            foreach ($yum as $k => $v) {
                if (strlen($v)) {
                    $state[$k] = $v;
                }
                setcookie("jetpackState[{$k}]", false, 0, $path, $domain);
            }
        }
        if ($restate) {
            foreach ($state as $k => $v) {
                setcookie("jetpackState[{$k}]", $v, 0, $path, $domain);
            }
            return;
        }
        // Get a state variable
        if (isset($key) && !isset($value)) {
            if (array_key_exists($key, $state)) {
                return $state[$key];
            }
            return null;
        }
        // Set a state variable
        if (isset($key) && isset($value)) {
            if (is_array($value) && isset($value[0])) {
                $value = $value[0];
            }
            $state[$key] = $value;
            setcookie("jetpackState[{$key}]", $value, 0, $path, $domain);
        }
    }

Usage Example

Example #1
0
function jetpack_json_api_configuration_load()
{
    if (isset($_POST['action']) && $_POST['action'] == 'save_options' && wp_verify_nonce($_POST['_wpnonce'], 'json-api')) {
        Jetpack_Options::update_option('json_api_full_management', isset($_POST['json_api_full_management']));
        Jetpack::state('message', 'module_configured');
        wp_safe_redirect(Jetpack::module_configuration_url('json-api'));
        exit;
    }
}
All Usage Examples Of Jetpack::state
Jetpack