[ Index ]

PHP Cross Reference of zeList

title

Body

[close]

/includes/ -> functions.php (source)

   1  <?php
   2  
   3  
   4  /**
   5   * Check wether a bookmark is visible or not
   6   * @param $link_id
   7   * @return (bool)
   8   */
   9  function link_is_visible($link_id) {
  10    global $wpdb;
  11    $link_id = absint($link_id);
  12    if ( ! $visibles = wp_cache_get( "visibles", 'links' ) ) {
  13      $visibles = $wpdb->get_col("SELECT link_id FROM $wpdb->links WHERE link_visible = 'Y'");
  14      wp_cache_add( "visibles", $visibles, 'links');
  15    }
  16    if(in_array($link_id,$visibles)) return true;
  17    else return false;
  18  }
  19  
  20  
  21  /**
  22   * Creating and hacking link taxonomies
  23   * @return unknown_type
  24   */
  25  function zelist_add_taxonomies() {
  26    global $wp_rewrite;
  27  
  28    $use_rewrite = get_option('zelist_rewrite');
  29    //$zelist_root_slug = str_replace(get_option('home'),'',get_permalink(get_option('zelist_root')));
  30  
  31    if(get_option('show_on_front') == 'pages' && get_option('page_on_front') == $zelist_root) $true_root = trailingslashit('');
  32    else {
  33      $zelist_page = get_post(get_option('zelist_root'));
  34      $true_root = trailingslashit($zelist_page->post_name);
  35    }
  36  
  37    $rewrite = ($use_rewrite) ? false : array('slug' => 'link_category');
  38    register_taxonomy('link_category','link',array(
  39          'hierarchical' => true,
  40          'rewrite' => $rewrite,
  41          'update_count_callback' => '_update_link_category_term_count',
  42    ));
  43  
  44    $rewrite = ($use_rewrite) ? false : array('slug' => "link_tag");
  45    register_taxonomy(
  46          'link_tag',
  47          'link',
  48    array(
  49              'name' => 'link_tag',
  50              'object_type' => 'link',
  51              'hierarchical' => false,
  52              'update_count_callback' => '_update_link_term_count',
  53              'rewrite' => $rewrite
  54    )
  55    );
  56  
  57    $rewrite = ($use_rewrite) ? false : array('slug' => 'link_name');
  58    register_taxonomy('link','link',array('name' => 'link','object_type' => 'link','rewrite' => $rewrite));
  59  
  60    if($use_rewrite) {
  61      $array = array(
  62      'link' => array("%link_id%", '([0-9]+)', 'link='),
  63      'link_name' => array("%link_name%", '([^/\.]+)', 'link_name='),
  64      'link_tag' => array("%link_tag%", '([^/]+)', 'link_tag='),
  65      'link_category' => array("%link_category%", '([^/]+)', 'link_category='),
  66      'link_category_name' => array("%link_category_name%", '([^\.]+)', 'link_category_name='),
  67      'link_search' => array("%ls%", '([^/]+)', 'ls='),
  68      );
  69      global $wp;
  70      foreach($array as $tag => $rewrite) {
  71        $wp->add_query_var($tag);
  72        $wp_rewrite->add_rewrite_tag($rewrite[0], $rewrite[1], "$tag=");
  73      }
  74      $root = untrailingslashit($root);
  75      if($use_rewrite == 1) {
  76        $zelist_root = get_post(get_option('zelist_root'));
  77        $slug = $zelist_root->post_name;
  78        $wp_rewrite->add_permastruct('link_tag', $true_root."tag/%link_tag%.htm",false);
  79        $wp_rewrite->add_permastruct('link', $true_root."%link_id%-%link_name%.htm",false);
  80        $wp_rewrite->add_permastruct('link_category', $true_root."%link_category_name%/",false);
  81      }
  82      elseif($use_rewrite == 2) {
  83        $wp_rewrite->add_permastruct('link', $true_root . get_option('zelist_rewrite_prefix_link').'/%link_category_name%/%link_id%-%link_name%.htm', false);
  84        $wp_rewrite->add_permastruct('link_tag', $true_root. get_option('zelist_rewrite_prefix_link_tag').'/%link_tag%/', false);
  85        $wp_rewrite->add_permastruct('link_category', $true_root . get_option('zelist_rewrite_prefix_link_category').'/%link_category_name%/', false);
  86  
  87      }
  88      $wp_rewrite->flush_rules();
  89    }
  90  }
  91  
  92  
  93  
  94  /**
  95   * Roles and Capabilities
  96   **/
  97  function zelist_link_capabilities() {
  98    // adding capabilities
  99    global $wp_roles;
 100    if(!$wp_roles) return;
 101  
 102    $administrator = $wp_roles->get_role('administrator');
 103    $editor = $wp_roles->get_role('editor');
 104    $author = $wp_roles->get_role('author');
 105    $contributor = $wp_roles->get_role('contributor');
 106  
 107    $administrator->add_cap('manage_links_advanced');
 108    $editor->add_cap('manage_links_advanced');
 109  
 110    $administrator->add_cap('edit_other_links');
 111    $editor->add_cap('edit_other_links');
 112  
 113    $administrator->add_cap('publish_links');
 114    $editor->add_cap('publish_links');
 115  
 116    $administrator->add_cap('pending_links');
 117    $editor->add_cap('pending_links');
 118  
 119    $administrator->add_cap('delete_links');
 120    $editor->add_cap('delete_links');
 121  
 122    $administrator->add_cap('deny_links');
 123    $editor->add_cap('deny_links');
 124  
 125    $administrator->add_cap('dead_links');
 126    $editor->add_cap('dead_links');
 127  
 128    $administrator->add_cap('manage_links');
 129    $editor->add_cap('manage_links');
 130    $author->add_cap('manage_links');
 131    $contributor->add_cap('manage_links');
 132  }
 133  
 134  /**
 135   * Allow a contributor(or more) to edit his own links
 136   * @return unknown_type
 137   */
 138  function zelist_user_can_edit_his_own_links() {
 139    list($allcaps,$caps,$args) = func_get_args();
 140    if($args[0] != 'edit_links') return $allcaps;
 141    if(current_user_can('edit_other_links')) return $args[0];
 142  
 143    $link = get_bookmark($args[2]);
 144    $current_user = wp_get_current_user();
 145    if($link->link_owner == $current_user->ID) return $args[0];
 146    return false;
 147  }
 148  add_filter('user_has_cap','zelist_user_can_edit_his_own_links',10,4);
 149  
 150  
 151  /**
 152   * Ajax action to check wether an URL is already known / responding / or not
 153   * @return unknown_type
 154   */
 155  function zelist_check_url() {
 156  
 157    $type = (isset($_POST['type'])) ? trim($_POST['type']) : 'url';
 158    check_ajax_referer( 'check_url' );
 159    header('Content-type: text/json');
 160  
 161    $data = '';
 162    $ok = 0;
 163  
 164    $skip_existing = ($_POST['skip_existing']) ? true: false;
 165  
 166    // CLEAN URL
 167    $url = trim($_POST['url']);
 168    $url = clean_url($url);
 169    $url = wp_specialchars_decode($url);
 170  
 171    if(!$url || empty($url)) {
 172      $data = __('Wrong URL','zelist');
 173      $r = compact('ok','data');
 174      echo json_encode($r);
 175      die();
 176    }
 177  
 178    // IF link_url, check if known
 179    if(!$skip_existing && $type == 'url') {
 180      global $wpdb;
 181      $parse = parse_url($url);
 182      $host = $parse['host'];
 183      $host = trim($wpdb->prepare("%s",$host),"'");
 184      $query = "SELECT link_id FROM $wpdb->links WHERE link_url LIKE '%$host%'";
 185      $link_id = $wpdb->get_var($query);
 186  
 187      if($link_id) {
 188        $data .= __('Link already known');
 189        if(link_is_visible($link_id)) {
 190          $data .= ' | ';
 191          $data .= sprintf(__('<a href="%s">View link</a>'),get_link_permalink($link_id));
 192        }
 193        if(current_user_can('edit_links',$link_id)) {
 194          $data .= ' | ';
 195          $data .= sprintf(__('<a href="%s">Edit link</a>'),get_edit_bookmark_link($link_id));
 196        }
 197        $url = wp_specialchars_decode($url);
 198        $r = compact('ok','data','url','query','link_id');
 199        echo json_encode($r);
 200        die();
 201      }
 202  
 203  
 204    }
 205  
 206    $args = apply_filters('zelist_remote_request',array());
 207    $request = wp_remote_request($url,$args);
 208    $error = apply_filters('zelist_check_url_request',$request,$url,$args);
 209  
 210    if(is_wp_error($error)) {
 211      foreach($error->errors as $reason => $msgs)  foreach($msgs as $msg)  $data .= "\n$reason : $msg";
 212    }
 213    else {
 214      $request = $error;
 215  
 216      if(isset($request['response']) && isset($request['response']['message']) && $request['response']['message'] == 'OK') {
 217        $ok = 1;
 218        $data .= __('URL responding, seems good !','zelist');
 219        if($type == 'rss') {
 220          if(isset($request['response']['headers']['content-type']) && stripos($request['response']['body'],'<?xml')) {
 221            $data .= '<br />'.__('Looks like XML','zelist');
 222          }
 223          // @TODO WP RSS would be great if only it could load a XML string...
 224          if(function_exists('simplexml_load_string')) {
 225            $xml = $request['body'];
 226            if($rss = @simplexml_load_string($xml)) {
 227  
 228  
 229            //$data .= '<pre>'.print_r($rss,1).'</pre>';
 230            if($rss->channel->title) $data .= '<h4>'.sprintf(__('Feed: %s','zelist'),$rss->channel->title).'</h4>';
 231            if($rss->channel->item) {
 232              $data .= '<ul class="rss">';
 233              foreach($rss->channel->item as $item) {
 234                $data .= '<li><a target="_blank" href="'.$item->link.'">'.$item->title.'</a> ('.sprintf(__('%s ago'),human_time_diff(strtotime($item->pubDate))).')</li>';
 235              }
 236              $data .= '</ul>';
 237            }
 238            }
 239            else {
 240              $data  = __('XML wrong formed','zelist');
 241              $ok = 0;
 242            }
 243          }
 244        }
 245      }
 246      else {
 247        if(isset($request['response'])) $data = implode(' ',$request['response']);
 248      }
 249    }
 250    $r = compact('ok','data','url','type');
 251    echo json_encode($r);
 252    die();
 253  }
 254  add_action( 'wp_ajax_check_url','zelist_check_url');
 255  
 256  /**
 257   * Ajax action on a new link submission
 258   * @return unknown_type
 259   */
 260  function zelist_create_link() {
 261    check_ajax_referer( 'create_link' );
 262    header('Content-type: text/json');
 263    $warning = 1;
 264    $data = '';
 265    $redirect = false;
 266  
 267    $_POST['link_url'] = wp_specialchars( $_POST['link_url'] );
 268    $_POST['link_url'] = clean_url($_POST['link_url']);
 269  
 270    $_POST['link_name'] = wp_specialchars( $_POST['link_name'] );
 271  
 272    global $wpdb;
 273    $link_id = $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE %s",$_POST['link_url']));
 274    if($link_id) {
 275      $data .= __('Link already known');
 276      if(link_is_visible($link_id)) {
 277        $data .= ' | ';
 278        $data .= sprintf(__('<a href="%s">View link</a>'),get_link_permalink($link_id));
 279      }
 280      if(current_user_can('edit_links',$link_id)) {
 281        $data .= ' | ';
 282        $data .= sprintf(__('<a href="%s">Edit link</a>'),get_edit_bookmark_link($link_id));
 283      }
 284      $r = compact('warning','data','redirect');
 285      echo json_encode($r);
 286      die();
 287    }
 288    $error = add_link();
 289  
 290    if ( is_wp_error($error) ) {
 291      $data = $error;
 292    }
 293    else {
 294      $warning = 0;
 295      $link_id = $error;
 296  
 297      // set pending as a default status
 298      link_status($link_id,'pending');
 299      $data = sprintf(__('Link created: <a href="%s">Edit link</a>','zelist'),get_edit_bookmark_link($link_id));
 300      $redirect = get_edit_bookmark_link($error);
 301    }
 302  
 303    $r = compact('warning','data','redirect');
 304    echo json_encode($r);
 305    die();
 306  }
 307  add_action( 'wp_ajax_create_link','zelist_create_link');
 308  
 309  
 310  // replacement html_entity_decode code from php.net
 311  // author: laurynas dot butkus at gmail dot com
 312  
 313  function html_entity_decode_utf8($string) {
 314    static $trans_tbl;
 315  
 316    // replace numeric entities
 317    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
 318    $string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string);
 319  
 320    // replace literal entities
 321    if (!isset($trans_tbl))
 322    {
 323      $trans_tbl = array();
 324  
 325      foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
 326      $trans_tbl[$key] = utf8_encode($val);
 327    }
 328  
 329    return strtr($string, $trans_tbl);
 330  }
 331  
 332  
 333  function sans_accent($string)
 334  {
 335    $accent  =  array('�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�');
 336    $noaccent=  array('a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','u','u','u','u','y','y','b','y','b','s');
 337    $complet = array_combine($accent,$noaccent);
 338    $string = utf8_decode($string);
 339    foreach($complet as $a => $b) $string = str_replace($a,$b,$string);
 340    $string = utf8_encode($string);
 341    return $string;
 342  }
 343  
 344  // Returns the utf string corresponding to the unicode value (from php.net, courtesy - romans@void.lv)
 345  function code2utf($num)
 346  {
 347    if ($num < 128) return chr($num);
 348    if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
 349    if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
 350    if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
 351    return '';
 352  }
 353  
 354  // PHP4 compatibility
 355  if(!function_exists('array_combine')) {
 356    function array_combine($arr1, $arr2) {
 357      $out = array();
 358  
 359      $arr1 = array_values($arr1);
 360      $arr2 = array_values($arr2);
 361  
 362      foreach($arr1 as $key1 => $value1) {
 363        $out[(string)$value1] = $arr2[$key1];
 364      }
 365  
 366      return $out;
 367    }
 368  }


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