[ Index ]

PHP Cross Reference of zeList

title

Body

[close]

/includes/ -> query.php (source)

   1  <?php
   2  
   3  
   4  /**
   5   * Setup The Loop with query parameters.
   6   *
   7   * This will override the current WordPress Loop and shouldn't be used more than
   8   * once. This must not be used within the WordPress Loop.
   9   *
  10   * @since 1.5.0
  11   * @uses $zelist_query
  12   *
  13   * @param string $query
  14   * @return array List of links
  15   */
  16  function &query_links($query) {
  17      unset($GLOBALS['zelist_query']);
  18      $GLOBALS['zelist_query'] =& new zelist_query();
  19      return $GLOBALS['zelist_query']->query($query);
  20  }
  21  
  22  
  23  function zelist_get_bookmarks($args = null) {
  24      $defaults = array(
  25          'orderby' => 'name', 'order' => 'ASC',
  26          'limit' => -1, 'offset' => 0, 'links_per_page' => 10,
  27          'link_category' => '', 'category' => false,
  28          'category_name' => '', 'hide_invisible' => 1,
  29          'show_updated' => 0, 'include' => '',
  30          'exclude' => '', 'search' => '',
  31          'link_status' => 'publish',
  32          'link_owner' => 0,
  33          'zelist' => 1,    );
  34  
  35      $r = wp_parse_args( $args, $defaults );
  36      if($r['category'] !== false) {
  37          $r['link_category'] = $r['category'];
  38          $r['category'] = false;
  39      }
  40  
  41      if ( ! empty($r['numberlinks']) )
  42          $r['links_per_page'] = $r['numberlinks'];
  43      if ( ! empty($r['include']) ) {
  44          $inclinks = preg_split('/[\s,]+/',$r['include']);
  45          $r['links_per_page'] = count($inclinks);  // only the number of links included
  46          $r['link__in'] = $inclinks;
  47      } elseif ( ! empty($r['exclude']) )
  48          $r['link__not_in'] = preg_split('/[\s,]+/',$r['exclude']);
  49      global $get_links;
  50      $get_links = new zelist_query;
  51      return $get_links->query($r);
  52  }
  53  
  54  /**
  55   * Setup global link data.
  56   *
  57   * @since 1.5.0
  58   *
  59   * @param object $post Post data.
  60   * @return bool True when finished.
  61   */
  62  function setup_linkdata($bookmark) {
  63      global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  64      global $link,$post;
  65      $link = $bookmark;
  66      // WordPress bridge
  67      $post = (object) array();
  68      $post->ID = $link->link_id;
  69      $post->post_type = 'link';
  70      $post->post_title = $link->link_name;
  71      $post->post_author = $link->link_owner;
  72      $post->post_status = $link->link_status;
  73  
  74      $post->post_excerpt = $post->post_content = $link->link_description;
  75      $excerpt_length = apply_filters('excerpt_length', 55);
  76      $words = explode(' ', $link->link_description, $excerpt_length + 1);
  77      if(count($words) > $excerpt_length) {
  78          array_pop($words);
  79          array_push($words, '[...]');
  80          $post->post_excerpt = strip_tags(implode(' ', $words));
  81          }
  82  
  83  
  84  
  85      $post->post_date = $link->link_updated;
  86  
  87      $id = (int) $link->link_id;
  88      $authordata = get_userdata($link->link_owner);
  89  
  90      $day = mysql2date('d.m.y', $link->link_updated);
  91      $currentmonth = mysql2date('m', $link->link_updated);
  92      $numpages = 1;
  93      $page = get_query_var('page');
  94      if ( !$page )
  95      $page = 1;
  96      if ( is_single() || is_page() || is_feed() )
  97      $more = 1;
  98      $content = $link->link_description;
  99      if ( strpos( $content, '<!--nextpage-->' ) ) {
 100          if ( $page > 1 )
 101          $more = 1;
 102          $multipage = 1;
 103          $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
 104          $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
 105          $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
 106          $pages = explode('<!--nextpage-->', $content);
 107          $numpages = count($pages);
 108      } else {
 109          $pages[0] = $link->link_description;
 110          $multipage = 0;
 111      }
 112  
 113      return true;
 114  }
 115  
 116  class zelist_query {
 117      var $is_links = false;
 118      var $is_a_link = false;
 119      var $is_link_category = false;
 120      var $is_link_home = false;
 121      var $is_link_search = false;
 122      var $is_link_tag = false;
 123      var $query;
 124      var $queried_object;
 125      var $queried_object_id;
 126      var $request;
 127      var $links;
 128      var $link_count = 0;
 129      var $link;
 130      var $found_links = 0;
 131  
 132  	function zelist_query() {
 133          return $this->__construct();
 134      }
 135  
 136  	function __construct() {
 137      }
 138  
 139  	function init_query_flags() {
 140          $this->is_links = false;
 141          $this->is_a_link = false;
 142          $this->is_link_category = false;
 143          $this->is_link_home = false;
 144          $this->is_link_search = false;
 145          $this->is_link_tag = false;
 146          $this->is_paged = false;
 147      }
 148  
 149  	function init () {
 150          unset($this->links);
 151          unset($this->query);
 152          $this->query_vars = array();
 153          unset($this->queried_object);
 154          unset($this->queried_object_id);
 155          $this->link_count = 0;
 156          $this->current_link = -1;
 157          $this->in_the_loop = false;
 158  
 159          $this->init_query_flags();
 160      }
 161  
 162  	function parse_query($query) {
 163          if ( !empty($query) || !isset($this->query) ) {
 164              $this->init();
 165              if ( is_array($query) ) $this->query_vars = $query;
 166              else parse_str($query, $this->query_vars);
 167              $this->query = $query;
 168          }
 169  
 170          $this->query_vars = $this->fill_query_vars($this->query_vars);
 171          $qv = &$this->query_vars;
 172  
 173          $qv['page_id'] =  absint($qv['page_id']);
 174          $qv['pagename'] = trim($qv['pagename']);
 175          $qv['link'] =  absint($qv['link']);
 176          $qv['link_category'] =  absint($qv['link_category']);
 177  
 178          $qv['link_name'] = trim( $qv['link_name'] );
 179          $qv['link_category_name'] = trim( $qv['link_category_name'] );
 180  
 181          if($qv['link_name'] != '' || $qv['link']) {
 182              $this->is_a_link = true;
 183          }
 184          elseif ( !empty($qv['ls'])) {
 185              $this->is_link_search = true;
 186          }
 187  
 188          if ( empty($qv['link_category']) || ($qv['link_category'] == '0') ) {
 189              $this->is_link_category = false;
 190          } else {
 191              if (strpos($qv['link_category'], '-') !== false) {
 192                  $this->is_link_category = false;
 193              } else {
 194                  $this->is_link_category = true;
 195              }
 196          }
 197  
 198  
 199          if ( '' != $qv['link_category_name'] ) {
 200              $this->is_link_category = true;
 201          }
 202          if ( !is_array($qv['link_category__in']) || empty($qv['link_category__in']) ) {
 203              $qv['link_category__in'] = array();
 204          } else {
 205              $qv['link_category__in'] = array_map('absint', $qv['link_category__in']);
 206              $this->is_link_category = true;
 207          }
 208  
 209          if ( !is_array($qv['link_category__not_in']) || empty($qv['link_category__not_in']) ) {
 210              $qv['link_category__not_in'] = array();
 211          } else {
 212              $qv['link_category__not_in'] = array_map('absint', $qv['link_category__not_in']);
 213          }
 214  
 215          if ( !is_array($qv['link_category__and']) || empty($qv['link_category__and']) ) {
 216              $qv['link_category__and'] = array();
 217          } else {
 218              $qv['link_category__and'] = array_map('absint', $qv['link_category__and']);
 219              $this->is_link_category = true;
 220          }
 221  
 222          if (  '' != $qv['link_tag'] ) {
 223              $this->is_link_tag = true;
 224          }
 225  
 226  
 227          $qv['link_tag_id'] = absint($qv['link_tag_id']);
 228          if (  !empty($qv['link_tag_id']) )
 229          $this->is_link_tag = true;
 230  
 231          if ( !is_array($qv['link_tag__in']) || empty($qv['link_tag__in']) ) {
 232              $qv['link_tag__in'] = array();
 233          } else {
 234              $qv['link_tag__in'] = array_map('absint', $qv['link_tag__in']);
 235              $this->is_link_tag = true;
 236          }
 237  
 238          if ( !is_array($qv['link_tag__not_in']) || empty($qv['link_tag__not_in']) ) {
 239              $qv['link_tag__not_in'] = array();
 240          } else {
 241              $qv['link_tag__not_in'] = array_map('absint', $qv['link_tag__not_in']);
 242          }
 243  
 244          if ( !is_array($qv['link_tag__and']) || empty($qv['link_tag__and']) ) {
 245              $qv['link_tag__and'] = array();
 246          } else {
 247              $qv['link_tag__and'] = array_map('absint', $qv['link_tag__and']);
 248              $this->is_link_category = true;
 249          }
 250  
 251          if ( !is_array($qv['link_tag_slug__in']) || empty($qv['link_tag_slug__in']) ) {
 252              $qv['link_tag_slug__in'] = array();
 253          } else {
 254              $qv['link_tag_slug__in'] = array_map('sanitize_title', $qv['link_tag_slug__in']);
 255              $this->is_link_tag = true;
 256          }
 257  
 258          if ( !is_array($qv['link_tag_slug__and']) || empty($qv['link_tag_slug__and']) ) {
 259              $qv['link_tag_slug__and'] = array();
 260          } else {
 261              $qv['link_tag_slug__and'] = array_map('sanitize_title', $qv['link_tag_slug__and']);
 262              $this->is_link_tag = true;
 263          }
 264  
 265          if ( '' != $qv['paged'] )
 266          $this->is_paged = true;
 267  
 268          if (
 269          (!$this->is_a_link && ! $this->is_link_tag && !$this->is_link_category && !$this->is_link_search)
 270          && (!empty($qv['page_id']) || '' != $qv['pagename'] )
 271          ) {
 272              if('' != $qv['pagename']) {
 273                  $page =& get_page_by_path($qv['pagename']);
 274                  $page_id = $page->ID;
 275              }
 276              else $page_id = $qv['page_id'];
 277              if ($page_id && $page_id == get_option('zelist_root')) {
 278                  //&& 'page' == get_option('show_on_front') // the homepage is a Page
 279                  //&& get_option('page_on_front') == get_option('zelist_root') // the homepage Page is actually the zelist root page
 280                  //&& $queried_object->ID ==  // and the requested page is the zelist root page
 281                  $this->is_link_home = true;
 282              }
 283          }
 284  
 285  
 286          if($this->is_link_home || $this->is_a_link || $this->is_link_tag || $this->is_link_category || $this->is_link_search) {
 287              $this->is_links = true;
 288          }
 289  
 290  
 291          if ( !empty($query) )
 292          do_action_ref_array('parse_link_query', array(&$this));
 293      }
 294  
 295      function &query($query) {
 296          $this->parse_query($query);
 297          //if($_SERVER['REMOTE_ADDR'] == '87.91.201.70') echo '<pre>'.print_r($this,1).'</pre>';
 298  
 299          return $this->get_links();
 300  
 301      }
 302  
 303      function &get_links() {
 304          global $wpdb, $user_ID;
 305          do_action_ref_array('pre_get_links', array(&$this));
 306          // Shorthand.
 307          $q = &$this->query_vars;
 308  
 309          $q = $this->fill_query_vars($q);
 310  
 311  
 312          // First let's clear some variables
 313          $distinct = '';
 314          $whichcat = '';
 315          $where = '';
 316          $limits = '';
 317          $join = '';
 318          $search = '';
 319          $groupby = '';
 320          $fields = "$wpdb->links.*";
 321  
 322          // do stuff
 323  
 324          if ( !isset($q['links_per_page']) || $q['links_per_page'] == 0 )
 325          $q['links_per_page'] = get_option('links_per_page');
 326  
 327          $q['links_per_page'] = (int) $q['links_per_page'];
 328          if ( $q['links_per_page'] < -1 ) $q['links_per_page'] = abs($q['links_per_page']);
 329          else if ( $q['links_per_page'] == 0 ) $q['links_per_page'] = get_option('links_per_page');
 330  
 331          // Paging
 332          if ( empty($q['nopaging']) && !$this->is_singular ) {
 333              $page = absint($q['paged']);
 334              if (empty($page)) {
 335                  $page = 1;
 336              }
 337              if ( empty($q['offset']) ) {
 338                  $pgstrt = '';
 339                  $pgstrt = ($page - 1) * $q['links_per_page'] . ', ';
 340                  $limits = 'LIMIT '.$pgstrt.$q['links_per_page'];
 341              } else { // we're ignoring $page and using 'offset'
 342                  $q['offset'] = absint($q['offset']);
 343                  $pgstrt = $q['offset'] . ', ';
 344                  $limits = 'LIMIT ' . $pgstrt . $q['links_per_page'];
 345              }
 346          }
 347  
 348  
 349          // If a link number is specified, load that link
 350          if ( $q['link'] ) {
 351              $where .= " AND {$wpdb->links}.link_id = " . $q['link'];
 352          } elseif ( $q['link__in'] ) {
 353              $link__in = implode(',', array_map( 'absint', $q['link__in'] ));
 354              $where .= " AND {$wpdb->links}.link_id IN ($link__in)";
 355          } elseif ( $q['link__not_in'] ) {
 356              $link__not_in = implode(',',  array_map( 'absint', $q['link__not_in'] ));
 357              $where .= " AND {$wpdb->links}.link_id NOT IN ($link__not_in)";
 358          }
 359  
 360          // rewrited urls, when no link id is given
 361          if (empty($q['link']) && '' != $q['link_name']) {
 362              $q['link_name'] = sanitize_title($q['link_name']);
 363              $where .= " AND $wpdb->links.link_name = '" . $q['link_name'] . "'";
 364          }
 365  
 366          // If a search pattern is specified, load the links that match
 367          if ( !empty($q['ls'])) {
 368              // added slashes screw with quote grouping when done early, so done later
 369              $q['ls'] = stripslashes($q['ls']);
 370  
 371              if ( !empty($q['sentence']) ) {
 372                  $q['search_terms'] = array($q['ls']);
 373              } else {
 374                  preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['ls'], $matches);
 375                  $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
 376              }
 377              $n = !empty($q['exact']) ? '' : '%';
 378              $searchand = '';
 379              foreach( (array) $q['search_terms'] as $term) {
 380                  $term = addslashes_gpc($term);
 381                  $search .= "{$searchand}(($wpdb->links.link_name LIKE '{$n}{$term}{$n}') "
 382                  ."OR ($wpdb->links.link_description LIKE '{$n}{$term}{$n}') "
 383                  ."OR ($wpdb->links.link_url LIKE '{$n}{$term}{$n}') ) ";
 384                  $searchand = ' AND ';
 385              }
 386              $term = $wpdb->escape($q['ls']);
 387              if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['ls'] )
 388              $search .= " OR ($wpdb->links.link_name LIKE '{$n}{$term}{$n}') OR ($wpdb->links.link_description LIKE '{$n}{$term}{$n}')";
 389  
 390              if ( !empty($search) )
 391              $search = " AND ({$search}) ";
 392          }
 393  
 394          // link status
 395          if(!empty($q['link_status'])) {
 396              list($link_stati,) = wp_edit_links_query();
 397              $status = (isset($link_stati[$q['link_status']])) ? $q['link_status'] : false;
 398              if($status) $where .= " AND $wpdb->links.link_status LIKE '$status'";
 399          }
 400          else {
 401              if(!is_admin() || !current_user_can('manage_links_advanced')) {
 402                  $where .= " AND $wpdb->links.link_visible = 'Y'";
 403              }
 404          }
 405  
 406          // link_owner
 407          if(!empty($q['link_owner'])) {
 408              $owner = absint($q['link_owner']);
 409              if($owner) $where .= " AND $wpdb->links.link_owner LIKE '$owner'";
 410          }
 411          if(is_admin() && !current_user_can('edit_other_links')) {
 412              $user = wp_get_current_user();
 413              $where .= " AND $wpdb->links.link_owner LIKE '$user->ID'";
 414          }
 415  
 416          // Category stuff
 417          if (
 418          empty($q['link_category']) || ($q['link_category'] == '0') ||
 419          $this->is_singular // Bypass cat checks if fetching specific links
 420          ) {
 421              $whichcat = '';
 422              if(get_option('zelist_not_directory'))
 423                $q['link_category__not_in'][] = get_option('zelist_not_directory');
 424          } else {
 425              $q['link_category'] = ''.urldecode($q['link_category']).'';
 426              $q['link_category'] = addslashes_gpc($q['link_category']);
 427              $cat_array = preg_split('/[,\s]+/', $q['link_category']);
 428              $q['link_category'] = '';
 429              $req_cats = array();
 430              foreach ( (array) $cat_array as $cat ) {
 431                  $cat = intval($cat);
 432                  $req_cats[] = $cat;
 433                  $in = ($cat > 0);
 434                  $cat = abs($cat);
 435                  if ( $in ) {
 436                      $q['link_category__in'][] = $cat;
 437                      $q['link_category__in'] = array_merge($q['link_category__in'], get_term_children($cat, 'link_category'));
 438                  } else {
 439                      $q['link_category__not_in'][] = $cat;
 440                      $q['link_category__not_in'] = array_merge($q['link_category__not_in'], get_term_children($cat, 'link_category'));
 441                  }
 442              }
 443              $q['link_category'] = implode(',', $req_cats);
 444          }
 445  
 446          if ( !empty($q['link_category__in']) ) {
 447              $groupby = "{$wpdb->links}.link_id";
 448          }
 449  
 450          if ( !empty($q['link_category__in']) ) {
 451              $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->links.link_id = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
 452              $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'link_category' ";
 453              $include_cats = "'" . implode("', '", $q['link_category__in']) . "'";
 454              $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
 455          }
 456  
 457          if ( !empty($q['link_category__not_in']) ) {
 458              if ( $wpdb->has_cap( 'subqueries' ) ) {
 459                  $cat_string = "'" . implode("', '", $q['link_category__not_in']) . "'";
 460                  $whichcat .= " AND $wpdb->links.link_id NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'link_category' AND tt.term_id IN ($cat_string) )";
 461              } else {
 462                  $ids = get_objects_in_term($q['link_category__not_in'], 'link_category');
 463                  if ( is_wp_error( $ids ) )
 464                  $ids = array();
 465                  if ( is_array($ids) && count($ids > 0) ) {
 466                      $out_links = "'" . implode("', '", $ids) . "'";
 467                      $whichcat .= " AND $wpdb->links.link_id NOT IN ($out_links)";
 468                  }
 469              }
 470          }
 471  
 472          // Category stuff for nice URLs
 473          if ( '' != $q['link_category_name'] && !$this->is_singular ) {
 474              $reqcat = get_link_category_by_path($q['link_category_name']);
 475              $q['link_category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['link_category_name'])));
 476              $cat_paths = '/' . trim($q['link_category_name'], '/');
 477              $q['link_category_name'] = sanitize_title(basename($cat_paths));
 478  
 479              $cat_paths = '/' . trim(urldecode($q['link_category_name']), '/');
 480              $q['link_category_name'] = sanitize_title(basename($cat_paths));
 481              $cat_paths = explode('/', $cat_paths);
 482              $cat_path = '';
 483              foreach ( (array) $cat_paths as $pathdir )
 484              $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
 485  
 486              //if we don't match the entire hierarchy fallback on just matching the nicename
 487              if ( empty($reqcat) )
 488              $reqcat = get_link_category_by_path($q['link_category_name'], false);
 489  
 490              if ( !empty($reqcat) )
 491              $reqcat = $reqcat->term_id;
 492              else
 493              $reqcat = 0;
 494  
 495              $q['link_category'] = $reqcat;
 496  
 497              $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->links.link_id = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
 498              $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'link_category' ";
 499              $in_cats = array($q['link_category']);
 500              $in_cats = array_merge($in_cats, get_term_children($q['link_category'], 'link_category'));
 501              $in_cats = "'" . implode("', '", $in_cats) . "'";
 502              $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
 503              $groupby = "{$wpdb->links}.link_id";
 504  
 505          }
 506  
 507          // Tags
 508          if ( '' != $q['link_tag'] ) {
 509              if ( strpos($q['link_tag'], ',') !== false ) {
 510                  $tags = preg_split('/[,\s]+/', $q['link_tag']);
 511                  foreach ( (array) $tags as $tag ) {
 512                      $tag = sanitize_term_field('slug', $tag, 0, 'link_tag', 'db');
 513                      $q['link_tag_slug__in'][] = $tag;
 514                  }
 515              } else if ( preg_match('/[+\s]+/', $q['link_tag']) ) {
 516                  $tags = preg_split('/[+\s]+/', $q['link_tag']);
 517                  foreach ( (array) $tags as $tag ) {
 518                      $tag = sanitize_term_field('slug', $tag, 0, 'link_tag', 'db');
 519                      $q['link_tag_slug__and'][] = $tag;
 520                  }
 521              } else {
 522                  $q['link_tag'] = sanitize_term_field('slug', $q['link_tag'], 0, 'link_tag', 'db');
 523                  $q['link_tag_slug__in'][] = $q['link_tag'];
 524              }
 525          }
 526  
 527          if ( !empty($q['link_tag__in']) || !empty($q['link_tag_slug__in']) ) {
 528              $groupby = "{$wpdb->links}.link_id";
 529          }
 530  
 531          if ( !empty($q['link_tag__in']) ) {
 532              $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->links.link_id = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
 533              $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'link_tag' ";
 534              $include_tags = "'" . implode("', '", $q['link_tag__in']) . "'";
 535              $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
 536              $reqtag = is_term( $q['link_tag__in'][0], 'link_tag' );
 537              if ( !empty($reqtag) )
 538              $q['link_tag_id'] = $reqtag['term_id'];
 539          }
 540  
 541          if ( !empty($q['link_tag_slug__in']) ) {
 542              $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->links.link_id = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
 543              $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'link_tag' ";
 544              $include_tags = "'" . implode("', '", $q['link_tag_slug__in']) . "'";
 545              $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
 546              $reqtag = get_term_by( 'slug', $q['link_tag_slug__in'][0], 'link_tag' );
 547              if ( !empty($reqtag) )
 548              $q['link_tag_id'] = $reqtag->term_id;
 549          }
 550  
 551          if ( !empty($q['link_tag__not_in']) ) {
 552              if ( $wpdb->has_cap( 'subqueries' ) ) {
 553                  $tag_string = "'" . implode("', '", $q['link_tag__not_in']) . "'";
 554                  $whichcat .= " AND $wpdb->links.link_id NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'link_tag' AND tt.term_id IN ($tag_string) )";
 555              } else {
 556                  $ids = get_objects_in_term($q['link_tag__not_in'], 'link_tag');
 557                  if ( is_wp_error( $ids ) )
 558                  $ids = array();
 559                  if ( is_array($ids) && count($ids > 0) ) {
 560                      $out_links = "'" . implode("', '", $ids) . "'";
 561                      $whichcat .= " AND $wpdb->links.link_id NOT IN ($out_links)";
 562                  }
 563              }
 564          }
 565  
 566          $where .= $search.$whichcat.$whichauthor.$whichmimetype;
 567  
 568          // Order by
 569          if ( empty($q['orderby']) ) {
 570              $order = (!empty($q['order'])) ? $q['order'] : 'DESC';
 571              $q['orderby'] = "$wpdb->links.link_published $order, $wpdb->links.link_updated $order ";
 572          } else {
 573              // Used to filter values
 574              $allowed_keys = array('id','url','name','image','target','category','description','visible','owner','rating','added','updated','rel','notes','rss', 'rand');
 575              if ( !empty($q['meta_key']) ) {
 576                  $allowed_keys[] = $q['meta_key'];
 577                  $allowed_keys[] = 'meta_value';
 578              }
 579              $q['orderby'] = urldecode($q['orderby']);
 580              $q['orderby'] = addslashes_gpc($q['orderby']);
 581              $orderby_array = explode(' ',$q['orderby']);
 582              if ( empty($orderby_array) )
 583              $orderby_array[] = $q['orderby'];
 584              $q['orderby'] = '';
 585              for ($i = 0; $i < count($orderby_array); $i++) {
 586                  // Only allow certain values for safety
 587                  $orderby = $orderby_array[$i];
 588                  switch ($orderby) {
 589                      case 'menu_order':
 590                          break;
 591                      case 'id':
 592                          $orderby = "$wpdb->links.link_id";
 593                          break;
 594                      case 'rand':
 595                          $orderby = 'RAND()';
 596                          break;
 597                      case $q['meta_key']:
 598                      case 'meta_value':
 599                          $orderby = "$wpdb->linkmeta.meta_value";
 600                          break;
 601                      default:
 602                          $orderby = "$wpdb->links.link_" . $orderby;
 603                  }
 604                  if ( in_array($orderby_array[$i], $allowed_keys) )
 605                  $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
 606              }
 607              // append ASC or DESC at the end
 608              if ( !empty($q['orderby']))
 609              $q['orderby'] .= " {$q['order']}";
 610  
 611              if ( empty($q['orderby']) )
 612              $q['orderby'] = "$wpdb->links.link_date ".$q['order'];
 613          }
 614  
 615          // linkmeta queries
 616          if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
 617          $join .= " LEFT JOIN $wpdb->linkmeta ON ($wpdb->links.link_id = $wpdb->linkmeta.link_id) ";
 618          if ( ! empty($q['meta_key']) )
 619          $where .= $wpdb->prepare(" AND $wpdb->linkmeta.meta_key = %s ", $q['meta_key']);
 620          if ( ! empty($q['meta_value']) ) {
 621              if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
 622              $q['meta_compare'] = '=';
 623  
 624              $where .= $wpdb->prepare("AND $wpdb->linkmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
 625          }
 626  
 627          // Apply filters on where and join prior to paging so that any
 628          // manipulations to them are reflected in the paging by day queries.
 629          if ( !$q['suppress_filters'] ) {
 630              $where = apply_filters('links_where', $where);
 631              $join = apply_filters('links_join', $join);
 632          }
 633  
 634          $orderby = $q['orderby'];
 635          // Apply link-paging filters on where and join.  Only plugins that
 636          // manipulate paging queries should use these hooks.
 637          if ( !$q['suppress_filters'] ) {
 638              $where = apply_filters('links_where_paged', $where);
 639              $groupby = apply_filters('links_groupby', $groupby);
 640              $join = apply_filters('links_join_paged', $join);
 641              $orderby = apply_filters('links_orderby', $orderby);
 642              $distinct = apply_filters('links_distinct', $distinct);
 643              $limits = apply_filters( 'link_limits', $limits );
 644  
 645              if ( ! empty($q['meta_key']) )
 646              $fields = "$fields, $wpdb->linkmeta.meta_value";
 647  
 648              $fields = apply_filters('links_fields', $fields);
 649          }
 650  
 651          // Announce current selection parameters.  For use by caching plugins.
 652          do_action( 'links_selection', $where . $groupby . $orderby . $limits . $join );
 653  
 654          // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
 655          if ( !$q['suppress_filters'] ) {
 656              $where = apply_filters('links_where_', $where);
 657              $groupby = apply_filters('links_groupby_', $groupby);
 658              $join = apply_filters('links_join_request', $join);
 659              $orderby = apply_filters('links_orderby_request', $orderby);
 660              $distinct = apply_filters('links_distinct_request', $distinct);
 661              $fields = apply_filters('links_fields_request', $fields);
 662              $limits = apply_filters( 'link_limits_request', $limits );
 663          }
 664  
 665          if ( ! empty($groupby) )
 666          $groupby = 'GROUP BY ' . $groupby;
 667          if ( !empty( $orderby ) )
 668          $orderby = 'ORDER BY ' . $orderby;
 669          $found_rows = '';
 670          if ( !empty($limits) )
 671          $found_rows = 'SQL_CALC_FOUND_ROWS';
 672  
 673          $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->links $join WHERE 1=1 $where $groupby $orderby $limits";
 674  
 675          if ( !$q['suppress_filters'] )
 676          $this->request = apply_filters('links_request', $this->request);
 677  
 678          $this->links = $wpdb->get_results($this->request);
 679          //echo '<pre>'.print_r($this,1).'</pre>';
 680          //if($_SERVER['REMOTE_ADDR'] == '87.91.201.70') echo "\n\nQ =".'<pre>'.print_r($q,1).'</pre>';
 681          //if($_SERVER['REMOTE_ADDR'] == '87.91.201.70') echo "\n<br />REQUEST = $this->request \n<br />";
 682          //if($_SERVER['REMOTE_ADDR'] == '87.91.201.70') echo '<pre>'.print_r($this->links,1).'</pre>';
 683  
 684  
 685          // Raw results filter.  Prior to status checks.
 686          if ( !$q['suppress_filters'] )
 687          $this->links = apply_filters('links_results', $this->links);
 688  
 689  
 690          if ( !empty($limits) ) {
 691              //echo "<br />limits $limits";
 692              $found_links_query = apply_filters( 'found_links_query', 'SELECT FOUND_ROWS()' );
 693              $this->found_links = $wpdb->get_var( $found_links_query );
 694              $this->found_links = apply_filters( 'found_links', $this->found_links );
 695              $this->max_num_pages = ceil($this->found_links / $q['links_per_page']);
 696          }
 697  
 698          if ( !$q['suppress_filters'] )
 699          $this->links = apply_filters('the_links', $this->links);
 700          update_link_caches($this->links);
 701  
 702          $this->link_count = count($this->links);
 703          if ($this->link_count > 0) {
 704              $this->link = $this->links[0];
 705          }
 706          $this->get_queried_object_id();
 707          do_action('zelist_query_get_links');
 708          return $this->links;
 709      }
 710  
 711  	function fill_query_vars($array) {
 712          $keys = array(
 713              'error'
 714              , 'link'
 715              , 'ls'
 716              , 'link_name'
 717              , 'link_tag'
 718              , 'link_tag_id'
 719              , 'link_category'
 720              , 'link_category_name'
 721              , 'link_status'
 722              , 'link_owner'
 723              , 'ls'
 724              , 'page_id'
 725              , 'pagename'
 726              , 'links_per_page'
 727              , 'offset'
 728              , 'paged'
 729              , 'order'
 730              , 'orderby'
 731              );
 732  
 733              // unset not valid keys (zelist query can be called with full wp_query->query_vars array)
 734              // foreach($array as $key => $value) {
 735              // if(!in_array($key,$keys)) unset($array[$key]);
 736              // }
 737              foreach ($keys as $key) {
 738                  if ( !isset($array[$key]))
 739                  $array[$key] = '';
 740              }
 741              $array_keys = array('link_category__in', 'link_category__not_in', 'link_category__and', 'link__in', 'link__not_in',
 742              'link_tag__in', 'link_tag__not_in', 'link_tag__and', 'link_tag_slug__in', 'link_tag_slug__and');
 743  
 744              foreach ( $array_keys as $key ) {
 745                  if ( !isset($array[$key]))
 746                  $array[$key] = array();
 747              }
 748              return $array;
 749      }
 750  
 751      /**
 752       * Retrieve query variable.
 753       *
 754       * @since 1.5.0
 755       * @access public
 756       *
 757       * @param string $query_var Query variable key.
 758       * @return mixed
 759       */
 760  	function get($query_var) {
 761          if (isset($this->query_vars[$query_var])) {
 762              return $this->query_vars[$query_var];
 763          }
 764  
 765          return '';
 766      }
 767  
 768      /**
 769       * Set query variable.
 770       *
 771       * @since 1.5.0
 772       * @access public
 773       *
 774       * @param string $query_var Query variable key.
 775       * @param mixed $value Query variable value.
 776       */
 777  	function set($query_var, $value) {
 778          $this->query_vars[$query_var] = $value;
 779      }
 780  
 781      /**
 782       * Retrieve queried object.
 783       *
 784       * If queried object is not set, then the queried object will be set from
 785       * the category, tag, taxonomy, posts page, single post, page, or author
 786       * query variable. After it is set up, it will be returned.
 787       *
 788       * @since 1.5.0
 789       * @access public
 790       *
 791       * @return object
 792       */
 793  	function get_queried_object() {
 794          if (isset($this->queried_object)) {
 795              return $this->queried_object;
 796          }
 797  
 798          $this->queried_object = NULL;
 799          $this->queried_object_id = 0;
 800  
 801          if ($this->is_link_category) {
 802              global $link_category,$link_category_id;
 803              $link_category_id = $this->get('link_category');
 804              $link_category = &get_link_category($link_category_id);
 805              if ( is_wp_error( $link_category ) ) return NULL;
 806              $this->queried_object = &$link_category;
 807              $this->queried_object_id = (int) $cat;
 808          } else if ($this->is_link_tag) {
 809              global $link_tag,$link_tag_id;
 810              if(!$link_tag_id = $this->get('link_tag_id')) {
 811                  $link_tag_slug = $this->get('link_tag');
 812                  $link_tag = get_term_by('slug',$link_tag_slug,'link_tag');
 813                  $link_tag_id = $link_tag->term_taxonomy_id ;
 814              }
 815              $link_tag = &get_term($link_tag_id, 'link_tag');
 816              if ( is_wp_error( $link_tag ) ) return NULL;
 817              $this->queried_object = &$link_tag;
 818              $this->queried_object_id = (int) $link_tag_id;
 819          } else if ($this->is_a_link) {
 820              global $link,$link_id;
 821              $link = $this->queried_object = $this->link;
 822              $link_id = $this->queried_object_id = (int) $this->link->link_id;
 823  
 824          }
 825          return $this->queried_object;
 826      }
 827  
 828      /**
 829       * Retrieve ID of the current queried object.
 830       *
 831       * @since 1.5.0
 832       * @access public
 833       *
 834       * @return int
 835       */
 836  	function get_queried_object_id() {
 837          $this->get_queried_object();
 838  
 839          if (isset($this->queried_object_id)) {
 840              return $this->queried_object_id;
 841          }
 842  
 843          return 0;
 844      }
 845  
 846  	function have_links() {
 847          if ($this->current_link + 1 < $this->link_count) {
 848              return true;
 849          } elseif ($this->current_link + 1 == $this->link_count && $this->link_count > 0) {
 850              do_action('loop_end');
 851              // Do some cleaning up after the loop
 852              $this->rewind_links();
 853          }
 854  
 855          $this->in_the_loop = false;
 856          return false;
 857      }
 858  
 859  	function rewind_links() {
 860          $this->current_link = -1;
 861          if ($this->link_count > 0) {
 862              $this->link = $this->links[0];
 863          }
 864      }
 865  
 866  	function the_link() {
 867          global $link;
 868          $this->in_the_loop = true;
 869          $link = $this->next_link();
 870          setup_linkdata($link);
 871  
 872          if ( $this->current_link == 0 ) // loop has just started
 873          do_action('loop_start');
 874      }
 875  
 876  	function next_link() {
 877  
 878          $this->current_link++;
 879  
 880          $this->link = $this->links[$this->current_link];
 881          return $this->link;
 882      }
 883  
 884  }
 885  
 886  
 887  /**
 888   * Retrieve variable in the zelist_query class.
 889   *
 890   * @see zelist_query::get()
 891   * @since 1.5.0
 892   * @uses $zelist_query
 893   *
 894   * @param string $var The variable key to retrieve.
 895   * @return mixed
 896   */
 897  function get_zelist_query_var($var) {
 898      global $zelist_query;
 899      if($zelist_query) return $zelist_query->get($var);
 900  }
 901  
 902  /**
 903   * Set query variable.
 904   *
 905   * @see zelist_query::set()
 906   * @since 2.2.0
 907   * @uses $zelist_query
 908   *
 909   * @param string $var Query variable key.
 910   * @param mixed $value
 911   * @return null
 912   */
 913  function set_zelist_query_var($var, $value) {
 914      global $zelist_query;
 915      if($zelist_query) return $zelist_query->set($var, $value);
 916  }
 917  
 918  
 919  function have_links() {
 920      global $zelist_query;
 921      if($zelist_query) return $zelist_query->have_links();
 922  }
 923  
 924  
 925  function the_link() {
 926      global $zelist_query;
 927      if($zelist_query) $zelist_query->the_link();
 928  }
 929  
 930  
 931  function rewind_links() {
 932      global $zelist_query;
 933      if($zelist_query) return $zelist_query->rewind_links();
 934  }
 935  
 936  /**
 937   * Set is_links = true
 938   */
 939  function it_is_links() {
 940      global $zelist_query;
 941      if(!isset($zelist_query)) $zelist_query = new zelist_query;
 942      $zelist_query->is_links = true;
 943      do_action('it_is_links');
 944  }
 945  
 946  /**
 947   * Tests wether we are on a links page
 948   * @return (bool)
 949   */
 950  function is_links() {
 951      global $zelist_query;
 952      if(isset($zelist_query)) return $zelist_query->is_links;
 953      return false;
 954  }
 955  
 956  function is_a_link() { // is_link() is a reserved function name for PHP, see http://www.php.net/manual/en/function.is-link.php
 957      global $zelist_query;
 958      if(isset($zelist_query)) return $zelist_query->is_a_link;
 959      return false;
 960  }
 961  
 962  function is_link_category() {
 963      global $zelist_query;
 964      if(isset($zelist_query)) return $zelist_query->is_link_category;
 965      return false;
 966  }
 967  
 968  function is_link_tag() {
 969      global $zelist_query;
 970      if(isset($zelist_query)) return $zelist_query->is_link_tag;
 971      return false;
 972  }
 973  function is_link_home() {
 974      global $zelist_query;
 975      if(isset($zelist_query)) return $zelist_query->is_link_home;
 976      return false;
 977  }
 978  function is_link_search() {
 979      global $zelist_query;
 980      if(isset($zelist_query)) return $zelist_query->is_link_search;
 981      return false;
 982  }


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