[ Index ]

PHP Cross Reference of zeList

title

Body

[close]

/includes/ -> template-functions.php (source)

   1  <?php
   2  
   3  function zelist_the_front_content() {
   4    $post_id = get_option('zelist_root');
   5    $post = get_post($post_id);
   6    $content = $post->post_content;
   7    if(!strlen($content)) return;
   8  
   9    $content = apply_filters('zelist_front_content',$content,$post);
  10    $content = apply_filters('the_content',$content,$post);
  11    echo $content;
  12  
  13  }
  14  
  15  /**
  16   * Returns a list of link stats
  17   * @return unknown_type
  18   */
  19  function zelist_stats() {
  20    if ( ! $stats = wp_cache_get( "stats", 'links' ) ) {
  21      $stats = (array) wp_count_links();
  22  
  23      if(isset($stats['publish'])) {
  24        $stats['text_publish'] = sprintf(__('%d sites published','zelist'),$stats['publish']);
  25        $stats['text_publish_short'] = sprintf(__('%d sites','zelist'),$stats['publish']);
  26        $stats['ratio_published'] = round($stats['publish']/$stats['total'],4);
  27        $stats['text_ratio_published'] = sprintf(__('Accepted sites: %d%% ','zelist'),round(100*$stats['ratio_published'],0));
  28      }
  29      if(isset($stats['deny'])) {
  30        $stats['ratio_denied'] = round($stats['deny']/$stats['total'],4);
  31        $stats['text_ratio_denied'] = sprintf(__('Denied sites: %d%% ','zelist'),round(100*$stats['ratio_denied'],0));
  32      }
  33      if(isset($stats['pending'])) {
  34        $stats['text_pending'] = sprintf(__('%d sites pending review','zelist'),$stats['pending']);
  35      }
  36  
  37      $categories = get_categories('type=link');
  38      $stats['categories'] = count($categories);
  39      $stats['text_categories'] = sprintf(__('%d categories','zelist'),count($categories));
  40  
  41      global $wpdb;
  42      $stats['users'] = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users JOIN $wpdb->links ON $wpdb->links.link_owner = $wpdb->users.ID WHERE $wpdb->links.link_status = 'publish'");
  43      if($stats['users']) $stats['text_users'] = sprintf(__('%d webmasters','zelist'),$stats['users']);
  44  
  45      $stats['last_published'] = $wpdb->get_var("SELECT link_published FROM $wpdb->links WHERE link_status = 'publish' ORDER BY link_published DESC LIMIT 1");
  46      if($stats['last_published']) $stats['text_last_published'] = sprintf(__('Last site published %s ago','zelist'),human_time_diff(strtotime($stats['last_published'])));
  47  
  48      $stats['last_submitted'] = $wpdb->get_var("SELECT link_added FROM $wpdb->links ORDER BY link_added DESC LIMIT 1");
  49      if($stats['last_submitted']) $stats['text_last_submitted'] = sprintf(__('Last site submitted %s ago','zelist'),human_time_diff(strtotime($stats['last_submitted'])));
  50  
  51      wp_cache_add( "stats", $stats, 'links');
  52    }
  53    return $stats;
  54  }
  55  
  56  
  57  /**
  58   * Used to display a breadcrumb on links and links categories pages
  59   * @param bool $echo echo or not
  60   * @param string $class class of the div or list
  61   * @param bool $home add a link to directory home or not
  62   * @param string $style list or flat
  63   * @param string $separator the string used between each element
  64   */
  65  function breadcrumbs($args = '')
  66  {
  67    $defaults = array(
  68      'echo' => 1,
  69      'class' => 'breadcrumbs',
  70      'home' => get_option('zelist_breadcrumbs_show_home'),
  71      'separator' => '&nbsp;>&nbsp;',
  72      'style' => 'flat'
  73      );
  74      $args = wp_parse_args( $args, $defaults );
  75      extract($args, EXTR_SKIP);
  76  
  77      $crumbs = array();
  78  
  79      if(is_links())
  80      {
  81        if(is_a_link())
  82        {
  83          global $link;
  84          $categories = wp_get_link_categories( $link->link_id);
  85          $current_cat = get_link_category($categories[0]);
  86        }
  87        elseif(is_link_category())
  88        {
  89          global $link_category_id;
  90          $current_cat = get_link_category($link_category_id);
  91        }
  92  
  93        if($current_cat)
  94        {
  95          if(is_wp_error($current_cat)) return false;
  96          $crumbs[] = '<a href="'.get_link_category_link($current_cat->cat_ID).'">'.$current_cat->name.'</a>';
  97          while($current_cat->parent != 0)
  98          {
  99            $link_category = get_link_category($current_cat->parent);
 100            $current_cat = $link_category;
 101            $crumbs[] = '<a href="'.get_link_category_link($current_cat->cat_ID).'">'.$current_cat->name.'</a>';
 102          }
 103        }
 104      }
 105  
 106      if(!$crumbs) return;
 107  
 108      if($home)
 109      {
 110        $crumbs[] = '<a href="'.get_permalink(get_option('zelist_root')).'">'.__('Directory','zelist').'</a>';
 111      }
 112  
 113      $crumbs = array_reverse($crumbs);
 114  
 115      if($style == 'list')
 116      {
 117        $html = '
 118      <ul';
 119        if($class) $html .= ' class="'.$class.'"';
 120        $html .= '>';
 121        foreach($crumbs as $crumb) $html .= '
 122        <li>'.$separator.$crumb.'</li>';
 123        $html .= '
 124      </ul>';
 125      }
 126      else {
 127        $html .= implode($separator,$crumbs);
 128      }
 129  
 130      $html = apply_filters('zelist_breadcrumbs',$html);
 131      if($echo) echo $html;
 132      else return $html;
 133  
 134  }
 135  
 136  
 137  
 138  function related_links($link_id = 0,$args = '') {
 139    $defaults = array(
 140           'limit' => 5,
 141           'hide_invisible' => 1,
 142           'echo' => 1,
 143           'style' => 'list',
 144          'show_excerpt' => 1,
 145          'show_description' => 0,
 146          'excerpt_length' => false,
 147           'show_score' => false,
 148           'return' => 0, // array return
 149          'title_li' => __('Related links','zelist'),
 150  
 151    );
 152    $args = wp_parse_args( $args, $defaults );
 153    extract($args, EXTR_OVERWRITE);
 154  
 155    if(current_user_can('manage_options')) $show_score = true;
 156  
 157    if($show_description) $show_excerpt = 0;
 158    $limit = absint($limit);
 159  
 160    if($link_id) $link = get_link($link_id);
 161    else global $link;
 162  
 163    if(!$link) return;
 164  
 165    $link_tags = wp_get_object_terms($link_id, 'link_tag', 'fields=name');
 166  
 167    $match = '';
 168    if(substr_count($link->link_name,' ') > 4) $match .= $link->link_name;
 169    $match .=  ' '.implode(' ',$link_tags);
 170    $match .= ' '.$link->link_description;
 171  
 172    $match = html_entity_decode_utf8($match);
 173    $match = strip_tags(strtolower($match));
 174    $match = sans_accent($match);
 175    $match = preg_replace('#([^\w]+)#',' ',$match);
 176    $match = str_replace(array("\s\s","\r\n","\n")," ",$match);
 177    $words = explode(' ',$match);
 178    $match = array();
 179    foreach($words as $word) {
 180      if(strlen($word) > 3) $match[] = $word;
 181    }
 182    $match = implode(' ',$match);
 183  
 184    $match = (strlen($match) > 200) ?  substr($match,0,strrpos(substr($match,0,200),' ')) : $match;
 185  
 186  
 187    global $wpdb;
 188    $query = $wpdb->prepare(
 189      "SELECT link_id, MATCH(link_name, link_description) AGAINST (%s) as score FROM $wpdb->links "
 190    ."WHERE  MATCH(link_name,link_description) AGAINST(%s) ",$match,$match);
 191    if($hide_invisible) $query .= "AND link_status = 'publish' AND link_id <> $link->link_id ";
 192    $query .= "ORDER BY score DESC LIMIT $limit";
 193  
 194    $results = $wpdb->get_results($query);
 195    if(!$results) return false;
 196  
 197    foreach($results as $result) {
 198      $related_links_ids[] = $result->link_id;
 199      $scores[$result->link_id] = $result->score;
 200    }
 201    $related_links = zelist_get_bookmarks('include='.implode(',',$related_links_ids));
 202    $related_links = apply_filters('zelist_related_links_links',$related_links,$scores);
 203  
 204    $results = $related_links;
 205    $related_links = array();
 206    foreach($results as $i => $related_link) {
 207      $related_link->score = $scores[$related_link->link_id];
 208      $related_links[$related_link->score] = $related_link;
 209    }
 210    unset($results);
 211    krsort($related_links);
 212  
 213    // array return
 214    if($return == 1) {
 215      return $related_links;
 216    }
 217  
 218    $output = '';
 219    if($style == 'list') $output .= '<ul class="related">';
 220    else $output .= '<div class="related">';
 221    if(!empty($title_li)) {
 222      if($style == 'list') $output .= '<li class="title">'.$title_li.'</li>';
 223      else $output .= '<h3>'.$title_li.'</h3>';
 224    }
 225  
 226    foreach($related_links as $related_link) {
 227      $output .= "\n";
 228      if($style == 'list') $output .= '<li>';
 229      $output .= '<a href="'.get_link_permalink($related_link->link_id).'">'.$related_link->link_name.'</a>';
 230      if($show_score) $output .= ' ('.number_format($scores[$related_link->link_id],2).')';
 231      if($show_description) $output .= '<p>'.$related_link->link_description.'</p>';
 232      if($show_excerpt) $output .= '<p>'.zelist_trim_excerpt($related_link->link_description,$excerpt_length).'</p>';
 233      if($style == 'list') $output .= '</li>';
 234      else $output .= '<br />';
 235    }
 236    if($style == 'list') $output .= '</ul>';
 237    else $output .= '</div>';
 238  
 239    $output = apply_filters('zelist_related_links',$output,$related_links,$scores);
 240    if($echo) echo $output;
 241    else return $output;
 242  }
 243  


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