Rarst / fragment-cache

WordPress plugin for partial and async caching.
Other
140 stars 10 forks source link

Review handling of widgets in Customizer #12

Closed Rarst closed 5 years ago

Rarst commented 6 years ago

The last patch/approach with intercepting ajax request is a mess really.

@westonruter shared something to look into while back, which I need to get to finally:

the wp_ajax_update-widget action in the customizer is only used to invoke the WP_Widget::update() method to calculate the new widget instance state to then return back to the client. This is for back-compat for widgets which have historically relied heavily on PHP for their architecture. These update-widget Ajax requests are not supposed to be writing anything to the DB. So if you want to listen for when a widget is saved in the customizer, I think you should instead be looking at the customize_save_after action, and in there iterate over $wp_customize->settings() to identify which widgets were saved as part of the changeset. For example:

add_action( 'customize_save_after', function( $wp_customize ) {
  foreach ( $wp_customize->settings() as $setting ) {
      if ( 'widget' === $setting->type && null !== $setting->post_value() ) {
          // @todo Clear fragment cache for the widget.
      }
  }
});
Rarst commented 5 years ago

Not worth messing with since working solution in place. Probably will need to be revisited when Gutenberg gets to messing these parts up.