/** * 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); }
/** * 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); }
/** * 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); }
/** * 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; }