| | 2805 | * Prepares response data to be serialized to JSON. |
| | 2806 | * |
| | 2807 | * This supports the JsonSerializable interface for PHP 5.2-5.3 as well. |
| | 2808 | * |
| | 2809 | * @ignore |
| | 2810 | * @since 4.4.0 |
| | 2811 | * @access private |
| | 2812 | * |
| | 2813 | * @param mixed $data Native representation. |
| | 2814 | * @return array|string Data ready for `json_encode()`. |
| | 2815 | */ |
| | 2816 | function _wp_json_prepare_data( $data ) { |
| | 2817 | if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) { |
| | 2818 | return $data; |
| | 2819 | } |
| | 2820 | |
| | 2821 | switch ( gettype( $data ) ) { |
| | 2822 | case 'boolean': |
| | 2823 | case 'integer': |
| | 2824 | case 'double': |
| | 2825 | case 'string': |
| | 2826 | case 'NULL': |
| | 2827 | // These values can be passed through. |
| | 2828 | return $data; |
| | 2829 | |
| | 2830 | case 'array': |
| | 2831 | // Arrays must be mapped in case they also return objects. |
| | 2832 | return array_map( '_wp_json_prepare_data', $data ); |
| | 2833 | |
| | 2834 | case 'object': |
| | 2835 | if ( $data instanceof JsonSerializable ) { |
| | 2836 | $data = $data->jsonSerialize(); |
| | 2837 | } else { |
| | 2838 | $data = get_object_vars( $data ); |
| | 2839 | } |
| | 2840 | |
| | 2841 | // Now, pass the array (or whatever was returned from jsonSerialize through). |
| | 2842 | return _wp_json_prepare_data( $data ); |
| | 2843 | |
| | 2844 | default: |
| | 2845 | return null; |
| | 2846 | } |
| | 2847 | } |
| | 2848 | |
| | 2849 | /** |