Make WordPress Core


Ignore:
Timestamp:
06/04/2021 12:44:41 AM (4 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.