PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / trunk
Block Animations, Motion & Scroll Effects – Ghost Kit vtrunk
3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-reusable-widget.php

class-reusable-widget.php in Block Animations, Motion & Scroll Effects – Ghost Kit trunk, at classes/class-reusable-widget.php

146 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget for reusable blocks
4 *
5 * @package ghostkit
6 */
7
8 /**
9 * GhostKit_Reusable_Widget
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 }
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_id = isset( $instance['block'] ) && ! empty( $instance['block'] ) ? $instance['block'] : '';
35 $post = $block_id ? get_post( $block_id ) : false;
36
37 if ( $block_id && isset( $post->post_content ) && $post->post_content && function_exists( 'has_blocks' ) && function_exists( 'parse_blocks' ) ) {
38 // phpcs:disable
39 echo $args['before_widget'];
40
41 if ( ! empty( $title ) ) {
42 echo $args[ 'before_title' ] . $title . $args[ 'after_title' ];
43 }
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
53 echo apply_filters( 'the_content', $post->post_content );
54
55 if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
56 bbp_remove_all_filters( 'the_content' );
57 }
58
59 echo $args['after_widget'];
60 // phpcs:enable
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 );
77
78 GhostKit_Assets::enqueue_script( 'ghostkit-admin-reusable-widget', 'build/assets/admin/js/reusable-widget', array(), null, false );
79 ?>
80
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>
85
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 }
120
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 }
137 }
138
139 /**
140 * Register Widget
141 */
142 function ghostkit_register_reusable_widget() {
143 register_widget( 'GhostKit_Reusable_Widget' );
144 }
145 add_action( 'widgets_init', 'ghostkit_register_reusable_widget' );
146