Skip to content

Commit

Permalink
Widget visibility: Move filter referer check
Browse files Browse the repository at this point in the history
Previously registration of the front-end hooks was not done in the
referer was the widgets.php admin page. This prevented the API from
running the filters which made filtered out widgets uneditable.

This also had the effect of running the filters if you clicked from the
widgets page to e.g. the homepage, which would look confusing to users.

This has been solved by changing the check so that filters aren't ran if
on the REST API AND the referer is the widgets.php admin page.
  • Loading branch information
dsas committed Jul 14, 2021
1 parent e1f0849 commit 7a63701
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ public static function init() {
}

if ( ! $add_html_to_form && ! $handle_widget_updates && ! $add_data_assets_to_page &&
! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true ) &&
// Don't filter widgets when editing them - otherwise they could get filtered out and become impossible to edit.
( false === strpos( wp_get_raw_referer(), '/wp-admin/widgets.php' ) )
! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true )
) {
// Not hit any known widget admin endpoint, register widget display hooks instead.
add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) );
Expand Down Expand Up @@ -707,6 +705,12 @@ public static function filter_widget( $instance ) {
return $instance;
}

// Don't filter widgets from the REST API when it's called via the widgets admin page - otherwise they could get
// filtered out and become impossible to edit.
if ( strpos( wp_get_raw_referer(), '/wp-admin/widgets.php' ) && false !== strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) ) {
return $instance;
}

// Store the results of all in-page condition lookups so that multiple widgets with
// the same visibility conditions don't result in duplicate DB queries.
static $condition_result_cache = array();
Expand Down

0 comments on commit 7a63701

Please sign in to comment.