WP_REST_Global_Styles_Revisions_Controller::get_items( WP_REST_Request $request ): WP_REST_Response|WP_Error

Returns paginated revisions of the given global styles config custom post type.

Description

The bulk of the body is taken from WP_REST_Revisions_Controller->get_items, but global styles does not require as many parameters.

Parameters

$requestWP_REST_Requestrequired
The request instance.

Return

WP_REST_Response|WP_Error

Source

public function get_items( $request ) {
	$parent = $this->get_parent( $request['parent'] );

	if ( is_wp_error( $parent ) ) {
		return $parent;
	}

	$global_styles_config = $this->get_decoded_global_styles_json( $parent->post_content );

	if ( is_wp_error( $global_styles_config ) ) {
		return $global_styles_config;
	}

	if ( wp_revisions_enabled( $parent ) ) {
		$registered = $this->get_collection_params();
		$query_args = array(
			'post_parent'    => $parent->ID,
			'post_type'      => 'revision',
			'post_status'    => 'inherit',
			'posts_per_page' => -1,
			'orderby'        => 'date ID',
			'order'          => 'DESC',
		);

		$parameter_mappings = array(
			'offset'   => 'offset',
			'page'     => 'paged',
			'per_page' => 'posts_per_page',
		);

		foreach ( $parameter_mappings as $api_param => $wp_param ) {
			if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
				$query_args[ $wp_param ] = $request[ $api_param ];
			}
		}

		$revisions_query = new WP_Query();
		$revisions       = $revisions_query->query( $query_args );
		$offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
		$page            = (int) $query_args['paged'];
		$total_revisions = $revisions_query->found_posts;

		if ( $total_revisions < 1 ) {
			// Out-of-bounds, run the query again without LIMIT for total count.
			unset( $query_args['paged'], $query_args['offset'] );
			$count_query = new WP_Query();
			$count_query->query( $query_args );

			$total_revisions = $count_query->found_posts;
		}

		if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
			$max_pages = (int) ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
		} else {
			$max_pages = $total_revisions > 0 ? 1 : 0;
		}
		if ( $total_revisions > 0 ) {
			if ( $offset >= $total_revisions ) {
				return new WP_Error(
					'rest_revision_invalid_offset_number',
					__( 'The offset number requested is larger than or equal to the number of available revisions.' ),
					array( 'status' => 400 )
				);
			} elseif ( ! $offset && $page > $max_pages ) {
				return new WP_Error(
					'rest_revision_invalid_page_number',
					__( 'The page number requested is larger than the number of pages available.' ),
					array( 'status' => 400 )
				);
			}
		}
	} else {
		$revisions       = array();
		$total_revisions = 0;
		$max_pages       = 0;
		$page            = (int) $request['page'];
	}

	$response = array();

	foreach ( $revisions as $revision ) {
		$data       = $this->prepare_item_for_response( $revision, $request );
		$response[] = $this->prepare_response_for_collection( $data );
	}

	$response = rest_ensure_response( $response );

	$response->header( 'X-WP-Total', (int) $total_revisions );
	$response->header( 'X-WP-TotalPages', (int) $max_pages );

	$request_params = $request->get_query_params();
	$base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) );
	$base           = add_query_arg( urlencode_deep( $request_params ), $base_path );

	if ( $page > 1 ) {
		$prev_page = $page - 1;

		if ( $prev_page > $max_pages ) {
			$prev_page = $max_pages;
		}

		$prev_link = add_query_arg( 'page', $prev_page, $base );
		$response->link_header( 'prev', $prev_link );
	}
	if ( $max_pages > $page ) {
		$next_page = $page + 1;
		$next_link = add_query_arg( 'page', $next_page, $base );

		$response->link_header( 'next', $next_link );
	}

	return $response;
}

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.