<!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>[41044] branches/4.8: Widgets: Add Custom HTML 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/41044">41044</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/41044","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-07-14 07:40:23 +0000 (Fri, 14 Jul 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 Custom HTML widget.
Ports <a href="https://core.trac.wordpress.org/changeset/40893">[40893]</a>, <a href="https://core.trac.wordpress.org/changeset/40926">[40926]</a>, <a href="https://core.trac.wordpress.org/changeset/41000">[41000]</a> to the 4.8 branch.
Props westonruter, timmydcrawford, obenland.
Fixes <a href="https://core.trac.wordpress.org/ticket/40907">#40907</a> for 4.8.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches48srcwpincludesclasswpcustomizewidgetsphp">branches/4.8/src/wp-includes/class-wp-customize-widgets.php</a></li>
<li><a href="#branches48srcwpincludesdefaultfiltersphp">branches/4.8/src/wp-includes/default-filters.php</a></li>
<li><a href="#branches48srcwpincludesdefaultwidgetsphp">branches/4.8/src/wp-includes/default-widgets.php</a></li>
<li><a href="#branches48srcwpincludeswidgetsclasswpwidgettextphp">branches/4.8/src/wp-includes/widgets/class-wp-widget-text.php</a></li>
<li><a href="#branches48srcwpincludeswidgetsphp">branches/4.8/src/wp-includes/widgets.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#branches48testsphpunittestswidgetscustomhtmlwidgetphp">branches/4.8/tests/phpunit/tests/widgets/custom-html-widget.php</a></li>
</ul>
<h3>Property Changed</h3>
<ul>
<li><a href="#branches48">branches/4.8/</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/4.8
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/4.8 2017-07-14 05:46:19 UTC (rev 41043)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/4.8 2017-07-14 07:40:23 UTC (rev 41044)
</ins><a id="branches48"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/4.8</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.1:18031
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:18512,40872-40873,40879,40881,40883,40885,40897,40901,41009,41022,41037
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:18512,40872-40873,40879,40881,40883,40885,40893,40897,40901,40926,41000,41009,41022,41037
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches48srcwpincludesclasswpcustomizewidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.8/src/wp-includes/class-wp-customize-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.8/src/wp-includes/class-wp-customize-widgets.php 2017-07-14 05:46:19 UTC (rev 41043)
+++ branches/4.8/src/wp-includes/class-wp-customize-widgets.php 2017-07-14 07:40:23 UTC (rev 41044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,9 +35,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected $core_widget_id_bases = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'archives', 'calendar', 'categories', 'links', 'meta',
- 'nav_menu', 'pages', 'recent-comments', 'recent-posts',
- 'rss', 'search', 'tag_cloud', 'text',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'archives',
+ 'calendar',
+ 'categories',
+ 'custom_html',
+ 'links',
+ 'media_audio',
+ 'media_image',
+ 'media_video',
+ 'meta',
+ 'nav_menu',
+ 'pages',
+ 'recent-comments',
+ 'recent-posts',
+ 'rss',
+ 'search',
+ 'tag_cloud',
+ 'text',
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="branches48srcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.8/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.8/src/wp-includes/default-filters.php 2017-07-14 05:46:19 UTC (rev 41043)
+++ branches/4.8/src/wp-includes/default-filters.php 2017-07-14 07:40:23 UTC (rev 41044)
</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_custom_html_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="branches48srcwpincludesdefaultwidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.8/src/wp-includes/default-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.8/src/wp-includes/default-widgets.php 2017-07-14 05:46:19 UTC (rev 41043)
+++ branches/4.8/src/wp-includes/default-widgets.php 2017-07-14 07:40:23 UTC (rev 41044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -60,3 +60,141 @@
</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">+
+/**
+ * Core class used to implement a Custom HTML widget.
+ *
+ * Note that this class is only located in this file in the 4.8 branch
+ * for the sake of automatic updates. In 4.9 and above, it is located at
+ * `wp-includes/widgets/class-wp-widget-custom-html.php`.
+ *
+ * @since 4.8.1
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Custom_HTML extends WP_Widget {
+
+ /**
+ * Default instance.
+ *
+ * @since 4.8.1
+ * @var array
+ */
+ protected $default_instance = array(
+ 'title' => '',
+ 'content' => '',
+ );
+
+ /**
+ * Sets up a new Custom HTML widget instance.
+ *
+ * @since 4.8.1
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_custom_html',
+ 'description' => __( 'Arbitrary HTML code.' ),
+ 'customize_selective_refresh' => true,
+ );
+ $control_ops = array(
+ 'width' => 400,
+ 'height' => 350,
+ );
+ parent::__construct( 'custom_html', __( 'Custom HTML' ), $widget_ops, $control_ops );
+ }
+
+ /**
+ * Outputs the content for the current Custom HTML 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 Custom HTML 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 Custom HTML widget.
+ *
+ * @since 4.8.1
+ *
+ * @param string $content The widget content.
+ * @param array $instance Array of settings for the current widget.
+ * @param WP_Widget_Custom_HTML $this Current Custom HTML widget instance.
+ */
+ $content = apply_filters( 'widget_custom_html_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 Custom HTML 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 Custom HTML 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="branches48srcwpincludeswidgetsclasswpwidgettextphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.8/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">--- branches/4.8/src/wp-includes/widgets/class-wp-widget-text.php 2017-07-14 05:46:19 UTC (rev 41043)
+++ branches/4.8/src/wp-includes/widgets/class-wp-widget-text.php 2017-07-14 07:40:23 UTC (rev 41044)
</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="branches48srcwpincludeswidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.8/src/wp-includes/widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.8/src/wp-includes/widgets.php 2017-07-14 05:46:19 UTC (rev 41043)
+++ branches/4.8/src/wp-includes/widgets.php 2017-07-14 07:40:23 UTC (rev 41044)
</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_Custom_HTML' );
+
</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="branches48testsphpunittestswidgetscustomhtmlwidgetphpfromrev40926trunktestsphpunittestswidgetscustomhtmlwidgetphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: branches/4.8/tests/phpunit/tests/widgets/custom-html-widget.php (from rev 40926, trunk/tests/phpunit/tests/widgets/custom-html-widget.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.8/tests/phpunit/tests/widgets/custom-html-widget.php (rev 0)
+++ branches/4.8/tests/phpunit/tests/widgets/custom-html-widget.php 2017-07-14 07:40:23 UTC (rev 41044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,165 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_Widget_Custom_HTML functionality.
+ *
+ * @package WordPress
+ * @subpackage widgets
+ */
+
+/**
+ * Test wp-includes/widgets/class-wp-widget-custom-html.php
+ *
+ * @group widgets
+ */
+class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase {
+
+ /**
+ * Args passed to the widget_custom_html_content filter.
+ *
+ * @var array
+ */
+ protected $widget_custom_html_content_args;
+
+ /**
+ * Test constructor.
+ *
+ * @covers WP_Widget_Custom_HTML::__constructor
+ */
+ function test_constructor() {
+ $widget = new WP_Widget_Custom_HTML();
+ $this->assertEquals( 'custom_html', $widget->id_base );
+ $this->assertEquals( 'widget_custom_html', $widget->widget_options['classname'] );
+ $this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
+ }
+
+ /**
+ * Test widget method.
+ *
+ * @covers WP_Widget_Custom_HTML::widget
+ */
+ function test_widget() {
+ $widget = new WP_Widget_Custom_HTML();
+ $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_custom_html_content', 'balanceTags' ) );
+
+ update_option( 'use_balanceTags', 0 );
+ add_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 );
+ ob_start();
+ $this->widget_custom_html_content_args = null;
+ $widget->widget( $args, $instance );
+ $output = ob_get_clean();
+ $this->assertNotEmpty( $this->widget_custom_html_content_args );
+ $this->assertContains( '[filter:widget_custom_html_content]', $output );
+ $this->assertNotContains( '<p>', $output );
+ $this->assertNotContains( '<br>', $output );
+ $this->assertNotContains( '</u>', $output );
+ $this->assertEquals( $instance, $this->widget_custom_html_content_args[1] );
+ $this->assertSame( $widget, $this->widget_custom_html_content_args[2] );
+ remove_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_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 Custom HTML widget.
+ *
+ * @param string $widget_content The widget content.
+ * @param array $instance Array of settings for the current widget.
+ * @param WP_Widget_Custom_HTML $widget Current Custom HTML widget instance.
+ * @return string Widget content.
+ */
+ function filter_widget_custom_html_content( $widget_content, $instance, $widget ) {
+ $this->widget_custom_html_content_args = func_get_args();
+
+ $widget_content .= '[filter:widget_custom_html_content]';
+ return $widget_content;
+ }
+
+ /**
+ * Test update method.
+ *
+ * @covers WP_Widget_Custom_HTML::update
+ */
+ function test_update() {
+ $widget = new WP_Widget_Custom_HTML();
+ $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>