PluginProbe
Gutenberg / 9.2.2
Gutenberg v9.2.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-widget-block.php

class-wp-widget-block.php in Gutenberg 9.2.2, at lib/class-wp-widget-block.php

121 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget API: WP_Widget_Block class
4 *
5 * @package Gutenberg
6 */
7
8 /**
9 * Core class used to implement a Block widget.
10 *
11 * @see WP_Widget
12 */
13 class WP_Widget_Block extends WP_Widget {
14
15 /**
16 * Default instance.
17 *
18 * @since 4.8.1
19 * @var array
20 */
21 protected $default_instance = array(
22 'content' => '',
23 );
24
25 /**
26 * Sets up a new Block widget instance.
27 *
28 * @since 4.8.1
29 */
30 public function __construct() {
31 $widget_ops = array(
32 'classname' => 'widget_block',
33 'description' => __( 'Gutenberg block.', 'gutenberg' ),
34 'customize_selective_refresh' => true,
35 );
36 $control_ops = array(
37 'width' => 400,
38 'height' => 350,
39 );
40 parent::__construct( 'block', __( 'Block', 'gutenberg' ), $widget_ops, $control_ops );
41 add_action( 'is_wide_widget_in_customizer', array( $this, 'set_is_wide_widget_in_customizer' ), 10, 2 );
42 }
43
44 /**
45 * Outputs the content for the current Block widget instance.
46 *
47 * @param array $args Display arguments including 'before_title', 'after_title',
48 * 'before_widget', and 'after_widget'.
49 * @param array $instance Settings for the current Block widget instance.
50 *
51 * @since 4.8.1
52 *
53 * @global WP_Post $post Global post object.
54 */
55 public function widget( $args, $instance ) {
56 echo $args['before_widget'];
57 echo do_blocks( $instance['content'] );
58 echo $args['after_widget'];
59 }
60
61 /**
62 * Handles updating settings for the current Block widget instance.
63 *
64 * @param array $new_instance New settings for this instance as input by the user via
65 * WP_Widget::form().
66 * @param array $old_instance Old settings for this instance.
67 *
68 * @return array Settings to save or bool false to cancel saving.
69 * @since 4.8.1
70 */
71 public function update( $new_instance, $old_instance ) {
72 $instance = array_merge( $this->default_instance, $old_instance );
73 $instance['content'] = $new_instance['content'];
74
75 return $instance;
76 }
77
78 /**
79 * Outputs the Block widget settings form.
80 *
81 * @param array $instance Current instance.
82 *
83 * @see WP_Widget_Custom_HTML::render_control_template_scripts()
84 */
85 public function form( $instance ) {
86 $instance = wp_parse_args( (array) $instance, $this->default_instance );
87 echo do_blocks( $instance['content'] );
88 $textarea_id = $this->get_field_id( 'content' );
89 ?>
90 <br/>
91 <textarea id="<?php echo $textarea_id; ?>" name="<?php echo $this->get_field_name( 'content' ); ?>"
92 class="content sync-input" hidden><?php echo esc_textarea( $instance['content'] ); ?></textarea>
93 <script>
94 (function() {
95 var link = "<?php echo esc_js( admin_url( 'themes.php?page=gutenberg-widgets' ) ); ?>";
96 var container = jQuery('#<?php echo $textarea_id; ?>').closest(".form").find('.widget-control-actions .alignleft');
97 container.prepend(jQuery('<span> |</span>'));
98 container.prepend(jQuery('<a href="'+link+'" class="button-link">Edit</a>'));
99 })();
100 </script>
101 <?php
102 }
103
104 /**
105 * Make sure no block widget is considered to be wide.
106 *
107 * @param boolean $is_wide Is regarded wide.
108 * @param string $widget_id Widget ID.
109 *
110 * @return bool Updated is_wide value.
111 */
112 public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
113 if ( strpos( $widget_id, 'block-' ) === 0 ) {
114 return false;
115 }
116
117 return $is_wide;
118 }
119
120 }
121