From d7433cb44f194362f448b6508c8803cbe3a13650 Mon Sep 17 00:00:00 2001 From: Dean Sas Date: Fri, 2 Jul 2021 09:16:17 +0100 Subject: [PATCH 01/24] [not verified] Widget visiblity: Load conditions for gutenberg widgets Lets the widget visibility conditions run using the new gutenberg based widgets screen. --- .../modules/widget-visibility/widget-conditions.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php index 2574690f7367b..6db2e95047293 100644 --- a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php +++ b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php @@ -10,11 +10,13 @@ class Jetpack_Widget_Conditions { static $passed_template_redirect = false; public static function init() { - if ( is_admin() ) { + global $pagenow; + + if ( is_customize_preview() || 'widgets.php' === $pagenow || 'themes.php' === $pagenow ) { add_action( 'sidebar_admin_setup', array( __CLASS__, 'widget_admin_setup' ) ); add_filter( 'widget_update_callback', array( __CLASS__, 'widget_update' ), 10, 3 ); add_action( 'in_widget_form', array( __CLASS__, 'widget_conditions_admin' ), 10, 3 ); - } elseif ( ! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) { + } elseif ( ! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true ) ) { add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) ); add_filter( 'sidebars_widgets', array( __CLASS__, 'sidebars_widgets' ) ); add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) ); @@ -22,11 +24,6 @@ public static function init() { } public static function widget_admin_setup() { - // Return early if we are not in the block editor. - if ( wp_should_load_block_editor_scripts_and_styles() ) { - return; - } - wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); wp_style_add_data( 'widget-conditions', 'rtl', 'replace' ); wp_enqueue_script( From c053b90a14d6f9afab743d8ec03b2fdfa96dc6d4 Mon Sep 17 00:00:00 2001 From: Dean Sas Date: Fri, 2 Jul 2021 16:03:45 +0100 Subject: [PATCH 02/24] [not verified] Widget visibility: Include filters in API Register the filters & actions for adding conditional visibility to legacy widgets when te widget-types API is being called. This means that the conditional visbility HTML will be included in the response that gutenberg'd widgets use. --- .../jetpack/modules/widget-visibility/widget-conditions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php index 6db2e95047293..a80f1ca52cab8 100644 --- a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php +++ b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php @@ -12,7 +12,7 @@ class Jetpack_Widget_Conditions { public static function init() { global $pagenow; - if ( is_customize_preview() || 'widgets.php' === $pagenow || 'themes.php' === $pagenow ) { + if ( is_customize_preview() || 'widgets.php' === $pagenow || 'themes.php' === $pagenow || str_starts_with( $_SERVER['REQUEST_URI'], '/wp-json/wp/v2/widget-types' ) ) { add_action( 'sidebar_admin_setup', array( __CLASS__, 'widget_admin_setup' ) ); add_filter( 'widget_update_callback', array( __CLASS__, 'widget_update' ), 10, 3 ); add_action( 'in_widget_form', array( __CLASS__, 'widget_conditions_admin' ), 10, 3 ); From 4c97d8b71913db5156cf12d0aee56c0d995b43d1 Mon Sep 17 00:00:00 2001 From: Dean Sas Date: Fri, 2 Jul 2021 16:19:44 +0100 Subject: [PATCH 03/24] [not verified] Widget visibility: Update the JS for Gutenberg widgets The conditional widget visiblity code held some assumptions about the page it was on: - The ID of the element that wraps it - Where the visibility button should display These selectors have been updated to be gutenberg friendly. Backwards compatibility should be maintained too for the classic widgets screen, though I haven't yet tested this. --- .../widget-conditions/widget-conditions.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js index 4353eab00f7ac..9941c508d7995 100644 --- a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js +++ b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js @@ -1,10 +1,17 @@ /* global isRtl, widget_conditions_parent_pages, widget_conditions_data, jQuery */ jQuery( function ( $ ) { - var widgets_shell = $( 'div#widgets-right' ); + // Gutenberg widgets + customizer screen. + var widgets_shell = $( '#widgets-editor' ); - if ( ! widgets_shell && ! widgets_shell.length ) { - widgets_shell = $( 'form#customize-controls' ); + if ( 0 === widgets_shell.length ) { + // Pre-gutenberg widget screen. + widgets_shell = $( 'div#widgets-right' ); + + // Pre-gutenberg customizer screen. + if ( 0 === widgets_shell.length ) { + widgets_shell = $( 'form#customize-controls' ); + } } function setWidgetMargin( $widget ) { @@ -51,8 +58,15 @@ jQuery( function ( $ ) { } function moveWidgetVisibilityButton( $widget ) { - var $displayOptionsButton = $widget.find( 'a.display-options' ).first(); - $displayOptionsButton.insertBefore( $widget.find( 'input.widget-control-save' ) ); + var $displayOptionsButton = $widget.find( 'a.display-options' ).first(), + $relativeWidget = $widget.find( 'input.widget-control-save' ); + + if ( 0 === $relativeWidget.length ) { + // The save button doesn't exist in gutenberg widget editor, the conditional HTML ought to be displayed + // last inside the widget options, so display the button before that. + $relativeWidget = $widget.find( 'div.widget-conditional' ); + } + $displayOptionsButton.insertBefore( $relativeWidget ); // Widgets with no configurable options don't show the Save button's container. $displayOptionsButton From 26b287bbe0890471af2658d7204720bcfca5fa11 Mon Sep 17 00:00:00 2001 From: Dean Sas Date: Mon, 5 Jul 2021 14:47:02 +0100 Subject: [PATCH 04/24] [not verified] Widget visibility: Don't use $_POST directly Don't use $_POST directly for conditions - it isn't populated when using the API. Instead output form names using the `WP_Widget::get_field_name` method when creating the form in `widget_conditions_admin` and access the values using instance variables in `widget_update` --- .../widget-visibility/widget-conditions.php | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php index a80f1ca52cab8..211126ae04a9a 100644 --- a/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php +++ b/projects/plugins/jetpack/modules/widget-visibility/widget-conditions.php @@ -292,7 +292,7 @@ public static function get_pages() { /** * Add the widget conditions to each widget in the admin. * - * @param $widget unused. + * @param WP_Widget $widget Widget to add conditions settings to. * @param $return unused. * @param array $instance The widget settings. */ @@ -357,7 +357,7 @@ class="
- ' ); ?> + get_field_name( 'conditions[action]' ) ) . '">' ); ?>
@@ -375,7 +375,7 @@ class=" ?>
- @@ -395,7 +395,7 @@ class=" - /> + " value="has" /> @@ -444,7 +444,7 @@ class="