Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON as storage type instead of serialized #99

Open
JoryHogeveen opened this issue Aug 30, 2018 · 0 comments
Open

Use JSON as storage type instead of serialized #99

JoryHogeveen opened this issue Aug 30, 2018 · 0 comments

Comments

@JoryHogeveen
Copy link
Owner

JoryHogeveen commented Aug 30, 2018

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant