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.
https://wordpress.org/plugins/view-admin-as/
GNU General Public License v2.0
45 stars 4 forks source link

Use JSON as storage type instead of serialized #99

Open JoryHogeveen opened 5 years ago

JoryHogeveen commented 5 years ago

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:

/**
 * 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;
}