<!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>[42211] trunk: Introduce `meta_box_sanitize_cb` taxonomy argument.</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/42211">42211</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/42211","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-11-21 03:14:52 +0000 (Tue, 21 Nov 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'>Introduce `meta_box_sanitize_cb` taxonomy argument.

The `meta_box_cb` argument was introduced in <a href="https://core.trac.wordpress.org/changeset/25572">[25572]</a> to allow plugin
authors to provide a custom callback for rendering their taxonomy's meta
box on the post edit screen. However, the routine used to handle the saving
of these custom taxonomy meta boxes was not customizable, but was instead
based simply on whether the taxonomy was hierarchicaly. See <a href="https://core.trac.wordpress.org/changeset/13535">[13535]</a>.

The new `meta_box_sanitize_cb` argument defaults to the "tag" routine for
non-hierarchical taxonomies and the "category" routine for hierarchical ones,
thereby maintaining the current default behavior. Developers can override this
when the data passed from their `meta_box_cb` differs.

Props boonebgorges, ZaneMatthew, stephenharris.
Fixes <a href="https://core.trac.wordpress.org/ticket/36514">#36514</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludespostphp">trunk/src/wp-admin/includes/post.php</a></li>
<li><a href="#trunksrcwpincludesclasswptaxonomyphp">trunk/src/wp-includes/class-wp-taxonomy.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunitteststaxonomyphp">trunk/tests/phpunit/tests/taxonomy.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/post.php      2017-11-20 23:45:09 UTC (rev 42210)
+++ trunk/src/wp-admin/includes/post.php        2017-11-21 03:14:52 UTC (rev 42211)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -327,46 +327,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Convert taxonomy input to term IDs, to avoid ambiguity.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( isset( $post_data['tax_input'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
-                       if ( is_taxonomy_hierarchical( $taxonomy ) ) {
-                               continue;
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $tax_object = get_taxonomy( $taxonomy );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        /*
-                        * Assume that a 'tax_input' string is a comma-separated list of term names.
-                        * Some languages may use a character other than a comma as a delimiter, so we standardize on
-                        * commas before parsing the list.
-                        */
-                       if ( ! is_array( $terms ) ) {
-                               $comma = _x( ',', 'tag delimiter' );
-                               if ( ',' !== $comma ) {
-                                       $terms = str_replace( $comma, ',', $terms );
-                               }
-                               $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
+                               $post_data['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       $clean_terms = array();
-                       foreach ( $terms as $term ) {
-                               // Empty terms are invalid input.
-                               if ( empty( $term ) ) {
-                                       continue;
-                               }
-
-                               $_term = get_terms( $taxonomy, array(
-                                       'name' => $term,
-                                       'fields' => 'ids',
-                                       'hide_empty' => false,
-                               ) );
-
-                               if ( ! empty( $_term ) ) {
-                                       $clean_terms[] = intval( $_term[0] );
-                               } else {
-                                       // No existing term was found, so pass the string. A new term will be created.
-                                       $clean_terms[] = $term;
-                               }
-                       }
-
-                       $post_data['tax_input'][ $taxonomy ] = $clean_terms;
</del><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">@@ -1870,3 +1835,61 @@
</span><span class="cx" style="display: block; padding: 0 10px">        wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">        exit;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Sanitizes POST values from a checkbox taxonomy metabox.
+ *
+ * @since 5.0.0
+ *
+ * @param mixed $terms Raw term data from the 'tax_input' field.
+ * @return array
+ */
+function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonmy, $terms ) {
+       return array_map( 'intval', $terms );
+}
+
+/**
+ * Sanitizes POST values from an input taxonomy metabox.
+ *
+ * @since 5.0.0
+ *
+ * @param mixed $terms Raw term data from the 'tax_input' field.
+ * @return array
+ */
+function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) {
+       /*
+        * Assume that a 'tax_input' string is a comma-separated list of term names.
+        * Some languages may use a character other than a comma as a delimiter, so we standardize on
+        * commas before parsing the list.
+        */
+       if ( ! is_array( $terms ) ) {
+               $comma = _x( ',', 'tag delimiter' );
+               if ( ',' !== $comma ) {
+                       $terms = str_replace( $comma, ',', $terms );
+               }
+               $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
+       }
+
+       $clean_terms = array();
+       foreach ( $terms as $term ) {
+               // Empty terms are invalid input.
+               if ( empty( $term ) ) {
+                       continue;
+               }
+
+               $_term = get_terms( $taxonomy, array(
+                       'name' => $term,
+                       'fields' => 'ids',
+                       'hide_empty' => false,
+               ) );
+
+               if ( ! empty( $_term ) ) {
+                       $clean_terms[] = intval( $_term[0] );
+               } else {
+                       // No existing term was found, so pass the string. A new term will be created.
+                       $clean_terms[] = $term;
+               }
+       }
+
+       return $clean_terms;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesclasswptaxonomyphp"></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/class-wp-taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-taxonomy.php       2017-11-20 23:45:09 UTC (rev 42210)
+++ trunk/src/wp-includes/class-wp-taxonomy.php 2017-11-21 03:14:52 UTC (rev 42211)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,6 +128,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $meta_box_cb = null;
</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">+         * The callback function for sanitizing taxonomy data saved from a meta box.
+        *
+        * @since 5.0.0
+        * @access public
+        * @var callable
+        */
+       public $meta_box_sanitize_cb = null;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * An array of object types this taxonomy is registered for.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -257,6 +266,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'show_in_quick_edit'    => null,
</span><span class="cx" style="display: block; padding: 0 10px">                        'show_admin_column'     => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'meta_box_cb'           => null,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'meta_box_sanitize_cb'  => null,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'capabilities'          => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'rewrite'               => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        'query_var'             => $this->name,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -345,6 +355,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $args['name'] = $this->name;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Default meta box sanitization callback depends on the value of 'meta_box_cb'.
+               if ( null === $args['meta_box_sanitize_cb'] ) {
+                       switch ( $args['meta_box_cb'] ) {
+                               case 'post_categories_meta_box' :
+                                       $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
+                               break;
+
+                               case 'post_tags_meta_box' :
+                               default :
+                                       $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input';
+                               break;
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $args as $property_name => $property_value ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->$property_name = $property_value;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></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/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php        2017-11-20 23:45:09 UTC (rev 42210)
+++ trunk/src/wp-includes/taxonomy.php  2017-11-21 03:14:52 UTC (rev 42211)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -297,6 +297,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.5.0 Introduced `publicly_queryable` argument.
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class'
</span><span class="cx" style="display: block; padding: 0 10px">  *              arguments to register the Taxonomy in REST API.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 Introduced `meta_box_sanitize_cb` argument.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global array $wp_taxonomies Registered taxonomies.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -339,6 +340,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                                                post_categories_meta_box() is used for hierarchical taxonomies, and
</span><span class="cx" style="display: block; padding: 0 10px">  *                                                post_tags_meta_box() is used for non-hierarchical. If false, no meta
</span><span class="cx" style="display: block; padding: 0 10px">  *                                                box is shown.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type callable      $meta_box_sanitize_cb  Callback function for sanitizing taxonomy data saved from a meta
+ *                                                box. If no callback is defined, an appropriate one is determined
+ *                                                based on the value of `$meta_box_cb`.
</ins><span class="cx" style="display: block; padding: 0 10px">  *     @type array         $capabilities {
</span><span class="cx" style="display: block; padding: 0 10px">  *         Array of capabilities for this taxonomy.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span></span></pre></div>
<a id="trunktestsphpunitteststaxonomyphp"></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/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/taxonomy.php    2017-11-20 23:45:09 UTC (rev 42210)
+++ trunk/tests/phpunit/tests/taxonomy.php      2017-11-21 03:14:52 UTC (rev 42211)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -791,4 +791,42 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 'foo', $taxonomy->name );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 36514
+        */
+       public function test_edit_post_hierarchical_taxonomy() {
+
+               $taxonomy_name = 'foo';
+               $term_name     = 'bar';
+
+               register_taxonomy( $taxonomy_name, array( 'post' ), array(
+                       'hierarchical' => false,
+                       'meta_box_cb'  => 'post_categories_meta_box',
+               ) );
+               $post = self::factory()->post->create_and_get( array(
+                       'post_type' => 'post',
+               ) );
+
+               $term_id  = self::factory()->term->create_object( array(
+                       'name'     => $term_name,
+                       'taxonomy' => $taxonomy_name,
+               ) );
+
+               wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+               $updated_post_id = edit_post( array(
+                       'post_ID'   => $post->ID,
+                       'post_type' => 'post',
+                       'tax_input' => array(
+                               $taxonomy_name => array(
+                                       (string) $term_id // Cast term_id as string to match whats sent in WP Admin.
+                               ),
+                       ),
+               ) );
+
+               $terms_obj = get_the_terms( $updated_post_id, $taxonomy_name );
+               $problematic_term = current( wp_list_pluck( $terms_obj, 'name' ) );
+
+               $this->assertEquals( $problematic_term, $term_name );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>