61
61
/** WP_Nav_Menu_Widget class */
62
62
require_once( ABSPATH . WPINC . '/widgets/class-wp-nav-menu-widget.php' );
65
* Core class used to implement a Custom HTML widget.
67
* Note that this class is only located in this file in the 4.8 branch
68
* for the sake of automatic updates. In 4.9 and above, it is located at
69
* `wp-includes/widgets/class-wp-widget-custom-html.php`.
75
class WP_Widget_Custom_HTML extends WP_Widget {
83
protected $default_instance = array(
89
* Sets up a new Custom HTML widget instance.
93
public function __construct() {
95
'classname' => 'widget_custom_html',
96
'description' => __( 'Arbitrary HTML code.' ),
97
'customize_selective_refresh' => true,
103
parent::__construct( 'custom_html', __( 'Custom HTML' ), $widget_ops, $control_ops );
107
* Outputs the content for the current Custom HTML widget instance.
111
* @param array $args Display arguments including 'before_title', 'after_title',
112
* 'before_widget', and 'after_widget'.
113
* @param array $instance Settings for the current Custom HTML widget instance.
115
public function widget( $args, $instance ) {
117
$instance = array_merge( $this->default_instance, $instance );
119
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
120
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
122
// Prepare instance data that looks like a normal Text widget.
123
$simulated_text_widget_instance = array_merge( $instance, array(
124
'text' => isset( $instance['content'] ) ? $instance['content'] : '',
125
'filter' => false, // Because wpautop is not applied.
126
'visual' => false, // Because it wasn't created in TinyMCE.
128
unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop.
130
/** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */
131
$content = apply_filters( 'widget_text', $instance['content'], $simulated_text_widget_instance, $this );
134
* Filters the content of the Custom HTML widget.
138
* @param string $content The widget content.
139
* @param array $instance Array of settings for the current widget.
140
* @param WP_Widget_Custom_HTML $this Current Custom HTML widget instance.
142
$content = apply_filters( 'widget_custom_html_content', $content, $instance, $this );
144
// Inject the Text widget's container class name alongside this widget's class name for theme styling compatibility.
145
$args['before_widget'] = preg_replace( '/(?<=\sclass=["\'])/', 'widget_text ', $args['before_widget'] );
147
echo $args['before_widget'];
148
if ( ! empty( $title ) ) {
149
echo $args['before_title'] . $title . $args['after_title'];
151
echo '<div class="textwidget custom-html-widget">'; // The textwidget class is for theme styling compatibility.
154
echo $args['after_widget'];
158
* Handles updating settings for the current Custom HTML widget instance.
162
* @param array $new_instance New settings for this instance as input by the user via
164
* @param array $old_instance Old settings for this instance.
165
* @return array Settings to save or bool false to cancel saving.
167
public function update( $new_instance, $old_instance ) {
168
$instance = array_merge( $this->default_instance, $old_instance );
169
$instance['title'] = sanitize_text_field( $new_instance['title'] );
170
if ( current_user_can( 'unfiltered_html' ) ) {
171
$instance['content'] = $new_instance['content'];
173
$instance['content'] = wp_kses_post( $new_instance['content'] );
179
* Outputs the Custom HTML widget settings form.
183
* @param array $instance Current instance.
186
public function form( $instance ) {
187
$instance = wp_parse_args( (array) $instance, $this->default_instance );
190
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
191
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
195
<label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:' ); ?></label>
196
<textarea class="widefat code" rows="16" cols="20" id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
199
<?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
201
$probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
202
$allowed_html = wp_kses_allowed_html( 'post' );
203
$disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
205
<?php if ( ! empty( $disallowed_html ) ) : ?>
207
<?php _e( 'Some HTML tags are not permitted, including:' ); ?>
208
<code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
64
/** WP_Widget_Custom_HTML class */
65
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-custom-html.php' );