| 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 |
$content = do_blocks( $instance['content'] ); |
| 58 |
|
| 59 |
// Handle embeds for block widgets. |
| 60 |
// |
| 61 |
// When this feature is added to core it may need to be implemented |
| 62 |
// differently. WP_Widget_Text is a good reference, that applies a |
| 63 |
// filter for its content, which WP_Embed uses in its constructor. |
| 64 |
// See https://core.trac.wordpress.org/ticket/51566. |
| 65 |
global $wp_embed; |
| 66 |
echo $wp_embed->autoembed( $content ); |
| 67 |
|
| 68 |
echo $args['after_widget']; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Handles updating settings for the current Block widget instance. |
| 73 |
* |
| 74 |
* @param array $new_instance New settings for this instance as input by the user via |
| 75 |
* WP_Widget::form(). |
| 76 |
* @param array $old_instance Old settings for this instance. |
| 77 |
* |
| 78 |
* @return array Settings to save or bool false to cancel saving. |
| 79 |
* @since 4.8.1 |
| 80 |
*/ |
| 81 |
public function update( $new_instance, $old_instance ) { |
| 82 |
$instance = array_merge( $this->default_instance, $old_instance ); |
| 83 |
$instance['content'] = $new_instance['content']; |
| 84 |
|
| 85 |
return $instance; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Outputs the Block widget settings form. |
| 90 |
* |
| 91 |
* @param array $instance Current instance. |
| 92 |
* |
| 93 |
* @see WP_Widget_Custom_HTML::render_control_template_scripts() |
| 94 |
*/ |
| 95 |
public function form( $instance ) { |
| 96 |
$instance = wp_parse_args( (array) $instance, $this->default_instance ); |
| 97 |
echo do_blocks( $instance['content'] ); |
| 98 |
$textarea_id = $this->get_field_id( 'content' ); |
| 99 |
?> |
| 100 |
<br/> |
| 101 |
<textarea id="<?php echo $textarea_id; ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" |
| 102 |
class="content sync-input" hidden><?php echo esc_textarea( $instance['content'] ); ?></textarea> |
| 103 |
<script> |
| 104 |
(function() { |
| 105 |
var link = "<?php echo esc_js( admin_url( 'themes.php?page=gutenberg-widgets' ) ); ?>"; |
| 106 |
var container = jQuery('#<?php echo $textarea_id; ?>').closest(".form").find('.widget-control-actions .alignleft'); |
| 107 |
container.prepend(jQuery('<span> |</span>')); |
| 108 |
container.prepend(jQuery('<a href="'+link+'" class="button-link">Edit</a>')); |
| 109 |
})(); |
| 110 |
</script> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Make sure no block widget is considered to be wide. |
| 116 |
* |
| 117 |
* @param boolean $is_wide Is regarded wide. |
| 118 |
* @param string $widget_id Widget ID. |
| 119 |
* |
| 120 |
* @return bool Updated is_wide value. |
| 121 |
*/ |
| 122 |
public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) { |
| 123 |
if ( strpos( $widget_id, 'block-' ) === 0 ) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
return $is_wide; |
| 128 |
} |
| 129 |
} |
| 130 |
|