Created
July 12, 2019 11:30
-
-
Save nameDark/96686685fc6e755b6b117d17f9619cee to your computer and use it in GitHub Desktop.
Gutenberg Reusable Blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class WP_Gutenberg_Reusable_Blocks_Widgets { | |
private static $instance; | |
public static function instance() { | |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Gutenberg_Reusable_Blocks_Widgets ) ) { | |
self::$instance = new WP_Gutenberg_Reusable_Blocks_Widgets; | |
self::$instance->initing(); | |
} | |
return self::$instance; | |
} | |
private function initing() { | |
add_action( 'widgets_init', function () { | |
register_widget( 'GUTENRBW_WIDGET' ); | |
} ); | |
} | |
} | |
add_action( 'after_setup_theme', 'GutenReusableBlocksWidgets', 90 ); | |
function GutenReusableBlocksWidgets() { | |
return WP_Gutenberg_Reusable_Blocks_Widgets::instance(); | |
} | |
function gutenrbw_global_blocks() { | |
global $wpdb; | |
$blocks = $wpdb->get_results( "SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE post_type = 'wp_block' AND post_status = 'publish' ORDER BY post_title ASC " ); | |
update_option( 'gutenberg_reusable_blocks_widgetopts', $blocks ); | |
return apply_filters( 'gutenberg_reusable_blocks_widget_get', $blocks ); | |
} | |
class GUTENRBW_WIDGET extends WP_Widget { | |
public function __construct() { | |
$widget_ops = array( | |
'classname' => 'gutenberg-reusable-widget', | |
'description' => __( 'Display Gutenberg Reusable saved Blocks anywhere as widget.', 'gold' ), | |
); | |
parent::__construct( 'gutenrbw_widget', __( 'Reusable Block', 'gold' ), $widget_ops ); | |
} | |
public function widget( $args, $instance ) { | |
if ( isset( $instance['block'] ) && ! empty( $instance['block'] ) ) { | |
echo $args['before_widget']; | |
if ( ! empty( $instance['title'] ) ) { | |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; | |
} | |
echo apply_filters( 'the_content', get_post_field( 'post_content', $instance['block'] ) ); | |
echo $args['after_widget']; | |
} | |
} | |
public function form( $instance ) { | |
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; | |
$block_selected = ! empty( $instance['block'] ) ? $instance['block'] : ''; | |
$blocks = gutenrbw_global_blocks(); ?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'gold' ); ?></label> | |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" | |
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" | |
value="<?php echo esc_attr( $title ); ?>"> | |
</p> | |
<?php if ( ! empty( $blocks ) ) { ?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>"><?php _e( 'Choice from saved Reusable Blocks:', 'gold' ); ?></label> | |
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>" | |
name="<?php echo esc_attr( $this->get_field_name( 'block' ) ); ?>"> | |
<option values=""><?php _e( 'Select Reusable Block', 'gold' ); ?></option> | |
<?php foreach ( $blocks as $block ) { | |
$selected = ( $block_selected == $block->ID ) ? 'selected="selected"' : ''; ?> | |
<option value="<?php echo $block->ID; ?>" <?php echo $selected; ?>><?php echo $block->post_title; ?></option> | |
<?php } ?> | |
</select> | |
</p> | |
<?php | |
} else { ?> | |
<p><?php _e( 'No saved reusable blocks yet.', 'gold' ); ?></p> | |
<?php } | |
} | |
public function update( $new_instance, $old_instance ) { | |
$instance = array(); | |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; | |
$instance['block'] = ( ! empty( $new_instance['block'] ) ) ? strip_tags( $new_instance['block'] ) : ''; | |
return $instance; | |
} | |
} | |
//How to use | |
require_once(get_template_directory().'/inc/reusable-block-widgets.php'); | |
add_action('admin_menu', function(){ | |
add_menu_page( | |
'Gutenberg Reusable Blocks', | |
'Gutenberg Reusable Blocks', | |
'manage_options', | |
'/edit.php?post_type=wp_block', | |
'', | |
'dashicons-portfolio', | |
81 | |
); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment