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);
}
}