Ticket #12526: has_term.diff

File has_term.diff, 6.1 KB (added by ptahdunbar, 3 years ago)

introduces has_term, deprecates in_category in favor of has_category, and has_category and has_tag are wrappers for has_term

Line 
1Index: wp-includes/category-template.php
2===================================================================
3--- wp-includes/category-template.php   (revision 14413)
4+++ wp-includes/category-template.php   (working copy)
5@@ -230,47 +230,7 @@
6        return apply_filters( 'the_category', $thelist, $separator, $parents );
7 }
8 
9-
10 /**
11- * Check if the current post in within any of the given categories.
12- *
13- * The given categories are checked against the post's categories' term_ids, names and slugs.
14- * Categories given as integers will only be checked against the post's categories' term_ids.
15- *
16- * Prior to v2.5 of WordPress, category names were not supported.
17- * Prior to v2.7, category slugs were not supported.
18- * Prior to v2.7, only one category could be compared: in_category( $single_category ).
19- * Prior to v2.7, this function could only be used in the WordPress Loop.
20- * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
21- *
22- * @since 1.2.0
23- *
24- * @uses is_object_in_term()
25- *
26- * @param int|string|array $category. Category ID, name or slug, or array of said.
27- * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
28- * @return bool True if the current post is in any of the given categories.
29- */
30-function in_category( $category, $_post = null ) {
31-       if ( empty( $category ) )
32-               return false;
33-
34-       if ( $_post ) {
35-               $_post = get_post( $_post );
36-       } else {
37-               $_post =& $GLOBALS['post'];
38-       }
39-
40-       if ( !$_post )
41-               return false;
42-
43-       $r = is_object_in_term( $_post->ID, 'category', $category );
44-       if ( is_wp_error( $r ) )
45-               return false;
46-       return $r;
47-}
48-
49-/**
50  * Display the category list for the post.
51  *
52  * @since 0.71
53@@ -969,35 +929,63 @@
54 }
55 
56 /**
57- * Check if the current post has any of given tags.
58+ * Check if the current post has any of given categories.
59  *
60- * The given tags are checked against the post's tags' term_ids, names and slugs.
61- * Tags given as integers will only be checked against the post's tags' term_ids.
62- * If no tags are given, determines if post has any tags.
63+ * @since 3.0.0
64  *
65- * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids)
66- * Prior to v2.7, this function could only be used in the WordPress Loop.
67- * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
68+ * @uses is_object_in_term()
69+ * @see has_term()
70  *
71+ * @param string|int|array $tag Optional. The category name/category_id/slug or array of them to check for.
72+ * @param int|post object Optional.  Post to check instead of the current post.
73+ * @return bool True if the current post has any of the the given categories (or any category, if no category specified).
74+ */
75+function has_category( $category = '', $_post = null ) {
76+       return has_term( $category, 'category', $_post );
77+}
78+
79+/**
80+ * Check if the current post has any of given tags.
81+ *
82  * @since 2.6.0
83  *
84  * @uses is_object_in_term()
85+ * @see has_term()
86  *
87- * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
88- * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
89+ * @param string|int|array $tag Optional. The tag name/tag_id/slug or array of them to check for.
90+ * @param int|post object Optional.  Post to check instead of the current post.
91  * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
92  */
93 function has_tag( $tag = '', $_post = null ) {
94-       if ( $_post ) {
95+       return has_term( $tag, 'post_tag', $_post );
96+}
97+
98+
99+/**
100+ * Check if the current post has any of given terms.
101+ *
102+ * The given terms are checked against the post's terms' term_ids, names and slugs.
103+ * Terms given as integers will only be checked against the post's terms' term_ids.
104+ * If no terms are given, determines if post has any terms.
105+ *
106+ * @since 3.0.0
107+ *
108+ * @uses is_object_in_term()
109+ *
110+ * @param string|int|array $tag Optional. The term name/term_id/slug or array of them to check for.
111+ * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
112+ * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
113+ */
114+function has_term( $term = '', $taxonomy = '', $_post = null ) {
115+       if ( $_post )
116                $_post = get_post( $_post );
117-       } else {
118+       else
119                $_post =& $GLOBALS['post'];
120-       }
121 
122        if ( !$_post )
123                return false;
124 
125-       $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
126+       $r = is_object_in_term( $_post->ID, $taxonomy, $term );
127        if ( is_wp_error( $r ) )
128                return false;
129        return $r;
130Index: wp-admin/includes/deprecated.php
131===================================================================
132--- wp-admin/includes/deprecated.php    (revision 14413)
133+++ wp-admin/includes/deprecated.php    (working copy)
134@@ -162,4 +162,31 @@
135        return unregister_setting( $option_group, $option_name, $sanitize_callback );
136 }
137 
138+/**
139+ * Check if the current post in within any of the given categories.
140+ *
141+ * The given categories are checked against the post's categories' term_ids, names and slugs.
142+ * Categories given as integers will only be checked against the post's categories' term_ids.
143+ *
144+ * Prior to v2.5 of WordPress, category names were not supported.
145+ * Prior to v2.7, category slugs were not supported.
146+ * Prior to v2.7, only one category could be compared: in_category( $single_category ).
147+ * Prior to v2.7, this function could only be used in the WordPress Loop.
148+ * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
149+ *
150+ * @since 1.2.0
151+ * @deprecated 3.0.0
152+ * @deprecated Use has_category()
153+ * @see has_category()
154+ *
155+ * @uses is_object_in_term()
156+ *
157+ * @param int|string|array $category. Category ID, name or slug, or array of said.
158+ * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
159+ * @return bool True if the current post is in any of the given categories.
160+ */
161+function in_category( $category, $_post = null ) {
162+       _deprecated_function( __FUNCTION__, '3.0', 'has_category()' );
163+       return has_category( $category, $_post );
164+}
165 ?>
166\ No newline at end of file