~canonical-sysadmins/wordpress/4.9.1

« back to all changes in this revision

Viewing changes to wp-includes/default-widgets.php

  • Committer: Barry Price
  • Date: 2017-11-17 04:49:02 UTC
  • mfrom: (1.1.30 upstream)
  • Revision ID: barry.price@canonical.com-20171117044902-5frux4ycbq6g9fyf
Merge WP4.9 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
/** WP_Widget_Media_Video class */
32
32
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-video.php' );
33
33
 
34
 
/** WP_Widget_Meta class */
35
 
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-meta.php' );
 
34
/** WP_Widget_Media_Gallery class */
 
35
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-gallery.php' );
36
36
 
37
37
/** WP_Widget_Meta class */
38
38
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-meta.php' );
61
61
/** WP_Nav_Menu_Widget class */
62
62
require_once( ABSPATH . WPINC . '/widgets/class-wp-nav-menu-widget.php' );
63
63
 
64
 
/**
65
 
 * Core class used to implement a Custom HTML widget.
66
 
 *
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`.
70
 
 *
71
 
 * @since 4.8.1
72
 
 *
73
 
 * @see WP_Widget
74
 
 */
75
 
class WP_Widget_Custom_HTML extends WP_Widget {
76
 
 
77
 
        /**
78
 
         * Default instance.
79
 
         *
80
 
         * @since 4.8.1
81
 
         * @var array
82
 
         */
83
 
        protected $default_instance = array(
84
 
                'title' => '',
85
 
                'content' => '',
86
 
        );
87
 
 
88
 
        /**
89
 
         * Sets up a new Custom HTML widget instance.
90
 
         *
91
 
         * @since 4.8.1
92
 
         */
93
 
        public function __construct() {
94
 
                $widget_ops = array(
95
 
                        'classname' => 'widget_custom_html',
96
 
                        'description' => __( 'Arbitrary HTML code.' ),
97
 
                        'customize_selective_refresh' => true,
98
 
                );
99
 
                $control_ops = array(
100
 
                        'width' => 400,
101
 
                        'height' => 350,
102
 
                );
103
 
                parent::__construct( 'custom_html', __( 'Custom HTML' ), $widget_ops, $control_ops );
104
 
        }
105
 
 
106
 
        /**
107
 
         * Outputs the content for the current Custom HTML widget instance.
108
 
         *
109
 
         * @since 4.8.1
110
 
         *
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.
114
 
         */
115
 
        public function widget( $args, $instance ) {
116
 
 
117
 
                $instance = array_merge( $this->default_instance, $instance );
118
 
 
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 );
121
 
 
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.
127
 
                ) );
128
 
                unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop.
129
 
 
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 );
132
 
 
133
 
                /**
134
 
                 * Filters the content of the Custom HTML widget.
135
 
                 *
136
 
                 * @since 4.8.1
137
 
                 *
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.
141
 
                 */
142
 
                $content = apply_filters( 'widget_custom_html_content', $content, $instance, $this );
143
 
 
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'] );
146
 
 
147
 
                echo $args['before_widget'];
148
 
                if ( ! empty( $title ) ) {
149
 
                        echo $args['before_title'] . $title . $args['after_title'];
150
 
                }
151
 
                echo '<div class="textwidget custom-html-widget">'; // The textwidget class is for theme styling compatibility.
152
 
                echo $content;
153
 
                echo '</div>';
154
 
                echo $args['after_widget'];
155
 
        }
156
 
 
157
 
        /**
158
 
         * Handles updating settings for the current Custom HTML widget instance.
159
 
         *
160
 
         * @since 4.8.1
161
 
         *
162
 
         * @param array $new_instance New settings for this instance as input by the user via
163
 
         *                            WP_Widget::form().
164
 
         * @param array $old_instance Old settings for this instance.
165
 
         * @return array Settings to save or bool false to cancel saving.
166
 
         */
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'];
172
 
                } else {
173
 
                        $instance['content'] = wp_kses_post( $new_instance['content'] );
174
 
                }
175
 
                return $instance;
176
 
        }
177
 
 
178
 
        /**
179
 
         * Outputs the Custom HTML widget settings form.
180
 
         *
181
 
         * @since 4.8.1
182
 
         *
183
 
         * @param array $instance Current instance.
184
 
         * @returns void
185
 
         */
186
 
        public function form( $instance ) {
187
 
                $instance = wp_parse_args( (array) $instance, $this->default_instance );
188
 
                ?>
189
 
                <p>
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'] ); ?>"/>
192
 
                </p>
193
 
 
194
 
                <p>
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>
197
 
                </p>
198
 
 
199
 
                <?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
200
 
                        <?php
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 ) );
204
 
                        ?>
205
 
                        <?php if ( ! empty( $disallowed_html ) ) : ?>
206
 
                                <p>
207
 
                                        <?php _e( 'Some HTML tags are not permitted, including:' ); ?>
208
 
                                        <code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
209
 
                                </p>
210
 
                        <?php endif; ?>
211
 
                <?php endif; ?>
212
 
                <?php
213
 
        }
214
 
}
 
64
/** WP_Widget_Custom_HTML class */
 
65
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-custom-html.php' );