/**
  * Display a javascript collection for autocompletion script !
  *
  * @return void
  * @author Amaury Balmer
  */
 public static function ajax_local_tags()
 {
     status_header(200);
     // Send good header HTTP
     header("Content-Type: application/json; charset=" . get_bloginfo('charset'));
     $taxonomy = 'post_tag';
     if (isset($_REQUEST['taxonomy']) && taxonomy_exists($_REQUEST['taxonomy'])) {
         $taxonomy = $_REQUEST['taxonomy'];
     }
     if ((int) wp_count_terms($taxonomy, 'ignore_empty=false') == 0) {
         // No tags to suggest
         json_encode(array());
         exit;
     }
     // Prepare search
     $search = isset($_GET['term']) ? trim(stripslashes($_GET['term'])) : '';
     // Get all terms, or filter with search
     $terms = SimpleTags_Admin::getTermsForAjax($taxonomy, $search);
     if (empty($terms) || $terms == false) {
         json_encode(array());
         exit;
     }
     // Format terms
     $results = array();
     foreach ((array) $terms as $term) {
         $term->name = stripslashes($term->name);
         $term->name = str_replace(array("\r\n", "\r", "\n"), '', $term->name);
         $results[] = array('id' => $term->term_id, 'label' => $term->name, 'value' => $term->name);
     }
     echo json_encode($results);
     exit;
 }
 /**
  * Suggest tags from local database
  *
  */
 public static function ajax_suggest_local()
 {
     status_header(200);
     header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
     if ((int) wp_count_terms('post_tag', 'ignore_empty=false') == 0) {
         // No tags to suggest
         echo '<p>' . __('No terms in your WordPress database.', 'simpletags') . '</p>';
         exit;
     }
     // Get data
     $content = stripslashes($_POST['content']) . ' ' . stripslashes($_POST['title']);
     $content = trim($content);
     if (empty($content)) {
         echo '<p>' . __('No text was sent.', 'simpletags') . '</p>';
         exit;
     }
     // Get all terms
     $terms = SimpleTags_Admin::getTermsForAjax('post_tag', '');
     if (empty($terms) || $terms == false) {
         echo '<p>' . __('No results from your WordPress database.', 'simpletags') . '</p>';
         exit;
     }
     $flag = false;
     foreach ((array) $terms as $term) {
         $term = stripslashes($term->name);
         if (is_string($term) && !empty($term) && stristr($content, $term)) {
             $flag = true;
             echo '<span class="local">' . esc_html($term) . '</span>' . "\n";
         }
     }
     if ($flag == false) {
         echo '<p>' . __('No correspondance between your content and terms from the WordPress database.', 'simpletags') . '</p>';
     } else {
         echo '<div class="clear"></div>';
     }
     exit;
 }
 /**
  * Display a span list for click tags
  *
  * @return void
  * @author Amaury Balmer
  */
 public static function ajax_click_tags()
 {
     status_header(200);
     // Send good header HTTP
     header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
     if ((int) wp_count_terms('post_tag', 'ignore_empty=false') == 0) {
         // No tags to suggest
         echo '<p>' . __('No terms in your WordPress database.', 'simpletags') . '</p>';
         exit;
     }
     // Prepare search
     $search = isset($_GET['q']) ? trim(stripslashes($_GET['q'])) : '';
     $post_id = isset($_GET['post_id']) ? intval($_GET['post_id']) : 0;
     // Order tags before selection (count-asc/count-desc/name-asc/name-desc/random)
     $order_click_tags = strtolower(SimpleTags_Plugin::get_option_value('order_click_tags'));
     $order_by = $order = '';
     switch ($order_click_tags) {
         case 'count-asc':
             $order_by = 'tt.count';
             $order = 'ASC';
             break;
         case 'random':
             $order_by = 'RAND()';
             $order = '';
             break;
         case 'count-desc':
             $order_by = 'tt.count';
             $order = 'DESC';
             break;
         case 'name-desc':
             $order_by = 't.name';
             $order = 'DESC';
             break;
         default:
             // name-asc
             $order_by = 't.name';
             $order = 'ASC';
             break;
     }
     // Get all terms, or filter with search
     $terms = SimpleTags_Admin::getTermsForAjax('post_tag', $search, $order_by, $order);
     if (empty($terms) || $terms == false) {
         echo '<p>' . __('No results from your WordPress database.', 'simpletags') . '</p>';
         exit;
     }
     // Get terms for current post
     $post_terms = array();
     if ($post_id > 0) {
         $post_terms = wp_get_post_terms($post_id, 'post_tag', array('fields' => 'ids'));
     }
     foreach ((array) $terms as $term) {
         $class_current = in_array($term->term_id, $post_terms) ? 'used_term' : '';
         echo '<span class="local ' . $class_current . '">' . esc_html(stripslashes($term->name)) . '</span>' . "\n";
     }
     echo '<div class="clear"></div>';
     exit;
 }