[ Index ]

PHP Cross Reference of zeList

title

Body

[close]

/includes/ -> link-category.php (source)

   1  <?php
   2  
   3  
   4  /**
   5   * Return title for a link category
   6   * @param $category
   7   * @return unknown_type
   8   */
   9  function zelist_single_cat_title($category) {
  10    if(isset($category)) return $category;
  11    if(!is_link_category()) return $category;
  12    $cat = get_query_var('link_category');
  13    $category = get_link_category($cat);
  14    return $category->name;
  15  }
  16  add_filter('single_cat_title','zelist_single_cat_title',5);
  17  
  18  
  19  /**
  20   * Display or retrieve page title for link category archive.
  21   *
  22   * This is useful for category template file or files, because it is optimized
  23   * for category page title and with less overhead than {@link wp_title()}.
  24   *
  25   * It does not support placing the separator after the title, but by leaving the
  26   * prefix parameter empty, you can set the title separator manually. The prefix
  27   * does not automatically place a space between the prefix, so if there should
  28   * be a space, the parameter value will need to have it at the end.
  29   *
  30   * @since 0.71
  31   *
  32   * @param string $prefix Optional. What to display before the title.
  33   * @param bool $display Optional, default is true. Whether to display or retrieve title.
  34   * @return string|null Title when retrieving, null when displaying or failure.
  35   */
  36  function single_link_cat_title($prefix = '', $display = true ) {
  37    $cat = intval( get_zelist_query_var('link_category') );
  38    $link_category = get_term($cat,'link_category');
  39    if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {
  40      $my_cat_name = apply_filters('single_link_cat_title', $link_category->name);
  41      if ( !empty($my_cat_name) ) {
  42        if ( $display )
  43        echo $prefix.strip_tags($my_cat_name);
  44        else
  45        return strip_tags($my_cat_name);
  46      }
  47    } else if ( is_tag() ) {
  48      return single_tag_title($prefix, $display);
  49    }
  50  }
  51  
  52  
  53  
  54  /**
  55   * Retrieve link_category based on URL containing the link_category slug.
  56   *
  57   * Breaks the $link_category_path parameter up to get the link_category slug.
  58   *
  59   * Tries to find the child path and will return it. If it doesn't find a
  60   * match, then it will return the first link_category matching slug, if $full_match,
  61   * is set to false. If it does not, then it will return null.
  62   *
  63   * It is also possible that it will return a WP_Error object on failure. Check
  64   * for it when using this function.
  65   *
  66   * @since 2.1.0
  67   *
  68   * @param string $link_category_path URL containing link_category slugs.
  69   * @param bool $full_match Optional. Whether should match full path or not.
  70   * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
  71   * @return null|object|array Null on failure. Type is based on $output value.
  72   */
  73  function get_link_category_by_path( $link_category_path, $full_match = true, $output = OBJECT ) {
  74    $link_category_path = rawurlencode( urldecode( $link_category_path ) );
  75    $link_category_path = str_replace( '%2F', '/', $link_category_path );
  76    $link_category_path = str_replace( '%20', ' ', $link_category_path );
  77    $link_category_paths = '/' . trim( $link_category_path, '/' );
  78    $leaf_path  = sanitize_title( basename( $link_category_paths ) );
  79    $link_category_paths = explode( '/', $link_category_paths );
  80    $full_path = '';
  81    foreach ( (array) $link_category_paths as $pathdir )
  82    $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
  83  
  84    $categories = get_terms( 'link_category', "get=all&slug=$leaf_path" );
  85  
  86    if ( empty( $categories ) )
  87    return null;
  88  
  89    foreach ( $categories as $link_category ) {
  90      $path = '/' . $leaf_path;
  91      $curlink_category = $link_category;
  92      while ( ( $curlink_category->parent != 0 ) && ( $curlink_category->parent != $curlink_category->term_id ) ) {
  93        $curlink_category = get_term( $curlink_category->parent, 'link_category' );
  94        if ( is_wp_error( $curlink_category ) )
  95        return $curlink_category;
  96        $path = '/' . $curlink_category->slug . $path;
  97      }
  98  
  99      if ( $path == $full_path )
 100      return get_link_category( $link_category->term_id, $output );
 101    }
 102  
 103    // If full matching is not required, return the first cat that matches the leaf.
 104    if ( ! $full_match )
 105    return get_link_category( $categories[0]->term_id, $output );
 106  
 107    return null;
 108  }
 109  
 110  /**
 111   * Retrieve category name based on category ID.
 112   *
 113   * @since 0.71
 114   *
 115   * @param int $cat_ID Category ID.
 116   * @return string Category name.
 117   */
 118  function get_the_link_category_by_ID( $cat_ID ) {
 119    $cat_ID = (int) $cat_ID;
 120    $category = &get_link_category( $cat_ID );
 121    if ( is_wp_error( $category ) )
 122    return $category;
 123    return $category->name;
 124  }
 125  
 126  /**
 127   * Retrieve link category link URL.
 128   *
 129   * @since 1.0.0
 130   * @uses apply_filters() Calls 'link_category_link' filter on category link and category ID.
 131   *
 132   * @param int $category_id Category ID.
 133   * @return string
 134   */
 135  function get_link_category_link( $category_id ) {
 136    global $wp_rewrite;
 137    $catlink = '';
 138  
 139    if($wp_rewrite->using_permalinks() && get_option('zelist_rewrite'))
 140    $catlink = $wp_rewrite->get_extra_permastruct('link_category');
 141  
 142    if (empty( $catlink ) ) {
 143      $root = untrailingslashit(get_permalink(get_option('zelist_root')));
 144      $catlink = add_query_arg('link_category',$category_id,$root);
 145    }
 146    else {
 147      $use_trailing_slashes = (substr($catlink, -1, 1) == '/' ) ? true : false;
 148      $category = &get_link_category( $category_id );
 149      if ( is_wp_error( $category ) )
 150      return $category;
 151      $category_nicename = $category->slug;
 152  
 153      if ( $category->parent == $category_id ) // recursive recursion
 154      $category->parent = 0;
 155      elseif ($category->parent != 0 )
 156      $category_nicename = get_link_category_parents( $category->parent, false, '/', true ) . $category_nicename;
 157  
 158      $code = array('%link_category%','%link_category_name%');
 159      $replace = array($category->cat_ID,$category_nicename);
 160      $catlink = str_replace( $code,$replace,$catlink );
 161      if ( $use_trailing_slashes ) $catlink = trailingslashit($catlink);
 162      else $catlink = untrailingslashit($catlink);
 163      $home = trailingslashit(untrailingslashit(get_option('home')));
 164      //$home = get_option('home');
 165  
 166      $catlink = $home . $catlink;
 167    }
 168    return apply_filters( 'link_category_link', $catlink, $category_id );
 169  }
 170  
 171  /**
 172   * Retrieve link to a link category. REMOVED due to messing with regular categories links
 173   * @see zelist_check_categories_taxonomy() hack/filter
 174   * @param $catlink
 175   * @param $category_id
 176   * @return unknown_type
 177   */
 178  function link_category_link($catlink, $category_id ) {
 179    global $is_link_categories_taxonomy;
 180    if(!$is_link_categories_taxonomy) return $catlink; // this filter is needed only into wp_list_categories loops
 181    $category = &get_link_category($category_id);
 182    if($category && $category->taxonomy != 'link_category') return $catlink;
 183    $catlink = get_link_category_link($category_id);
 184    return $catlink;
 185  }
 186  add_filter('category_link','link_category_link',10,2);
 187  
 188  
 189  /**
 190   * The worst hack ever, due to missing hooks during wp_list_categories
 191   * Used by link_category_link filter
 192   * @param $taxonomy
 193   * @param $args
 194   * @return $taxonomy (unchanged)
 195   */
 196  function zelist_check_categories_taxonomy($taxonomy,$args) {
 197    global $is_link_categories_taxonomy;
 198    if($args['type'] == 'link') $is_link_categories_taxonomy = true;
 199    else $is_link_categories_taxonomy = false;
 200    return $taxonomy;
 201  }
 202  add_filter( 'get_categories_taxonomy', 'zelist_check_categories_taxonomy',10,2);
 203  
 204  
 205  /**
 206   * Retrieve category parents with separator.
 207   *
 208   * @since 1.2.0
 209   *
 210   * @param int $id Category ID.
 211   * @param bool $link Optional, default is false. Whether to format with link.
 212   * @param string $separator Optional, default is '/'. How to separate categories.
 213   * @param bool $nicename Optional, default is false. Whether to use nice name for display.
 214   * @param array $visited Optional. Already linked to categories to prevent duplicates.
 215   * @return string
 216   */
 217  function get_link_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
 218    $chain = '';
 219    // one modification !
 220    $parent = &get_link_category( $id );
 221  
 222    if ( is_wp_error( $parent ) )
 223    return $parent;
 224  
 225    if ( $nicename )
 226    $name = $parent->slug;
 227    else
 228    $name = $parent->cat_name;
 229  
 230    if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
 231      $visited[] = $parent->parent;
 232      $chain .= get_link_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
 233    }
 234  
 235    if ( $link )
 236    $chain .= '<a href="' . get_link_category_link( $parent->term_id ) . '" title="' . sprintf( __( "View all links in %s" , 'zelist'), $parent->cat_name ) . '">'.$name.'</a>' . $separator;
 237    else
 238    $chain .= $name.$separator;
 239    return $chain;
 240  }
 241  
 242  /**
 243   * Retrieve link category object by category slug.
 244   *
 245   * @since 2.3.0
 246   *
 247   * @param string $slug The category slug.
 248   * @return object Category data object
 249   */
 250  function get_link_category_by_slug( $slug  ) {
 251    $category = get_term_by( 'slug', $slug, 'link_category' );
 252    if ( $category )
 253    _make_cat_compat( $category );
 254  
 255    return $category;
 256  }
 257  
 258  /**
 259   * Retrieve link category object by category name
 260   * @param $name
 261   * @return unknown_type
 262   */
 263  function get_link_category_by_name($name) {
 264    $category = get_term_by( 'name', $name, 'link_category' );
 265    if ( $category )
 266    _make_cat_compat( $category );
 267    return $category;
 268  }
 269  
 270  /**
 271   * Retrieves link category data given a category ID or category object.
 272   *
 273   * If you pass the $category parameter an object, which is assumed to be the
 274   * category row object retrieved the database. It will cache the category data.
 275   *
 276   * If you pass $category an integer of the category ID, then that category will
 277   * be retrieved from the database, if it isn't already cached, and pass it back.
 278   *
 279   * If you look at get_term(), then both types will be passed through several
 280   * filters and finally sanitized based on the $filter parameter value.
 281   *
 282   * The category will converted to maintain backwards compatibility.
 283   *
 284   * @since 1.5.1
 285   * @uses get_term() Used to get the category data from the taxonomy.
 286   *
 287   * @param int|object $category Category ID or Category row object
 288   * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
 289   * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
 290   * @return mixed Category data in type defined by $output parameter.
 291   */
 292  function &get_link_category( $category, $output = OBJECT, $filter = 'raw' ) {
 293    $category = get_term( $category, 'link_category', $output, $filter );
 294    if ( is_wp_error( $category ) )
 295    return $category;
 296  
 297    _make_cat_compat( $category );
 298  
 299    return $category;
 300  }
 301  
 302  /**
 303   * Retrieve link categories.
 304   *
 305   * @since 0.71
 306   * @uses $post
 307   *
 308   * @param int $id Optional, default to current post ID. The post ID.
 309   * @return array
 310   */
 311  function get_the_link_category( $id = false ) {
 312    global $link;
 313  
 314    $id = (int) $id;
 315    if ( !$id )
 316    $id = (int) $link->link_id;
 317  
 318    $categories = get_object_term_cache( $id, 'link_category' );
 319    if ( !$categories ) {
 320      $categories = wp_get_object_terms( $id, 'link_category' );
 321      wp_cache_add($id, $categories, 'link_category_relationships');
 322    }
 323  
 324    if ( !empty( $categories ) )
 325    usort( $categories, '_usort_terms_by_name' );
 326    else
 327    $categories = array();
 328  
 329    foreach ( (array) array_keys( $categories ) as $key ) {
 330      _make_cat_compat( $categories[$key] );
 331    }
 332    return $categories;
 333  }
 334  
 335  
 336  
 337  /**
 338   * Hack some sense into get_categories for link_categories
 339   * @param $terms
 340   * @param $taxonomies
 341   * @param $args
 342   * @return $terms
 343   */
 344  function zelist_filter_get_terms($terms, $taxonomies = '', $args = '') {
 345    if(!in_array('link_category',$taxonomies)) return $terms;
 346  
 347    // remove regular category
 348    if(!is_admin() && $exclude_category = absint(get_option('zelist_not_directory'))) {
 349      foreach($terms as $i => $term) {
 350        if($term->term_id == $exclude_category) {
 351          unset($terms[$i]);
 352          break;
 353        }
 354      }
 355    }
 356  
 357    if(!$args['show_count']) return $terms;
 358    _hack_pad_term_counts($terms, $taxonomies[0]);
 359    return $terms;
 360  }
 361  add_filter('get_terms', 'zelist_filter_get_terms',10,3);
 362  
 363  /**
 364   * Add count of children to parent count, works with all hierarchical taxonomies
 365   *
 366   * Recalculates term counts by including items from child terms. Assumes all
 367   * relevant children are already in the $terms argument.
 368   *
 369   * @package WordPress
 370   * @subpackage Taxonomy
 371   * @access private
 372   * @since 2.3.0
 373   * @uses $wpdb
 374   *
 375   * @param array $terms List of Term IDs
 376   * @param string $taxonomy Term Context
 377   * @return null Will break from function if conditions are not met.
 378   */
 379  function _hack_pad_term_counts(&$terms, $taxonomy) {
 380    global $wpdb;
 381  
 382    // "This function only works for post categories."
 383    // NO ! it works for all hierarchial taxonomies, you dumb ass
 384    if(!is_taxonomy_hierarchical($taxonomy))
 385    return;
 386  
 387  
 388    if(!$terms || !count($terms)) return false;
 389  
 390  
 391    $taxo = get_taxonomy($taxonomy);
 392  
 393    $term_hier = _get_term_hierarchy($taxonomy);
 394  
 395    if ( empty($term_hier) )
 396    return;
 397  
 398    $term_items = array();
 399  
 400  
 401    foreach ( (array) $terms as $key => $term ) {
 402      $terms_by_id[$term->term_id] = & $terms[$key];
 403      $term_ids[$term->term_taxonomy_id] = $term->term_id;
 404    }
 405  
 406  
 407    // Get the object and term ids and stick them in a lookup table
 408    $join = '';
 409    $where = "WHERE term_taxonomy_id IN (".join(',', array_keys($term_ids)).") ";
 410    $object_type = $taxo->object_type;
 411    if($object_type == 'post') {
 412      $join .= "INNER JOIN $wpdb->posts ON object_id = ID ";
 413      $where .= "AND post_type = 'post' AND post_status = 'publish'";
 414    }
 415    elseif($object_type == 'link') {
 416      $join .= "INNER JOIN $wpdb->links ON object_id = link_id ";
 417      $where .= "AND link_status = 'publish'";
 418    }
 419    $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships $join $where");
 420    foreach ( $results as $row ) {
 421      $id = $term_ids[$row->term_taxonomy_id];
 422      $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
 423    }
 424  
 425    // Touch every ancestor's lookup row for each post in each term
 426    foreach ( $term_ids as $term_id ) {
 427      $child = $term_id;
 428      while ( $parent = $terms_by_id[$child]->parent ) {
 429        if ( !empty($term_items[$term_id]) )
 430        foreach ( $term_items[$term_id] as $item_id => $touches ) {
 431          $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
 432        }
 433        $child = $parent;
 434      }
 435    }
 436  
 437  
 438    // Transfer the touched cells
 439    foreach ( (array) $term_items as $id => $items )
 440    if ( isset($terms_by_id[$id]) )
 441    $terms_by_id[$id]->count = count($items);
 442  }
 443  
 444  
 445  
 446  /**
 447   * Returns a link category with edit context
 448   * @param $id
 449   * @return unknown_type
 450   */
 451  function get_link_category_to_edit( $id ) {
 452    $category = get_link_category( $id, OBJECT, 'edit' );
 453    return $category;
 454  }
 455  
 456  /**
 457   * Retrieve the list of categories for a link.
 458   *
 459   * Compatibility layer for themes and plugins. Also an easy layer of abstraction
 460   * away from the complexity of the taxonomy layer.
 461   *
 462   * @since 2.1.0
 463   *
 464   * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here.
 465   *
 466   * @param int $link_id Optional. The link ID.
 467   * @param array $args Optional. Overwrite the defaults.
 468   * @return array
 469   */
 470  function wp_get_link_categories( $link_id = 0, $args = array() ) {
 471    $link_id = (int) $link_id;
 472    $defaults = array('fields' => 'ids');
 473    $args = wp_parse_args( $args, $defaults );
 474    $cats = wp_get_object_terms($link_id, 'link_category', $args);
 475    return $cats;
 476  }
 477  
 478  
 479  /**
 480   * Will update link category count based on links.
 481   *
 482   * Private function for the default callback for link_tag and category
 483   * taxonomies.
 484   *
 485   * @package WordPress
 486   * @subpackage Taxonomy
 487   * @access private
 488   * @since 2.3.0
 489   * @uses $wpdb
 490   *
 491   * @param array $terms List of Term taxonomy IDs
 492   */
 493  function _update_link_category_term_count( $terms ) {
 494    global $wpdb;
 495  
 496    foreach ( (array) $terms as $term ) {
 497      $count = $wpdb->get_var( $wpdb->prepare(
 498          "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->term_relationships.object_id AND link_status = 'publish' AND term_taxonomy_id = %d", $term ) );
 499      $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
 500    }
 501  }
 502  


Generated: Sat May 30 23:51:06 2009 Cross-referenced by PHPXref 0.7