<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[40893] trunk: Widgets: Add widget dedicated for HTML Code, taking over this role of the Text widget.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/40893">40893</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/40893","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-06-09 23:53:27 +0000 (Fri, 09 Jun 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Widgets: Add widget dedicated for HTML Code, taking over this role of the Text widget.

Props westonruter, timmydcrawford.
See <a href="https://core.trac.wordpress.org/ticket/40951">#40951</a>, <a href="https://core.trac.wordpress.org/ticket/35243">#35243</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/40907">#40907</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesdefaultwidgetsphp">trunk/src/wp-includes/default-widgets.php</a></li>
<li><a href="#trunksrcwpincludeswidgetsclasswpwidgettextphp">trunk/src/wp-includes/widgets/class-wp-widget-text.php</a></li>
<li><a href="#trunksrcwpincludeswidgetsphp">trunk/src/wp-includes/widgets.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeswidgetsclasswpwidgethtmlcodephp">trunk/src/wp-includes/widgets/class-wp-widget-html-code.php</a></li>
<li><a href="#trunktestsphpunittestswidgetshtmlcodewidgetphp">trunk/tests/phpunit/tests/widgets/html-code-widget.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2017-06-09 19:28:12 UTC (rev 40892)
+++ trunk/src/wp-includes/default-filters.php   2017-06-09 23:53:27 UTC (rev 40893)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -170,6 +170,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'widget_text_content', 'convert_smilies',  20 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'widget_text_content', 'wpautop'              );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'widget_html_code_content', 'balanceTags' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'date_i18n', 'wp_maybe_decline_date' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> // RSS filters
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultwidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-widgets.php 2017-06-09 19:28:12 UTC (rev 40892)
+++ trunk/src/wp-includes/default-widgets.php   2017-06-09 23:53:27 UTC (rev 40893)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -60,3 +60,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /** WP_Nav_Menu_Widget class */
</span><span class="cx" style="display: block; padding: 0 10px"> require_once( ABSPATH . WPINC . '/widgets/class-wp-nav-menu-widget.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/** WP_Widget_HTML_Code class */
+require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-html-code.php' );
</ins></span></pre></div>
<a id="trunksrcwpincludeswidgetsclasswpwidgethtmlcodephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/widgets/class-wp-widget-html-code.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/widgets/class-wp-widget-html-code.php                               (rev 0)
+++ trunk/src/wp-includes/widgets/class-wp-widget-html-code.php 2017-06-09 23:53:27 UTC (rev 40893)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,139 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Widget API: WP_Widget_HTML_Code class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.8.1
+ */
+
+/**
+ * Core class used to implement a HTML Code widget.
+ *
+ * @since 4.8.1
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_HTML_Code extends WP_Widget {
+
+       /**
+        * Default instance.
+        *
+        * @since 4.8.1
+        * @var array
+        */
+       protected $default_instance = array(
+               'title' => '',
+               'content' => '',
+       );
+
+       /**
+        * Sets up a new HTML Code widget instance.
+        *
+        * @since 4.8.1
+        */
+       public function __construct() {
+               $widget_ops = array(
+                       'classname' => 'widget_html_code',
+                       'description' => __( 'Arbitrary HTML code.' ),
+                       'customize_selective_refresh' => true,
+               );
+               $control_ops = array();
+               parent::__construct( 'html_code', __( 'HTML Code' ), $widget_ops, $control_ops );
+       }
+
+       /**
+        * Outputs the content for the current HTML Code widget instance.
+        *
+        * @since 4.8.1
+        *
+        * @param array $args     Display arguments including 'before_title', 'after_title',
+        *                        'before_widget', and 'after_widget'.
+        * @param array $instance Settings for the current HTML Code widget instance.
+        */
+       public function widget( $args, $instance ) {
+
+               $instance = array_merge( $this->default_instance, $instance );
+
+               /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+               $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
+
+               $content = $instance['content'];
+
+               /**
+                * Filters the content of the HTML Code widget.
+                *
+                * @since 4.8.1
+                *
+                * @param string              $content  The widget content.
+                * @param array               $instance Array of settings for the current widget.
+                * @param WP_Widget_HTML_Code $this     Current HTML Code widget instance.
+                */
+               $content = apply_filters( 'widget_html_code_content', $content, $instance, $this );
+
+               echo $args['before_widget'];
+               if ( ! empty( $title ) ) {
+                       echo $args['before_title'] . $title . $args['after_title'];
+               }
+               echo $content;
+               echo $args['after_widget'];
+       }
+
+       /**
+        * Handles updating settings for the current HTML Code widget instance.
+        *
+        * @since 4.8.1
+        *
+        * @param array $new_instance New settings for this instance as input by the user via
+        *                            WP_Widget::form().
+        * @param array $old_instance Old settings for this instance.
+        * @return array Settings to save or bool false to cancel saving.
+        */
+       public function update( $new_instance, $old_instance ) {
+               $instance = array_merge( $this->default_instance, $old_instance );
+               $instance['title'] = sanitize_text_field( $new_instance['title'] );
+               if ( current_user_can( 'unfiltered_html' ) ) {
+                       $instance['content'] = $new_instance['content'];
+               } else {
+                       $instance['content'] = wp_kses_post( $new_instance['content'] );
+               }
+               return $instance;
+       }
+
+       /**
+        * Outputs the HTML Code widget settings form.
+        *
+        * @since 4.8.1
+        *
+        * @param array $instance Current instance.
+        * @returns void
+        */
+       public function form( $instance ) {
+               $instance = wp_parse_args( (array) $instance, $this->default_instance );
+               ?>
+               <p>
+                       <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+                       <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'] ); ?>"/>
+               </p>
+
+               <p>
+                       <label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:' ); ?></label>
+                       <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>
+               </p>
+
+               <?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
+                       <?php
+                       $probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
+                       $allowed_html = wp_kses_allowed_html( 'post' );
+                       $disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
+                       ?>
+                       <?php if ( ! empty( $disallowed_html ) ) : ?>
+                               <p>
+                                       <?php _e( 'Some HTML tags are not permitted, including:' ); ?>
+                                       <code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
+                               </p>
+                       <?php endif; ?>
+               <?php endif; ?>
+               <?php
+       }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludeswidgetsclasswpwidgettextphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/widgets/class-wp-widget-text.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/widgets/class-wp-widget-text.php    2017-06-09 19:28:12 UTC (rev 40892)
+++ trunk/src/wp-includes/widgets/class-wp-widget-text.php      2017-06-09 23:53:27 UTC (rev 40893)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,7 +25,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px">                $widget_ops = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'classname' => 'widget_text',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'description' => __( 'Arbitrary text or HTML.' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'description' => __( 'Arbitrary text.' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         'customize_selective_refresh' => true,
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                $control_ops = array(
</span></span></pre></div>
<a id="trunksrcwpincludeswidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/widgets.php 2017-06-09 19:28:12 UTC (rev 40892)
+++ trunk/src/wp-includes/widgets.php   2017-06-09 23:53:27 UTC (rev 40893)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1474,6 +1474,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        register_widget( 'WP_Nav_Menu_Widget' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        register_widget( 'WP_Widget_HTML_Code' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Fires after all default WordPress widgets have been registered.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunktestsphpunittestswidgetshtmlcodewidgetphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/widgets/html-code-widget.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/widgets/html-code-widget.php                            (rev 0)
+++ trunk/tests/phpunit/tests/widgets/html-code-widget.php      2017-06-09 23:53:27 UTC (rev 40893)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,153 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_Widget_HTML_Code functionality.
+ *
+ * @package    WordPress
+ * @subpackage widgets
+ */
+
+/**
+ * Test wp-includes/widgets/class-wp-widget-html-code.php
+ *
+ * @group widgets
+ */
+class Test_WP_Widget_HTML_Code extends WP_UnitTestCase {
+
+       /**
+        * Args passed to the widget_html_code_content filter.
+        *
+        * @var array
+        */
+       protected $widget_html_code_content_args;
+
+       /**
+        * Test widget method.
+        *
+        * @covers WP_Widget_HTML_Code::widget
+        */
+       function test_widget() {
+               $widget = new WP_Widget_HTML_Code();
+               $content = "<i>Custom HTML</i>\n\n<b>CODE</b>\nLast line.<u>unclosed";
+
+               $args = array(
+                       'before_title'  => '<h2>',
+                       'after_title'   => "</h2>\n",
+                       'before_widget' => '<section>',
+                       'after_widget'  => "</section>\n",
+               );
+               $instance = array(
+                       'title'   => 'Foo',
+                       'content' => $content,
+               );
+
+               $this->assertEquals( 10, has_filter( 'widget_html_code_content', 'balanceTags' ) );
+
+               update_option( 'use_balanceTags', 0 );
+               add_filter( 'widget_html_code_content', array( $this, 'filter_widget_html_code_content' ), 5, 3 );
+               ob_start();
+               $this->widget_html_code_content_args = null;
+               $widget->widget( $args, $instance );
+               $output = ob_get_clean();
+               $this->assertNotEmpty( $this->widget_html_code_content_args );
+               $this->assertContains( '[filter:widget_html_code_content]', $output );
+               $this->assertNotContains( '<p>', $output );
+               $this->assertNotContains( '<br>', $output );
+               $this->assertNotContains( '</u>', $output );
+               $this->assertEquals( $instance, $this->widget_html_code_content_args[1] );
+               $this->assertSame( $widget, $this->widget_html_code_content_args[2] );
+               remove_filter( 'widget_html_code_content', array( $this, 'filter_widget_html_code_content' ), 5, 3 );
+
+               update_option( 'use_balanceTags', 1 );
+               ob_start();
+               $widget->widget( $args, $instance );
+               $output = ob_get_clean();
+               $this->assertContains( '</u>', $output );
+       }
+
+       /**
+        * Filters the content of the HTML Code widget.
+        *
+        * @param string              $widget_content The widget content.
+        * @param array               $instance       Array of settings for the current widget.
+        * @param WP_Widget_HTML_Code $widget         Current HTML Code widget instance.
+        * @return string Widget content.
+        */
+       function filter_widget_html_code_content( $widget_content, $instance, $widget ) {
+               $this->widget_html_code_content_args = func_get_args();
+
+               $widget_content .= '[filter:widget_html_code_content]';
+               return $widget_content;
+       }
+
+       /**
+        * Test update method.
+        *
+        * @covers WP_Widget_HTML_Code::update
+        */
+       function test_update() {
+               $widget = new WP_Widget_HTML_Code();
+               $instance = array(
+                       'title'   => "The\n<b>Title</b>",
+                       'content' => "The\n\n<b>Code</b>",
+               );
+
+               wp_set_current_user( $this->factory()->user->create( array(
+                       'role' => 'administrator',
+               ) ) );
+
+               // Should return valid instance.
+               $expected = array(
+                       'title'   => sanitize_text_field( $instance['title'] ),
+                       'content' => $instance['content'],
+               );
+               $result = $widget->update( $instance, array() );
+               $this->assertEquals( $result, $expected );
+
+               // Make sure KSES is applying as expected.
+               add_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ), 10, 2 );
+               $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+               $instance['content'] = '<script>alert( "Howdy!" );</script>';
+               $expected['content'] = $instance['content'];
+               $result = $widget->update( $instance, array() );
+               $this->assertEquals( $result, $expected );
+               remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) );
+
+               add_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 );
+               $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+               $instance['content'] = '<script>alert( "Howdy!" );</script>';
+               $expected['content'] = wp_kses_post( $instance['content'] );
+               $result = $widget->update( $instance, array() );
+               $this->assertEquals( $result, $expected );
+               remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 );
+       }
+
+       /**
+        * Grant unfiltered_html cap via map_meta_cap.
+        *
+        * @param array  $caps    Returns the user's actual capabilities.
+        * @param string $cap     Capability name.
+        * @return array Caps.
+        */
+       function grant_unfiltered_html_cap( $caps, $cap ) {
+               if ( 'unfiltered_html' === $cap ) {
+                       $caps = array_diff( $caps, array( 'do_not_allow' ) );
+                       $caps[] = 'unfiltered_html';
+               }
+               return $caps;
+       }
+
+       /**
+        * Revoke unfiltered_html cap via map_meta_cap.
+        *
+        * @param array  $caps    Returns the user's actual capabilities.
+        * @param string $cap     Capability name.
+        * @return array Caps.
+        */
+       function revoke_unfiltered_html_cap( $caps, $cap ) {
+               if ( 'unfiltered_html' === $cap ) {
+                       $caps = array_diff( $caps, array( 'unfiltered_html' ) );
+                       $caps[] = 'do_not_allow';
+               }
+               return $caps;
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>