| @@ -5,37 +5,37 @@ | ||
| 5 | 5 | * @package ghostkit |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Ghostkit_Reusable_Widget | |
| 9 | + * GhostKit_Reusable_Widget | |
| 10 | 10 | */ |
| 11 | -class Ghostkit_Reusable_Widget extends WP_Widget { | |
| 12 | - /** | |
| 13 | - * Sets up the widgets name etc | |
| 14 | - */ | |
| 15 | - public function __construct() { | |
| 16 | - parent::__construct( | |
| 17 | - 'ghostkit_reusable_widget', | |
| 18 | - esc_html__( 'Reusable Block', 'ghostkit' ), | |
| 19 | - array( | |
| 20 | - 'classname' => 'ghostkit-reusable-widget', | |
| 21 | - 'description' => esc_html__( 'Display Gutenberg Reusable Blocks.', 'ghostkit' ), | |
| 22 | - ) | |
| 23 | - ); | |
| 24 | - } | |
| 11 | +class GhostKit_Reusable_Widget extends WP_Widget { | |
| 12 | + /** | |
| 13 | + * Sets up the widgets name etc | |
| 14 | + */ | |
| 15 | + public function __construct() { | |
| 16 | + parent::__construct( | |
| 17 | + 'ghostkit_reusable_widget', | |
| 18 | + esc_html__( 'Reusable Block', 'ghostkit' ), | |
| 19 | + array( | |
| 20 | + 'classname' => 'ghostkit-reusable-widget', | |
| 21 | + 'description' => esc_html__( 'Display Gutenberg Reusable Blocks.', 'ghostkit' ), | |
| 22 | + ) | |
| 23 | + ); | |
| 24 | + } | |
| 25 | 25 | |
| 26 | - /** | |
| 27 | - * Outputs the content of the widget | |
| 28 | - * | |
| 29 | - * @param array $args args. | |
| 30 | - * @param array $instance instance. | |
| 31 | - */ | |
| 32 | - public function widget( $args, $instance ) { | |
| 33 | - $title = ! empty( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : ''; | |
| 34 | - $block = isset( $instance['block'] ) && ! empty( $instance['block'] ) ? $instance['block'] : ''; | |
| 35 | - $post = $block ? get_post( $block ) : false; | |
| 26 | + /** | |
| 27 | + * Outputs the content of the widget | |
| 28 | + * | |
| 29 | + * @param array $args args. | |
| 30 | + * @param array $instance instance. | |
| 31 | + */ | |
| 32 | + public function widget( $args, $instance ) { | |
| 33 | + $title = ! empty( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : ''; | |
| 34 | + $block_id = isset( $instance['block'] ) && ! empty( $instance['block'] ) ? $instance['block'] : ''; | |
| 35 | + $post = $block_id ? get_post( $block_id ) : false; | |
| 36 | 36 | |
| 37 | - if ( $block && $post->post_content ) { | |
| 37 | + if ( $block_id && isset( $post->post_content ) && $post->post_content && function_exists( 'has_blocks' ) && function_exists( 'parse_blocks' ) ) { | |
| 38 | 38 | // phpcs:disable |
| 39 | 39 | echo $args['before_widget']; |
| 40 | 40 | |
| 41 | 41 | if ( ! empty( $title ) ) { |
| @@ -41,74 +41,100 @@ | ||
| 41 | 41 | if ( ! empty( $title ) ) { |
| 42 | 42 | echo $args[ 'before_title' ] . $title . $args[ 'after_title' ]; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | + // fix for bbPress. | |
| 46 | + // filter 'the_content' may not work in bbPress | |
| 47 | + // https://github.com/nk-crew/ghostkit/issues/72. | |
| 48 | + // https://bbpress.org/forums/topic/the_content-filter-is-removed-by-not-restored-on-custom-wp_query/ | |
| 49 | + if ( function_exists( 'is_bbpress' ) && is_bbpress() ) { | |
| 50 | + bbp_restore_all_filters( 'the_content' ); | |
| 51 | + } | |
| 52 | + | |
| 45 | 53 | echo apply_filters( 'the_content', $post->post_content ); |
| 46 | 54 | |
| 55 | + if ( function_exists( 'is_bbpress' ) && is_bbpress() ) { | |
| 56 | + bbp_remove_all_filters( 'the_content' ); | |
| 57 | + } | |
| 58 | + | |
| 47 | 59 | echo $args['after_widget']; |
| 48 | 60 | // phpcs:enable |
| 49 | - } | |
| 50 | - } | |
| 51 | - /** | |
| 52 | - * Outputs the options form on admin | |
| 53 | - * | |
| 54 | - * @param array $instance The widget options. | |
| 55 | - */ | |
| 56 | - public function form( $instance ) { | |
| 57 | - $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; | |
| 58 | - $selected_block = ! empty( $instance['block'] ) ? $instance['block'] : ''; | |
| 59 | - $blocks = get_posts( array( | |
| 60 | - 'post_type' => 'wp_block', | |
| 61 | - ) ); | |
| 62 | - ?> | |
| 61 | + } | |
| 62 | + } | |
| 63 | + /** | |
| 64 | + * Outputs the options form on admin | |
| 65 | + * | |
| 66 | + * @param array $instance The widget options. | |
| 67 | + */ | |
| 68 | + public function form( $instance ) { | |
| 69 | + $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; | |
| 70 | + $selected_block = ! empty( $instance['block'] ) ? $instance['block'] : ''; | |
| 71 | + $blocks = get_posts( | |
| 72 | + array( | |
| 73 | + 'post_type' => 'wp_block', | |
| 74 | + 'numberposts' => -1, | |
| 75 | + ) | |
| 76 | + ); | |
| 63 | 77 | |
| 64 | - <p> | |
| 65 | - <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'ghostkit' ); ?></label> | |
| 66 | - <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 ); ?>"> | |
| 67 | - </p> | |
| 78 | + GhostKit_Assets::enqueue_script( 'ghostkit-admin-reusable-widget', 'build/assets/admin/js/reusable-widget', array(), null, false ); | |
| 79 | + ?> | |
| 68 | 80 | |
| 69 | - <?php | |
| 70 | - if ( ! empty( $blocks ) ) { | |
| 71 | - ?> | |
| 72 | - <p> | |
| 73 | - <label for="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>"><?php echo esc_attr__( 'Select Block:', 'ghostkit' ); ?></label> | |
| 74 | - <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'block' ) ); ?>"> | |
| 75 | - <option value="" disabled <?php selected( ! $selected_block ); ?>><?php echo esc_html__( '--- Select block ---', 'ghostkit' ); ?></option> | |
| 76 | - <?php | |
| 77 | - foreach ( $blocks as $block ) { | |
| 78 | - ?> | |
| 79 | - <option value="<?php echo esc_attr( $block->ID ); ?>" <?php selected( $selected_block, $block->ID ); ?>> | |
| 80 | - <?php echo esc_html( $block->post_title ); ?> | |
| 81 | - </option> | |
| 82 | - <?php | |
| 83 | - } | |
| 84 | - ?> | |
| 85 | - </select> | |
| 86 | - </p> | |
| 87 | - <?php | |
| 88 | - } else { | |
| 89 | - ?> | |
| 90 | - <p><?php echo esc_attr__( 'No reusable blocks found.', 'ghostkit' ); ?></p> | |
| 91 | - <?php | |
| 92 | - } | |
| 93 | - } | |
| 81 | + <p> | |
| 82 | + <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'ghostkit' ); ?></label> | |
| 83 | + <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 ); ?>"> | |
| 84 | + </p> | |
| 94 | 85 | |
| 95 | - /** | |
| 96 | - * Processing widget options on save | |
| 97 | - * | |
| 98 | - * @param array $new_instance new options. | |
| 99 | - * @param array $old_instance previous options. | |
| 100 | - * | |
| 101 | - * @return array | |
| 102 | - */ | |
| 103 | - public function update( $new_instance, $old_instance ) { | |
| 104 | - $instance = array( | |
| 105 | - 'title' => ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '', | |
| 106 | - 'block' => ! empty( $new_instance['block'] ) ? sanitize_text_field( $new_instance['block'] ) : '', | |
| 107 | - ); | |
| 86 | + <?php | |
| 87 | + if ( ! empty( $blocks ) ) { | |
| 88 | + ?> | |
| 89 | + <p> | |
| 90 | + <label for="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>"><?php echo esc_attr__( 'Select Block:', 'ghostkit' ); ?></label> | |
| 91 | + <select class="widefat gkt-reusable-block-select" id="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'block' ) ); ?>"> | |
| 92 | + <option value="" disabled <?php selected( '', $selected_block ); ?>><?php echo esc_html__( '--- Select block ---', 'ghostkit' ); ?></option> | |
| 93 | + <?php | |
| 94 | + foreach ( $blocks as $block ) { | |
| 95 | + ?> | |
| 96 | + <option value="<?php echo esc_attr( $block->ID ); ?>" <?php selected( $selected_block, $block->ID ); ?>> | |
| 97 | + <?php echo esc_html( $block->post_title ); ?> | |
| 98 | + </option> | |
| 99 | + <?php | |
| 100 | + } | |
| 101 | + ?> | |
| 102 | + </select> | |
| 103 | + </p> | |
| 104 | + <?php | |
| 105 | + } else { | |
| 106 | + ?> | |
| 107 | + <p><?php echo esc_attr__( 'No reusable blocks found.', 'ghostkit' ); ?></p> | |
| 108 | + <?php | |
| 109 | + } | |
| 110 | + ?> | |
| 111 | + <p class="gkt-reusable-block-edit-button" style="display: none" data-admin-url="<?php echo esc_url( get_admin_url() ); ?>"> | |
| 112 | + <label for="<?php echo esc_attr( $this->get_field_id( 'edit_button' ) ); ?>"><?php esc_attr_e( 'Edit:', 'ghostkit' ); ?></label> | |
| 113 | + <br> | |
| 114 | + <a class="button" id="<?php echo esc_attr( $this->get_field_id( 'edit_button' ) ); ?>" href="" target="_blank"> | |
| 115 | + <?php esc_attr_e( 'Edit Reusable Block', 'ghostkit' ); ?> | |
| 116 | + </a> | |
| 117 | + </p> | |
| 118 | + <?php | |
| 119 | + } | |
| 108 | 120 | |
| 109 | - return $instance; | |
| 110 | - } | |
| 121 | + /** | |
| 122 | + * Processing widget options on save | |
| 123 | + * | |
| 124 | + * @param array $new_instance new options. | |
| 125 | + * @param array $old_instance previous options. | |
| 126 | + * | |
| 127 | + * @return array | |
| 128 | + */ | |
| 129 | + public function update( $new_instance, $old_instance ) { | |
| 130 | + $instance = array( | |
| 131 | + 'title' => ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '', | |
| 132 | + 'block' => ! empty( $new_instance['block'] ) ? sanitize_text_field( $new_instance['block'] ) : '', | |
| 133 | + ); | |
| 134 | + | |
| 135 | + return $instance; | |
| 136 | + } | |
| 111 | 137 | } |
| 112 | 138 | |
| 113 | 139 | /** |
| 114 | 140 | * Register Widget |
| @@ -113,7 +139,7 @@ | ||
| 113 | 139 | /** |
| 114 | 140 | * Register Widget |
| 115 | 141 | */ |
| 116 | 142 | function ghostkit_register_reusable_widget() { |
| 117 | - register_widget( 'Ghostkit_Reusable_Widget' ); | |
| 143 | + register_widget( 'GhostKit_Reusable_Widget' ); | |
| 118 | 144 | } |
| 119 | 145 | add_action( 'widgets_init', 'ghostkit_register_reusable_widget' ); |