Make WordPress Core

Ticket #6357: 6357-the_taxonomies.diff

File 6357-the_taxonomies.diff, 12.4 KB (added by andy, 16 years ago)

taxonomic links and a basic template tag

  • wp-includes/taxonomy.php

     
    3434 *
    3535 * @uses $wp_taxonomies
    3636 *
    37  * @param array|string $object_type Name of the type of taxonomy object
     37 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
    3838 * @return array The names of all taxonomy of $object_type.
    3939 */
    40 function get_object_taxonomies($object_type) {
     40function get_object_taxonomies($object) {
    4141        global $wp_taxonomies;
    4242
     43        if ( is_object($object) ) {
     44                if ( $object->post_type == 'attachment' )
     45                        return get_attachment_taxonomies($object);
     46                $object = $object->post_type;
     47        }
     48
     49        $object = (array) $object;
     50
    4351        $taxonomies = array();
    4452        foreach ( $wp_taxonomies as $taxonomy ) {
    45                 if ( in_array($object_type, (array) $taxonomy->object_type) )
     53                if ( array_intersect($object, (array) $taxonomy->object_type) )
    4654                        $taxonomies[] = $taxonomy->name;
    4755        }
    4856
     
    156164                if ( !isset($args['rewrite']['slug']) )
    157165                        $args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy);
    158166                $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', "taxonomy=$taxonomy&term=");
    159                 $wp_rewrite->add_permastruct("{$args['rewrite']['slug']}/%$taxonomy%");
     167                $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%");
    160168        }
    161169
    162170        $args['name'] = $taxonomy;
     
    10361044
    10371045        $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
    10381046        $args = wp_parse_args( $args, $defaults );
     1047
     1048        $terms = array();
     1049        if ( count($taxonomies) > 1 ) {
     1050                foreach ( $taxonomies as $index => $taxonomy ) {
     1051                        $t = get_taxonomy($taxonomy);
     1052                        if ( is_array($t->args) && $args != array_merge($args, $t->args) ) {
     1053                                unset($taxonomies[$index]);
     1054                                $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
     1055                        }
     1056                }
     1057        } else {
     1058                $t = get_taxonomy($taxonomies[0]);
     1059                if ( is_array($t->args) )
     1060                        $args = array_merge($args, $t->args);
     1061        }
     1062
    10391063        extract($args, EXTR_SKIP);
    10401064
    10411065        if ( 'count' == $orderby )
     
    10671091        $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order";
    10681092
    10691093        if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    1070                 $terms = $wpdb->get_results($query);
     1094                $terms = array_merge($terms, $wpdb->get_results($query));
    10711095                update_term_cache($terms);
    10721096        } else if ( 'ids' == $fields || 'names' == $fields ) {
    1073                 $terms = $wpdb->get_col($query);
     1097                $terms = array_merge($terms, $wpdb->get_col($query));
    10741098        } else if ( 'tt_ids' == $fields ) {
    10751099                $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order");
    10761100        }
     
    18991923        }
    19001924}
    19011925
     1926/**
     1927 * get_term_link() - Generates a permalink for a taxonomy term archive
     1928 *
     1929 * @param object|int|string $term
     1930 * @param string $taxonomy
     1931 * @return string HTML link to taxonomy term archive
     1932 */
     1933function get_term_link( $term, $taxonomy ) {
     1934        global $wp_rewrite;
     1935
     1936        $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
     1937
     1938        if ( !is_object($term) ) {
     1939                if ( is_int($term) ) {
     1940                        $term = &get_term($term, $taxonomy);
     1941                } else {
     1942                        $term = &get_term_by('slug', $term, $taxonomy);
     1943                        if ( is_wp_error($term) )
     1944                                return $term;
     1945                }
     1946        }
     1947        if ( is_wp_error( $term ) )
     1948                return $term;
     1949
     1950        $slug = $term->slug;
     1951
     1952        if ( empty($termlink) ) {
     1953                $file = get_option('home') . '/';
     1954                $termlink = "$file?taxonomy=$taxonomy&term=$slug";
     1955        } else {
     1956                $termlink = str_replace("%$taxonomy%", $slug, $termlink);
     1957                $termlink = get_option('home') . user_trailingslashit($termlink, 'category');
     1958        }
     1959        return apply_filters('term_link', $termlink, $term, $taxonomy);
     1960}
     1961
     1962function the_taxonomies($post = 0) {
     1963        echo join(' ', get_the_taxonomies($post));
     1964}
     1965
     1966function get_the_taxonomies($post = 0) {
     1967        if ( is_int($post) )
     1968                $post =& get_post($post);
     1969        elseif ( !is_object($post) )
     1970                $post =& $GLOBALS['post'];
     1971
     1972        $taxonomies = array();
     1973
     1974        if ( !$post )
     1975                return $taxonomies;
     1976
     1977        $_template = '%2$s: %1$l.';
     1978
     1979        foreach ( get_object_taxonomies($post) as $taxonomy ) {
     1980                $t = (array) get_taxonomy($taxonomy);
     1981                if ( empty($t['label']) )
     1982                        $t['label'] = $taxonomy;
     1983                if ( empty($t['args']) )
     1984                        $t['args'] = array();
     1985                if ( empty($t['template']) )
     1986                        $t['template'] = $_template;
     1987
     1988                $terms = get_object_term_cache($post->ID, $taxonomy);
     1989                if ( empty($terms) )
     1990                        $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
     1991
     1992                $links = array();
     1993
     1994                foreach ( $terms as $term )
     1995                        $links[] = "<a href='" . attribute_escape(get_term_link($term, $taxonomy)) . "'>$term->name</a>";
     1996
     1997                if ( $links )
     1998                        $taxonomies[$taxonomy] = wp_sprintf($t['template'], $links, $t['label']);
     1999        }
     2000        return $taxonomies;
     2001}
    19022002?>
  • wp-includes/media.php

     
    437437                echo wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
    438438}
    439439
     440function get_attachment_taxonomies($attachment) {
     441        if ( is_int( $attachment ) )
     442                $attachment = get_post($attachment);
     443        else if ( is_array($attachment) )
     444                $attachment = (object) $attachment;
     445
     446        if ( ! is_object($attachment) )
     447                return array();
     448
     449        $filename = basename($attachment->guid);
     450
     451        $objects = array('attachment');
     452
     453        if ( false !== strpos($filename, '.') )
     454                $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
     455        if ( !empty($attachment->post_mime_type) ) {
     456                $objects[] = 'attachment:' . $attachment->post_mime_type;
     457                if ( false !== strpos($attachment->post_mime_type, '/') )
     458                        foreach ( explode('/', $attachment->post_mime_type) as $token )
     459                                if ( !empty($token) )
     460                                        $objects[] = "attachment:$token";
     461        }
     462
     463        $taxonomies = array();
     464        foreach ( $objects as $object )
     465                if ( $taxes = get_object_taxonomies($object) )
     466                        $taxonomies = array_merge($taxonomies, $taxes);
     467
     468        return array_unique($taxonomies);
     469}
     470
    440471?>
  • wp-includes/query.php

     
    11471147                                $term_ids[] = $term->term_id;
    11481148                        $post_ids = get_objects_in_term($term_ids, $q['taxonomy']);
    11491149
    1150                         if ( count($post_ids) ) {
     1150                        if ( !is_wp_error($post_ids) && count($post_ids) ) {
    11511151                                $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
    11521152                                $post_type = 'any';
    11531153                                $q['post_status'] = 'publish';
     
    12921292                                        $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
    12931293                        }
    12941294                        if ( $post_status_join ) {
    1295                                 $join .= " INNER JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
     1295                                $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
    12961296                                foreach ( $statuswheres as $index => $statuswhere )
    12971297                                        $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
    12981298                        }
  • wp-includes/formatting.php

     
    13761376        $args = (array) $args;
    13771377        $result = array_shift($args);
    13781378        if ( count($args) == 1 )
    1379                 $result .= $l['between_two'] . array_shift($args);
     1379                $result .= $l['between_only_two'] . array_shift($args);
    13801380        // Loop when more than two args
    13811381        while ( count($args) ) {
    13821382                $arg = array_shift($args);
  • wp-includes/rewrite.php

     
    461461                return $this->tag_structure;
    462462        }
    463463
     464        function get_extra_permastruct($name) {
     465                if ( isset($this->extra_permastructs[$name]) )
     466                        return $this->extra_permastructs[$name];
     467                return false;
     468        }
     469
    464470        function get_author_permastruct() {
    465471                if (isset($this->author_structure)) {
    466472                        return $this->author_structure;
     
    832838                $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
    833839
    834840                // Extra permastructs
    835                 $extra_rewrite = array();
    836841                foreach ( $this->extra_permastructs as $permastruct )
    837                         $extra_rewrite = array_merge($extra_rewrite, $this->generate_rewrite_rules($permastruct, EP_NONE));
     842                        $this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct, EP_NONE));
    838843
    839844                // Put them together.
    840845                if ( $this->use_verbose_page_rules )
    841                         $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $extra_rewrite, $this->extra_rules);
     846                        $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules);
    842847                else
    843                         $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $extra_rewrite, $page_rewrite, $this->extra_rules);
     848                        $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
    844849
    845850                do_action_ref_array('generate_rewrite_rules', array(&$this));
    846851                $this->rules = apply_filters('rewrite_rules_array', $this->rules);
     
    954959                $wp->add_query_var($name);
    955960        }
    956961
    957         function add_permastruct($struct, $with_front = true) {
     962        function add_permastruct($name, $struct, $with_front = true) {
    958963                if ( $with_front )
    959964                        $struct = $this->front . $struct;
    960                 $this->extra_permastructs[] = $struct;
     965                $this->extra_permastructs[$name] = $struct;
    961966        }
    962967
    963968        function flush_rules() {
  • wp-content/themes/default/image.php

     
    2323                                        <small>
    2424                                                This entry was posted on <?php the_time('l, F jS, Y') ?> at <?php the_time() ?>
    2525                                                and is filed under <?php the_category(', ') ?>.
     26                                                <?php the_taxonomies(); ?>
    2627                                                You can follow any responses to this entry through the <?php post_comments_feed_link('RSS 2.0'); ?> feed.
    2728
    2829                                                <?php if (('open' == $post-> comment_status) && ('open' == $post->ping_status)) {
  • wp-admin/includes/media.php

     
    426426        return wp_iframe( 'media_upload_library_form', $errors );
    427427}
    428428
    429 function get_attachment_taxonomies($attachment) {
    430         if ( is_int( $attachment ) )
    431                 $attachment = get_post($attachment);
    432         else if ( is_array($attachment) )
    433                 $attachment = (object) $attachment;
    434 
    435         if ( ! is_object($attachment) )
    436                 return array();
    437 
    438         $filename = basename($attachment->guid);
    439 
    440         $objects = array('attachment');
    441 
    442         if ( false !== strpos($filename, '.') )
    443                 $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
    444         if ( !empty($attachment->post_mime_type) ) {
    445                 $objects[] = 'attachment:' . $attachment->post_mime_type;
    446                 if ( false !== strpos($attachment->post_mime_type, '/') )
    447                         foreach ( explode('/', $attachment->post_mime_type) as $token )
    448                                 if ( !empty($token) )
    449                                         $objects[] = "attachment:$token";
    450         }
    451 
    452         $taxonomies = array();
    453         foreach ( $objects as $object )
    454                 if ( $taxes = get_object_taxonomies($object) )
    455                         $taxonomies = array_merge($taxonomies, $taxes);
    456 
    457         return array_unique($taxonomies);
    458 }
    459 
    460429function image_attachment_fields_to_edit($form_fields, $post) {
    461430        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
    462431                $form_fields['post_title']['required'] = true;