'parent', 'id' => 'term_id');
/**
* @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 Will only append content if style argument value is 'list'.
*/
function start_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent
\n";
}
/**
* @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 Will only append content if style argument value is 'list'.
*/
function end_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent
\n";
}
/**
* @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.
* @param array $args
*/
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = attribute_escape( $category->name);
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$link = 'description) )
$link .= 'title="' . sprintf(__( 'View all items filed under %s', 'bpcontents' ), $cat_name) . '"';
else
$link .= 'title="' . attribute_escape( apply_filters( 'oci_category_description', $category->description, $category, $taxonomy )) . '"';
$link .= '>';
$link .= $cat_name . '';
if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';
if ( empty($feed_image) )
$link .= '(';
$link .= '';
$link .= '';
if ( empty($feed_image) )
$link .= ')';
}
if ( isset($show_count) && $show_count )
$link .= ' (' . intval($category->count) . ')';
if ( isset($show_date) && $show_date ) {
$link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
}
if ( isset($current_category) && $current_category )
$_current_category = oci_get_category( $current_category, $taxonomy );
if ( 'list' == $args['style'] ) {
$output .= "\t
term_id;
if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
$class .= ' current-cat';
elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
$class .= ' current-cat-parent';
$output .= ' class="'.$class.'"';
$output .= ">$link\n";
} else {
$output .= "\t$link \n";
}
}
/**
* @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 Only uses 'list' for whether should append to output.
*/
function end_el(&$output, $page, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$output .= "
\n";
}
}
class OCI_Walker_Category_Checklist extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
function start_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent
\n";
}
}
/**
* Retrieves all category IDs.
*
* @since 2.0.0
* @link http://codex.wordpress.org/Function_Reference/get_all_category_ids
*
* @return object List of all of the category IDs.
*/
function oci_get_all_category_ids($taxonomy) {
if ( ! $cat_ids = wp_cache_get( 'all_category_ids', $taxonomy) ) {
$cat_ids = oci_get_terms( $taxonomy, 'fields=ids&get=all' );
wp_cache_add( 'all_category_ids', $cat_ids, $taxonomy );
}
return $cat_ids;
}
/**
* Retrieves category data given a category ID or category object.
*
* If you pass the $category parameter an object, which is assumed to be the
* category row object retrieved the database. It will cache the category data.
*
* If you pass $category an integer of the category ID, then that category will
* be retrieved from the database, if it isn't already cached, and pass it back.
*
* If you look at get_term(), then both types will be passed through several
* filters and finally sanitized based on the $filter parameter value.
*
* The category will converted to maintain backwards compatibility.
*
* @since 1.5.1
* @uses get_term() Used to get the category data from the taxonomy.
*
* @param int|object $category Category ID or Category row object
* @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
* @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
* @return mixed Category data in type defined by $output parameter.
*/
function &oci_get_category( $category, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
$category = oci_get_term( $category, $taxonomy, $output, $filter );
if ( is_wp_error( $category ) )
return $category;
return $category;
}
/**
* Retrieve list of category objects.
*
* If you change the type to 'link' in the arguments, then the link categories
* will be returned instead. Also all categories will be updated to be backwards
* compatible with pre-2.3 plugins and themes.
*
* @since 2.1.0
* @see get_terms() Type of arguments that can be changed.
* @link http://codex.wordpress.org/Function_Reference/get_categories
*
* @param string|array $args Optional. Change the defaults retrieving categories.
* @return array List of categories.
*/
function &oci_get_categories( $args = '' ) {
$args = wp_parse_args( $args, $defaults );
$taxonomy = $args['taxonomy'];
if (empty($taxonomy))
return;
$categories = (array) oci_get_terms( $taxonomy, $args );
return $categories;
}
/**
* Retrieve post categories.
*
* @since 0.71
* @uses $post
*
* @param int $id Optional, default to current post ID. The post ID.
* @return array
*/
function oci_get_the_category( $id, $taxonomy ) {
$id = (int) $id;
$categories = get_object_term_cache( $id, $taxonomy );
if ( false === $categories ) {
$categories = oci_get_object_terms( $id, $taxonomy );
wp_cache_add($id, $categories, 'oci_category_relationships');
}
if ( !empty( $categories ) )
usort( $categories, '_usort_terms_by_name' );
else
$categories = array();
return $categories;
}
/**
* Check if a category is an ancestor of another category.
*
* You can use either an id or the category object for both parameters. If you
* use an integer the category will be retrieved.
*
* @since 2.1.0
*
* @param int|object $cat1 ID or object to check if this is the parent category.
* @param int|object $cat2 The child category.
* @return bool Whether $cat2 is child of $cat1
*/
function oci_cat_is_ancestor_of( $cat1, $cat2, $taxonomy ) {
if ( is_int( $cat1 ) )
$cat1 = &oci_get_category( $cat1, $taxonomy );
if ( is_int( $cat2 ) )
$cat2 = &oci_get_category( $cat2, $taxonomy );
if ( !$cat1->term_id || !$cat2->parent )
return false;
if ( $cat2->parent == $cat1->term_id )
return true;
return oci_cat_is_ancestor_of( $cat1, oci_get_category( $cat2->parent, $taxonomy ), $taxonomy );
}
/**
* Sanitizes category data based on context.
*
* @since 2.3.0
* @uses sanitize_term() See this function for what context are supported.
*
* @param object|array $category Category data
* @param string $context Optional. Default is 'display'.
* @return object|array Same type as $category with sanitized data for safe use.
*/
function oci_sanitize_category( $category, $taxonomy, $context = 'display' ) {
return sanitize_term( $category, $taxonomy, $context );
}
/**
* Sanitizes data in single category key field.
*
* @since 2.3.0
* @uses sanitize_term_field() See function for more details.
*
* @param string $field Category key to sanitize
* @param mixed $value Category value to sanitize
* @param int $cat_id Category ID
* @param string $context What filter to use, 'raw', 'display', etc.
* @return mixed Same type as $value after $value has been sanitized.
*/
function oci_sanitize_category_field( $field, $value, $cat_id, $taxonomy, $context ) {
return sanitize_term_field( $field, $value, $cat_id, $taxonomy, $context );
}
/**
* Remove the category cache data based on ID.
*
* @since 2.1.0
* @uses clean_term_cache() Clears the cache for the category based on ID
*
* @param int $id Category ID
*/
function oci_clean_category_cache( $id, $taxonomy ) {
oci_clean_term_cache( $id, $taxonomy );
}
/**
* Retrieve category children list separated before and after the term IDs.
*
* @since 1.2.0
*
* @param int $id Category ID to retrieve children.
* @param string $before Optional. Prepend before category term ID.
* @param string $after Optional, default is empty string. Append after category term ID.
* @param array $visited Optional. Category Term IDs that have already been added.
* @return string
*/
function oci_get_category_children( $id, $taxonomy, $before = '/', $after = '', $visited = array() ) {
if ( 0 == $id )
return '';
$chain = '';
/** TODO: consult hierarchy */
$cat_ids = oci_get_all_category_ids($taxonomy);
foreach ( (array) $cat_ids as $cat_id ) {
if ( $cat_id == $id )
continue;
$category = oci_get_category( $cat_id, $taxonomy );
if ( is_wp_error( $category ) )
return $category;
if ( $category->parent == $id && !in_array( $category->term_id, $visited ) ) {
$visited[] = $category->term_id;
$chain .= $before.$category->term_id.$after;
$chain .= oci_get_category_children( $category->term_id, $taxonomy, $before, $after );
}
}
return $chain;
}
/**
* Retrieve category link URL.
*
* @since 1.0.0
* @uses apply_filters() Calls 'category_link' filter on category link and category ID.
*
* @param int $category_id Category ID.
* @return string
*/
function oci_get_category_link( $category_id, $taxonomy, $url = '', $type = 'all' ) {
global $wp_rewrite, $bp;
// $catlink = $wp_rewrite->get_category_permastruct();
if ( empty( $catlink ) ) {
// allow url override for term links
if (!$url)
$file = $bp->root_domain . '/' . OCI_CONTENTS_SLUG . '/';
else
$file = $url;
$category = &oci_get_category( $category_id, $taxonomy );
if ( is_wp_error( $category ) )
return $category;
$catlink = "$file?taxonomy=$taxonomy&term=$category->slug&type=$type";
} else {
$category = &oci_get_category( $category_id );
if ( is_wp_error( $category ) )
return $category;
$category_nicename = $category->slug;
if ( $category->parent == $category_id ) // recursive recursion
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = oci_get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
$catlink = str_replace( '%category%', $category_nicename, $catlink );
$catlink = get_option( 'home' ) . user_trailingslashit( $catlink, $taxonomy );
}
return apply_filters( 'oci_category_link', $catlink, $category_id, $taxonomy );
}
/**
* Retrieve category parents with separator.
*
* @since 1.2.0
*
* @param int $id Category ID.
* @param bool $link Optional, default is false. Whether to format with link.
* @param string $separator Optional, default is '/'. How to separate categories.
* @param bool $nicename Optional, default is false. Whether to use nice name for display.
* @param array $visited Optional. Already linked to categories to prevent duplicates.
* @return string
*/
function oci_get_category_parents( $id, $taxonomy, $url = '', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = &oci_get_category( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
else
$name = $parent->cat_name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= oci_get_category_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
}
if ( $link )
$chain .= 'cat_name ) . '">'.$name.'' . $separator;
else
$chain .= $name.$separator;
return $chain;
}
/**
* Retrieve category list in either HTML list or custom format.
*
* @since 1.5.1
*
* @param string $separator Optional, default is empty string. Separator for between the categories.
* @param string $parents Optional. How to display the parents.
* @param int $post_id Optional. Post ID to retrieve categories.
* @return string
*/
function oci_get_the_category_list( $id, $taxonomy, $url, $separator = '', $parents='' ) {
global $wp_rewrite;
$categories = oci_get_the_category( $id, $taxonomy );
if ( empty( $categories ) )
return apply_filters( 'oci_the_category', __( 'Uncategorized', 'bpcontents' ), $separator, $parents );
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
$thelist = '';
if ( '' == $separator ) {
$thelist .= '