Automattic\WooCommerce\Internal\EmailEditor

PageRenderer::preload_rest_api_data()privateWC 1.0

Preload REST API data for the email editor.

Method of the class: PageRenderer{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->preload_rest_api_data( $post ): void;
$post(\WP_Post) (required)
Current post being edited.

PageRenderer::preload_rest_api_data() code WC 9.8.1

private function preload_rest_api_data( \WP_Post $post ): void {
	$email_post_type    = $post->post_type;
	$user_theme_post_id = $this->user_theme->get_user_theme_post()->ID;
	$template_slug      = get_post_meta( $post->ID, '_wp_page_template', true );
	$routes             = array(
		"/wp/v2/{$email_post_type}/" . intval( $post->ID ) . '?context=edit',
		"/wp/v2/types/{$email_post_type}?context=edit",
		'/wp/v2/global-styles/' . intval( $user_theme_post_id ) . '?context=edit', // Global email styles.
		'/wp/v2/block-patterns/patterns',
		'/wp/v2/templates?context=edit',
		'/wp/v2/block-patterns/categories',
		'/wp/v2/settings',
		'/wp/v2/types?context=view',
		'/wp/v2/taxonomies?context=view',
	);

	if ( $template_slug ) {
		$routes[] = '/wp/v2/templates/lookup?slug=' . $template_slug;
	} else {
		$routes[] = "/wp/v2/{$email_post_type}?context=edit&per_page=30&status=publish,sent";
	}

	// Preload the data for the specified routes.
	$preload_data = array_reduce(
		$routes,
		'rest_preload_api_request',
		array()
	);

	// Add inline script to set up preloading middleware.
	wp_add_inline_script(
		'wp-blocks',
		sprintf(
			'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
			wp_json_encode( $preload_data )
		)
	);
}