<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[14108] trunk/wp-includes: Introduce the wp_filter_object_list() helper,
  with an $operator arg.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14108">14108</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-04-16 14:08:58 +0000 (Fri, 16 Apr 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce the wp_filter_object_list() helper, with an $operator arg. Fixes an intersection bug in get_post_types() and get_taxonomies(). Also switches $operator default from 'or' to 'and' for get_post_stati(). props scribu, fixes <a href="http://trac.wordpress.org/ticket/12966">#12966</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
<li><a href="#trunkwpincludesqueryphp">trunk/wp-includes/query.php</a></li>
<li><a href="#trunkwpincludestaxonomyphp">trunk/wp-includes/taxonomy.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (14107 => 14108)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-04-16 14:03:48 UTC (rev 14107)
+++ trunk/wp-includes/functions.php        2010-04-16 14:08:58 UTC (rev 14108)
</span><span class="lines">@@ -2901,7 +2901,7 @@
</span><span class="cx">  * @param array|string $list
</span><span class="cx">  * @return array Sanitized array of IDs
</span><span class="cx">  */
</span><del>-function wp_parse_id_list($list) {
</del><ins>+function wp_parse_id_list( $list ) {
</ins><span class="cx">         if ( !is_array($list) )
</span><span class="cx">                 $list = preg_split('/[\s,]+/', $list);
</span><span class="cx"> 
</span><span class="lines">@@ -2909,6 +2909,41 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Filters a list of objects, based on a set of key =&gt; value arguments
+ *
+ * @since 3.0.0
+ *
+ * @param array $list An array of objects to filter
+ * @param array $args An array of key =&gt; value arguments to match against each object
+ * @param string $operator The logical operation to perform. 'or' means only one element 
+ *        from the array needs to match; 'and' means all elements must match. The default is 'and'.
+ * @param bool|string $field A field from the object to place instead of the entire object
+ * @return array A list of objects or object fields
+ */
+function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
+        if ( !is_array($list) )
+                return array();
+
+        if ( empty($args) )
+                $args = array();
+
+        if ( empty($args) &amp;&amp; !$field )
+                return $list;        // nothing to do
+
+        $count = count($args);
+
+        $filtered = array();
+
+        foreach ( $list as $key =&gt; $obj ) {
+                $matched = count(array_intersect_assoc(get_object_vars($obj), $args));
+                if ( ('and' == $operator &amp;&amp; $matched == $count) || ('or' == $operator &amp;&amp; $matched &lt;= $count) )
+                        $filtered[$key] = $field ? $obj-&gt;$field : $obj;
+        }
+
+        return $filtered;
+}
+
+/**
</ins><span class="cx">  * Determines if default embed handlers should be loaded.
</span><span class="cx">  *
</span><span class="cx">  * Checks to make sure that the embeds library hasn't already been loaded. If
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (14107 => 14108)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-04-16 14:03:48 UTC (rev 14107)
+++ trunk/wp-includes/post.php        2010-04-16 14:08:58 UTC (rev 14108)
</span><span class="lines">@@ -631,42 +631,18 @@
</span><span class="cx">  * @see register_post_status
</span><span class="cx">  * @see get_post_status_object
</span><span class="cx">  *
</span><del>- * @param array|string $args An array of key =&gt; value arguments to match against the post statuses.
- *  Only post statuses having attributes that match all arguments are returned.
</del><ins>+ * @param array|string $args An array of key =&gt; value arguments to match against the post status objects.
</ins><span class="cx">  * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
</span><del>- * @param string $operator Whether the elements in $args should be logicallly 'or'ed or 'and'ed together. 'or' means only one element from the array needs to match. 'and' means all elements must match. The default is 'or'.
</del><ins>+ * @param string $operator The logical operation to perform. 'or' means only one element 
+ *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
</ins><span class="cx">  * @return array A list of post type names or objects
</span><span class="cx">  */
</span><del>-function get_post_stati( $args = array(), $output = 'names', $operator = 'or' ) {
</del><ins>+function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
</ins><span class="cx">         global $wp_post_statuses;
</span><span class="cx"> 
</span><del>-        $do_names = false;
-        if ( 'names' == $output )
-                $do_names = true;
</del><ins>+        $field = ('names' == $output) ? 'name' : false;
</ins><span class="cx"> 
</span><del>-        if ( 'and' == $operator )
-                $arg_count = count($args);
-        else
-                $arg_count = 0;
-
-        $post_statuses = array();
-        foreach ( (array) $wp_post_statuses as $post_status ) {
-                if ( empty($args) ) {
-                        if ( $do_names )
-                                $post_statuses[] = $post_status-&gt;name;
-                        else
-                                $post_statuses[] = $post_status;
-                } elseif ( $intersect = array_intersect_assoc((array) $post_status, $args) ) {
-                        if ( $arg_count &amp;&amp; ( $arg_count != count($intersect) ) )
-                                continue;
-                        if ( $do_names )
-                                $post_statuses[] = $post_status-&gt;name;
-                        else
-                                $post_statuses[] = $post_status;
-                }
-        }
-
-        return $post_statuses;
</del><ins>+        return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -701,7 +677,7 @@
</span><span class="cx">  * @param mixed $the_post Optional. Post object or post ID.
</span><span class="cx">  * @return bool|string post type or false on failure.
</span><span class="cx">  */
</span><del>-function get_post_type($the_post = false) {
</del><ins>+function get_post_type( $the_post = false ) {
</ins><span class="cx">         global $post;
</span><span class="cx"> 
</span><span class="cx">         if ( false === $the_post )
</span><span class="lines">@@ -745,36 +721,19 @@
</span><span class="cx">  * @since 2.9.0
</span><span class="cx">  * @uses $wp_post_types
</span><span class="cx">  * @see register_post_type
</span><del>- * @see get_post_types
</del><span class="cx">  *
</span><del>- * @param array|string $args An array of key =&gt; value arguments to match against the post types.
- *  Only post types having attributes that match all arguments are returned.
</del><ins>+ * @param array|string $args An array of key =&gt; value arguments to match against the post type objects.
</ins><span class="cx">  * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
</span><ins>+ * @param string $operator The logical operation to perform. 'or' means only one element 
+ *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
</ins><span class="cx">  * @return array A list of post type names or objects
</span><span class="cx">  */
</span><del>-function get_post_types( $args = array(), $output = 'names' ) {
</del><ins>+function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
</ins><span class="cx">         global $wp_post_types;
</span><span class="cx"> 
</span><del>-        $do_names = false;
-        if ( 'names' == $output )
-                $do_names = true;
</del><ins>+        $field = ('names' == $output) ? 'name' : false;
</ins><span class="cx"> 
</span><del>-        $post_types = array();
-        foreach ( (array) $wp_post_types as $post_type ) {
-                if ( empty($args) ) {
-                        if ( $do_names )
-                                $post_types[] = $post_type-&gt;name;
-                        else
-                                $post_types[] = $post_type;
-                } elseif ( array_intersect_assoc((array) $post_type, $args) ) {
-                        if ( $do_names )
-                                $post_types[] = $post_type-&gt;name;
-                        else
-                                $post_types[] = $post_type;
-                }
-        }
-
-        return $post_types;
</del><ins>+        return wp_filter_object_list($wp_post_types, $args, $operator, $field);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/query.php (14107 => 14108)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/query.php        2010-04-16 14:03:48 UTC (rev 14107)
+++ trunk/wp-includes/query.php        2010-04-16 14:08:58 UTC (rev 14108)
</span><span class="lines">@@ -2255,7 +2255,7 @@
</span><span class="cx"> 
</span><span class="cx">                         if ( is_admin() ) {
</span><span class="cx">                                 // Add protected states that should show in the admin all list.
</span><del>-                                $admin_all_states = get_post_stati( array('protected' =&gt; true, 'show_in_admin_all_list' =&gt; true), 'names', 'and' );
</del><ins>+                                $admin_all_states = get_post_stati( array('protected' =&gt; true, 'show_in_admin_all_list' =&gt; true) );
</ins><span class="cx">                                 foreach ( (array) $admin_all_states as $state )
</span><span class="cx">                                         $where .= &quot; OR $wpdb-&gt;posts.post_status = '$state'&quot;;
</span><span class="cx">                         }
</span></span></pre></div>
<a id="trunkwpincludestaxonomyphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/taxonomy.php (14107 => 14108)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/taxonomy.php        2010-04-16 14:03:48 UTC (rev 14107)
+++ trunk/wp-includes/taxonomy.php        2010-04-16 14:08:58 UTC (rev 14108)
</span><span class="lines">@@ -67,25 +67,21 @@
</span><span class="cx">  * @uses $wp_taxonomies
</span><span class="cx">  * @see register_taxonomy
</span><span class="cx">  *
</span><del>- * @param array $args An array of key =&gt; value arguments to match against the taxonomies.
- *  Only taxonomies having attributes that match all arguments are returned.
</del><ins>+ * @param array $args An array of key =&gt; value arguments to match against the taxonomy objects.
</ins><span class="cx">  * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
</span><ins>+ * @param string $operator The logical operation to perform. 'or' means only one element 
+ *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
</ins><span class="cx">  * @return array A list of taxonomy names or objects
</span><span class="cx">  */
</span><del>-function get_taxonomies( $args = array(), $output = 'names' ) {
</del><ins>+function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
</ins><span class="cx">         global $wp_taxonomies;
</span><span class="cx"> 
</span><del>-        $taxonomies = array();
-        foreach ( (array) $wp_taxonomies as $taxname =&gt; $taxobj )
-                if ( empty($args) || array_intersect_assoc((array) $taxobj, $args) )
-                        $taxonomies[$taxname] = $taxobj;
</del><ins>+        $field = ('names' == $output) ? 'name' : false;
</ins><span class="cx"> 
</span><del>-        if ( 'names' == $output )
-                return array_keys($taxonomies);
-
-        return $taxonomies;
</del><ins>+        return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> /**
</span><span class="cx">  * Return all of the taxonomy names that are of $object_type.
</span><span class="cx">  *
</span></span></pre>
</div>
</div>

</body>
</html>