Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block editor usecase #68

Open
wants to merge 33 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
74157aa
Release notes
kasparsd Apr 27, 2020
9242557
Link to configure widgets, too
kasparsd Apr 27, 2020
89a12df
Merge pull request #66 from kasparsd/release/1.3.2
kasparsd Apr 27, 2020
fc9046b
Update JS deps
kasparsd Oct 31, 2021
e11d873
Can be just one language by default
kasparsd Oct 31, 2021
19223d6
Prevent the package from being published
kasparsd Oct 31, 2021
f1a41b1
Enforce Node version
kasparsd Oct 31, 2021
ad6a139
Move to GitHub actions as a test
kasparsd Oct 31, 2021
8c7b557
Remove Travis
kasparsd Oct 31, 2021
f5a3654
Check for mess too
kasparsd Oct 31, 2021
bb1b121
Require the previous version which works with the WP eslint config
kasparsd Oct 31, 2021
9a762af
Fix WP prettier issue
kasparsd Oct 31, 2021
4bb51d6
Don’t need to mock this for this test
kasparsd Oct 31, 2021
75698eb
Update deps
kasparsd Oct 31, 2021
a97c665
Patch the notice
kasparsd Oct 31, 2021
46f8612
Add a setting to disable block widget editor
kasparsd Oct 31, 2021
b135f31
Make it dynamic
kasparsd Oct 31, 2021
5160743
Display a notice when the legacy editor enabled via our settings
kasparsd Oct 31, 2021
a20a2e4
Add block editor overrides
kasparsd Oct 31, 2021
0ddf6bd
Default to linting the current directory
kasparsd Oct 31, 2021
9b3261c
Introduce a command to autofix things
kasparsd Oct 31, 2021
ab0b30d
Use the correct helper when no translation needed
kasparsd Oct 31, 2021
45b31de
Remove unused
kasparsd Oct 31, 2021
541fe95
Introduce helpers for modifying widget data
kasparsd Oct 31, 2021
8bacd99
Use the new helpers for updating the widgets
kasparsd Oct 31, 2021
742872e
Report just the setting
kasparsd Oct 31, 2021
f8789d6
Generic block editor helper
kasparsd Oct 31, 2021
6ee6a79
Match the rest of method naming
kasparsd Oct 31, 2021
5b60159
Translate the link name too
kasparsd Oct 31, 2021
6976354
Report where this works
kasparsd Oct 31, 2021
830786a
Improved styling
kasparsd Oct 31, 2021
8db0e82
Merge remote-tracking branch 'origin/develop' into fix/legacy-widgets
kasparsd Oct 31, 2021
fbc6fd5
Override the default as needed
kasparsd Nov 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
.context-group-custom_post_types_taxonomies .context-group-wrap { max-height:10em; overflow:auto; padding:0.5em; margin-bottom:0.5em; background:#fff; border:1px solid #ddd; }
.context-group-custom_post_types_taxonomies label { display:block; padding:0.25em 0; }

.context-group-wrap .wc-field-text-word-count input { width:3em; }
.widget-context .context-group-word_count .context-group-wrap label { display: inline-block; }
.widget-context .context-group-word_count .context-group-wrap select,
.widget-context .context-group-word_count .context-group-wrap input[type="text"] { width:5em; display:inline-block; }

.context-group-admin_notes textarea { height:4em; }

Expand All @@ -53,6 +55,13 @@
.widget-content,
#widgets-right .widget { clear:left; }

.wp-block-legacy-widget__edit-form .widget-context { font-size:13px; line-height: 1.5; color:#000; }
.wp-block-legacy-widget__edit-form .widget-context .widget-context-header h3 { font-weight:bold; line-height: 2.1; line-height: inherit; font-size:14px; color: #000; }
.wp-block-legacy-widget__edit-form .context-toggle { font-size:1em; }
.wp-block-legacy-widget__edit-form .widget-context .context-group { padding-left: 1em; border-left:1px solid #ddd; }
.wp-block-legacy-widget__edit-form .widget-context .context-group-incexc { padding-left:0; border:none; }
.wp-block-legacy-widget__edit-form .widget-context-legacy-notice { padding:0.5em 1em; margin:1em 0; font-size:14px; }

@media screen and ( max-width: 900px ) {
.widget-context-settings-wrap { padding-right:0; }
.widget-context-form { margin:0; }
Expand Down
12 changes: 7 additions & 5 deletions assets/js/widget-context.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/* global jQuery */
jQuery( document ).ready( function( $ ) {
function showHideControls( widgetId ) {
var condition = $( '#widget-context-' + widgetId + ' .wc-field-select-condition select' ).val();
function showHideControls( widgetId, state ) {
if ( ! state ) {
state = $( '#widget-context-' + widgetId + ' .wc-field-select-condition select' ).val();
}

$( '#widget-context-' + widgetId ).toggleClass( 'context-global', ( condition === 'show' || condition === 'hide' ) );
$( '#widget-context-' + widgetId ).toggleClass( 'context-global', ( state === 'show' || state === 'hide' ) );
}

$( '.widget-context-inside' ).each( function() {
showHideControls( $( this ).data( 'widget-id' ) );
} );

$( '#widgets-right, #widgets-left, #customize-theme-controls' ).on( 'change', '.wc-field-select-condition select', function() {
showHideControls( $( this ).parent().data( 'widget-id' ) );
$( '#widgets-right, #widgets-left, #customize-theme-controls, .edit-widgets-block-editor' ).on( 'change', '.wc-field-select-condition select', function( condition ) {
showHideControls( $( this ).parent().data( 'widget-id' ), condition.target.value );
} );

$( document ).on( 'widget-updated widget-added', function( e, widget ) {
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
"scripts": {
"lint": [
"composer validate --no-check-publish --strict",
"phpcs ."
"phpcs"
],
"lint-fix": [
"phpcbf"
],
"mess": [
"phpmd ./src text phpmd.xml --suffixes php"
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<arg name="colors" />
<arg value="s" /><!-- Show sniff codes in all reports. -->

<file>.</file>

<rule ref="PHPCompatibility" />
<config name="testVersion" value="5.6-" />

Expand Down
181 changes: 143 additions & 38 deletions src/WidgetContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public function init() {
// Add admin menu for config
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );

// Save widget context settings, when in admin area
// Allow users to disable the block widget editor.
add_filter( 'gutenberg_use_widgets_block_editor', array( $this, 'maybe_disable_block_widget_editor' ) );
add_filter( 'use_widgets_block_editor', array( $this, 'maybe_disable_block_widget_editor' ) );
add_action( 'admin_notices', array( $this, 'maybe_notify_of_disabled_block_widget_editor' ) );

// Save widget context settings, when in admin area of the legacy widget editing screen.
add_action( 'sidebar_admin_setup', array( $this, 'save_widget_context_settings' ) );

// Fix legacy context option naming
Expand Down Expand Up @@ -163,20 +168,6 @@ function define_widget_contexts() {
uasort( $this->contexts, array( $this, 'sort_context_by_weight' ) );
}


public function get_context_options( $widget_id = null ) {
if ( ! $widget_id ) {
return $this->context_options;
}

if ( isset( $this->context_options[ $widget_id ] ) ) {
return $this->context_options[ $widget_id ];
}

return null;
}


public function get_context_settings( $widget_id = null ) {
if ( ! $widget_id ) {
return $this->context_settings;
Expand Down Expand Up @@ -283,34 +274,74 @@ function save_widget_context_settings() {
return;
}

// Delete a widget
// Delete a widget.
if ( isset( $_POST['delete_widget'] ) && isset( $_POST['the-widget-id'] ) ) {
unset( $this->context_options[ $_POST['the-widget-id'] ] );
$this->delete_context_settings_for_widget( $_POST['the-widget-id'] );
}

// Add / Update
$this->context_options = array_merge( $this->context_options, $_POST['wl'] );

$sidebars_widgets = wp_get_sidebars_widgets();
$all_widget_ids = array();

// Get a lits of all widget IDs
foreach ( $sidebars_widgets as $widget_area => $widgets ) {
foreach ( $widgets as $widget_order => $widget_id ) {
$all_widget_ids[] = $widget_id;
// Add or update widgets.
if ( ! empty( $_POST['wl'] ) && is_array( $_POST['wl'] ) ) {
foreach ( $_POST['wl'] as $widget_id => $widget_context_settings ) {
$this->update_context_settings_for_widget( $widget_id, $widget_context_settings );
}
}

$all_widget_ids = $this->get_all_widget_ids();

// Remove non-existant widget contexts from the settings
foreach ( $this->context_options as $widget_id => $widget_context ) {
if ( ! in_array( $widget_id, $all_widget_ids, true ) ) {
unset( $this->context_options[ $widget_id ] );
$this->delete_context_settings_for_widget( $widget_id );
}
}

update_option( $this->options_name, $this->context_options );
$this->store_widget_context_settings();
}

/**
* Get widget IDs from all widget areas.
*
* @return array
*/
public function get_all_widget_ids() {
$all_widget_ids = array();

// Get a list of all widget IDs.
foreach ( wp_get_sidebars_widgets() as $widget_area => $widgets ) {
foreach ( $widgets as $widget_order => $widget_id ) {
$all_widget_ids[] = $widget_id;
}
}

return $all_widget_ids;
}

/**
* Setting context settings for a widget by ID.
*
* @return void
*/
protected function update_context_settings_for_widget( $widget_id, $settings ) {
$this->context_options[ $widget_id ] = $settings;
}

/**
* Delete context settings for a widget by ID.
*
* @return void
*/
protected function delete_context_settings_for_widget( $widget_id ) {
unset( $this->context_options[ $widget_id ] );
}

/**
* Persist the context settings for all widgets.
*
* @return void
*/
protected function store_widget_context_settings() {
update_option( $this->options_name, $this->context_options );
}

function maybe_unset_widgets_by_context( $sidebars_widgets ) {
// Don't run this at the backend or before
Expand Down Expand Up @@ -686,6 +717,12 @@ function display_widget_context( $widget_id = null ) {
$has_controls = array_diff( $controls_not_core, $controls_disabled );

if ( empty( $controls ) || empty( $has_controls ) ) {
$controls = array(
sprintf(
'<p class="error">%s</p>',
__( 'No widget controls enabled.', 'widget-context' )
),
);

if ( current_user_can( 'edit_theme_options' ) ) {
$controls = array(
Expand All @@ -694,17 +731,10 @@ function display_widget_context( $widget_id = null ) {
sprintf(
/* translators: %s is a URL to the settings page. */
__( 'No widget controls enabled. You can enable them in <a href="%s">Widget Context settings</a>.', 'widget-context' ),
$this->plugin_settings_admin_url()
esc_url( $this->plugin_settings_admin_url() )
)
),
);
} else {
$controls = array(
sprintf(
'<p class="error">%s</p>',
__( 'No widget controls enabled.', 'widget-context' )
),
);
}
}

Expand Down Expand Up @@ -1064,16 +1094,74 @@ public function customize_widgets_admin_url() {
return admin_url( 'customize.php?autofocus[panel]=widgets' );
}

/**
* Disable the widget block editor, if necessary.
*
* @return boolean
*/
public function maybe_disable_block_widget_editor( $enabled ) {
if ( $this->widget_block_editor_disabled() ) {
return false;
}

return $enabled;
}

/**
* If the block widget editor is enabled.
*
* @return boolean
*/
public function widget_block_editor_enabled() {
return function_exists( 'wp_use_widgets_block_editor' ) && wp_use_widgets_block_editor();
}

/**
* Check if the WP environment supports widget block editor.
*
* @return boolean
*/
public function widget_block_editor_supported() {
return function_exists( 'wp_use_widgets_block_editor' );
}

/**
* Check if the widget block editor is enabled in our settings.
*
* @return boolean
*/
public function widget_block_editor_disabled() {
return ! empty( $this->context_settings['widget_block_editor_disable'] );
}

/**
* Get the URL to the plugin settings page.
*
* @return string
*/
public function plugin_settings_admin_url() {
return admin_url( 'themes.php?page=widget_context_settings' );
return admin_url( sprintf( 'themes.php?page=%s', $this->settings_name ) );
}

/**
* Maybe display a notice about the widget interface.
*
* @return void
*/
public function maybe_notify_of_disabled_block_widget_editor() {
$admin_screen = get_current_screen();

if ( ! empty( $admin_screen->base ) && 'widgets' === $admin_screen->base ) {
if ( $this->widget_block_editor_supported() && $this->widget_block_editor_disabled() ) {
printf(
'<div class="notice notice-info"><p>%s <a class="button" href="%s">%s</a></p></div>',
esc_html__( 'The legacy widget editor is currently enabled in the Widget Context settings.', 'widget-context' ),
esc_url( $this->plugin_settings_admin_url() ),
esc_html__( 'Configure', 'widget-context' )
);
}
}
}

function widget_context_admin_view() {
$context_controls = array();
Expand Down Expand Up @@ -1142,6 +1230,23 @@ function widget_context_admin_view() {
</p>
</td>
</tr>
<?php if ( $this->widget_block_editor_supported() ) : ?>
<tr>
<th scrope="row">
<?php esc_html_e( 'Widgets Editing', 'widget-context' ); ?>
</th>
<td>
<label>
<input name="<?php echo esc_attr( $this->settings_name ); ?>[widget_block_editor_disable]" type="hidden" value="0" />
<input name="<?php echo esc_attr( $this->settings_name ); ?>[widget_block_editor_disable]" type="checkbox" value="1" <?php checked( $this->widget_block_editor_disabled(), true, true ); ?> />
<?php esc_html_e( 'Use legacy widget editor', 'widget-context' ); ?>
</label>
<p class="help">
<?php esc_html_e( 'Enables the widget editing interface used before WordPress version 5.8.', 'widget-context' ); ?>
</p>
</td>
</tr>
<?php endif; // Classic Widgets. ?>
<tr>
<th scrope="row">
<?php esc_html_e( 'Configure Widgets', 'widget-context' ); ?>
Expand Down