Skip to content

Commit adfd136

Browse files
committed
Move Walker_Category and Walker_CategoryDropdown into their own files via svn cp. Remove them from category-template.php. Load them in category.php. svn cp category.php over to category-functions.php, which also loads now in category.php.
See #33413. git-svn-id: https://develop.svn.wordpress.org/trunk@34110 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1ccae57 commit adfd136

5 files changed

Lines changed: 628 additions & 599 deletions

File tree

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
<?php
2+
/**
3+
* WordPress Category API
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Retrieve list of category objects.
10+
*
11+
* If you change the type to 'link' in the arguments, then the link categories
12+
* will be returned instead. Also all categories will be updated to be backwards
13+
* compatible with pre-2.3 plugins and themes.
14+
*
15+
* @since 2.1.0
16+
* @see get_terms() Type of arguments that can be changed.
17+
* @link https://codex.wordpress.org/Function_Reference/get_categories
18+
*
19+
* @param string|array $args Optional. Change the defaults retrieving categories.
20+
* @return array List of categories.
21+
*/
22+
function get_categories( $args = '' ) {
23+
$defaults = array( 'taxonomy' => 'category' );
24+
$args = wp_parse_args( $args, $defaults );
25+
26+
$taxonomy = $args['taxonomy'];
27+
28+
/**
29+
* Filter the taxonomy used to retrieve terms when calling {@see get_categories()}.
30+
*
31+
* @since 2.7.0
32+
*
33+
* @param string $taxonomy Taxonomy to retrieve terms from.
34+
* @param array $args An array of arguments. See {@see get_terms()}.
35+
*/
36+
$taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
37+
38+
// Back compat
39+
if ( isset($args['type']) && 'link' == $args['type'] ) {
40+
_deprecated_argument( __FUNCTION__, '3.0', '' );
41+
$taxonomy = $args['taxonomy'] = 'link_category';
42+
}
43+
44+
$categories = (array) get_terms( $taxonomy, $args );
45+
46+
foreach ( array_keys( $categories ) as $k )
47+
_make_cat_compat( $categories[$k] );
48+
49+
return $categories;
50+
}
51+
52+
/**
53+
* Retrieves category data given a category ID or category object.
54+
*
55+
* If you pass the $category parameter an object, which is assumed to be the
56+
* category row object retrieved the database. It will cache the category data.
57+
*
58+
* If you pass $category an integer of the category ID, then that category will
59+
* be retrieved from the database, if it isn't already cached, and pass it back.
60+
*
61+
* If you look at get_term(), then both types will be passed through several
62+
* filters and finally sanitized based on the $filter parameter value.
63+
*
64+
* The category will converted to maintain backwards compatibility.
65+
*
66+
* @since 1.5.1
67+
*
68+
* @param int|object $category Category ID or Category row object
69+
* @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
70+
* @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
71+
* @return object|array|WP_Error|null Category data in type defined by $output parameter.
72+
* WP_Error if $category is empty, null if it does not exist.
73+
*/
74+
function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
75+
$category = get_term( $category, 'category', $output, $filter );
76+
77+
if ( is_wp_error( $category ) )
78+
return $category;
79+
80+
_make_cat_compat( $category );
81+
82+
return $category;
83+
}
84+
85+
/**
86+
* Retrieve category based on URL containing the category slug.
87+
*
88+
* Breaks the $category_path parameter up to get the category slug.
89+
*
90+
* Tries to find the child path and will return it. If it doesn't find a
91+
* match, then it will return the first category matching slug, if $full_match,
92+
* is set to false. If it does not, then it will return null.
93+
*
94+
* It is also possible that it will return a WP_Error object on failure. Check
95+
* for it when using this function.
96+
*
97+
* @since 2.1.0
98+
*
99+
* @param string $category_path URL containing category slugs.
100+
* @param bool $full_match Optional. Whether full path should be matched.
101+
* @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
102+
* @return object|array|WP_Error|void Type is based on $output value.
103+
*/
104+
function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
105+
$category_path = rawurlencode( urldecode( $category_path ) );
106+
$category_path = str_replace( '%2F', '/', $category_path );
107+
$category_path = str_replace( '%20', ' ', $category_path );
108+
$category_paths = '/' . trim( $category_path, '/' );
109+
$leaf_path = sanitize_title( basename( $category_paths ) );
110+
$category_paths = explode( '/', $category_paths );
111+
$full_path = '';
112+
foreach ( (array) $category_paths as $pathdir ) {
113+
$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
114+
}
115+
$categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
116+
117+
if ( empty( $categories ) ) {
118+
return;
119+
}
120+
121+
foreach ( $categories as $category ) {
122+
$path = '/' . $leaf_path;
123+
$curcategory = $category;
124+
while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
125+
$curcategory = get_term( $curcategory->parent, 'category' );
126+
if ( is_wp_error( $curcategory ) ) {
127+
return $curcategory;
128+
}
129+
$path = '/' . $curcategory->slug . $path;
130+
}
131+
132+
if ( $path == $full_path ) {
133+
$category = get_term( $category->term_id, 'category', $output );
134+
_make_cat_compat( $category );
135+
return $category;
136+
}
137+
}
138+
139+
// If full matching is not required, return the first cat that matches the leaf.
140+
if ( ! $full_match ) {
141+
$category = get_term( reset( $categories )->term_id, 'category', $output );
142+
_make_cat_compat( $category );
143+
return $category;
144+
}
145+
}
146+
147+
/**
148+
* Retrieve category object by category slug.
149+
*
150+
* @since 2.3.0
151+
*
152+
* @param string $slug The category slug.
153+
* @return object Category data object
154+
*/
155+
function get_category_by_slug( $slug ) {
156+
$category = get_term_by( 'slug', $slug, 'category' );
157+
if ( $category )
158+
_make_cat_compat( $category );
159+
160+
return $category;
161+
}
162+
163+
/**
164+
* Retrieve the ID of a category from its name.
165+
*
166+
* @since 1.0.0
167+
*
168+
* @param string $cat_name Category name.
169+
* @return int 0, if failure and ID of category on success.
170+
*/
171+
function get_cat_ID( $cat_name ) {
172+
$cat = get_term_by( 'name', $cat_name, 'category' );
173+
if ( $cat )
174+
return $cat->term_id;
175+
return 0;
176+
}
177+
178+
/**
179+
* Retrieve the name of a category from its ID.
180+
*
181+
* @since 1.0.0
182+
*
183+
* @param int $cat_id Category ID
184+
* @return string Category name, or an empty string if category doesn't exist.
185+
*/
186+
function get_cat_name( $cat_id ) {
187+
$cat_id = (int) $cat_id;
188+
$category = get_term( $cat_id, 'category' );
189+
if ( ! $category || is_wp_error( $category ) )
190+
return '';
191+
return $category->name;
192+
}
193+
194+
/**
195+
* Check if a category is an ancestor of another category.
196+
*
197+
* You can use either an id or the category object for both parameters. If you
198+
* use an integer the category will be retrieved.
199+
*
200+
* @since 2.1.0
201+
*
202+
* @param int|object $cat1 ID or object to check if this is the parent category.
203+
* @param int|object $cat2 The child category.
204+
* @return bool Whether $cat2 is child of $cat1
205+
*/
206+
function cat_is_ancestor_of( $cat1, $cat2 ) {
207+
return term_is_ancestor_of( $cat1, $cat2, 'category' );
208+
}
209+
210+
/**
211+
* Sanitizes category data based on context.
212+
*
213+
* @since 2.3.0
214+
*
215+
* @param object|array $category Category data
216+
* @param string $context Optional. Default is 'display'.
217+
* @return object|array Same type as $category with sanitized data for safe use.
218+
*/
219+
function sanitize_category( $category, $context = 'display' ) {
220+
return sanitize_term( $category, 'category', $context );
221+
}
222+
223+
/**
224+
* Sanitizes data in single category key field.
225+
*
226+
* @since 2.3.0
227+
*
228+
* @param string $field Category key to sanitize
229+
* @param mixed $value Category value to sanitize
230+
* @param int $cat_id Category ID
231+
* @param string $context What filter to use, 'raw', 'display', etc.
232+
* @return mixed Same type as $value after $value has been sanitized.
233+
*/
234+
function sanitize_category_field( $field, $value, $cat_id, $context ) {
235+
return sanitize_term_field( $field, $value, $cat_id, 'category', $context );
236+
}
237+
238+
/* Tags */
239+
240+
/**
241+
* Retrieves all post tags.
242+
*
243+
* @since 2.3.0
244+
* @see get_terms() For list of arguments to pass.
245+
*
246+
* @param string|array $args Tag arguments to use when retrieving tags.
247+
* @return array List of tags.
248+
*/
249+
function get_tags( $args = '' ) {
250+
$tags = get_terms( 'post_tag', $args );
251+
252+
if ( empty( $tags ) ) {
253+
$return = array();
254+
return $return;
255+
}
256+
257+
/**
258+
* Filter the array of term objects returned for the 'post_tag' taxonomy.
259+
*
260+
* @since 2.3.0
261+
*
262+
* @param array $tags Array of 'post_tag' term objects.
263+
* @param array $args An array of arguments. @see get_terms()
264+
*/
265+
$tags = apply_filters( 'get_tags', $tags, $args );
266+
return $tags;
267+
}
268+
269+
/**
270+
* Retrieve post tag by tag ID or tag object.
271+
*
272+
* If you pass the $tag parameter an object, which is assumed to be the tag row
273+
* object retrieved the database. It will cache the tag data.
274+
*
275+
* If you pass $tag an integer of the tag ID, then that tag will
276+
* be retrieved from the database, if it isn't already cached, and pass it back.
277+
*
278+
* If you look at get_term(), then both types will be passed through several
279+
* filters and finally sanitized based on the $filter parameter value.
280+
*
281+
* @since 2.3.0
282+
*
283+
* @param int|object $tag
284+
* @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
285+
* @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
286+
* @return object|array|WP_Error|null Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist.
287+
*/
288+
function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
289+
return get_term( $tag, 'post_tag', $output, $filter );
290+
}
291+
292+
/* Cache */
293+
294+
/**
295+
* Remove the category cache data based on ID.
296+
*
297+
* @since 2.1.0
298+
*
299+
* @param int $id Category ID
300+
*/
301+
function clean_category_cache( $id ) {
302+
clean_term_cache( $id, 'category' );
303+
}
304+
305+
/**
306+
* Update category structure to old pre 2.3 from new taxonomy structure.
307+
*
308+
* This function was added for the taxonomy support to update the new category
309+
* structure with the old category one. This will maintain compatibility with
310+
* plugins and themes which depend on the old key or property names.
311+
*
312+
* The parameter should only be passed a variable and not create the array or
313+
* object inline to the parameter. The reason for this is that parameter is
314+
* passed by reference and PHP will fail unless it has the variable.
315+
*
316+
* There is no return value, because everything is updated on the variable you
317+
* pass to it. This is one of the features with using pass by reference in PHP.
318+
*
319+
* @since 2.3.0
320+
* @access private
321+
*
322+
* @param array|object $category Category Row object or array
323+
*/
324+
function _make_cat_compat( &$category ) {
325+
if ( is_object( $category ) && ! is_wp_error( $category ) ) {
326+
$category->cat_ID = &$category->term_id;
327+
$category->category_count = &$category->count;
328+
$category->category_description = &$category->description;
329+
$category->cat_name = &$category->name;
330+
$category->category_nicename = &$category->slug;
331+
$category->category_parent = &$category->parent;
332+
} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
333+
$category['cat_ID'] = &$category['term_id'];
334+
$category['category_count'] = &$category['count'];
335+
$category['category_description'] = &$category['description'];
336+
$category['cat_name'] = &$category['name'];
337+
$category['category_nicename'] = &$category['slug'];
338+
$category['category_parent'] = &$category['parent'];
339+
}
340+
}

0 commit comments

Comments
 (0)