Make WordPress Core

Changeset 41044


Ignore:
Timestamp:
07/14/2017 07:40:23 AM (9 years ago)
Author:
westonruter
Message:

Widgets: Add Custom HTML widget.

Ports [40893], [40926], [41000] to the 4.8 branch.
Props westonruter, timmydcrawford, obenland.
Fixes #40907 for 4.8.

Location:
branches/4.8
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • branches/4.8/src/wp-includes/class-wp-customize-widgets.php

    r40395 r41044  
    3636         */
    3737        protected $core_widget_id_bases = array(
    38                 'archives', 'calendar', 'categories', 'links', 'meta',
    39                 'nav_menu', 'pages', 'recent-comments', 'recent-posts',
    40                 'rss', 'search', 'tag_cloud', 'text',
     38                'archives',
     39                'calendar',
     40                'categories',
     41                'custom_html',
     42                'links',
     43                'media_audio',
     44                'media_image',
     45                'media_video',
     46                'meta',
     47                'nav_menu',
     48                'pages',
     49                'recent-comments',
     50                'recent-posts',
     51                'rss',
     52                'search',
     53                'tag_cloud',
     54                'text',
    4155        );
    4256
  • branches/4.8/src/wp-includes/default-filters.php

    r40776 r41044  
    170170add_filter( 'widget_text_content', 'convert_smilies',  20 );
    171171add_filter( 'widget_text_content', 'wpautop'              );
     172
     173add_filter( 'widget_custom_html_content', 'balanceTags' );
    172174
    173175add_filter( 'date_i18n', 'wp_maybe_decline_date' );
  • branches/4.8/src/wp-includes/default-widgets.php

    r40640 r41044  
    6161/** WP_Nav_Menu_Widget class */
    6262require_once( ABSPATH . WPINC . '/widgets/class-wp-nav-menu-widget.php' );
     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 */
     75class 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                $content = $instance['content'];
     123
     124                /**
     125                 * Filters the content of the Custom HTML widget.
     126                 *
     127                 * @since 4.8.1
     128                 *
     129                 * @param string                $content  The widget content.
     130                 * @param array                 $instance Array of settings for the current widget.
     131                 * @param WP_Widget_Custom_HTML $this     Current Custom HTML widget instance.
     132                 */
     133                $content = apply_filters( 'widget_custom_html_content', $content, $instance, $this );
     134
     135                echo $args['before_widget'];
     136                if ( ! empty( $title ) ) {
     137                        echo $args['before_title'] . $title . $args['after_title'];
     138                }
     139                echo $content;
     140                echo $args['after_widget'];
     141        }
     142
     143        /**
     144         * Handles updating settings for the current Custom HTML widget instance.
     145         *
     146         * @since 4.8.1
     147         *
     148         * @param array $new_instance New settings for this instance as input by the user via
     149         *                            WP_Widget::form().
     150         * @param array $old_instance Old settings for this instance.
     151         * @return array Settings to save or bool false to cancel saving.
     152         */
     153        public function update( $new_instance, $old_instance ) {
     154                $instance = array_merge( $this->default_instance, $old_instance );
     155                $instance['title'] = sanitize_text_field( $new_instance['title'] );
     156                if ( current_user_can( 'unfiltered_html' ) ) {
     157                        $instance['content'] = $new_instance['content'];
     158                } else {
     159                        $instance['content'] = wp_kses_post( $new_instance['content'] );
     160                }
     161                return $instance;
     162        }
     163
     164        /**
     165         * Outputs the Custom HTML widget settings form.
     166         *
     167         * @since 4.8.1
     168         *
     169         * @param array $instance Current instance.
     170         * @returns void
     171         */
     172        public function form( $instance ) {
     173                $instance = wp_parse_args( (array) $instance, $this->default_instance );
     174                ?>
     175                <p>
     176                        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
     177                        <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'] ); ?>"/>
     178                </p>
     179
     180                <p>
     181                        <label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:' ); ?></label>
     182                        <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>
     183                </p>
     184
     185                <?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
     186                        <?php
     187                        $probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
     188                        $allowed_html = wp_kses_allowed_html( 'post' );
     189                        $disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
     190                        ?>
     191                        <?php if ( ! empty( $disallowed_html ) ) : ?>
     192                                <p>
     193                                        <?php _e( 'Some HTML tags are not permitted, including:' ); ?>
     194                                        <code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
     195                                </p>
     196                        <?php endif; ?>
     197                <?php endif; ?>
     198                <?php
     199        }
     200}
  • branches/4.8/src/wp-includes/widgets.php

    r40806 r41044  
    14751475        register_widget( 'WP_Nav_Menu_Widget' );
    14761476
     1477        register_widget( 'WP_Widget_Custom_HTML' );
     1478
    14771479        /**
    14781480         * Fires after all default WordPress widgets have been registered.
  • branches/4.8/src/wp-includes/widgets/class-wp-widget-text.php

    r40675 r41044  
    2626                $widget_ops = array(
    2727                        'classname' => 'widget_text',
    28                         'description' => __( 'Arbitrary text or HTML.' ),
     28                        'description' => __( 'Arbitrary text.' ),
    2929                        'customize_selective_refresh' => true,
    3030                );
Note: See TracChangeset for help on using the changeset viewer.