Make WordPress Core

Ticket #29839: 20141115_02-taxonomy.diff

File 20141115_02-taxonomy.diff, 3.2 KB (added by theMikeD, 11 years ago)

Fix for #29839

  • src/wp-includes/taxonomy.php

     
    16161616 *     @type int          $child_of          Term ID to retrieve child terms of. If multiple taxonomies
    16171617 *                                           are passed, $child_of is ignored. Default 0.
    16181618 *     @type int|string   $parent            Parent term ID to retrieve direct-child terms of. Default empty.
     1619 *     @type bool         $childless_terms   The opposite of parent: returns terms that have no children.
     1620 *                                           Null if taxonomy is not hierarchical. Default false
    16191621 *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
    16201622 *                                           object cache. Default is 'core'.
    16211623 * }
     
    16401642
    16411643        $defaults = array('orderby' => 'name', 'order' => 'ASC',
    16421644                'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
    1643                 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
     1645                'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 'childless_terms' => false,
    16441646                'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
    16451647                'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
    16461648        $args = wp_parse_args( $args, $defaults );
     
    16591661        }
    16601662
    16611663        if ( 'all' == $args['get'] ) {
     1664                $args['childless_terms'] = 0;
    16621665                $args['child_of'] = 0;
    16631666                $args['hide_empty'] = 0;
    16641667                $args['hierarchical'] = false;
     
    16911694                }
    16921695        }
    16931696
     1697    // Check for heirarchical taxonomy
     1698        $childless_terms = $args['childless_terms']; // boolean
     1699        if ( $childless_terms ) {
     1700                // Empty if taxonomy is not heirarchical; term ids if it is.
     1701                // Note that we are not checking for a specific child or parent, just the
     1702                //  existence of an heirarchical taxonomy
     1703                $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
     1704                if ( empty( $hierarchy )) {
     1705                        return $empty_array;
     1706                }
     1707        }
     1708
    16941709        // $args can be whatever, only use the args defined in defaults to compute the key
    16951710        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    16961711        $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
     
    17981813                }
    17991814        }
    18001815
     1816        if ( $childless_terms ) {
     1817                $terms_to_exclude = "";
     1818                $term_heirarchy = _get_term_hierarchy( reset( $taxonomies ) );
     1819                if ( ! empty( $term_heirarchy ) ) {
     1820                        // Any term listed in $term_heirarchy has by definition a child term, so exclude it
     1821                        foreach ( array_keys($term_heirarchy) as $excluded_index=>$excluded_id ) {
     1822                                if ( empty($terms_to_exclude) ) {
     1823                                        $terms_to_exclude = $excluded_id;
     1824                                } else {
     1825                                        $terms_to_exclude .= ', ' . $excluded_id;
     1826                                }
     1827                        }
     1828                }
     1829                if ( empty( $exclusions ) ) {
     1830                        $exclusions = $terms_to_exclude;
     1831                } else {
     1832                        $exclusions .= ', ' . $terms_to_exclude;
     1833                }
     1834        }
     1835
    18011836        if ( ! empty( $exclusions ) ) {
    18021837                $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
    18031838        }