home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / category-template.php < prev    next >
Encoding:
PHP Script  |  2008-07-09  |  16.1 KB  |  582 lines

  1. <?php
  2.  
  3. function get_category_children($id, $before = '/', $after = '', $visited=array()) {
  4.     if ( 0 == $id )
  5.         return '';
  6.  
  7.     $chain = '';
  8.     // TODO: consult hierarchy
  9.     $cat_ids = get_all_category_ids();
  10.     foreach ( $cat_ids as $cat_id ) {
  11.         if ( $cat_id == $id )
  12.             continue;
  13.  
  14.         $category = get_category($cat_id);
  15.         if ( is_wp_error( $category ) )
  16.             return $category;
  17.         if ( $category->parent == $id && !in_array($category->term_id, $visited) ) {
  18.             $visited[] = $category->term_id;
  19.             $chain .= $before.$category->term_id.$after;
  20.             $chain .= get_category_children($category->term_id, $before, $after);
  21.         }
  22.     }
  23.     return $chain;
  24. }
  25.  
  26. function get_category_link($category_id) {
  27.     global $wp_rewrite;
  28.     $catlink = $wp_rewrite->get_category_permastruct();
  29.  
  30.     if ( empty($catlink) ) {
  31.         $file = get_option('home') . '/';
  32.         $catlink = $file . '?cat=' . $category_id;
  33.     } else {
  34.         $category = &get_category($category_id);
  35.         if ( is_wp_error( $category ) )
  36.             return $category;
  37.         $category_nicename = $category->slug;
  38.  
  39.         if ( $parent = $category->parent )
  40.             $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename;
  41.  
  42.         $catlink = str_replace('%category%', $category_nicename, $catlink);
  43.         $catlink = get_option('home') . user_trailingslashit($catlink, 'category');
  44.     }
  45.     return apply_filters('category_link', $catlink, $category_id);
  46. }
  47.  
  48. function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE, $visited = array()){
  49.     $chain = '';
  50.     $parent = &get_category($id);
  51.     if ( is_wp_error( $parent ) )
  52.         return $parent;
  53.  
  54.     if ( $nicename )
  55.         $name = $parent->slug;
  56.     else
  57.         $name = $parent->cat_name;
  58.  
  59.     if ( $parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited) ) {
  60.         $visited[] = $parent->parent;
  61.         $chain .= get_category_parents($parent->parent, $link, $separator, $nicename, $visited);
  62.     }
  63.  
  64.     if ( $link )
  65.         $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
  66.     else
  67.         $chain .= $name.$separator;
  68.     return $chain;
  69. }
  70.  
  71. function get_the_category($id = false) {
  72.     global $post;
  73.  
  74.     $id = (int) $id;
  75.     if ( !$id )
  76.         $id = (int) $post->ID;
  77.  
  78.     $categories = get_object_term_cache($id, 'category');
  79.     if ( false === $categories )
  80.         $categories = wp_get_object_terms($id, 'category');
  81.  
  82.     if ( !empty($categories) )
  83.         usort($categories, '_usort_terms_by_name');
  84.     else
  85.         $categories = array();
  86.  
  87.     foreach(array_keys($categories) as $key) {
  88.         _make_cat_compat($categories[$key]);
  89.     }
  90.  
  91.     return $categories;
  92. }
  93.  
  94. function _usort_terms_by_name($a, $b) {
  95.     return strcmp($a->name, $b->name);
  96. }
  97.  
  98. function _usort_terms_by_ID($a, $b) {
  99.     if ( $a->term_id > $b->term_id )
  100.         return 1;
  101.     elseif ( $a->term_id < $b->term_id )
  102.         return -1;
  103.     else
  104.         return 0;
  105. }
  106.  
  107. function get_the_category_by_ID($cat_ID) {
  108.     $cat_ID = (int) $cat_ID;
  109.     $category = &get_category($cat_ID);
  110.     if ( is_wp_error( $category ) )
  111.         return $category;
  112.     return $category->name;
  113. }
  114.  
  115. function get_the_category_list($separator = '', $parents='', $post_id = false) {
  116.     global $wp_rewrite;
  117.     $categories = get_the_category($post_id);
  118.     if (empty($categories))
  119.         return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
  120.  
  121.     $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
  122.  
  123.     $thelist = '';
  124.     if ( '' == $separator ) {
  125.         $thelist .= '<ul class="post-categories">';
  126.         foreach ( $categories as $category ) {
  127.             $thelist .= "\n\t<li>";
  128.             switch ( strtolower($parents) ) {
  129.                 case 'multiple':
  130.                     if ($category->parent)
  131.                         $thelist .= get_category_parents($category->parent, TRUE);
  132.                     $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>';
  133.                     break;
  134.                 case 'single':
  135.                     $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
  136.                     if ($category->parent)
  137.                         $thelist .= get_category_parents($category->parent, FALSE);
  138.                     $thelist .= $category->name.'</a></li>';
  139.                     break;
  140.                 case '':
  141.                 default:
  142.                     $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
  143.             }
  144.         }
  145.         $thelist .= '</ul>';
  146.     } else {
  147.         $i = 0;
  148.         foreach ( $categories as $category ) {
  149.             if ( 0 < $i )
  150.                 $thelist .= $separator . ' ';
  151.             switch ( strtolower($parents) ) {
  152.                 case 'multiple':
  153.                     if ( $category->parent )
  154.                         $thelist .= get_category_parents($category->parent, TRUE);
  155.                     $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
  156.                     break;
  157.                 case 'single':
  158.                     $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
  159.                     if ( $category->parent )
  160.                         $thelist .= get_category_parents($category->parent, FALSE);
  161.                     $thelist .= "$category->cat_name</a>";
  162.                     break;
  163.                 case '':
  164.                 default:
  165.                     $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>';
  166.             }
  167.             ++$i;
  168.         }
  169.     }
  170.     return apply_filters('the_category', $thelist, $separator, $parents);
  171. }
  172.  
  173. /*
  174.  * in_category() - Checks whether the current post is within a particular category
  175.  *
  176.  * This function checks to see if the post is within the supplied category.  The categoy
  177.  * can be specified by number or name and will be checked as a name first to allow for categories with numeric names.
  178.  * Note: Prior to v2.5 of WordPress category names where not supported.
  179.  *
  180.  * @since 1.2.0
  181.  *
  182.  * @param int|string $category
  183.  * @return bool true if the post is in the supplied category
  184. */
  185. function in_category( $category ) { // Check if the current post is in the given category
  186.     global $post;
  187.  
  188.     if ( empty($category) )
  189.         return false;
  190.  
  191.     // If category is not an int, check to see if it's a name
  192.     if ( ! is_int($category) ) {
  193.         $cat_ID = get_cat_ID($category);
  194.         if ( $cat_ID )
  195.             $category = $cat_ID;
  196.     }
  197.  
  198.     $categories = get_object_term_cache($post->ID, 'category');
  199.     if ( false === $categories )
  200.         $categories = wp_get_object_terms($post->ID, 'category');
  201.     if ( array_key_exists($category, $categories) )
  202.         return true;
  203.     else
  204.         return false;
  205. }
  206.  
  207. function the_category($separator = '', $parents='', $post_id = false) {
  208.     echo get_the_category_list($separator, $parents, $post_id);
  209. }
  210.  
  211. function category_description($category = 0) {
  212.     global $cat;
  213.     if ( !$category )
  214.         $category = $cat;
  215.  
  216.     return get_term_field('description', $category, 'category');
  217. }
  218.  
  219. function wp_dropdown_categories($args = '') {
  220.     $defaults = array(
  221.         'show_option_all' => '', 'show_option_none' => '',
  222.         'orderby' => 'ID', 'order' => 'ASC',
  223.         'show_last_update' => 0, 'show_count' => 0,
  224.         'hide_empty' => 1, 'child_of' => 0,
  225.         'exclude' => '', 'echo' => 1,
  226.         'selected' => 0, 'hierarchical' => 0,
  227.         'name' => 'cat', 'class' => 'postform',
  228.         'depth' => 0, 'tab_index' => 0
  229.     );
  230.  
  231.     $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
  232.  
  233.     $r = wp_parse_args( $args, $defaults );
  234.     $r['include_last_update_time'] = $r['show_last_update'];
  235.     extract( $r );
  236.  
  237.     $tab_index_attribute = '';
  238.     if ( (int) $tab_index > 0 )
  239.         $tab_index_attribute = " tabindex=\"$tab_index\"";
  240.  
  241.     $categories = get_categories($r);
  242.  
  243.     $output = '';
  244.     if ( ! empty($categories) ) {
  245.         $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
  246.  
  247.         if ( $show_option_all ) {
  248.             $show_option_all = apply_filters('list_cats', $show_option_all);
  249.             $output .= "\t<option value='0'>$show_option_all</option>\n";
  250.         }
  251.  
  252.         if ( $show_option_none) {
  253.             $show_option_none = apply_filters('list_cats', $show_option_none);
  254.             $output .= "\t<option value='-1'>$show_option_none</option>\n";
  255.         }
  256.  
  257.         if ( $hierarchical )
  258.             $depth = $r['depth'];  // Walk the full depth.
  259.         else
  260.             $depth = -1; // Flat.
  261.  
  262.         $output .= walk_category_dropdown_tree($categories, $depth, $r);
  263.         $output .= "</select>\n";
  264.     }
  265.  
  266.     $output = apply_filters('wp_dropdown_cats', $output);
  267.  
  268.     if ( $echo )
  269.         echo $output;
  270.  
  271.     return $output;
  272. }
  273.  
  274. function wp_list_categories($args = '') {
  275.     $defaults = array(
  276.         'show_option_all' => '', 'orderby' => 'name',
  277.         'order' => 'ASC', 'show_last_update' => 0,
  278.         'style' => 'list', 'show_count' => 0,
  279.         'hide_empty' => 1, 'use_desc_for_title' => 1,
  280.         'child_of' => 0, 'feed' => '', 'feed_type' => '',
  281.         'feed_image' => '', 'exclude' => '', 'current_category' => 0,
  282.         'hierarchical' => true, 'title_li' => __('Categories'),
  283.         'echo' => 1, 'depth' => 0
  284.     );
  285.  
  286.     $r = wp_parse_args( $args, $defaults );
  287.  
  288.     if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
  289.         $r['pad_counts'] = true;
  290.     }
  291.  
  292.     if ( isset( $r['show_date'] ) ) {
  293.         $r['include_last_update_time'] = $r['show_date'];
  294.     }
  295.  
  296.     extract( $r );
  297.  
  298.     $categories = get_categories($r);
  299.  
  300.     $output = '';
  301.     if ( $title_li && 'list' == $style )
  302.             $output = '<li class="categories">' . $r['title_li'] . '<ul>';
  303.  
  304.     if ( empty($categories) ) {
  305.         if ( 'list' == $style )
  306.             $output .= '<li>' . __("No categories") . '</li>';
  307.         else
  308.             $output .= __("No categories");
  309.     } else {
  310.         global $wp_query;
  311.  
  312.         if( !empty($show_option_all) )
  313.             if ('list' == $style )
  314.                 $output .= '<li><a href="' .  get_bloginfo('url')  . '">' . $show_option_all . '</a></li>';
  315.             else
  316.                 $output .= '<a href="' .  get_bloginfo('url')  . '">' . $show_option_all . '</a>';
  317.  
  318.         if ( empty( $r['current_category'] ) && is_category() )
  319.             $r['current_category'] = $wp_query->get_queried_object_id();
  320.  
  321.         if ( $hierarchical )
  322.             $depth = $r['depth'];
  323.         else
  324.             $depth = -1; // Flat.
  325.  
  326.         $output .= walk_category_tree($categories, $depth, $r);
  327.     }
  328.  
  329.     if ( $title_li && 'list' == $style )
  330.         $output .= '</ul></li>';
  331.  
  332.     $output = apply_filters('wp_list_categories', $output);
  333.  
  334.     if ( $echo )
  335.         echo $output;
  336.     else
  337.         return $output;
  338. }
  339.  
  340. function wp_tag_cloud( $args = '' ) {
  341.     $defaults = array(
  342.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  343.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  344.         'exclude' => '', 'include' => ''
  345.     );
  346.     $args = wp_parse_args( $args, $defaults );
  347.  
  348.     $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
  349.  
  350.     if ( empty($tags) )
  351.         return;
  352.  
  353.     $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
  354.  
  355.     if ( is_wp_error( $return ) )
  356.         return false;
  357.  
  358.     $return = apply_filters( 'wp_tag_cloud', $return, $args );
  359.  
  360.     if ( 'array' == $args['format'] )
  361.         return $return;
  362.  
  363.     echo $return;
  364. }
  365.  
  366. // $tags = prefetched tag array ( get_tags() )
  367. // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
  368. // $args['orderby'] = 'name', 'count'
  369. function wp_generate_tag_cloud( $tags, $args = '' ) {
  370.     global $wp_rewrite;
  371.     $defaults = array(
  372.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  373.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
  374.     );
  375.     $args = wp_parse_args( $args, $defaults );
  376.     extract($args);
  377.  
  378.     if ( !$tags )
  379.         return;
  380.     $counts = $tag_links = array();
  381.     foreach ( (array) $tags as $tag ) {
  382.         $counts[$tag->name] = $tag->count;
  383.         $tag_links[$tag->name] = get_tag_link( $tag->term_id );
  384.         if ( is_wp_error( $tag_links[$tag->name] ) )
  385.             return $tag_links[$tag->name];
  386.         $tag_ids[$tag->name] = $tag->term_id;
  387.     }
  388.  
  389.     $min_count = min($counts);
  390.     $spread = max($counts) - $min_count;
  391.     if ( $spread <= 0 )
  392.         $spread = 1;
  393.     $font_spread = $largest - $smallest;
  394.     if ( $font_spread <= 0 )
  395.         $font_spread = 1;
  396.     $font_step = $font_spread / $spread;
  397.  
  398.     // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
  399.     if ( 'name' == $orderby )
  400.         uksort($counts, 'strnatcasecmp');
  401.     else
  402.         asort($counts);
  403.  
  404.     if ( 'DESC' == $order )
  405.         $counts = array_reverse( $counts, true );
  406.     elseif ( 'RAND' == $order ) {
  407.         $keys = array_rand( $counts, count($counts) );
  408.         foreach ( $keys as $key )
  409.             $temp[$key] = $counts[$key];
  410.         $counts = $temp;
  411.         unset($temp);
  412.     }
  413.  
  414.     $a = array();
  415.  
  416.     $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
  417.  
  418.     foreach ( $counts as $tag => $count ) {
  419.         $tag_id = $tag_ids[$tag];
  420.         $tag_link = clean_url($tag_links[$tag]);
  421.         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext('%d topic','%d topics',$count), $count ) ) . "'$rel style='font-size: " .
  422.             ( $smallest + ( ( $count - $min_count ) * $font_step ) )
  423.             . "$unit;'>$tag</a>";
  424.     }
  425.  
  426.     switch ( $format ) :
  427.     case 'array' :
  428.         $return =& $a;
  429.         break;
  430.     case 'list' :
  431.         $return = "<ul class='wp-tag-cloud'>\n\t<li>";
  432.         $return .= join("</li>\n\t<li>", $a);
  433.         $return .= "</li>\n</ul>\n";
  434.         break;
  435.     default :
  436.         $return = join("\n", $a);
  437.         break;
  438.     endswitch;
  439.  
  440.     return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
  441. }
  442.  
  443. //
  444. // Helper functions
  445. //
  446.  
  447. function walk_category_tree() {
  448.     $walker = new Walker_Category;
  449.     $args = func_get_args();
  450.     return call_user_func_array(array(&$walker, 'walk'), $args);
  451. }
  452.  
  453. function walk_category_dropdown_tree() {
  454.     $walker = new Walker_CategoryDropdown;
  455.     $args = func_get_args();
  456.     return call_user_func_array(array(&$walker, 'walk'), $args);
  457. }
  458.  
  459. //
  460. // Tags
  461. //
  462.  
  463. function get_tag_link( $tag_id ) {
  464.     global $wp_rewrite;
  465.     $taglink = $wp_rewrite->get_tag_permastruct();
  466.  
  467.     $tag = &get_term($tag_id, 'post_tag');
  468.     if ( is_wp_error( $tag ) )
  469.         return $tag;
  470.     $slug = $tag->slug;
  471.  
  472.     if ( empty($taglink) ) {
  473.         $file = get_option('home') . '/';
  474.         $taglink = $file . '?tag=' . $slug;
  475.     } else {
  476.         $taglink = str_replace('%tag%', $slug, $taglink);
  477.         $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
  478.     }
  479.     return apply_filters('tag_link', $taglink, $tag_id);
  480. }
  481.  
  482. function get_the_tags( $id = 0 ) {
  483.     return apply_filters( 'get_the_tags', get_the_terms($id, 'post_tag') );
  484. }
  485.  
  486. function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
  487.     return apply_filters( 'the_tags', get_the_term_list(0, 'post_tag', $before, $sep, $after) );
  488. }
  489.  
  490. function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
  491.     return the_terms( 0, 'post_tag', $before, $sep, $after );
  492. }
  493.  
  494. function get_the_terms( $id = 0, $taxonomy ) {
  495.     global $post;
  496.  
  497.      $id = (int) $id;
  498.  
  499.     if ( ! $id && ! in_the_loop() )
  500.         return false; // in-the-loop function
  501.  
  502.     if ( !$id )
  503.         $id = (int) $post->ID;
  504.  
  505.     $terms = get_object_term_cache($id, $taxonomy);
  506.     if ( false === $terms )
  507.         $terms = wp_get_object_terms($id, $taxonomy);
  508.  
  509.     if ( empty( $terms ) )
  510.         return false;
  511.  
  512.     return $terms;
  513. }
  514.  
  515. function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
  516.     $terms = get_the_terms($id, $taxonomy);
  517.  
  518.     if ( is_wp_error($terms) )
  519.         return $terms;
  520.  
  521.     if ( empty( $terms ) )
  522.         return false;
  523.  
  524.     foreach ( $terms as $term ) {
  525.         $link = get_term_link($term, $taxonomy);
  526.         if ( is_wp_error( $link ) )
  527.             return $link;
  528.         $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
  529.     }
  530.  
  531.     $term_links = apply_filters( "term_links-$taxonomy", $term_links );
  532.  
  533.     return $before . join($sep, $term_links) . $after;
  534. }
  535.  
  536. function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
  537.     $return = get_the_term_list($id, $taxonomy, $before, $sep, $after);
  538.     if ( is_wp_error( $return ) )
  539.         return false;
  540.     else
  541.         echo $return;
  542. }
  543.  
  544. /**
  545.  * Check if the current post has the given tag
  546.  *
  547.  * @package WordPress
  548.  * @since 2.6
  549.  *
  550.  * @uses wp_get_object_terms() Gets the tags.
  551.  *
  552.  * @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for
  553.  * @return bool True if the current post has the given tag, or any tag, if no tag specified
  554.  */
  555. function has_tag($tag = '') {
  556.     global $post;
  557.     $taxonomy = 'post_tag';
  558.  
  559.     if ( !in_the_loop() ) return false; // in-the-loop function
  560.  
  561.     $post_id = (int) $post->ID;
  562.  
  563.     $terms = get_object_term_cache($post_id, $taxonomy);
  564.     if (empty($terms))
  565.          $terms = wp_get_object_terms($post_id, $taxonomy);
  566.     if (empty($terms)) return false;
  567.  
  568.     if (empty($tag)) return (!empty($terms));
  569.  
  570.     $tag = (array) $tag;
  571.  
  572.     foreach($terms as $term) {
  573.         if ( in_array( $term->term_id, $tag ) ) return true;
  574.         if ( in_array( $term->name, $tag ) ) return true;
  575.         if ( in_array( $term->slug, $tag ) ) return true;
  576.     }
  577.  
  578.     return false;
  579. }
  580.  
  581. ?>
  582.