OSDN Git Service

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