Make WordPress Core

Changeset 51068


Ignore:
Timestamp:
06/04/2021 12:44:41 AM (3 years ago)
Author:
noisysocks
Message:

REST API: Delete fresh_site option when updating widgets via REST API

Adds new hooks (rest_save_sidebar, rest_delete_widget, rest_after_save_widget)
to the widgets REST API and uses them to delete the fresh_site option when
updating widgets via the REST API. This ensures that starter content isn't
loaded in the Customizer after a user makes changes.

Fixes #53317.
Props kevin940726, garrett-eclipse, andraganescu, hellofromtonya.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r51058 r51068  
    247247
    248248// Mark site as no longer fresh.
    249 foreach ( array( 'publish_post', 'publish_page', 'wp_ajax_save-widget', 'wp_ajax_widgets-order', 'customize_save_after' ) as $action ) {
     249foreach (
     250    array(
     251        'publish_post',
     252        'publish_page',
     253        'wp_ajax_save-widget',
     254        'wp_ajax_widgets-order',
     255        'customize_save_after',
     256        'rest_after_save_widget',
     257        'rest_delete_widget',
     258        'rest_save_sidebar',
     259    ) as $action
     260) {
    250261    add_action( $action, '_delete_option_fresh_site', 0 );
    251262}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php

    r50995 r51068  
    210210        $sidebar = $this->get_sidebar( $request['id'] );
    211211
     212        /**
     213         * Fires after a sidebar is updated via the REST API.
     214         *
     215         * @since 5.8.0
     216         * @param array           $sidebar  The updated sidebar.
     217         * @param WP_REST_Request $request  Request object.
     218         */
     219        do_action( 'rest_save_sidebar', $sidebar, $request );
     220
    212221        return $this->prepare_item_for_response( $sidebar, $request );
    213222    }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php

    r51060 r51068  
    187187        $sidebar_id = $request['sidebar'];
    188188
    189         $widget_id = $this->save_widget( $request );
     189        $widget_id = $this->save_widget( $request, $sidebar_id );
    190190
    191191        if ( is_wp_error( $widget_id ) ) {
     
    249249            $request->has_param( 'form_data' )
    250250        ) {
    251             $maybe_error = $this->save_widget( $request );
     251            $maybe_error = $this->save_widget( $request, $sidebar_id );
    252252            if ( is_wp_error( $maybe_error ) ) {
    253253                return $maybe_error;
     
    356356            );
    357357        }
     358
     359        /**
     360         * Fires after a widget is deleted via the REST API.
     361         *
     362         * @since 5.8.0
     363         * @param string           $widget_id  ID of the widget marked for deletion.
     364         * @param string           $sidebar_id ID of the sidebar the widget was deleted from.
     365         * @param WP_REST_Response $response   The response data.
     366         * @param WP_REST_Request  $request    The request sent to the API.
     367         */
     368        do_action( 'rest_delete_widget', $widget_id, $sidebar_id, $response, $request );
    358369
    359370        return $response;
     
    386397     * @since 5.8.0
    387398     *
    388      * @param WP_REST_Request $request Full details about the request.
     399     * @param WP_REST_Request $request    Full details about the request.
     400     * @param string          $sidebar_id ID of the sidebar the widget belongs to.
    389401     *
    390402     * @return string|WP_Error The saved widget ID.
    391403     */
    392     protected function save_widget( $request ) {
     404    protected function save_widget( $request, $sidebar_id ) {
    393405        global $wp_widget_factory, $wp_registered_widget_updates;
    394406
     
    402414            $number        = isset( $parsed_id['number'] ) ? $parsed_id['number'] : null;
    403415            $widget_object = $wp_widget_factory->get_widget_object( $id_base );
     416            $creating      = false;
    404417        } elseif ( $request['id_base'] ) {
    405418            // Saving a new widget.
     
    408421            $number        = $widget_object ? next_widget_id_number( $id_base ) : null;
    409422            $id            = $widget_object ? $id_base . '-' . $number : $id_base;
     423            $creating      = true;
    410424        } else {
    411425            return new WP_Error(
     
    503517        }
    504518
     519        /**
     520         * Fires after a widget is created or updated via the REST API.
     521         *
     522         * @since 5.8.0
     523         * @param string          $id         ID of the widget being saved.
     524         * @param string          $sidebar_id ID of the sidebar containing the widget being saved.
     525         * @param WP_REST_Request $request    Request object.
     526         * @param bool            $creating   True when creating a widget, false when updating.
     527         */
     528        do_action( 'rest_after_save_widget', $id, $sidebar_id, $request, $creating );
     529
    505530        return $id;
    506531    }
Note: See TracChangeset for help on using the changeset viewer.