Skip to content

Commit

Permalink
Merge pull request #1979 from Codeinwp/fix/widget_styles
Browse files Browse the repository at this point in the history
fix: widget_styles
  • Loading branch information
HardeepAsrani authored Dec 18, 2023
2 parents a256849 + 761f6fb commit 9eb25ea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
6 changes: 3 additions & 3 deletions inc/class-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function enqueue_block_editor_assets() {
}

global $wp_roles;

wp_localize_script(
'otter-blocks',
'themeisleGutenberg',
Expand Down Expand Up @@ -329,7 +329,7 @@ public function enqueue_block_assets() {
return;
}



if ( is_singular() ) {
$this->enqueue_dependencies();
Expand Down Expand Up @@ -362,7 +362,7 @@ function ( $content ) {
}

if ( $has_widgets ) {

add_filter(
'wp_footer',
function ( $content ) {
Expand Down
106 changes: 103 additions & 3 deletions inc/css/class-css-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use ThemeIsle\GutenbergBlocks\Base_CSS;

use ThemeIsle\GutenbergBlocks\Registration;
use tubalmartin\CssMin\Minifier as CSSmin;

/**
Expand All @@ -30,7 +31,89 @@ public function init() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
add_action( 'rest_api_init', array( $this, 'autoload_block_classes' ) );
add_action( 'before_delete_post', array( __CLASS__, 'delete_css_file' ) );
add_action( 'customize_save_after', array( __CLASS__, 'save_widgets_styles' ) );
add_action( 'customize_save_after', array( $this, 'customize_save_after' ) );
add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 );
}

/**
* Method used to register actively used widgets.
*
* @return void
*/
private function register_used_widgets() {
$registration = Registration::instance();
$widgets_used = $registration::$widget_used;
if ( empty( $widgets_used ) ) {
$sidebar_widgets = get_option( 'sidebars_widgets' );
foreach ( $sidebar_widgets as $sidebar => $widgets ) {
if ( 'wp_inactive_widgets' === $sidebar || ! is_array( $widgets ) ) {
continue;
}
foreach ( $widgets as $widget ) {
$widgets_used[] = $widget;
}
}
$registration::$widget_used = $widgets_used;
}
}

/**
* Method used to add a filter for widget rendering before the partial is rendered.
*
* @param array $partial_args Partial args.
* @param string $partial_id Partial ID.
*
* @return array
*/
public function customize_dynamic_partial_args( $partial_args, $partial_id ) {
if ( preg_match( '/^widget\[(?P<widget_id>.+)\]$/', $partial_id, $matches ) ) {
add_filter( 'widget_block_content', array( $this, 'customize_widget_block_content' ), 10, 3 );
}

return $partial_args;
}

/**
* Add inline styles for partially rendered block inside customizer.
*
* @param string $block_content The block content.
* @param array $block The block data.
* @param \WP_Widget $instance The widget instance.
*
* @return string
*/
public function customize_widget_block_content( $block_content, $block, $instance ) {
$widget_data = get_option( 'widget_block', array() );
$partial_widget = (object) $widget_data[ $instance->number ];
if ( ! isset( $widget_data[ $instance->number ] ) ) {
return $block_content;
}
if ( ! $widget_data[ $instance->number ] ) {
return $block_content;
}

$content = $partial_widget->content;
$blocks = parse_blocks( $content );

if ( ! is_array( $blocks ) || empty( $blocks ) ) {
return $block_content;
}

$animations = boolval( preg_match( '/\banimated\b/', $content ) );
$css = $this->cycle_through_static_blocks( $blocks, $animations );

return '<style>.customize-previewing ' . $css . '</style>' . $block_content;
}

/**
* Method after the customizer save is done.
*
* @return void
*/
public function customize_save_after() {
$this->register_used_widgets();

$this->save_widgets_styles();
}

/**
Expand Down Expand Up @@ -96,7 +179,7 @@ public function register_routes() {
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'save_widgets_styles' ),
'callback' => array( $this, 'save_widgets_styles_rest' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
Expand All @@ -105,6 +188,23 @@ public function register_routes() {
);
}

/**
* When in REST API context, autoload widgets used so that all css data is updated.
*
* @param \WP_REST_Request $request The request object.
*
* @return \WP_REST_Response | \WP_Error
*/
public function save_widgets_styles_rest( \WP_REST_Request $request ) {
$this->register_used_widgets();

$response = $this->save_widgets_styles();
if ( is_null( $response ) ) {
$response = true;
}
return rest_ensure_response( $response );
}

/**
* Function to save post CSS.
*
Expand Down Expand Up @@ -439,7 +539,7 @@ public static function compress( $css ) {

/**
* Mark in post meta if the post has a review block.
*
*
* @param int $post_id Post ID.
* @since 2.4.0
* @access public
Expand Down

0 comments on commit 9eb25ea

Please sign in to comment.