WP_REST_Global_Styles_Controller::prepare_item_for_database()protectedWP 5.9.0

Prepares a single global styles config for update.

{} It's a method of the class: WP_REST_Global_Styles_Controller{}

No Hooks.

Return

stdClass. Changes to pass to wp_update_post.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_item_for_database( $request );
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 5.9.0 Introduced.

Code of WP_REST_Global_Styles_Controller::prepare_item_for_database() WP 6.0

protected function prepare_item_for_database( $request ) {
	$changes     = new stdClass();
	$changes->ID = $request['id'];

	$post            = get_post( $request['id'] );
	$existing_config = array();
	if ( $post ) {
		$existing_config     = json_decode( $post->post_content, true );
		$json_decoding_error = json_last_error();
		if ( JSON_ERROR_NONE !== $json_decoding_error || ! isset( $existing_config['isGlobalStylesUserThemeJSON'] ) ||
			! $existing_config['isGlobalStylesUserThemeJSON'] ) {
			$existing_config = array();
		}
	}

	if ( isset( $request['styles'] ) || isset( $request['settings'] ) ) {
		$config = array();
		if ( isset( $request['styles'] ) ) {
			$config['styles'] = $request['styles'];
		} elseif ( isset( $existing_config['styles'] ) ) {
			$config['styles'] = $existing_config['styles'];
		}
		if ( isset( $request['settings'] ) ) {
			$config['settings'] = $request['settings'];
		} elseif ( isset( $existing_config['settings'] ) ) {
			$config['settings'] = $existing_config['settings'];
		}
		$config['isGlobalStylesUserThemeJSON'] = true;
		$config['version']                     = WP_Theme_JSON::LATEST_SCHEMA;
		$changes->post_content                 = wp_json_encode( $config );
	}

	// Post title.
	if ( isset( $request['title'] ) ) {
		if ( is_string( $request['title'] ) ) {
			$changes->post_title = $request['title'];
		} elseif ( ! empty( $request['title']['raw'] ) ) {
			$changes->post_title = $request['title']['raw'];
		}
	}

	return $changes;
}