PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 1.6.3
Block Animations, Motion & Scroll Effects – Ghost Kit v1.6.3
3.7.2 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 1.6.3, at classes/class-reusable-widget.php

120 lines 4.1 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 = isset( $instance['block'] ) && ! empty( $instance['block'] ) ? $instance['block'] : '';
35 $post = $block ? get_post( $block ) : false;
36
37 if ( $block && $post->post_content ) {
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 echo apply_filters( 'the_content', $post->post_content );
46
47 echo $args['after_widget'];
48 // 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 ?>
63
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>
68
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 }
94
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 );
108
109 return $instance;
110 }
111 }
112
113 /**
114 * Register Widget
115 */
116 function ghostkit_register_reusable_widget() {
117 register_widget( 'Ghostkit_Reusable_Widget' );
118 }
119 add_action( 'widgets_init', 'ghostkit_register_reusable_widget' );
120