Make WordPress Core

Changeset 34844


Ignore:
Timestamp:
10/06/2015 02:44:16 AM (10 years ago)
Author:
wonderboymusic
Message:

REST API: add json_last_error_msg() compatibility function for PHP <5.5 to compat.php

Props rmmcue.
See #33982.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r32672 r34844  
    261261    define( 'JSON_PRETTY_PRINT', 128 );
    262262}
     263
     264if ( ! function_exists( 'json_last_error_msg' ) ) :
     265    /**
     266     * Retrieves the error string of the last json_encode() or json_decode() call.
     267     *
     268     * @since 4.4.0
     269     *
     270     * @internal This is a compatibility function for PHP <5.5
     271     *
     272     * @return bool|string Returns the error message on success, "No Error" if no error has occurred,
     273     *                     or false on failure.
     274     */
     275    function json_last_error_msg() {
     276        // See https://core.trac.wordpress.org/ticket/27799.
     277        if ( ! function_exists( 'json_last_error' ) ) {
     278            return false;
     279        }
     280
     281        $last_error_code = json_last_error();
     282
     283        // Just in case JSON_ERROR_NONE is not defined.
     284        $error_code_none = defined( 'JSON_ERROR_NONE' ) ? JSON_ERROR_NONE : 0;
     285
     286        switch ( true ) {
     287            case $last_error_code === $error_code_none:
     288                return 'No error';
     289
     290            case defined( 'JSON_ERROR_DEPTH' ) && JSON_ERROR_DEPTH === $last_error_code:
     291                return 'Maximum stack depth exceeded';
     292
     293            case defined( 'JSON_ERROR_STATE_MISMATCH' ) && JSON_ERROR_STATE_MISMATCH === $last_error_code:
     294                return 'State mismatch (invalid or malformed JSON)';
     295
     296            case defined( 'JSON_ERROR_CTRL_CHAR' ) && JSON_ERROR_CTRL_CHAR === $last_error_code:
     297                return 'Control character error, possibly incorrectly encoded';
     298
     299            case defined( 'JSON_ERROR_SYNTAX' ) && JSON_ERROR_SYNTAX === $last_error_code:
     300                return 'Syntax error';
     301
     302            case defined( 'JSON_ERROR_UTF8' ) && JSON_ERROR_UTF8 === $last_error_code:
     303                return 'Malformed UTF-8 characters, possibly incorrectly encoded';
     304
     305            case defined( 'JSON_ERROR_RECURSION' ) && JSON_ERROR_RECURSION === $last_error_code:
     306                return 'Recursion detected';
     307
     308            case defined( 'JSON_ERROR_INF_OR_NAN' ) && JSON_ERROR_INF_OR_NAN === $last_error_code:
     309                return 'Inf and NaN cannot be JSON encoded';
     310
     311            case defined( 'JSON_ERROR_UNSUPPORTED_TYPE' ) && JSON_ERROR_UNSUPPORTED_TYPE === $last_error_code:
     312                return 'Type is not supported';
     313
     314            default:
     315                return 'An unknown error occurred';
     316        }
     317    }
     318endif;
Note: See TracChangeset for help on using the changeset viewer.