PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

/wp-includes/ -> bookmark.php (source)

   1  <?php
   2  
   3  function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') {
   4      global $wpdb;
   5  
   6      $link = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark_id));
   7      $link->link_category = array_unique( wp_get_object_terms($link->link_id, 'link_category', 'fields=ids') );
   8  
   9      $link = sanitize_bookmark($link, $filter);
  10  
  11      if ( $output == OBJECT ) {
  12          return $link;
  13      } elseif ( $output == ARRAY_A ) {
  14          return get_object_vars($link);
  15      } elseif ( $output == ARRAY_N ) {
  16          return array_values(get_object_vars($link));
  17      } else {
  18          return $link;
  19      }
  20  }
  21  
  22  function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
  23      $bookmark = (int) $bookmark;
  24      $bookmark = get_bookmark( $bookmark );
  25  
  26      if ( is_wp_error($bookmark) )
  27          return $bookmark;
  28  
  29      if ( !is_object($bookmark) )
  30          return '';
  31  
  32      if ( !isset($bookmark->$field) )
  33          return '';
  34  
  35      return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
  36  }
  37  
  38  // Deprecate
  39  function get_link($bookmark_id, $output = OBJECT) {
  40      return get_bookmark($bookmark_id, $output);
  41  }
  42  
  43  function get_bookmarks($args = '') {
  44      global $wpdb;
  45  
  46      $defaults = array(
  47          'orderby' => 'name', 'order' => 'ASC',
  48          'limit' => -1, 'category' => '',
  49          'category_name' => '', 'hide_invisible' => 1,
  50          'show_updated' => 0, 'include' => '',
  51          'exclude' => ''
  52      );
  53  
  54      $r = wp_parse_args( $args, $defaults );
  55      extract( $r, EXTR_SKIP );
  56  
  57      $key = md5( serialize( $r ) );
  58      if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
  59          if ( isset( $cache[ $key ] ) )
  60              return apply_filters('get_bookmarks', $cache[ $key ], $r );
  61  
  62      $inclusions = '';
  63      if ( !empty($include) ) {
  64      $exclude = '';  //ignore exclude, category, and category_name params if using include
  65      $category = '';
  66      $category_name = '';
  67          $inclinks = preg_split('/[\s,]+/',$include);
  68          if ( count($inclinks) ) {
  69              foreach ( $inclinks as $inclink ) {
  70                  if (empty($inclusions))
  71                      $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
  72                  else
  73                      $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
  74              }
  75          }
  76      }
  77      if (!empty($inclusions))
  78          $inclusions .= ')';
  79  
  80      $exclusions = '';
  81      if ( !empty($exclude) ) {
  82          $exlinks = preg_split('/[\s,]+/',$exclude);
  83          if ( count($exlinks) ) {
  84              foreach ( $exlinks as $exlink ) {
  85                  if (empty($exclusions))
  86                      $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
  87                  else
  88                      $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
  89              }
  90          }
  91      }
  92      if (!empty($exclusions))
  93          $exclusions .= ')';
  94  
  95      if ( ! empty($category_name) ) {
  96          if ( $category = get_term_by('name', $category_name, 'link_category') )
  97              $category = $category->term_id;
  98      }
  99  
 100      $category_query = '';
 101      $join = '';
 102      if ( !empty($category) ) {
 103          $incategories = preg_split('/[\s,]+/',$category);
 104          if ( count($incategories) ) {
 105              foreach ( $incategories as $incat ) {
 106                  if (empty($category_query))
 107                      $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
 108                  else
 109                      $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
 110              }
 111          }
 112      }
 113      if (!empty($category_query)) {
 114          $category_query .= ") AND taxonomy = 'link_category'";
 115          $join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
 116      }
 117  
 118      if (get_option('links_recently_updated_time')) {
 119          $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
 120      } else {
 121          $recently_updated_test = '';
 122      }
 123  
 124      if ($show_updated) {
 125          $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
 126      }
 127  
 128      $orderby = strtolower($orderby);
 129      $length = '';
 130      switch ($orderby) {
 131          case 'length':
 132              $length = ", CHAR_LENGTH(link_name) AS length";
 133              break;
 134          case 'rand':
 135              $orderby = 'rand()';
 136              break;
 137          default:
 138              $orderby = "link_" . $orderby;
 139      }
 140  
 141      if ( 'link_id' == $orderby )
 142          $orderby = "$wpdb->links.link_id";
 143  
 144      $visible = '';
 145      if ( $hide_invisible )
 146          $visible = "AND link_visible = 'Y'";
 147  
 148      $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
 149      $query .= " $exclusions $inclusions";
 150      $query .= " ORDER BY $orderby $order";
 151      if ($limit != -1)
 152          $query .= " LIMIT $limit";
 153  
 154      $results = $wpdb->get_results($query);
 155  
 156      $cache[ $key ] = $results;
 157      wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
 158  
 159      return apply_filters('get_bookmarks', $results, $r);
 160  }
 161  
 162  function sanitize_bookmark($bookmark, $context = 'display') {
 163      $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
 164          'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
 165          'link_rel', 'link_notes', 'link_rss', );
 166  
 167      $do_object = false;
 168      if ( is_object($bookmark) )
 169          $do_object = true;
 170  
 171      foreach ( $fields as $field ) {
 172          if ( $do_object )
 173              $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
 174          else
 175              $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
 176      }
 177  
 178      return $bookmark;
 179  }
 180  
 181  function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
 182      $int_fields = array('link_id', 'link_rating');
 183      if ( in_array($field, $int_fields) )
 184          $value = (int) $value;
 185  
 186      $yesno = array('link_visible');
 187      if ( in_array($field, $yesno) )
 188          $value = preg_replace('/[^YNyn]/', '', $value);
 189  
 190      if ( 'link_target' == $field ) {
 191          $targets = array('_top', '_blank');
 192          if ( ! in_array($value, $targets) )
 193              $value = '';
 194      }
 195  
 196      if ( 'raw' == $context )
 197          return $value;
 198  
 199      if ( 'edit' == $context ) {
 200          $format_to_edit = array('link_notes');
 201          $value = apply_filters("edit_$field", $value, $bookmark_id);
 202  
 203          if ( in_array($field, $format_to_edit) ) {
 204              $value = format_to_edit($value);
 205          } else {
 206              $value = attribute_escape($value);
 207          }
 208      } else if ( 'db' == $context ) {
 209          $value = apply_filters("pre_$field", $value);
 210      } else {
 211          // Use display filters by default.
 212          $value = apply_filters($field, $value, $bookmark_id, $context);
 213      }
 214  
 215      if ( 'attribute' == $context )
 216          $value = attribute_escape($value);
 217      else if ( 'js' == $context )
 218          $value = js_escape($value);
 219  
 220      return $value;
 221  }
 222  
 223  function delete_get_bookmark_cache() {
 224      wp_cache_delete( 'get_bookmarks', 'bookmark' );
 225  }
 226  add_action( 'add_link', 'delete_get_bookmark_cache' );
 227  add_action( 'edit_link', 'delete_get_bookmark_cache' );
 228  add_action( 'delete_link', 'delete_get_bookmark_cache' );
 229  
 230  ?>


Generated Thu Dec 6 06:47:08 2007 for RedAlt XRefs Cross-referenced by PHPXref 0.6 and RedAlt