REST API: Fire `wp_after_insert_post` later in new post object endpoints.

In the new menu items, global items and template controllers manually call the function `wp_after_insert_post()` to manually fire the action of the same name after all potential data is updated.

This accounts for the use case in which a theme or plugin developer has modified the endpoints use of either taxonomy or meta data.

The new parameter `$fire_after_hooks` is added to `wp_update_nav_menu_item()` to control whether the `wp_after_insert_post` action is fired when it in turn calls `wp_insert_post()`.

Props spacedmonkey, peterwilsoncc, zieladam.
Fixes #54536.


Built from https://develop.svn.wordpress.org/trunk@52276


git-svn-id: http://core.svn.wordpress.org/trunk@51868 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2021-11-30 01:10:08 +00:00
parent e4b553a201
commit 43290b2e04
5 changed files with 34 additions and 16 deletions

View File

@ -410,13 +410,15 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
* to be pre-slashed since they are passed directly to APIs that expect slashed data. * to be pre-slashed since they are passed directly to APIs that expect slashed data.
* *
* @since 3.0.0 * @since 3.0.0
* @since 5.9.0 Added the `$fire_after_hooks` parameter.
* *
* @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
* @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
* @param array $menu_item_data The menu item's data. * @param array $menu_item_data The menu item's data.
* @param bool $fire_after_hooks Whether to fire the after insert hooks. Default true.
* @return int|WP_Error The menu item's database ID or WP_Error object on failure. * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
*/ */
function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) { function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array(), $fire_after_hooks = true ) {
$menu_id = (int) $menu_id; $menu_id = (int) $menu_id;
$menu_item_db_id = (int) $menu_item_db_id; $menu_item_db_id = (int) $menu_item_db_id;
@ -526,7 +528,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
if ( ! $update ) { if ( ! $update ) {
$post['ID'] = 0; $post['ID'] = 0;
$post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft'; $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
$menu_item_db_id = wp_insert_post( $post, true ); $menu_item_db_id = wp_insert_post( $post, true, $fire_after_hooks );
if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) { if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
return $menu_item_db_id; return $menu_item_db_id;
} }

View File

@ -146,13 +146,13 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function update_item( $request ) { public function update_item( $request ) {
$post = get_post( $request['id'] ); $post_before = get_post( $request['id'] );
if ( ! $post || 'wp_global_styles' !== $post->post_type ) { if ( ! $post_before || 'wp_global_styles' !== $post_before->post_type ) {
return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.' ), array( 'status' => 404 ) ); return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.' ), array( 'status' => 404 ) );
} }
$changes = $this->prepare_item_for_database( $request ); $changes = $this->prepare_item_for_database( $request );
$result = wp_update_post( wp_slash( (array) $changes ), true ); $result = wp_update_post( wp_slash( (array) $changes ), true, false );
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
return $result; return $result;
} }
@ -163,10 +163,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
return $fields_update; return $fields_update;
} }
return $this->prepare_item_for_response( wp_after_insert_post( $post, true, $post_before );
get_post( $request['id'] ),
$request $response = $this->prepare_item_for_response( $post, $request );
);
return rest_ensure_response( $response );
} }
/** /**

View File

@ -122,7 +122,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
} }
$prepared_nav_item = (array) $prepared_nav_item; $prepared_nav_item = (array) $prepared_nav_item;
$nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ) ); $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );
if ( is_wp_error( $nav_menu_item_id ) ) { if ( is_wp_error( $nav_menu_item_id ) ) {
if ( 'db_insert_error' === $nav_menu_item_id->get_error_code() ) { if ( 'db_insert_error' === $nav_menu_item_id->get_error_code() ) {
$nav_menu_item_id->add_data( array( 'status' => 500 ) ); $nav_menu_item_id->add_data( array( 'status' => 500 ) );
@ -181,7 +181,10 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
*/ */
do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, true ); do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, true );
$response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request ); $post = get_post( $nav_menu_item_id );
wp_after_insert_post( $post, false, null );
$response = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
$response->set_status( 201 ); $response->set_status( 201 );
@ -204,7 +207,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
if ( is_wp_error( $valid_check ) ) { if ( is_wp_error( $valid_check ) ) {
return $valid_check; return $valid_check;
} }
$post_before = get_post( $request['id'] );
$prepared_nav_item = $this->prepare_item_for_database( $request ); $prepared_nav_item = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_nav_item ) ) { if ( is_wp_error( $prepared_nav_item ) ) {
@ -213,7 +216,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
$prepared_nav_item = (array) $prepared_nav_item; $prepared_nav_item = (array) $prepared_nav_item;
$nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ) ); $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );
if ( is_wp_error( $nav_menu_item_id ) ) { if ( is_wp_error( $nav_menu_item_id ) ) {
if ( 'db_update_error' === $nav_menu_item_id->get_error_code() ) { if ( 'db_update_error' === $nav_menu_item_id->get_error_code() ) {
@ -245,6 +248,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
} }
} }
$post = get_post( $nav_menu_item_id );
$nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id ); $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
$fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request ); $fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );
@ -257,6 +261,8 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false ); do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false );
wp_after_insert_post( $post, true, $post_before );
$response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request ); $response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );

View File

@ -266,6 +266,8 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
} }
$post_before = get_post( $template->wp_id );
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
wp_delete_post( $template->wp_id, true ); wp_delete_post( $template->wp_id, true );
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
@ -283,9 +285,12 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
} }
if ( 'custom' === $template->source ) { if ( 'custom' === $template->source ) {
$result = wp_update_post( wp_slash( (array) $changes ), true ); $update = true;
$result = wp_update_post( wp_slash( (array) $changes ), false );
} else { } else {
$result = wp_insert_post( wp_slash( (array) $changes ), true ); $update = false;
$post_before = null;
$result = wp_insert_post( wp_slash( (array) $changes ), false );
} }
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
@ -309,6 +314,8 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
do_action( "rest_after_insert_{$this->post_type}", $post, $request, false ); do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
wp_after_insert_post( $post, $update, $post_before );
$response = $this->prepare_item_for_response( $template, $request ); $response = $this->prepare_item_for_response( $template, $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );
@ -367,6 +374,8 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
do_action( "rest_after_insert_{$this->post_type}", $post, $request, true ); do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
wp_after_insert_post( $post, false, null );
$response = $this->prepare_item_for_response( $template, $request ); $response = $this->prepare_item_for_response( $template, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-52275'; $wp_version = '5.9-alpha-52276';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.