Example #1
0
 /**
  * Initialize Simple Tags client
  *
  * @return SimpleTags
  */
 public function __construct()
 {
     // Load translation
     add_action('init', array(__CLASS__, 'init_translation'));
     // Add pages in WP_Query
     if ((int) SimpleTags_Plugin::get_option_value('use_tag_pages') == 1) {
         add_action('init', array(__CLASS__, 'init'), 11);
         add_action('parse_query', array(__CLASS__, 'parse_query'));
     }
     // Call autolinks ?
     if ((int) SimpleTags_Plugin::get_option_value('auto_link_tags') == 1) {
         require STAGS_DIR . '/inc/class.client.autolinks.php';
         new SimpleTags_Client_Autolinks();
     }
     // Call related posts ?
     if ((int) SimpleTags_Plugin::get_option_value('active_related_posts') == 1) {
         require STAGS_DIR . '/inc/class.client.related_posts.php';
         new SimpleTags_Client_RelatedPosts();
     }
     // Call auto terms ?
     require STAGS_DIR . '/inc/class.client.autoterms.php';
     new SimpleTags_Client_Autoterms();
     // Call post tags ?
     require STAGS_DIR . '/inc/class.client.post_tags.php';
     new SimpleTags_Client_PostTags();
     return true;
 }
 /**
  * Build HTML of form
  *
  * @param object $post
  *
  * @return void
  * @author Amaury Balmer
  */
 public static function metabox($post)
 {
     // Get auto options
     $auto_options = get_option(STAGS_OPTIONS_NAME_AUTO);
     // Auto terms for this CPT ?
     if ((int) SimpleTags_Plugin::get_option_value('active_autotags') == 1 && isset($auto_options[$post->post_type]) && !empty($auto_options[$post->post_type])) {
         $meta_value = get_post_meta($post->ID, '_exclude_autotags', true);
         echo '<p>' . "\n";
         echo '<label><input type="checkbox" name="exclude_autotags" value="true" ' . checked($meta_value, true, false) . ' /> ' . __('Disable auto tags ?', 'simpletags') . '</label><br />' . "\n";
         echo '</p>' . "\n";
         echo '<input type="hidden" name="_meta_autotags" value="true" />';
     }
     $taxonomies = get_object_taxonomies($post->post_type);
     if ((int) SimpleTags_Plugin::get_option_value('auto_link_tags') == 1 && in_array('post_tag', $taxonomies)) {
         $meta_value = get_post_meta($post->ID, '_exclude_autolinks', true);
         echo '<p>' . "\n";
         echo '<label><input type="checkbox" name="exclude_autolinks" value="true" ' . checked($meta_value, true, false) . ' /> ' . __('Disable auto links ?', 'simpletags') . '</label><br />' . "\n";
         echo '</p>' . "\n";
         echo '<input type="hidden" name="_meta_autolink" value="true" />';
     }
 }
 public static function set_default_option()
 {
     self::$options = (array) (include STAGS_DIR . '/inc/helper.options.default.php');
     self::update_option();
 }
 /**
  * Generate extended tag cloud
  *
  * @param string $args
  * @return string|array
  */
 public static function extendedTagCloud($args = '', $copyright = true)
 {
     $defaults = array('taxonomy' => 'post_tag', 'size' => 'true', 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'color' => 'true', 'maxcolor' => '#000000', 'mincolor' => '#CCCCCC', 'number' => 45, 'format' => 'flat', 'selectionby' => 'count', 'selection' => 'desc', 'orderby' => 'random', 'order' => 'asc', 'exclude' => '', 'include' => '', 'limit_days' => 0, 'min_usage' => 0, 'notagstext' => __('No tags.', 'simpletags'), 'xformat' => __('<a href="%tag_link%" id="tag-link-%tag_id%" class="st-tags t%tag_scale%" title="%tag_count% topics" %tag_rel% style="%tag_size% %tag_color%">%tag_name%</a>', 'simpletags'), 'title' => __('<h4>Tag Cloud</h4>', 'simpletags'), 'category' => 0);
     // Get options
     $options = SimpleTags_Plugin::get_option();
     // Get values in DB
     $defaults['taxonomy'] = $options['cloud_taxonomy'];
     $defaults['selectionby'] = $options['cloud_selectionby'];
     $defaults['selection'] = $options['cloud_selection'];
     $defaults['orderby'] = $options['cloud_orderby'];
     $defaults['order'] = $options['cloud_order'];
     $defaults['number'] = $options['cloud_limit_qty'];
     $defaults['notagstext'] = $options['cloud_notagstext'];
     $defaults['title'] = $options['cloud_title'];
     $defaults['maxcolor'] = $options['cloud_max_color'];
     $defaults['mincolor'] = $options['cloud_min_color'];
     $defaults['largest'] = $options['cloud_max_size'];
     $defaults['smallest'] = $options['cloud_min_size'];
     $defaults['unit'] = $options['cloud_unit'];
     $defaults['xformat'] = $options['cloud_xformat'];
     $defaults['format'] = $options['cloud_format'];
     if (empty($args)) {
         $args = $options['cloud_adv_usage'];
     }
     $args = wp_parse_args($args, $defaults);
     // Get correct taxonomy ?
     $taxonomy = self::_get_current_taxonomy($args['taxonomy']);
     // Get terms
     $terms = self::getTags($args, $taxonomy);
     extract($args);
     // Params to variables
     // If empty use default xformat !
     if (empty($xformat)) {
         $xformat = $defaults['xformat'];
     }
     // Clean memory
     $args = array();
     $defaults = array();
     unset($args, $defaults);
     if (empty($terms)) {
         return SimpleTags_Client::output_content('st-tag-cloud', $format, $title, $notagstext, $copyright);
     }
     $counts = $terms_data = array();
     foreach ((array) $terms as $term) {
         $counts[$term->name] = $term->count;
         $terms_data[$term->name] = $term;
     }
     // Remove temp data from memory
     $terms = array();
     unset($terms);
     // Use full RBG code
     if (strlen($maxcolor) == 4) {
         $maxcolor = $maxcolor . substr($maxcolor, 1, strlen($maxcolor));
     }
     if (strlen($mincolor) == 4) {
         $mincolor = $mincolor . substr($mincolor, 1, strlen($mincolor));
     }
     // Check as smallest inferior or egal to largest
     if ($smallest > $largest) {
         $smallest = $largest;
     }
     // Scaling - Hard value for the moment
     $scale_min = 1;
     $scale_max = 10;
     $minval = min($counts);
     $maxval = max($counts);
     $minout = max($scale_min, 0);
     $maxout = max($scale_max, $minout);
     $scale = $maxval > $minval ? ($maxout - $minout) / ($maxval - $minval) : 0;
     // HTML Rel (tag/no-follow)
     $rel = SimpleTags_Client::get_rel_attribut();
     // Remove color marquer if color = false
     if ($color == 'false') {
         $xformat = str_replace('%tag_color%', '', $xformat);
     }
     // Remove size marquer if size = false
     if ($size == 'false') {
         $xformat = str_replace('%tag_size%', '', $xformat);
     }
     // Order terms before output
     // count, name, rand | asc, desc
     $orderby = strtolower($orderby);
     if ($orderby == 'count') {
         asort($counts);
     } elseif ($orderby == 'name') {
         uksort($counts, array(__CLASS__, 'uksortByName'));
     } else {
         // rand
         SimpleTags_Client::random_array($counts);
     }
     $order = strtolower($order);
     if ($order == 'desc' && $orderby != 'random') {
         $counts = array_reverse($counts);
     }
     $output = array();
     foreach ((array) $counts as $term_name => $count) {
         if (!is_object($terms_data[$term_name])) {
             continue;
         }
         $term = $terms_data[$term_name];
         $scale_result = (int) (($term->count - $minval) * $scale + $minout);
         $output[] = SimpleTags_Client::format_internal_tag($xformat, $term, $rel, $scale_result, $scale_max, $scale_min, $largest, $smallest, $unit, $maxcolor, $mincolor);
     }
     // Remove unused variables
     $counts = array();
     $terms = array();
     unset($counts, $terms, $term);
     return SimpleTags_Client::output_content('st-tag-cloud', $format, $title, $output, $copyright);
 }
    /**
     * public static function called on mass terms page
     *
     * @param string $taxonomy 
     * @return void
     * @author Amaury Balmer
     */
    public static function mass_terms_js($taxonomy = '')
    {
        // Get option
        $autocomplete_min = (int) SimpleTags_Plugin::get_option_value('autocomplete_min');
        ?>
		<script type="text/javascript">
			<!--
			st_init_autocomplete( '.autocomplete-input', "<?php 
        echo admin_url('admin-ajax.php?action=simpletags&st_action=helper_js_collection&taxonomy=' . $taxonomy);
        ?>
", <?php 
        echo $autocomplete_min;
        ?>
 );
			-->
		</script>
		<?php 
    }
 /**
  * Suggest tags from Tag4Site
  *
  */
 public static function ajax_tag4site()
 {
     status_header(200);
     header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
     // API Key ?
     if (SimpleTags_Plugin::get_option_value('tag4site_key') == '') {
         echo '<p>' . __('Tag4Site need an API key to work. You can register on service website to obtain a key and set it on Simple Tags options.', '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;
     }
     // Build params
     $response = wp_remote_post('http://api.tag4site.ru/', array('timeout' => 30, 'body' => array('api_key' => SimpleTags_Plugin::get_option_value('tag4site_key'), 'text' => $content, 'format' => 'json')));
     if (!is_wp_error($response) && $response != null) {
         if (wp_remote_retrieve_response_code($response) == 200) {
             $data = wp_remote_retrieve_body($response);
         }
     }
     $data = json_decode($data);
     $code = $data->code;
     if ($code > 0) {
         $err = $data->error;
         echo '<p>' . __('Tag4Site API error #' . $code . ': ' . $err, 'simpletags') . '</p>';
         exit;
     }
     $data = $data->tags;
     if (empty($data)) {
         echo '<p>' . __('No data from Tag4Site API. Try again later.', 'simpletags') . '</p>';
         exit;
     }
     foreach ((array) $data as $term) {
         echo '<span class="local">' . esc_html($term->name) . '</span>' . "\n";
     }
     echo '<div class="clear"></div>';
     exit;
 }
 /**
  * Generate current post tags
  *
  * @param string $args
  * @return string
  */
 public static function extendedPostTags($args = '', $copyright = true)
 {
     // Get options
     $options = SimpleTags_Plugin::get_option();
     // Default values
     $defaults = array('before' => __('Tags: ', 'simpletags'), 'separator' => ', ', 'after' => '<br />', 'post_id' => 0, 'inc_cats' => 0, 'xformat' => __('<a href="%tag_link%" title="%tag_name_attribute%" %tag_rel%>%tag_name%</a>', 'simpletags'), 'notagtext' => __('No tag for this post.', 'simpletags'), 'number' => 0, 'format' => '');
     // Get values in DB
     $defaults['before'] = $options['tt_before'];
     $defaults['separator'] = $options['tt_separator'];
     $defaults['after'] = $options['tt_after'];
     $defaults['inc_cats'] = $options['tt_inc_cats'];
     $defaults['xformat'] = $options['tt_xformat'];
     $defaults['notagtext'] = $options['tt_notagstext'];
     $defaults['number'] = (int) $options['tt_number'];
     if (empty($args)) {
         $args = $options['tt_adv_usage'];
     }
     // Extract data in variables
     $args = wp_parse_args($args, $defaults);
     extract($args);
     // If empty use default xformat !
     if (empty($xformat)) {
         $xformat = $defaults['xformat'];
     }
     // Clean memory
     $args = array();
     $defaults = array();
     // Choose post ID
     $object_id = (int) $post_id;
     if ($object_id == 0) {
         global $post;
         $object_id = (int) $post->ID;
         if ($object_id == 0) {
             return false;
         }
     }
     // Get categories ?
     $taxonomy = (int) $inc_cats == 0 ? 'post_tag' : array('post_tag', 'category');
     // Get terms
     $terms = get_object_term_cache($object_id, $taxonomy);
     if (false === $terms) {
         $terms = wp_get_object_terms($object_id, $taxonomy);
     }
     // Hook
     $terms = apply_filters('get_the_tags', $terms);
     // Limit to max quantity if set
     $number = (int) $number;
     if ($number != 0) {
         shuffle($terms);
         // Randomize terms
         $terms = array_slice($terms, 0, $number);
     }
     // Return for object format
     if ($format == 'object') {
         return $terms;
     }
     // If no terms, return text nothing.
     if (empty($terms)) {
         return $notagtext;
     }
     // HTML Rel
     $rel = SimpleTags_Client::get_rel_attribut();
     // Prepare output
     foreach ((array) $terms as $term) {
         if (!is_object($term)) {
             continue;
         }
         $output[] = SimpleTags_Client::format_internal_tag($xformat, $term, $rel, null);
     }
     // Clean memory
     $terms = array();
     unset($terms, $term);
     // Array to string
     if (is_array($output) && !empty($output)) {
         $output = implode($separator, $output);
     } else {
         $output = $notagtext;
     }
     // Add container
     $output = $before . $output . $after;
     return SimpleTags_Client::output_content('', 'string', '', $output, $copyright);
 }
 /**
  * Suggest tags from Zemanta
  *
  */
 public static function ajax_zemanta()
 {
     status_header(200);
     header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
     // API Key ?
     if (SimpleTags_Plugin::get_option_value('zemanta_key') == '') {
         echo '<p>' . __('Zemanta need an API key to work. You can register on service website to obtain a key and set it on Simple Tags options.', '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;
     }
     // Build params
     $response = wp_remote_post('http://api.zemanta.com/services/rest/0.0/', array('body' => array('method' => 'zemanta.suggest', 'api_key' => SimpleTags_Plugin::get_option_value('zemanta_key'), 'text' => $content, 'format' => 'json', 'return_rdf_links' => 0, 'return_images' => 0)));
     if (!is_wp_error($response) && $response != null) {
         if (wp_remote_retrieve_response_code($response) == 200) {
             $data = wp_remote_retrieve_body($response);
         }
     }
     $data = json_decode($data);
     $data = $data->keywords;
     if (empty($data)) {
         echo '<p>' . __('No results from Zemanta API.', 'simpletags') . '</p>';
         exit;
     }
     foreach ((array) $data as $term) {
         echo '<span class="local">' . esc_html($term->name) . '</span>' . "\n";
     }
     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;
 }
 /**
  * Generate related posts
  *
  * @param string $user_args
  *
  * @return string|array
  */
 public static function get_related_posts($user_args = '', $copyright = true)
 {
     global $wpdb;
     // Get options
     $options = SimpleTags_Plugin::get_option();
     $defaults = array('taxonomy' => 'post_tag', 'post_type' => 'post', 'number' => 5, 'order' => 'count-desc', 'format' => 'list', 'separator' => '', 'include_page' => 'true', 'exclude_posts' => '', 'exclude_terms' => '', 'post_id' => 0, 'excerpt_wrap' => 55, 'limit_days' => 0, 'min_shared' => 1, 'title' => __('<h4>Related posts</h4>', 'simpletags'), 'nopoststext' => __('No related posts.', 'simpletags'), 'dateformat' => get_option('date_format'), 'xformat' => __('<a href="%post_permalink%" title="%post_title% (%post_date%)">%post_title%</a> (%post_comment%)', 'simpletags'));
     // Get values in DB
     $defaults['number'] = $options['rp_limit_qty'];
     $defaults['order'] = $options['rp_order'];
     $defaults['nopoststext'] = $options['rp_notagstext'];
     $defaults['title'] = $options['rp_title'];
     $defaults['xformat'] = $options['rp_xformat'];
     $defaults['taxonomy'] = $options['rp_taxonomy'];
     if (empty($user_args)) {
         $user_args = $options['rp_adv_usage'];
     }
     // Replace old markers by new
     $markers = array('%date%' => '%post_date%', '%permalink%' => '%post_permalink%', '%title%' => '%post_title%', '%commentcount%' => '%post_comment%', '%tagcount%' => '%post_tagcount%', '%postid%' => '%post_id%');
     if (!is_array($user_args)) {
         $user_args = strtr($user_args, $markers);
     }
     $args = wp_parse_args($user_args, $defaults);
     extract($args);
     // If empty use default xformat !
     if (empty($xformat)) {
         $xformat = $defaults['xformat'];
     }
     // Clean memory
     $args = array();
     $defaults = array();
     // Get current post data
     $object_id = (int) $post_id;
     if ($object_id == 0) {
         global $post;
         $object_id = (int) $post->ID;
         if ($object_id == 0) {
             return false;
         }
     }
     // Get cache if exist
     $results = false;
     // Generate key cache
     $key = md5(maybe_serialize($user_args) . '-' . $object_id);
     if ($cache = wp_cache_get('related_posts' . $taxonomy, 'simpletags')) {
         if (isset($cache[$key])) {
             $results = $cache[$key];
         }
     }
     // If cache not exist, get datas and set cache
     if ($results === false || $results === null) {
         // Get get tags
         $current_terms = get_the_terms((int) $object_id, $taxonomy);
         if ($current_terms == false || is_wp_error($current_terms)) {
             return SimpleTags_Client::output_content('st-related-posts', $format, $title, $nopoststext, $copyright);
         }
         // Number - Limit
         $number = (int) $number;
         if ($number == 0) {
             $number = 5;
         } elseif ($number > 50) {
             $number = 50;
         }
         $limit_sql = 'LIMIT 0, ' . $number;
         unset($number);
         // Order tags before output (count-asc/count-desc/date-asc/date-desc/name-asc/name-desc/random)
         $order_by = '';
         $order = strtolower($order);
         switch ($order) {
             case 'count-asc':
                 $order_by = 'counter ASC, p.post_title DESC';
                 break;
             case 'random':
                 $order_by = 'RAND()';
                 break;
             case 'date-asc':
                 $order_by = 'p.post_date ASC';
                 break;
             case 'date-desc':
                 $order_by = 'p.post_date DESC';
                 break;
             case 'name-asc':
                 $order_by = 'p.post_title ASC';
                 break;
             case 'name-desc':
                 $order_by = 'p.post_title DESC';
                 break;
             default:
                 // count-desc
                 $order_by = 'counter DESC, p.post_title DESC';
                 break;
         }
         // Limit days - 86400 seconds = 1 day
         $limit_days = (int) $limit_days;
         $limit_days_sql = '';
         if ($limit_days != 0) {
             $limit_days_sql = 'AND p.post_date > "' . date('Y-m-d H:i:s', time() - $limit_days * 86400) . '"';
         }
         unset($limit_days);
         // Make array post type
         if (is_string($post_type)) {
             $post_type = explode(',', $post_type);
         }
         // Include_page
         $include_page = strtolower($include_page);
         if ($include_page == 'true') {
             $post_type[] = 'page';
         }
         unset($include_page);
         // Build post type SQL
         $restrict_sql = "AND p.post_type IN ('" . implode("', '", $post_type) . "')";
         // Restrict posts
         $exclude_posts_sql = '';
         if ($exclude_posts != '') {
             $exclude_posts = (array) explode(',', $exclude_posts);
             $exclude_posts = array_unique($exclude_posts);
             $exclude_posts_sql = "AND p.ID NOT IN (";
             foreach ($exclude_posts as $value) {
                 $value = (int) $value;
                 if ($value > 0 && $value != $object_id) {
                     $exclude_posts_sql .= '"' . $value . '", ';
                 }
             }
             $exclude_posts_sql .= '"' . $object_id . '")';
         } else {
             $exclude_posts_sql = "AND p.ID <> {$object_id}";
         }
         unset($exclude_posts);
         // Restricts tags
         $terms_to_exclude = array();
         if ($exclude_terms != '') {
             $exclude_terms = (array) explode(',', $exclude_terms);
             $exclude_terms = array_unique($exclude_terms);
             foreach ($exclude_terms as $value) {
                 $terms_to_exclude[] = trim($value);
             }
         }
         unset($exclude_terms);
         // SQL Terms list
         $term_list = array();
         foreach ((array) $current_terms as $term) {
             if (!in_array($term->name, $terms_to_exclude)) {
                 $term_list[] = '"' . (int) $term->term_id . '"';
             }
         }
         $term_list = implode(', ', $term_list);
         // Build SQL terms subqueries array
         $include_terms_sql = array();
         if (!empty($term_list)) {
             $include_terms_sql[$taxonomy] = $term_list;
         }
         // Group Concat check if post_relatedtags is used by xformat...
         $select_gp_concat = '';
         if (strpos($xformat, '%post_relatedtags%') || $min_shared > 1) {
             $select_gp_concat = ', GROUP_CONCAT(tt.term_id) as terms_id';
         }
         // Check if post_excerpt is used by xformat...
         $select_excerpt = '';
         //if ( strpos( $xformat, '%post_excerpt%' ) ) {
         //	$select_excerpt = ', p.post_content, p.post_excerpt, p.post_password';
         //}
         // If empty return no posts text
         if (empty($include_terms_sql)) {
             return SimpleTags_Client::output_content('st-related-posts', $format, $title, $nopoststext, $copyright);
         }
         // Posts: title, comments_count, date, permalink, post_id, counter
         $results = $wpdb->get_results("\n\t\t\t\tSELECT p.*, COUNT(tr.object_id) AS counter {$select_excerpt} {$select_gp_concat}\n\t\t\t\tFROM {$wpdb->posts} AS p\n\t\t\t\tINNER JOIN {$wpdb->term_relationships} AS tr ON (p.ID = tr.object_id)\n\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)\n\t\t\t\tWHERE 1 = 1\n\t\t\t\tAND (tt.taxonomy = '{$taxonomy}' AND tt.term_id IN ({$term_list}))\n\t\t\t\t{$exclude_posts_sql}\n\t\t\t\tAND p.post_status = 'publish'\n\t\t\t\tAND p.post_date_gmt < '" . current_time('mysql') . "'\n\t\t\t\t{$limit_days_sql}\n\t\t\t\t{$restrict_sql}\n\t\t\t\tGROUP BY tr.object_id\n\t\t\t\tORDER BY {$order_by}\n\t\t\t\t{$limit_sql}");
         $cache[$key] = $results;
         wp_cache_set('related_posts' . $taxonomy, $cache, 'simpletags');
     }
     if ($format == 'object' || $format == 'array') {
         return $results;
     } elseif ($results === false || empty($results)) {
         return SimpleTags_Client::output_content('st-related-posts', $format, $title, $nopoststext, $copyright);
     }
     if (empty($dateformat)) {
         $dateformat = get_option('date_format');
     }
     $output = array();
     // Replace placeholders
     foreach ((array) $results as $result) {
         if ($min_shared > 1 && count(explode(',', $result->terms_id)) < $min_shared || !is_object($result)) {
             continue;
         }
         $element_loop = $xformat;
         $post_title = apply_filters('the_title', $result->post_title);
         $element_loop = str_replace('%post_date%', mysql2date($dateformat, $result->post_date), $element_loop);
         $element_loop = str_replace('%post_permalink%', get_permalink($result), $element_loop);
         $element_loop = str_replace('%post_title%', $post_title, $element_loop);
         $element_loop = str_replace('%post_title_attribute%', esc_html(strip_tags($post_title)), $element_loop);
         $element_loop = str_replace('%post_comment%', (int) $result->comment_count, $element_loop);
         $element_loop = str_replace('%post_tagcount%', (int) $result->counter, $element_loop);
         $element_loop = str_replace('%post_id%', $result->ID, $element_loop);
         if (isset($result->terms_id)) {
             $element_loop = str_replace('%post_relatedtags%', self::get_tags_from_id($result->terms_id, $taxonomy), $element_loop);
         }
         if (isset($result->post_excerpt) || isset($result->post_content)) {
             $element_loop = str_replace('%post_excerpt%', self::get_excerpt_post($result->post_excerpt, $result->post_content, $result->post_password, $excerpt_wrap), $element_loop);
         }
         $output[] = $element_loop;
     }
     // Clean memory
     $results = array();
     unset($results, $result);
     return SimpleTags_Client::output_content('st-related-posts', $format, $title, $output, $copyright, $separator);
 }
 /**
  * Replace text by link, except HTML tag, and already text into link, use PregEXP.
  * 
  * @param string $content
  * @param string $search
  * @param string $replace
  * @param string $case
  * @param string $rel
  */
 private static function _replace_by_links_regexp(&$content, $search = '', $replace = '', $case = '', $rel = '')
 {
     $must_tokenize = true;
     // will perform basic tokenization
     $tokens = null;
     // two kinds of tokens: markup and text
     $j = 0;
     $filtered = '';
     // will filter text token by token
     $match = '/(\\PL|\\A)(' . preg_quote($search, "/") . ')(\\PL|\\Z)/u' . $case;
     $substitute = '$1<a href="' . $replace . '" class="st_tag internal_tag" ' . $rel . ' title="' . esc_attr(sprintf(SimpleTags_Plugin::get_option_value('auto_link_title'), $search)) . "\">\$2</a>\$3";
     //$match = "/\b" . preg_quote($search, "/") . "\b/".$case;
     //$substitute = '<a href="'.$replace.'" class="st_tag internal_tag" '.$rel.' title="'. esc_attr( sprintf( __('Posts tagged with %s', 'simpletags'), $search ) )."\">$0</a>";
     // for efficiency only tokenize if forced to do so
     if ($must_tokenize) {
         // this regexp is taken from PHP Markdown by Michel Fortin: http://www.michelf.com/projects/php-markdown/
         $comment = '(?s:<!(?:--.*?--\\s*)+>)|';
         $processing_instruction = '(?s:<\\?.*?\\?>)|';
         $tag = '(?:<[/!$]?[-a-zA-Z0-9:]+\\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)';
         $markup = $comment . $processing_instruction . $tag;
         $flags = PREG_SPLIT_DELIM_CAPTURE;
         $tokens = preg_split("{({$markup})}", $content, -1, $flags);
         $must_tokenize = false;
     }
     // there should always be at least one token, but check just in case
     $anchor_level = 0;
     if (isset($tokens) && is_array($tokens) && count($tokens) > 0) {
         $i = 0;
         foreach ($tokens as $token) {
             if (++$i % 2 && $token != '') {
                 // this token is (non-markup) text
                 if ($anchor_level == 0) {
                     // linkify if not inside anchor tags
                     if (preg_match($match, $token)) {
                         // use preg_match for compatibility with PHP 4
                         $j++;
                         if ($j <= SimpleTags_Plugin::get_option_value('auto_link_max_by_tag') || SimpleTags_Plugin::get_option_value('auto_link_max_by_tag') == 0) {
                             // Limit replacement at 1 by default, or options value !
                             $token = preg_replace($match, $substitute, $token);
                             // only PHP 5 supports calling preg_replace with 5 arguments
                         }
                         $must_tokenize = true;
                         // re-tokenize next time around
                     }
                 }
             } else {
                 // this token is markup
                 if (preg_match("#<\\s*a\\s+[^>]*>#i", $token)) {
                     // found <a ...>
                     $anchor_level++;
                 } elseif (preg_match("#<\\s*/\\s*a\\s*>#i", $token)) {
                     // found </a>
                     $anchor_level--;
                 }
             }
             $filtered .= $token;
             // this token has now been filtered
         }
         $content = $filtered;
         // filtering completed for this link
     }
 }
Example #12
0
 /**
  * Ouput formatted options
  *
  * @param array $option_data 
  * @return string
  * @author Amaury Balmer
  */
 public static function print_options($option_data)
 {
     // Get options
     $option_actual = SimpleTags_Plugin::get_option();
     // Generate output
     $output = '';
     foreach ($option_data as $section => $options) {
         $colspan = count($options) > 1 ? 'colspan="2"' : '';
         $desc_html_tag = 'div';
         $output .= '<div class="group" id="' . sanitize_title($section) . '">' . PHP_EOL;
         $output .= '<fieldset class="options">' . PHP_EOL;
         $output .= '<legend>' . self::getNiceTitleOptions($section) . '</legend>' . PHP_EOL;
         $output .= '<table class="form-table">' . PHP_EOL;
         foreach ((array) $options as $option) {
             // Helper
             if ($option[2] == 'helper') {
                 $output .= '<tr style="vertical-align: middle;"><td class="helper" ' . $colspan . '>' . stripslashes($option[4]) . '</td></tr>' . PHP_EOL;
                 continue;
             }
             // Fix notices
             if (!isset($option_actual[$option[0]])) {
                 $option_actual[$option[0]] = '';
             }
             switch ($option[2]) {
                 case 'radio':
                     $input_type = array();
                     foreach ($option[3] as $value => $text) {
                         $input_type[] = '<label><input type="radio" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($value) . '" ' . checked($value, $option_actual[$option[0]], false) . ' /> ' . $text . '</label>' . PHP_EOL;
                     }
                     $input_type = implode('<br />', $input_type);
                     break;
                 case 'checkbox':
                     $desc_html_tag = 'span';
                     $input_type = '<input type="checkbox" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option[3]) . '" ' . ($option_actual[$option[0]] ? 'checked="checked"' : '') . ' />' . PHP_EOL;
                     break;
                 case 'dropdown':
                     $selopts = explode('/', $option[3]);
                     $seldata = '';
                     foreach ((array) $selopts as $sel) {
                         $seldata .= '<option value="' . esc_attr($sel) . '" ' . (isset($option_actual[$option[0]]) && $option_actual[$option[0]] == $sel ? 'selected="selected"' : '') . ' >' . ucfirst($sel) . '</option>' . PHP_EOL;
                     }
                     $input_type = '<select id="' . $option[0] . '" name="' . $option[0] . '">' . $seldata . '</select>' . PHP_EOL;
                     break;
                 case 'text-color':
                     $input_type = '<input type="text" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option_actual[$option[0]]) . '" class="text-color ' . $option[3] . '" /><div class="box_color ' . $option[0] . '"></div>' . PHP_EOL;
                     break;
                 case 'text':
                 default:
                     $input_type = '<input type="text" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option_actual[$option[0]]) . '" class="' . $option[3] . '" />' . PHP_EOL;
                     break;
                 case 'number':
                 default:
                     $input_type = '<input type="number" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option_actual[$option[0]]) . '" class="' . $option[3] . '" />' . PHP_EOL;
                     break;
             }
             // Additional Information
             $extra = '';
             if (!empty($option[4])) {
                 $extra = '<' . $desc_html_tag . ' class="stpexplan">' . __($option[4]) . '</' . $desc_html_tag . '>' . PHP_EOL;
             }
             // Output
             $output .= '<tr style="vertical-align: top;"><th scope="row"><label for="' . $option[0] . '">' . __($option[1]) . '</label></th><td>' . $input_type . '	' . $extra . '</td></tr>' . PHP_EOL;
         }
         $output .= '</table>' . PHP_EOL;
         $output .= '</fieldset>' . PHP_EOL;
         $output .= '</div>' . PHP_EOL;
     }
     return $output;
 }
 /**
  * Suggest tags from ProxemAPI
  *
  */
 public static function ajax_proxem_api()
 {
     status_header(200);
     header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
     // API Key ?
     if (SimpleTags_Plugin::get_option_value('proxem_key') == '') {
         echo '<p>' . __('Proxem API need an API key to work. You can register on service website to obtain a key and set it on Simple Tags options.', '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;
     }
     // Build params
     $response = wp_remote_post('https://proxem-thematization.p.mashape.com/api/wikiAnnotator/GetCategories?nbtopcat=10', array('headers' => array('X-Mashape-Key' => SimpleTags_Plugin::get_option_value('proxem_key'), 'Accept' => "application/json", 'Content-Type' => "text/plain"), 'body' => $content, 'timeout' => 15));
     if (!is_wp_error($response) && $response != null) {
         if (wp_remote_retrieve_response_code($response) == 200) {
             $data = wp_remote_retrieve_body($response);
         }
     }
     $data = json_decode($data);
     if ($data == false || !isset($data->categories)) {
         var_dump($response);
         return false;
     }
     if (empty($data->categories)) {
         echo '<p>' . __('No results from Proxem API.', 'simpletags') . '</p>';
         exit;
     }
     foreach ((array) $data->categories as $term) {
         echo '<span class="local">' . esc_html($term->name) . '</span>' . "\n";
     }
     echo '<div class="clear"></div>';
     exit;
 }