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