JoryHogeveen / view-admin-as

View the WordPress admin as a different role, switch between users, temporarily change your capabilities, set default screen settings for roles, manage your roles and capabilities.

Home Page:https://wordpress.org/plugins/view-admin-as/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use JSON as storage type instead of serialized

JoryHogeveen opened this issue · comments

Encountered an installation where a chartset other than UTF8 was used which was causing problems with get_option() (using maybe_unserialize()) from WordPress. The serialized string wasn't correct anymore and returned false causing DB storage not to function properly anymore.

Look into how to convert to JSON:

  • Find possible issues with older PHP versions
  • maybe_json_decode method (see below) = PHP 5.3+ > major release only
  • Other possible issues?
/**
 * Check if the string is JSON and decode it if so.
 * @link https://stackoverflow.com/questions/6041741/fastest-way-to-check-if-a-string-is-json-in-php
 * @param  string  $string
 * @param  bool    $assoc
 * @return array|mixed|object
 */
public static function maybe_json_decode( $string, $assoc = true ) {
	if ( ! $string || ! is_string( $string ) ) {
		return $string;
	}
	if ( 0 !== strpos( $string, '[' ) && 0 !== strpos( $string, '{' ) ) {
		return $string;
	}
	$var = json_decode( $string, $assoc );
	if ( JSON_ERROR_NONE === json_last_error() ) {
		return $var;
	}
	return $string;
}