[ Index ]

PHP Cross Reference of zeList

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Handles the AJAX call to clean descriptions (pretty invasive, use with care)
   5   * @return unknown_type
   6   */
   7  function zelist_ajax_clean_descriptions() {
   8    check_ajax_referer( 'actions');
   9    global $wpdb;
  10    $links = $wpdb->get_results("SELECT link_id, link_description FROM $wpdb->links");
  11    /* WHERE link_description LIKE '%&lt;%' OR link_description LIKE '%<%' */
  12    foreach($links as $link) {
  13      //echo "<br /><br />\n\n\n__________________________________________________________\nlink $link->link_id";
  14      //echo "<br />\n\n$link->link_description";
  15      $desc = $link->link_description;
  16      if(empty($desc)) continue;
  17  
  18      $desc = wp_specialchars_decode($desc);
  19      $desc = preg_replace('#</?font.*?>#is','',$desc);
  20      foreach(array('script','style','body','html','o:p','tbody','tfoot','thead','meta','title','col','table','tr','td','th','h','a','span','div','p') as $tag) {
  21        $desc = preg_replace("#<$tag.*?>(.*?)</$tag>#is",'$1',$desc);
  22        $desc = preg_replace("#</?$tag.*?>#is",'',$desc); // orphans
  23      }
  24      $desc = preg_replace('#<!--.*?-->#','',$desc);
  25  
  26      $charset = strtoupper(get_bloginfo('charset'));
  27      if(PHP_VERSION >= 5) $desc = html_entity_decode($desc,ENT_QUOTES,$charset);
  28      $desc = trim($desc);
  29      //echo "<br />DESC = \n_____\n$desc\n_____\n";
  30      $query = $wpdb->prepare("UPDATE $wpdb->links SET link_description = %s WHERE link_id = %d",$desc,$link->link_id);
  31      //echo "<br />$query\n_____\n";
  32      $wpdb->query($query);
  33  
  34    }
  35    echo '<div class="tip">'.sprintf(__('%d descriptions cleaned','zelist'),count($links)).'</div>';
  36    die();
  37  
  38  }
  39  add_action( 'wp_ajax_clean-descriptions','zelist_ajax_clean_descriptions');
  40  
  41  function zelist_ajax_inline_link_save() {
  42    check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
  43  
  44    if ( ! isset($_POST['link_id']) || ! ( $link_id = (int) $_POST['link_id'] ) ) exit;
  45  
  46    if ( ! current_user_can( 'edit_links', $link_id ) ) die( __('You are not allowed to edit this post.') );
  47  
  48    $data = &$_POST;
  49    $link = get_bookmark( $link_id, ARRAY_A );
  50    $data['link_description'] = $link['link_description'];
  51    $data['link_url'] = $link['link_url'];
  52  
  53    // rename
  54    $data['user_ID'] = $GLOBALS['user_ID'];
  55  
  56    // status
  57    $data['link_status'] = $data['_status'];
  58    unset($data['_status']);
  59  
  60    unset($data['user_ID']);
  61    unset($data['action']);
  62    unset($data['post_view']);
  63  
  64    // update the post
  65    $_POST = $data;
  66  
  67    edit_link($_POST['link_id']);
  68  
  69    $link = array();
  70    $mode = $_POST['post_view'];
  71    $link[] = get_bookmark($_POST['link_id']);
  72    link_rows($link);
  73    exit;
  74  }
  75  add_action( 'wp_ajax_inline-link-save','zelist_ajax_inline_link_save');
  76  
  77  function zelist_ajax_get_linktagcloud() {
  78  
  79    if ( !current_user_can( 'manage_categories' ) )
  80    die('-1');
  81  
  82    $ok = 0;
  83    $data = '';
  84  
  85    $tags = get_terms('link_tag', array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
  86  
  87    if ( empty( $tags ) ){
  88      $warning = __('No tags found!');
  89    }
  90    elseif ( is_wp_error($tags) ) {
  91      $warning = $tags->get_error_message();
  92    }
  93    else {
  94      foreach ( $tags as $key => $tag ) {
  95        $tags[ $key ]->link = '#';
  96        $tags[ $key ]->id = $tag->term_id;
  97      }
  98      $data = wp_generate_tag_cloud( $tags );
  99      $ok = 1;
 100    }
 101  
 102    echo json_encode(compact('ok','data'));
 103    die();
 104  }
 105  add_action( 'wp_ajax_get-linktagcloud','zelist_ajax_get_linktagcloud');
 106  
 107  /**
 108   * Ajax response for Ajax link_tag search
 109   * @return unknown_type
 110   */
 111  function zelist_ajax_link_tag_search() {
 112    global $wpdb;
 113    $s = $_GET['q']; // is this slashed already?
 114  
 115    if ( false !== strpos( $s, ',' ) ) {
 116      $s = explode( ',', $s );
 117      $s = $s[count( $s ) - 1];
 118    }
 119    $s = trim( $s );
 120    $s = addslashes($s);
 121  
 122    if ( strlen( $s ) < 2 )
 123    die; // require 2 chars for matching
 124    $results = $wpdb->get_col( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'link_tag' AND t.name LIKE ('%". $s . "%')" );
 125    echo join( $results, "\n" );
 126    die;
 127  }
 128  add_action( 'wp_ajax_ajax-link-tag-search','zelist_ajax_link_tag_search');
 129  
 130  /**
 131   * Ajax response for adding a new link_category (with parent)
 132   * @return unknown_type
 133   */
 134  function zelist_add_link_category() {
 135    check_ajax_referer( $action );
 136    if ( !current_user_can( 'manage_categories' ) )
 137    die('-1');
 138    $names = explode(',', $_POST['newcat']);
 139    $x = new WP_Ajax_Response();
 140    foreach ( $names as $cat_name ) {
 141      $cat_name = trim($cat_name);
 142      $slug = sanitize_title($cat_name);
 143      if ( '' === $slug )
 144      continue;
 145      if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
 146        $args = array('parent' => $_POST['newcat_parent']);
 147        $cat_id = wp_insert_term( $cat_name, 'link_category', $args );
 148      }
 149      $cat_id = $cat_id['term_id'];
 150      $cat_name = wp_specialchars(stripslashes($cat_name));
 151      $x->add( array(
 152              'what' => 'link-category',
 153              'id' => $cat_id,
 154              'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_name</label></li>",
 155              'position' => -1
 156      ) );
 157    }
 158    $x->send();
 159  
 160  }
 161  add_action('wp_ajax_extended-add-link-category','zelist_add_link_category');
 162  
 163  function zelist_ajax_updatecounts() {
 164    check_ajax_referer( 'actions' );
 165    header('Content: text/html;');
 166    global $wpdb;
 167  
 168    /*
 169     * What are you doing, Dave ?
 170     *
 171     $tags =  $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'link_tag'");
 172     wp_update_term_count($tags, 'link_tag');
 173     echo '<div class="success">'.sprintf(__('%d link tags updated','zelist'),count($tags)).'</div>';
 174  
 175     $categories =  $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'link_category'");
 176     wp_update_term_count($categories, 'link_category');
 177     echo '<div class="success">'.sprintf(__('%d link categories updated','zelist'),count($categories)).'</div>';
 178     *
 179     */
 180    zelist_update_link_counts();
 181  
 182    echo '<div class="success">'.__('Link categories and link tags counts updated','zelist').'</div>';
 183    die();
 184  
 185  }
 186  add_action( 'wp_ajax_updatecounts','zelist_ajax_updatecounts');
 187  
 188  
 189  
 190  function zelist_ajax_clean_tags() {
 191    check_ajax_referer( 'actions' );
 192    header('Content: text/html;');
 193  
 194    // update before deleting 0-count !
 195    zelist_update_link_counts();
 196  
 197    global $wpdb;
 198    $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE taxonomy = 'link_tag' AND count = 0");
 199    echo '<div class="tip">';
 200    printf(__('%d links tags cleaned','zelist'),$wpdb->rows_affected);
 201    echo '</div>';
 202    die();
 203  }
 204  add_action( 'wp_ajax_clean-tags','zelist_ajax_clean_tags');
 205  
 206  
 207  function zelist_ajax_deadlinks() {
 208    check_ajax_referer( 'actions' );
 209    header("Content: text/html;");
 210  
 211    $args = $_POST['args'];
 212    $defaults = array('unpublish' => 'on');
 213    $args = wp_parse_args($args,$defaults);
 214    extract($args,EXTR_OVERWRITE);
 215  
 216    global $wpdb;
 217    $date = date('Y-m-d',strtotime("-7 days"));
 218    $limit = 50;
 219    $query = "SELECT SQL_CALC_FOUND_ROWS $wpdb->links.link_id, $wpdb->links.link_status, $wpdb->links.link_url FROM $wpdb->links "
 220    ."WHERE $wpdb->links.link_id NOT IN (SELECT $wpdb->links.link_id FROM $wpdb->links "
 221    ."JOIN $wpdb->linkmeta ON $wpdb->linkmeta.link_id = $wpdb->links.link_id "
 222    ."WHERE $wpdb->linkmeta.meta_key = 'date_checked' "
 223    ."AND $wpdb->linkmeta.meta_value > '$date' "
 224    .")"
 225    ." AND $wpdb->links.link_status <> 'deny' "
 226    ."LIMIT $limit";
 227  
 228    $links = $wpdb->get_results($query);
 229  
 230    if(!$links) {
 231      echo '<div class="tip">'.__('All links were checked in the last 7 days','zelist').'</div>';
 232      die('');
 233    }
 234  
 235    $total = $wpdb->get_var("SELECT FOUND_ROWS();");
 236    if($total > count($links)) {
 237      echo '<div class="tip">';
 238      printf(__('Checking %d links on %d','zelist'),count($links),$total);
 239      echo '</div>';
 240    }
 241  
 242    echo '<input type="hidden" name="unpublish" id="unpublish" ';
 243    checked( $unpublish,'on');
 244    echo ' />';
 245    echo '<ul id="ajax_deadlinks">';
 246    wp_nonce_field('deadlink','nonce_deadlink');
 247    foreach($links as $link) {
 248      echo "<li id='link-$link->link_id'>
 249          <input type='hidden' id='url-$link->link_id' name='url-$link->link_id' value='$link->link_url' />
 250          <input type='hidden' id='status-$link->link_id' name='status-$link->link_id' value='$link->link_status' />
 251          &nbsp;#$link->link_id&nbsp;<a target='_blank' href='$link->link_url'>$link->link_url</a>
 252          &nbsp;/&nbsp;<span class='edit'><a href='".get_edit_bookmark_link($link->link_id)."'>".__('Edit')."</a></span>
 253          <img class='response' src='".admin_url('images/loading.gif')."' />
 254          </li>";
 255    }
 256    echo '</ul>';
 257  
 258  
 259    $img_ok = admin_url('images/yes.png');
 260    $img_nok = admin_url('images/no.png');
 261    ?>
 262  
 263  <script type="text/javascript"><!--
 264  jQuery(document).ready( function($) {
 265  var img_ok = '<?php echo $img_ok; ?>';
 266  var img_nok = '<?php echo $img_nok; ?>';
 267  
 268      jQuery('#ajax_deadlinks li').each(function(e) {
 269          var id = jQuery(this).attr('id');
 270          var link_id = id.substring(5);
 271          var link_url = jQuery('#url-' + link_id).val();
 272          var link_status = jQuery('#status-' + link_id).val();
 273  
 274          jQuery.ajax({
 275              type: 'POST',
 276              url: 'admin-ajax.php',
 277              dataType: 'json',
 278              data: {
 279                  action : 'deadlink',
 280                  link_id: link_id,
 281                  link_url: link_url,
 282                  link_status: link_status,
 283                  unpublish: jQuery('#unpublish').val(),
 284                  _ajax_nonce : jQuery('#nonce_deadlink').val()
 285          },
 286              success: function(response) {
 287              if(response.alive) {
 288                  jQuery('#' + id + ' img.response').attr('src',img_ok);
 289                  jQuery('li#link-' + link_id).addClass('success');
 290                  }
 291              else {
 292                  jQuery('#' + id + ' img.response').attr('src',img_nok);
 293                  jQuery('li#link-' + link_id).addClass('warning');
 294                  var error_string = '';
 295                  if(response.request.response) error_string = response.request.response.code + ' ' + response.request.response.message;
 296                  else if(response.request.errors) {
 297                      for(var error_type in response.request.errors) {
 298                          error_string = error_type + ' : ' + response.request.errors[error_type] + "\n";
 299                  }
 300                  }
 301                  if(error_string) jQuery('li#link-' + link_id).append('<span class="dead">' + error_string + '</span>');
 302  
 303                  }
 304              },
 305              error: function(response) {
 306                  jQuery('#' + id + ' img.response').attr('src',img_nok);
 307                  jQuery('li#link-' + link_id).addClass('warning');
 308                  if(response.responseText) jQuery('li#link-' + link_id).append('<span class="dead">' + response.responseText + '</span>');
 309  
 310              }
 311  
 312          });
 313      });
 314  });
 315  //--></script>
 316    <?php
 317    die('');
 318  }
 319  add_action( 'wp_ajax_deadlinks','zelist_ajax_deadlinks');
 320  
 321  
 322  
 323  function zelist_ajax_deadlink() {
 324    check_ajax_referer( 'deadlink' );
 325    header('Content-type: text/json');
 326    $link_id = absint($_POST['link_id']);
 327    $link_url = trim($_POST['link_url']);
 328    $unpublish = (isset($_POST['link_url']) && $_POST['link_url'] == 'off') ? 0 : 1;
 329  
 330    $message = " on lance ";
 331    $request = wp_remote_request($link_url,'blocking=1');
 332    if(is_wp_error($request) || !isset($request['response']) || !isset($request['response']['message']) || $request['response']['message'] != 'OK') {
 333      $alive = 0;
 334    }
 335    else $alive = 1;
 336  
 337    $message .= "alive = $alive";
 338    update_link_meta($link_id, 'alive',$alive);
 339    update_link_meta($link_id, 'date_checked', date('Y-m-d'));
 340    $message .= " metas updated";
 341    if(!$alive && $unpublish)  link_status($link_id,'dead');
 342    $message .= " metas updated";
 343  
 344    //file_put_contents(ZELIST_PATH.'/log_check_url.log',date('Y-m-d H:i:i')."@$link_id@$link_url=$alive=$query\n",FILE_APPEND);
 345    // full log by url
 346    if($debug && !$alive) file_put_contents(ZELIST_PATH.'/log_check_url_'.urlencode($link_url).'.log',"$link_id@$link_url=".print_r($request,1));
 347    echo json_encode(compact('alive','request','message'));
 348    die();
 349  }
 350  add_action( 'wp_ajax_deadlink','zelist_ajax_deadlink');
 351  
 352  
 353  
 354  function zelist_ajax_create_owner() {
 355    check_ajax_referer( 'owner_add' );
 356    header('Content-type: text/html');
 357  
 358    $email = $_POST['email'];
 359    if(is_email($email)) {
 360      if($owner = get_user_by_email($email)) $owner_id = $owner->ID;
 361      else $owner_id = wp_create_user($email, wp_generate_password(),$email);
 362      $_POST['link_owner'] = $owner_id;
 363    }
 364    $link_id = absint($_POST['link_id']);
 365    $link = get_bookmark($link_id);
 366    if($owner_id) link_owner($link_id,$owner_id);
 367    link_owner_meta_box($link);
 368    die();
 369  }
 370  add_action('wp_ajax_instant_create_owner','zelist_ajax_create_owner');
 371  
 372  
 373  /**
 374   * Extended Ajax add link category (with parent category)
 375   * @return unknown_type
 376   */
 377  function zelist_ajax_add_link_category() {
 378    check_ajax_referer( 'add-link-category' );
 379    if ( !current_user_can( 'manage_categories' ) ) die('-1');
 380  
 381    $x = new WP_Ajax_Response();
 382    if ( '' === trim($_POST['newcat']) ) {
 383      $x = new WP_Ajax_Response( array(
 384              'what' => 'cat',
 385              'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
 386      ) );
 387      $x->send();
 388    }
 389  
 390    $names = explode(',', $_POST['newcat']);
 391    foreach ( $names as $cat_name ) {
 392      $cat_name = trim($cat_name);
 393      $slug = sanitize_title($cat_name);
 394      if ( '' === $slug ) continue;
 395  
 396      if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
 397        $args = array('parent' => absint($_POST['parent']), 'slug' => $slug);
 398        $cat_id = wp_insert_term( $cat_name, 'link_category', $args);
 399      }
 400  
 401      $cat_id = $cat_id['term_id'];
 402      $cat_full_name = $cat_name = wp_specialchars(stripslashes($cat_name));
 403      $category = get_link_category($cat_id);
 404      $_cat = $category;
 405      while ( $_cat->parent > 0 ) {
 406        $_cat = get_link_category($category->parent);
 407        $cat_full_name = $_cat->name . ' &#8212; ' . $cat_full_name;
 408      }
 409  
 410      if($_POST['multiple']) $type = 'checkbox';
 411      else $type = 'radio';
 412  
 413      $x->add( array(
 414              'what' => 'link-category',
 415              'id' => $cat_id,
 416              'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='$cat_id' type='$type' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_full_name</label></li>",
 417              'position' => -1
 418      ));
 419    }
 420    $x->send();
 421  }
 422  add_action('wp_ajax_add-zelist-link-category','zelist_ajax_add_link_category');
 423  
 424  
 425  /**
 426   * Show directory activity on Right Now Box
 427   * @return unknown_type
 428   */
 429  function zelist_activity_report()
 430  {
 431    $num_cats  = wp_count_terms('link_category');
 432    $num_sites = wp_count_links();
 433    ?>
 434  <tr>
 435      <td class="t" colspan="4"
 436          style="padding-left: 10px; border-top: #ececec 4px double;"><?php printf(__('zeList version <span class="b">%s</span>','zelist'),get_option('zelist_version')); ?>
 437      </td>
 438  </tr>
 439  <tr>
 440      <td class="b b_approved"><a
 441          href="<?php echo ZELIST_ADMIN_URL_MANAGER; ?>"><?php echo $num_sites->publish; ?></a></td>
 442      <td class="t approved"><?php _e('Published links','zelist'); ?> &nbsp;(<?php echo round(100 * $num_sites->publish/$num_sites->total,0); ?>
 443      %)</td>
 444      <td class="b b_sites_cats"><a
 445          href="<?php echo ZELIST_ADMIN_URL_CATEGORIES; ?>"><?php echo $num_cats; ?></a></td>
 446      <td class="t sites_cats"><?php _e('Categories','zelist'); ?></td>
 447  </tr>
 448  <tr>
 449      <td class="b b_waiting"><a
 450          href="<?php echo ZELIST_ADMIN_URL_MANAGER.'&link_status=pending'; ?>"><?php echo $num_sites->pending; ?></a></td>
 451      <td class="t waiting"><?php _e('Pending','zelist'); ?></td>
 452      <td class="b b_spam"><a
 453          href="<?php echo ZELIST_ADMIN_URL_MANAGER.'&link_status=deny'; ?>"><?php echo $num_sites->deny; ?></a></td>
 454      <td class="t spam"><?php _e('Denied','zelist'); ?> &nbsp;(<?php echo round(100 * $num_sites->deny/$num_sites->total,0); ?>
 455      %)</td>
 456  </tr>
 457  
 458    <?php
 459  }
 460  add_action('right_now_table_end','zelist_activity_report');


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