<!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>[35580] trunk: Customize: Improve alignment of `WP_Customize_Nav_Menu_Item_Setting::sanitize()` behavior with `wp_update_nav_menu_item()`.</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/35580">35580</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/35580","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>2015-11-09 00:47:55 +0000 (Mon, 09 Nov 2015)</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'>Customize: Improve alignment of `WP_Customize_Nav_Menu_Item_Setting::sanitize()` behavior with `wp_update_nav_menu_item()`.

* Apply `title_save_pre`, `excerpt_save_pre`, and `content_save_pre` filters on a nav menu item's `title`, `attr_title`, and `description` properties respectively. This ensures that arbitrary markup can be supplied if the user has `unfiltered_html` cap, and for these fields to have markup stripped if not.
* Ensure a nav menu item's `post_status` is sanitized as `publish` or `draft` using the same conditions as `wp_update_nav_menu_item()`.
* Align `WP_Customize_Nav_Menu_Item_Setting::sanitize()` behavior for sanitizing `position` to be the same as `wp_update_nav_menu_item()`.
* Also apply `nav_menu_attr_title` and `nav_menu_description` filters in `WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()` to ensure that previewing markup entered into menu item description will preview the same way as when the nav menu item is saved.
* Add unit tests.

Fixes <a href="https://core.trac.wordpress.org/ticket/32812">#32812</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescustomizeclasswpcustomizenavmenuitemsettingphp">trunk/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php</a></li>
<li><a href="#trunktestsphpunittestscustomizenavmenuitemsettingphp">trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescustomizeclasswpcustomizenavmenuitemsettingphp"></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/customize/class-wp-customize-nav-menu-item-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php      2015-11-09 00:44:01 UTC (rev 35579)
+++ trunk/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php        2015-11-09 00:47:55 UTC (rev 35580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -572,6 +572,12 @@
</span><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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /** This filter is documented in wp-includes/nav-menu.php */
+               $post->attr_title = apply_filters( 'nav_menu_attr_title', $post->attr_title );
+
+               /** This filter is documented in wp-includes/nav-menu.php */
+               $post->description = apply_filters( 'nav_menu_description', wp_trim_words( $post->description, 200 ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $post;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -619,7 +625,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                $menu_item_value = array_merge( $default, $menu_item_value );
</span><span class="cx" style="display: block; padding: 0 10px">                $menu_item_value = wp_array_slice_assoc( $menu_item_value, array_keys( $default ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $menu_item_value['position'] = max( 0, intval( $menu_item_value['position'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $menu_item_value['position'] = intval( $menu_item_value['position'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( array( 'object_id', 'menu_item_parent', 'nav_menu_term_id' ) as $key ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Note we need to allow negative-integer IDs for previewed objects not inserted yet.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -638,14 +644,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $menu_item_value[ $key ] = implode( ' ', array_map( 'sanitize_html_class', $value ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( array( 'title', 'attr_title', 'description', 'original_title' ) as $key ) {
-                       // @todo Should esc_attr() the attr_title as well?
-                       $menu_item_value[ $key ] = sanitize_text_field( $menu_item_value[ $key ] );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $menu_item_value['original_title'] = sanitize_text_field( $menu_item_value['original_title'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Apply the same filters as when calling wp_insert_post().
+               $menu_item_value['title'] = apply_filters( 'title_save_pre', $menu_item_value['title'] );
+               $menu_item_value['attr_title'] = apply_filters( 'excerpt_save_pre', $menu_item_value['attr_title'] );
+               $menu_item_value['description'] = apply_filters( 'content_save_pre', $menu_item_value['description'] );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $menu_item_value['url'] = esc_url_raw( $menu_item_value['url'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! get_post_status_object( $menu_item_value['status'] ) ) {
-                       $menu_item_value['status'] = 'publish';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'publish' !== $menu_item_value['status'] ) {
+                       $menu_item_value['status'] = 'draft';
</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">                $menu_item_value['_invalid'] = (bool) $menu_item_value['_invalid'];
</span></span></pre></div>
<a id="trunktestsphpunittestscustomizenavmenuitemsettingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php     2015-11-09 00:44:01 UTC (rev 35579)
+++ trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php       2015-11-09 00:47:55 UTC (rev 35580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -438,6 +438,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_sanitize() {
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'customize_register', $this->wp_customize );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $menu_id = wp_create_nav_menu( 'Primary' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, 'nav_menu_item[123]' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( $setting->sanitize( 'not an array' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -449,37 +451,76 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'menu_item_parent' => 'asdasd',
</span><span class="cx" style="display: block; padding: 0 10px">                        'position' => -123,
</span><span class="cx" style="display: block; padding: 0 10px">                        'type' => 'custom<b>',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'title' => 'Hi<script>alert(1)</script>',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'title' => 'Hi<script>unfilteredHtml()</script>',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'url' => 'javascript:alert(1)',
</span><span class="cx" style="display: block; padding: 0 10px">                        'target' => '" onclick="',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'attr_title' => '<b>evil</b>',
-                       'description' => '<b>Hello world</b>',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'attr_title' => '<b>bolded</b><script>unfilteredHtml()</script>',
+                       'description' => '<b>Hello world</b><script>unfilteredHtml()</script>',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'classes' => 'hello " inject="',
</span><span class="cx" style="display: block; padding: 0 10px">                        'xfn' => 'hello " inject="',
</span><span class="cx" style="display: block; padding: 0 10px">                        'status' => 'forbidden',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'original_title' => 'Hi<script>alert(1)</script>',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'original_title' => 'Hi<script>unfilteredHtml()</script>',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'nav_menu_term_id' => 'heilo',
</span><span class="cx" style="display: block; padding: 0 10px">                        '_invalid' => false,
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $expected_sanitized = array(
+                       'object_id' => 0,
+                       'object' => 'bhellob',
+                       'menu_item_parent' => 0,
+                       'position' => -123,
+                       'type' => 'customb',
+                       'title' => current_user_can( 'unfiltered_html' ) ? 'Hi<script>unfilteredHtml()</script>' : 'HiunfilteredHtml()',
+                       'url' => '',
+                       'target' => 'onclick',
+                       'attr_title' => current_user_can( 'unfiltered_html' ) ? '<b>bolded</b><script>unfilteredHtml()</script>' : '<b>bolded</b>unfilteredHtml()',
+                       'description' => current_user_can( 'unfiltered_html' ) ? '<b>Hello world</b><script>unfilteredHtml()</script>' : '<b>Hello world</b>unfilteredHtml()',
+                       'classes' => 'hello  inject',
+                       'xfn' => 'hello  inject',
+                       'status' => 'draft',
+                       'original_title' => 'Hi',
+                       'nav_menu_term_id' => 0,
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $sanitized = $setting->sanitize( $unsanitized );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEqualSets( array_keys( $unsanitized ), array_keys( $sanitized ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 0, $sanitized['object_id'] );
-               $this->assertEquals( 'bhellob', $sanitized['object'] );
-               $this->assertEquals( 0, $sanitized['menu_item_parent'] );
-               $this->assertEquals( 0, $sanitized['position'] );
-               $this->assertEquals( 'customb', $sanitized['type'] );
-               $this->assertEquals( 'Hi', $sanitized['title'] );
-               $this->assertEquals( '', $sanitized['url'] );
-               $this->assertEquals( 'onclick', $sanitized['target'] );
-               $this->assertEquals( 'evil', $sanitized['attr_title'] );
-               $this->assertEquals( 'Hello world', $sanitized['description'] );
-               $this->assertEquals( 'hello  inject', $sanitized['classes'] );
-               $this->assertEquals( 'hello  inject', $sanitized['xfn'] );
-               $this->assertEquals( 'publish', $sanitized['status'] );
-               $this->assertEquals( 'Hi', $sanitized['original_title'] );
-               $this->assertEquals( 0, $sanitized['nav_menu_term_id'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $expected_sanitized as $key => $value ) {
+                       $this->assertEquals( $value, $sanitized[ $key ], "Expected $key to be sanitized." );
+               }
+
+               $nav_menu_item_id = wp_update_nav_menu_item( $menu_id, 0, array(
+                       'menu-item-object-id' => $unsanitized['object_id'],
+                       'menu-item-object' => $unsanitized['object'],
+                       'menu-item-parent-id' => $unsanitized['menu_item_parent'],
+                       'menu-item-position' => $unsanitized['position'],
+                       'menu-item-type' => $unsanitized['type'],
+                       'menu-item-title' => $unsanitized['title'],
+                       'menu-item-url' => $unsanitized['url'],
+                       'menu-item-description' => $unsanitized['description'],
+                       'menu-item-attr-title' => $unsanitized['attr_title'],
+                       'menu-item-target' => $unsanitized['target'],
+                       'menu-item-classes' => $unsanitized['classes'],
+                       'menu-item-xfn' => $unsanitized['xfn'],
+                       'menu-item-status' => $unsanitized['status'],
+               ) );
+
+               $post = get_post( $nav_menu_item_id );
+               $nav_menu_item = wp_setup_nav_menu_item( clone $post );
+
+               $this->assertEquals( $expected_sanitized['object_id'], $nav_menu_item->object_id );
+               $this->assertEquals( $expected_sanitized['object'], $nav_menu_item->object );
+               $this->assertEquals( $expected_sanitized['menu_item_parent'], $nav_menu_item->menu_item_parent );
+               $this->assertEquals( $expected_sanitized['position'], $post->menu_order );
+               $this->assertEquals( $expected_sanitized['type'], $nav_menu_item->type );
+               $this->assertEquals( $expected_sanitized['title'], $post->post_title );
+               $this->assertEquals( $expected_sanitized['url'], $nav_menu_item->url );
+               $this->assertEquals( $expected_sanitized['description'], $post->post_content );
+               $this->assertEquals( $expected_sanitized['attr_title'], $post->post_excerpt );
+               $this->assertEquals( $expected_sanitized['target'], $nav_menu_item->target );
+               $this->assertEquals( $expected_sanitized['classes'], implode( ' ', $nav_menu_item->classes ) );
+               $this->assertEquals( $expected_sanitized['xfn'], $nav_menu_item->xfn );
+               $this->assertEquals( $expected_sanitized['status'], $post->post_status );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -709,4 +750,53 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( $value_object->_invalid );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item().
+        *
+        * @see WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()
+        */
+       function test_value_as_wp_post_nav_menu_item() {
+               $post_id = self::factory()->post->create();
+
+               $setting = new WP_Customize_Nav_Menu_Item_Setting(
+                       $this->wp_customize,
+                       'nav_menu_item[123]'
+               );
+               $post_value = array(
+                       'object_id'        => $post_id,
+                       'object'           => 'post',
+                       'menu_item_parent' => 0,
+                       'position'         => 2,
+                       'type'             => 'post_type',
+                       'title'            => 'Hello World',
+                       'url'              => '',
+                       'target'           => '',
+                       'attr_title'       => '">attempted <b>baddie</b>',
+                       'description'      => 'Attempted <b>markup</b>',
+                       'classes'          => '',
+                       'xfn'              => '',
+                       'status'           => 'publish',
+                       'original_title'   => '',
+                       'nav_menu_term_id' => 0,
+                       '_invalid'         => false,
+               );
+               $this->wp_customize->set_post_value( $setting->id, $post_value );
+
+               $setting->preview();
+               $nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
+
+               $this->assertObjectNotHasAttribute( 'nav_menu_term_id', $nav_menu_item );
+               $this->assertObjectNotHasAttribute( 'status', $nav_menu_item );
+               $this->assertEquals( 'publish', $nav_menu_item->post_status );
+               $this->assertEquals( 'nav_menu_item', $nav_menu_item->post_type );
+               $this->assertObjectNotHasAttribute( 'position', $nav_menu_item );
+               $this->assertEquals( $post_value['position'], $nav_menu_item->menu_order );
+               $this->assertEquals( $post_value['title'], $nav_menu_item->post_title );
+               $this->assertEquals( 123, $nav_menu_item->ID );
+               $this->assertEquals( 123, $nav_menu_item->db_id );
+               $this->assertEquals( wp_get_current_user()->ID, $nav_menu_item->post_author );
+               $this->assertObjectHasAttribute( 'type_label', $nav_menu_item );
+               $this->assertEquals( '&#8220;>attempted baddie', $nav_menu_item->attr_title );
+               $this->assertEquals( 'Attempted markup', $nav_menu_item->description );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>