\n";
- break;
- default :
- $return = join( "\n", $a );
- break;
- endswitch;
-
- if ( $filter )
+ if ( $args['filter'] ) {
+ /**
+ * Filter the generated output of a tag cloud.
+ *
+ * The filter is only evaluated if a true value is passed
+ * to the $filter argument in wp_generate_tag_cloud().
+ *
+ * @since 2.3.0
+ *
+ * @see wp_generate_tag_cloud()
+ *
+ * @param array|string $return String containing the generated HTML tag cloud output
+ * or an array of tag links if the 'format' argument
+ * equals 'array'.
+ * @param array $tags An array of terms used in the tag cloud.
+ * @param array $args An array of wp_generate_tag_cloud() arguments.
+ */
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
- else
+ }
+
+ else
return $return;
}
+/**
+ * Callback for comparing objects based on name
+ *
+ * @since 3.1.0
+ * @access private
+ * @return int
+ */
+function _wp_object_name_sort_cb( $a, $b ) {
+ return strnatcasecmp( $a->name, $b->name );
+}
+
+/**
+ * Callback for comparing objects based on count
+ *
+ * @since 3.1.0
+ * @access private
+ * @return bool
+ */
+function _wp_object_count_sort_cb( $a, $b ) {
+ return ( $a->count > $b->count );
+}
+
//
// Helper functions
//
@@ -699,16 +925,17 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
* @uses Walker_Category to create HTML list content.
* @since 2.1.0
* @see Walker_Category::walk() for parameters and return description.
+ * @return string
*/
function walk_category_tree() {
$args = func_get_args();
// the user's options are the third parameter
- if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
+ if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
$walker = new Walker_Category;
- else
+ } else {
$walker = $args[2]['walker'];
-
- return call_user_func_array(array( &$walker, 'walk' ), $args );
+ }
+ return call_user_func_array( array( $walker, 'walk' ), $args );
}
/**
@@ -717,16 +944,278 @@ function walk_category_tree() {
* @uses Walker_CategoryDropdown to create HTML dropdown content.
* @since 2.1.0
* @see Walker_CategoryDropdown::walk() for parameters and return description.
+ * @return string
*/
function walk_category_dropdown_tree() {
$args = func_get_args();
// the user's options are the third parameter
- if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
+ if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
$walker = new Walker_CategoryDropdown;
- else
+ } else {
$walker = $args[2]['walker'];
+ }
+ return call_user_func_array( array( $walker, 'walk' ), $args );
+}
+
+/**
+ * Create HTML list of categories.
+ *
+ * @package WordPress
+ * @since 2.1.0
+ * @uses Walker
+ */
+class Walker_Category extends Walker {
+ /**
+ * What the class handles.
+ *
+ * @see Walker::$tree_type
+ * @since 2.1.0
+ * @var string
+ */
+ public $tree_type = 'category';
+
+ /**
+ * Database fields to use.
+ *
+ * @see Walker::$db_fields
+ * @since 2.1.0
+ * @todo Decouple this
+ * @var array
+ */
+ public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
+
+ /**
+ * Starts the list before the elements are added.
+ *
+ * @see Walker::start_lvl()
+ *
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param int $depth Depth of category. Used for tab indentation.
+ * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
+ * @see wp_list_categories()
+ */
+ public function start_lvl( &$output, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent
\n";
+ }
+
+ /**
+ * Ends the list of after the elements are added.
+ *
+ * @see Walker::end_lvl()
+ *
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param int $depth Depth of category. Used for tab indentation.
+ * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
+ * @wsee wp_list_categories()
+ */
+ public function end_lvl( &$output, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent
\n";
+ }
+
+ /**
+ * Start the element output.
+ *
+ * @see Walker::start_el()
+ *
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $category Category data object.
+ * @param int $depth Depth of category in reference to parents. Default 0.
+ * @param array $args An array of arguments. @see wp_list_categories()
+ * @param int $id ID of the current category.
+ */
+ public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
+ /** This filter is documented in wp-includes/category-template.php */
+ $cat_name = apply_filters(
+ 'list_cats',
+ esc_attr( $category->name ),
+ $category
+ );
+
+ // Don't generate an element if the category name is empty.
+ if ( ! $cat_name ) {
+ return;
+ }
+
+ $link = 'description ) ) {
+ /**
+ * Filter the category description for display.
+ *
+ * @since 1.2.0
+ *
+ * @param string $description Category description.
+ * @param object $category Category object.
+ */
+ $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
+ }
+
+ $link .= '>';
+ $link .= $cat_name . '';
+
+ if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
+ $link .= ' ';
+
+ if ( empty( $args['feed_image'] ) ) {
+ $link .= '(';
+ }
+
+ $link .= '';
+ }
+ $link .= '';
+
+ if ( empty( $args['feed_image'] ) ) {
+ $link .= ')';
+ }
+ }
+
+ if ( ! empty( $args['show_count'] ) ) {
+ $link .= ' (' . number_format_i18n( $category->count ) . ')';
+ }
+ if ( 'list' == $args['style'] ) {
+ $output .= "\t
term_id,
+ );
+
+ if ( ! empty( $args['current_category'] ) ) {
+ $_current_category = get_term( $args['current_category'], $category->taxonomy );
+ if ( $category->term_id == $args['current_category'] ) {
+ $css_classes[] = 'current-cat';
+ } elseif ( $category->term_id == $_current_category->parent ) {
+ $css_classes[] = 'current-cat-parent';
+ }
+ }
+
+ /**
+ * Filter the list of CSS classes to include with each category in the list.
+ *
+ * @since 4.2.0
+ *
+ * @see wp_list_categories()
+ *
+ * @param array $css_classes An array of CSS classes to be applied to each list item.
+ * @param object $category Category data object.
+ * @param int $depth Depth of page, used for padding.
+ * @param array $args An array of wp_list_categories() arguments.
+ */
+ $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
+
+ $output .= ' class="' . $css_classes . '"';
+ $output .= ">$link\n";
+ } else {
+ $output .= "\t$link \n";
+ }
+ }
+
+ /**
+ * Ends the element output, if needed.
+ *
+ * @see Walker::end_el()
+ *
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $page Not used.
+ * @param int $depth Depth of category. Not used.
+ * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
+ */
+ public function end_el( &$output, $page, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $output .= "
\n";
+ }
- return call_user_func_array(array( &$walker, 'walk' ), $args );
+}
+
+/**
+ * Create HTML dropdown list of Categories.
+ *
+ * @package WordPress
+ * @since 2.1.0
+ * @uses Walker
+ */
+class Walker_CategoryDropdown extends Walker {
+ /**
+ * @see Walker::$tree_type
+ * @since 2.1.0
+ * @var string
+ */
+ public $tree_type = 'category';
+
+ /**
+ * @see Walker::$db_fields
+ * @since 2.1.0
+ * @todo Decouple this
+ * @var array
+ */
+ public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
+
+ /**
+ * Start the element output.
+ *
+ * @see Walker::start_el()
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $category Category data object.
+ * @param int $depth Depth of category. Used for padding.
+ * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
+ * See {@see wp_dropdown_categories()}.
+ */
+ public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
+ $pad = str_repeat(' ', $depth * 3);
+
+ /** This filter is documented in wp-includes/category-template.php */
+ $cat_name = apply_filters( 'list_cats', $category->name, $category );
+
+ if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
+ $value_field = $args['value_field'];
+ } else {
+ $value_field = 'term_id';
+ }
+
+ $output .= "\t\n";
+ }
}
//
@@ -737,40 +1226,42 @@ function walk_category_dropdown_tree() {
* Retrieve the link to the tag.
*
* @since 2.3.0
- * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters.
+ * @see get_term_link()
*
- * @param int $tag_id Tag (term) ID.
- * @return string
+ * @param int|object $tag Tag ID or object.
+ * @return string Link on success, empty string if tag does not exist.
*/
-function get_tag_link( $tag_id ) {
- global $wp_rewrite;
- $taglink = $wp_rewrite->get_tag_permastruct();
+function get_tag_link( $tag ) {
+ if ( ! is_object( $tag ) )
+ $tag = (int) $tag;
+
+ $tag = get_term_link( $tag, 'post_tag' );
- $tag = &get_term( $tag_id, 'post_tag' );
if ( is_wp_error( $tag ) )
- return $tag;
- $slug = $tag->slug;
+ return '';
- if ( empty( $taglink ) ) {
- $file = get_option( 'home' ) . '/';
- $taglink = $file . '?tag=' . $slug;
- } else {
- $taglink = str_replace( '%tag%', $slug, $taglink );
- $taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' );
- }
- return apply_filters( 'tag_link', $taglink, $tag_id );
+ return $tag;
}
/**
* Retrieve the tags for a post.
*
* @since 2.3.0
- * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
*
* @param int $id Post ID.
- * @return array
+ * @return array|false|WP_Error Array of tag objects on success, false on failure.
*/
function get_the_tags( $id = 0 ) {
+
+ /**
+ * Filter the array of tags for the given post.
+ *
+ * @since 2.3.0
+ *
+ * @see get_the_terms()
+ *
+ * @param array $terms An array of tags for the given post.
+ */
return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
}
@@ -778,15 +1269,27 @@ function get_the_tags( $id = 0 ) {
* Retrieve the tags for a post formatted as a string.
*
* @since 2.3.0
- * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
*
* @param string $before Optional. Before tags.
* @param string $sep Optional. Between tags.
* @param string $after Optional. After tags.
- * @return string
+ * @param int $id Optional. Post ID. Defaults to the current post.
+ * @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
*/
-function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
- return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
+function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
+
+ /**
+ * Filter the tags list for a given post.
+ *
+ * @since 2.3.0
+ *
+ * @param string $tag_list List of tags.
+ * @param string $before String to use before tags.
+ * @param string $sep String to use between the tags.
+ * @param string $after String to use after tags.
+ * @param int $id Post ID.
+ */
+ return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
}
/**
@@ -797,7 +1300,6 @@ function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
- * @return string
*/
function the_tags( $before = null, $sep = ', ', $after = '' ) {
if ( null === $before )
@@ -808,7 +1310,7 @@ function the_tags( $before = null, $sep = ', ', $after = '' ) {
/**
* Retrieve tag description.
*
- * @since 2.8
+ * @since 2.8.0
*
* @param int $tag Optional. Tag ID. Will use global tag ID by default.
* @return string Tag description, available.
@@ -820,46 +1322,54 @@ function tag_description( $tag = 0 ) {
/**
* Retrieve term description.
*
- * @since 2.8
+ * @since 2.8.0
*
* @param int $term Optional. Term ID. Will use global term ID by default.
+ * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
* @return string Term description, available.
*/
function term_description( $term = 0, $taxonomy = 'post_tag' ) {
- if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
- global $wp_query;
- $term = $wp_query->get_queried_object();
- $taxonomy = $term->taxonomy;
- $term = $term->term_id;
+ if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
+ $term = get_queried_object();
+ if ( $term ) {
+ $taxonomy = $term->taxonomy;
+ $term = $term->term_id;
+ }
}
- return get_term_field( 'description', $term, $taxonomy );
+ $description = get_term_field( 'description', $term, $taxonomy );
+ return is_wp_error( $description ) ? '' : $description;
}
/**
* Retrieve the terms of the taxonomy that are attached to the post.
*
- * This function can only be used within the loop.
- *
* @since 2.5.0
*
- * @param int $id Post ID. Is not optional.
+ * @param int|object $post Post ID or object.
* @param string $taxonomy Taxonomy name.
- * @return array|bool False on failure. Array of term objects on success.
+ * @return array|false|WP_Error Array of term objects on success, false if there are no terms
+ * or the post does not exist, WP_Error on failure.
*/
-function get_the_terms( $id = 0, $taxonomy ) {
- global $post;
-
- $id = (int) $id;
-
- if ( ! $id && ! in_the_loop() )
- return false; // in-the-loop function
+function get_the_terms( $post, $taxonomy ) {
+ if ( ! $post = get_post( $post ) )
+ return false;
- if ( !$id )
- $id = (int) $post->ID;
+ $terms = get_object_term_cache( $post->ID, $taxonomy );
+ if ( false === $terms ) {
+ $terms = wp_get_object_terms( $post->ID, $taxonomy );
+ wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
+ }
- $terms = get_object_term_cache( $id, $taxonomy );
- if ( false === $terms )
- $terms = wp_get_object_terms( $id, $taxonomy );
+ /**
+ * Filter the list of terms attached to the given post.
+ *
+ * @since 3.1.0
+ *
+ * @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
+ * @param int $post_id Post ID.
+ * @param string $taxonomy Name of the taxonomy.
+ */
+ $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
if ( empty( $terms ) )
return false;
@@ -868,18 +1378,18 @@ function get_the_terms( $id = 0, $taxonomy ) {
}
/**
- * Retrieve terms as a list with specified format.
+ * Retrieve a post's terms as a list with specified format.
*
* @since 2.5.0
*
- * @param int $id Term ID.
+ * @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
- * @return string
+ * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
*/
-function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
+function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
@@ -888,14 +1398,27 @@ function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after
if ( empty( $terms ) )
return false;
+ $links = array();
+
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
- if ( is_wp_error( $link ) )
+ if ( is_wp_error( $link ) ) {
return $link;
- $term_links[] = '' . $term->name . '';
+ }
+ $links[] = '' . $term->name . '';
}
- $term_links = apply_filters( "term_links-$taxonomy", $term_links );
+ /**
+ * Filter the term links for a given taxonomy.
+ *
+ * The dynamic portion of the filter name, `$taxonomy`, refers
+ * to the taxonomy slug.
+ *
+ * @since 2.5.0
+ *
+ * @param array $links An array of term links.
+ */
+ $term_links = apply_filters( "term_links-$taxonomy", $links );
return $before . join( $sep, $term_links ) . $after;
}
@@ -905,19 +1428,44 @@ function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after
*
* @since 2.5.0
*
- * @param int $id Term ID.
+ * @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
- * @return null|bool False on WordPress error. Returns null when displaying.
+ * @return false|void False on WordPress error.
*/
-function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
- $return = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
- if ( is_wp_error( $return ) )
+function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
+ $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
+
+ if ( is_wp_error( $term_list ) )
return false;
- else
- echo $return;
+
+ /**
+ * Filter the list of terms to display.
+ *
+ * @since 2.9.0
+ *
+ * @param array $term_list List of terms to display.
+ * @param string $taxonomy The taxonomy name.
+ * @param string $before String to use before the terms.
+ * @param string $sep String to use between the terms.
+ * @param string $after String to use after the terms.
+ */
+ echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
+}
+
+/**
+ * Check if the current post has any of given category.
+ *
+ * @since 3.1.0
+ *
+ * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
+ * @param int|object $post Optional. Post to check instead of the current post.
+ * @return bool True if the current post has any of the given categories (or any category, if no category specified).
+ */
+function has_category( $category = '', $post = null ) {
+ return has_term( $category, 'category', $post );
}
/**
@@ -933,26 +1481,37 @@ function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
*
* @since 2.6.0
*
- * @uses is_object_in_term()
- *
* @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
- * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0
- * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
+ * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
+ * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
*/
-function has_tag( $tag = '', $_post = null ) {
- if ( $_post ) {
- $_post = get_post( $_post );
- } else {
- $_post =& $GLOBALS['post'];
- }
+function has_tag( $tag = '', $post = null ) {
+ return has_term( $tag, 'post_tag', $post );
+}
- if ( !$_post )
+/**
+ * Check if the current post has any of given terms.
+ *
+ * The given terms are checked against the post's terms' term_ids, names and slugs.
+ * Terms given as integers will only be checked against the post's terms' term_ids.
+ * If no terms are given, determines if post has any terms.
+ *
+ * @since 3.1.0
+ *
+ * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
+ * @param string $taxonomy Taxonomy name
+ * @param int|object $post Optional. Post to check instead of the current post.
+ * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
+ */
+function has_term( $term = '', $taxonomy = '', $post = null ) {
+ $post = get_post($post);
+
+ if ( !$post )
return false;
- $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
+ $r = is_object_in_term( $post->ID, $taxonomy, $term );
if ( is_wp_error( $r ) )
return false;
+
return $r;
}
-
-?>