Add post_types field to the template REST API response
We want to use post_type for filtering email templates. There is a PR that adds this property https://github.com/WordPress/wordpress-develop/pull/7530 but it is not merged. This commit added the property and also future compatibility check so that we don't attempt to add it after the PR is merged. [MAILPOET-6356]
This commit is contained in:
committed by
Rostislav Wolný
parent
ddd2108109
commit
a5db2564c8
@@ -248,10 +248,9 @@ export const getEmailTemplates = createRegistrySelector(
|
|||||||
} )
|
} )
|
||||||
// We still need to filter the templates because, in some cases, the API also returns custom templates
|
// We still need to filter the templates because, in some cases, the API also returns custom templates
|
||||||
// ignoring the post_type filter in the query
|
// ignoring the post_type filter in the query
|
||||||
?.filter(
|
?.filter( ( template ) =>
|
||||||
( template ) =>
|
|
||||||
// @ts-expect-error Missing property in type
|
// @ts-expect-error Missing property in type
|
||||||
template.plugin === 'mailpoet'
|
template.post_types.includes( 'mailpoet_email' )
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -8,6 +8,7 @@
|
|||||||
declare(strict_types = 1);
|
declare(strict_types = 1);
|
||||||
namespace MailPoet\EmailEditor\Engine\Templates;
|
namespace MailPoet\EmailEditor\Engine\Templates;
|
||||||
|
|
||||||
|
use MailPoet\EmailEditor\Validator\Builder;
|
||||||
use WP_Block_Template;
|
use WP_Block_Template;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,6 +45,7 @@ class Templates {
|
|||||||
*/
|
*/
|
||||||
public function initialize(): void {
|
public function initialize(): void {
|
||||||
$this->register_templates();
|
$this->register_templates();
|
||||||
|
$this->register_post_types_to_api();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,4 +88,41 @@ class Templates {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register post_types property to the templates rest api response.
|
||||||
|
*
|
||||||
|
* There is a PR that adds the property into the core https://github.com/WordPress/wordpress-develop/pull/7530
|
||||||
|
* Until it is merged, we need to add it manually.
|
||||||
|
*/
|
||||||
|
public function register_post_types_to_api(): void {
|
||||||
|
$controller = new \WP_REST_Templates_Controller( 'wp_template' );
|
||||||
|
$schema = $controller->get_item_schema();
|
||||||
|
// Future compatibility check if the post_types property is already registered.
|
||||||
|
if ( isset( $schema['properties']['post_types'] ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
register_rest_field(
|
||||||
|
'wp_template',
|
||||||
|
'post_types',
|
||||||
|
array(
|
||||||
|
'get_callback' => array( $this, 'get_post_types' ),
|
||||||
|
'update_callback' => null,
|
||||||
|
'schema' => Builder::string()->to_array(),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a callback function for adding post_types property to templates rest api response.
|
||||||
|
*
|
||||||
|
* @param array $response_object The rest API response object.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_post_types( $response_object ): array {
|
||||||
|
if ( isset( $response_object['plugin'] ) && $response_object['plugin'] !== $this->template_prefix ) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
return array( $this->post_type );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user