• Resolved jhotadhari

    (@jhotadhari)


    Hi,
    works great but can’t use it in columns block or any nested block.

    I updated classes/widget-area.php to search the blocks recursively:

    
    	/**
    	 * Save newly added widgets in options on post save.
    	 *
    	 * @param int    $post_id post id.
    	 * @param object $post
    	 * @param string $update
    	 */
    	public function update_widgets_log( $post_id, $post, $update ) {
    		// Check if user has permissions to save data.
    		if ( ! current_user_can( 'edit_post', $post_id ) ) {
    			return;
    		}
    
    		// Check if not an autosave.
    		if ( wp_is_post_autosave( $post_id ) ) {
    			return;
    		}
    
    		// Check if not a revision.
    		if ( wp_is_post_revision( $post_id ) ) {
    			return;
    		}
    
    		$saved_widgets = get_option( self::$option_name, [] );
    
    		$blocks = parse_blocks( $post->post_content );
    
    		if ( isset( $saved_widgets[ $post_id ] ) ) {
    			unset( $saved_widgets[ $post_id ] );
    		}
    
    		$saved_widgets = self::find_saved_widgets_recursive( $saved_widgets, $post_id, $blocks );
    
    		update_option( self::$option_name, $saved_widgets, true );
    
    	}
    
    	/**
    	 * Walk the post content blocks recursively and find widget area blocks.
    	 *
    	 * @param array	$saved_widgets
    	 * @param int	$post_id		post id.
    	 * @param array	$blocks
    	 * @return array	$saved_widgets
    	 */
    	protected static function find_saved_widgets_recursive( &$saved_widgets, $post_id, $blocks ) {
    
    		if ( ! empty( $blocks ) ) {
    			foreach ( $blocks as $block ) {
    				if ( isset( $block['blockName'] ) && self::$block_name === $block['blockName'] ) {
    					if ( '' !== trim( $block['attrs']['widget_area_title'] ) ) {
    						$saved_widgets[ $post_id ][] = $block['attrs']['widget_area_title'];
    					}
    				} elseif ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) {
    					self::find_saved_widgets_recursive( $saved_widgets, $post_id, $block['innerBlocks'] );
    				}
    			}
    		}
    
    		return $saved_widgets;
    	}
    

    This solves the issue.
    Can’t find the git repository to fork it.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘innerBlocks widget areas do not get saved’ is closed to new replies.