[ Index ]

PHP Cross Reference of zeList

title

Body

[close]

/includes/ -> link.php (source)

   1  <?php
   2  
   3  /**
   4   * Retrieve the time at which the link was written.
   5   *
   6   * @since 2.0.0
   7   *
   8   * @param string $d Either 'G', 'U', or php date format.
   9   * @param bool $gmt Whether of not to return the gmt time.
  10   * @param int|object $link Optional link ID or object. Default is global $link object.
  11   * @return string
  12   */
  13  function get_link_time( $d = 'U', $gmt = false, $link = null ) { // returns timestamp
  14      if($link) $link = get_link($link);
  15      else global $link;
  16  
  17      $time = $link->link_published;
  18      $time = mysql2date($d, $time);
  19      return apply_filters('get_link_time', $time, $d, $gmt);
  20  }
  21  
  22  /**
  23   * Retrieve link category list in either HTML list or custom format.
  24   *
  25   * @since 1.5.1
  26   *
  27   * @param string $separator Optional, default is empty string. Separator for between the categories.
  28   * @param string $parents Optional. How to display the parents.
  29   * @param int $post_id Optional. Post ID to retrieve categories.
  30   * @return string
  31   */
  32  function get_the_link_category_list( $separator = '', $parents='', $link_id = false ) {
  33      global $wp_rewrite,$link;
  34  
  35      if($link_id) $categories = get_the_link_category( $link_id );
  36      else $categories = get_the_link_category( $link->link_id );
  37  
  38      if ( empty( $categories ) )
  39      return apply_filters( 'the_link_category', __( 'Uncategorized' ), $separator, $parents );
  40  
  41      $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="link_category tag"' : 'rel="link_category"';
  42  
  43      $thelist = '';
  44      if ( '' == $separator ) {
  45          $thelist .= '<ul class="link-categories">';
  46          foreach ( $categories as $category ) {
  47              $thelist .= "\n\t<li>";
  48              switch ( strtolower( $parents ) ) {
  49                  case 'multiple':
  50                      if ( $category->parent )
  51                      $thelist .= get_link_category_parents( $category->parent, true, $separator );
  52                      $thelist .= '<a href="' . get_link_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all links in %s" ,'zelist' ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a></li>';
  53                      break;
  54                  case 'single':
  55                      $thelist .= '<a href="' . get_link_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all links in %s" ,'zelist' ), $category->name ) . '" ' . $rel . '>';
  56                      if ( $category->parent )
  57                      $thelist .= get_link_category_parents( $category->parent, false, $separator );
  58                      $thelist .= $category->name.'</a></li>';
  59                      break;
  60                  case '':
  61                  default:
  62                      $thelist .= '<a href="' . get_link_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all links in %s" ,'zelist' ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
  63              }
  64          }
  65          $thelist .= '</ul>';
  66      } else {
  67          $i = 0;
  68          foreach ( $categories as $category ) {
  69              if ( 0 < $i )
  70              $thelist .= $separator . ' ';
  71              switch ( strtolower( $parents ) ) {
  72                  case 'multiple':
  73                      if ( $category->parent )
  74                      $thelist .= get_link_category_parents( $category->parent, true, $separator );
  75                      $thelist .= '<a href="' . get_link_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all links in %s" ,'zelist' ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
  76                      break;
  77                  case 'single':
  78                      $thelist .= '<a href="' . get_link_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all links in %s" ,'zelist' ), $category->name ) . '" ' . $rel . '>';
  79                      if ( $category->parent )
  80                      $thelist .= get_link_category_parents( $category->parent, false, $separator );
  81                      $thelist .= "$category->cat_name</a>";
  82                      break;
  83                  case '':
  84                  default:
  85                      $thelist .= '<a href="' . get_link_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all links in %s" ,'zelist' ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a>';
  86              }
  87              ++$i;
  88          }
  89      }
  90      return apply_filters( 'the_link_category', $thelist, $separator, $parents );
  91  }
  92  
  93  /**
  94   * Return url to a single link page
  95   * @param $link_id
  96   * @param $leavename
  97   * @return unknown_type
  98   */
  99  function get_link_permalink($link_id = 0,$leavename = false) {
 100      $rewritecode = array(
 101      $leavename? '' : '%link_name%',
 102                  '%link_id%',
 103                  '%link_category%',
 104                  '%link_category_name%',
 105                  '%author%',
 106      );
 107      if($link_id != 0) $link = get_bookmark($link_id);
 108      else global $link;
 109  
 110      global $wp_rewrite;
 111      $permalink = '';
 112      if($wp_rewrite->using_permalinks() && get_option('zelist_rewrite'))
 113          $permalink = $wp_rewrite->get_extra_permastruct('link');
 114  
 115      if ( '' != $permalink && $link->link_visible == 'Y') {
 116          $use_trailing_slashes = (substr($permalink, -1, 1) == '/' ) ? true : false;
 117  
 118          $category = '';
 119          if ( strpos($permalink, '%link_category%') !== false || strpos($permalink, '%link_category_name%') !== false ) {
 120              $cats = get_the_link_category($link->link_id);
 121              if ( $cats ) {
 122                  usort($cats, '_usort_terms_by_ID'); // order by ID
 123                  $category = $cats[0]->slug;
 124                  $category_id = $cats[0]->cat_ID;
 125  
 126                  if ( $parent = $cats[0]->parent ) $category = get_link_category_parents($parent, false, '/', true) . $category;
 127              }
 128              // show default category in permalinks, without
 129              // having to assign it explicitly
 130              if ( empty($category) ) {
 131                  $default_category = get_category( get_option( 'default_link_category' ) );
 132                  $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
 133                  $category_id = is_wp_error( $default_category ) ? '' : $default_category->cat_ID;
 134              }
 135          }
 136  
 137          if (strpos($permalink, '%link_author%') !== false ) {
 138              $author = get_userdata($link->link_owner);
 139              $author = $author->user_nicename;
 140          }
 141  
 142          if(empty($link->link_slug)) $link->link_slug = sanitize_title($link->link_name);
 143          $rewritereplace =
 144          array(
 145          $link->link_slug,
 146          $link->link_id,
 147          $category_id,
 148          $category,
 149          $author,
 150          );
 151  
 152          $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
 153          $home = trailingslashit(untrailingslashit(get_option('home')));
 154          $permalink = $home.$permalink;
 155          return apply_filters('link_link', $permalink, $link, $leavename);
 156      }
 157      else {
 158          $root = untrailingslashit(get_permalink(get_option('zelist_root')));
 159          $permalink = add_query_arg('link',$link->link_id,$root);
 160          return apply_filters('link_link', $permalink, $link, $leavename);
 161      }
 162  }
 163  
 164  /**
 165   * Retrieve the time at which the link was written.
 166   *
 167   * @since 1.5.0
 168   *
 169   * @param string $d Either 'G', 'U', or php date format defaults to the value specified in the time_format option.
 170   * @param int|object $link Optional link ID or object. Default is global $link object.
 171   * @return string
 172   */
 173  function get_the_link_time( $d = '', $link = null ) {
 174      if($link) $link = get_link($link);
 175      else global $link;
 176  
 177      if ( '' == $d )
 178      $the_time = get_link_time(get_option('time_format'), false, $link);
 179      else
 180      $the_time = get_link_time($d, false, $link);
 181      return apply_filters('get_the_time', $the_time, $d, $link);
 182  }
 183  
 184  
 185  /**
 186   * Call major cache updating functions for list of Post objects.
 187   *
 188   * @package WordPress
 189   * @subpackage Cache
 190   * @since 1.5.0
 191   *
 192   * @uses $wpdb
 193   * @uses update_link_cache()
 194   * @uses update_object_term_cache()
 195   * @uses update_linkmeta_cache()
 196   *
 197   * @param array $links Array of Post objects
 198   */
 199  function update_link_caches(&$links) {
 200      // No point in doing all this work if we didn't match any links.
 201      if ( !$links )
 202      return;
 203  
 204      update_link_cache($links);
 205  
 206      $link_ids = array();
 207  
 208      for ($i = 0; $i < count($links); $i++)
 209      $link_ids[] = $links[$i]->ID;
 210  
 211      update_object_term_cache($link_ids, 'link');
 212  
 213      // update_linkmeta_cache($link_ids); @TODO waiting for WordPress to move to a neutral meta tables...
 214  }
 215  
 216  
 217  /**
 218   * Updates links in cache.
 219   *
 220   * @usedby update_page_cache() Aliased by this function.
 221   *
 222   * @package WordPress
 223   * @subpackage Cache
 224   * @since 1.5.1
 225   *
 226   * @param array $links Array of link objects
 227   */
 228  function update_link_cache(&$links) {
 229      if ( !$links )
 230      return;
 231  
 232      foreach ( $links as $link )
 233      wp_cache_add($link->ID, $link, 'links');
 234  }
 235  
 236  
 237  /**
 238   * Change link status
 239   * @param $link_id
 240   * @param $status
 241   * @return unknown_type
 242   */
 243  function link_status($link_id, $status = 'pending', $old_status = '') {
 244  
 245      global $wpdb;
 246      list($link_stati,) = wp_edit_links_query();
 247      if(!isset($link_stati[$status])) return false;
 248      if(empty($old_status)) {
 249        $old_status = $wpdb->get_var($wpdb->prepare("SELECT link_status FROM $wpdb->links WHERE link_id = %s",$link_id));
 250      }
 251      elseif(!isset($link_stati[$old_status])) return false;
 252  
 253      $visibility = ($status == 'publish') ? 'Y' : 'N';
 254      $link_published = ($status == 'publish' && $old_status != 'publish') ? ' link_published = NOW(),' : '';
 255      $query = $wpdb->prepare( "UPDATE $wpdb->links SET link_status = %s, $link_published link_visible = %s WHERE link_id = %s", $status, $visibility, $link_id );
 256      $wpdb->query( $query );
 257      do_action('from_'.$old_status.'_to_'.$status,$link_id);
 258      do_action( $status.'_link', $link_id, $old_status );
 259      if($old_status == 'publish' || $status == 'publish') zelist_update_link_counts();
 260  
 261      clean_bookmark_cache( $link_id );
 262      return true;
 263  }
 264  
 265  function link_owner($link_id,$owner_id) {
 266  global $wpdb;
 267      if(!$link_id = absint($link_id) || !$owner_id = absint($owner_id)) return false;
 268      $query = $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %s WHERE link_id = %s", $link_owner, $link_id );
 269      $wpdb->query( $query );
 270      do_action('link_owner',$link_id,$owner_id);
 271      clean_bookmark_cache( $link_id );
 272      return true;
 273  }
 274  
 275  /**
 276   * Update the link_updated date
 277   * @param $link_id
 278   * @return (bool)
 279   */
 280  function link_touch($link_id = 0) {
 281    if($link_id == 0) {
 282      global $link;
 283      $link_id = $link->link_id;
 284    }
 285    global $wpdb;
 286    if(empty($link_id)) return false;
 287    $query = apply_filters('zelist_touch_link',"UPDATE $wpdb->links SET link_updated = NOW() WHERE link_id = $link_id",$link_id);
 288    return $wpdb->query($query);
 289  }
 290  
 291  
 292  /**
 293   * Return the links count for a given user
 294   */
 295  function get_links_count_by_user($user_id, $hide_invisible = true) {
 296  
 297    $user_id = absint($user_id);
 298      if(!$user_id) return false;
 299  
 300      if(!$count = wp_cache_get($user_id,'link_count_by_user')) {
 301          global $wpdb;
 302  
 303          if($hide_invisible)
 304          $query = "SELECT count(link_id) as count FROM $wpdb->links WHERE link_status IN ('pending','publish') AND link_owner = $user_id";
 305          else
 306          $query = "SELECT count(link_id) as count FROM $wpdb->links WHERE link_owner = $user_id";
 307          $count = $wpdb->get_var($query);
 308          wp_cache_add($user_id, $count, 'link_count_by_user');
 309      }
 310      return apply_filters('get_links_count_by_user',$count,$user_id,$hide_invisible);
 311  }
 312  
 313  
 314    function zelist_update_link_counts() {
 315      // one liner for global update of links tags and link categories count
 316      global $wpdb;
 317      $result = $wpdb->query("UPDATE $wpdb->term_taxonomy
 318    JOIN (
 319      SELECT count(tr.object_id) as count, tr.term_taxonomy_id
 320      FROM $wpdb->term_relationships tr
 321      JOIN $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
 322      JOIN $wpdb->links l ON l.link_id = tr.object_id
 323      WHERE l.link_status = 'publish'
 324      GROUP BY term_taxonomy_id)
 325    as counts
 326    ON counts.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
 327    SET $wpdb->term_taxonomy.count = counts.count
 328    WHERE $wpdb->term_taxonomy.taxonomy = 'link_category'");
 329      return $result;
 330    }


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