Skip to content

Use JSON as storage type instead of serialized #99

Open
@JoryHogeveen

Description

@JoryHogeveen

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

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions