WP_REST_Request::parse_json_params

Advertisement

Summery Summery

Parses the JSON parameters.

Syntax Syntax

WP_REST_Request::parse_json_params()

Description Description

Avoids parsing the JSON data until we need to access it.

Return Return

(true|WP_Error) True if the JSON data was passed or no JSON data was provided, WP_Error if invalid JSON was passed.

Source Source

File: wp-includes/rest-api/class-wp-rest-request.php

		$this->parse_json_params();

		return $this->params['JSON'];
	}

	/**
	 * Parses the JSON parameters.
	 *
	 * Avoids parsing the JSON data until we need to access it.
	 *
	 * @since 4.4.0
	 * @since 4.7.0 Returns error instance if value cannot be decoded.
	 * @return true|WP_Error True if the JSON data was passed or no JSON data was provided, WP_Error if invalid JSON was passed.
	 */
	protected function parse_json_params() {
		if ( $this->parsed_json ) {
			return true;
		}

		$this->parsed_json = true;

		// Check that we actually got JSON.
		$content_type = $this->get_content_type();

		if ( empty( $content_type ) || 'application/json' !== $content_type['value'] ) {
			return true;
		}

		$body = $this->get_body();
		if ( empty( $body ) ) {
			return true;
		}

		$params = json_decode( $body, true );

		/*
		 * Check for a parsing error.
		 */
		if ( null === $params && JSON_ERROR_NONE !== json_last_error() ) {
			// Ensure subsequent calls receive error instance.
			$this->parsed_json = false;

Advertisement

Changelog Changelog

Changelog
Version Description
4.7.0 Returns error instance if value cannot be decoded.
4.4.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.