Пример #1
0
 function execute($args = '', $default_output_template = '<li>{link}</li>')
 {
     if (!PopularPosts::check_post_plugin_library(__('Post-Plugin Library missing'))) {
         return;
     }
     global $wpdb, $wp_version, $popular_posts_current_ID;
     $start_time = ppl_microtime();
     if (defined('POC_CACHE_4')) {
         $cache_key = 'popular-posts' . $args;
         $result = poc_cache_fetch($cache_key);
         if ($result !== false) {
             return $result . sprintf("<!-- popular Posts took %.3f ms (cached) -->", 1000 * (ppl_microtime() - $start_time));
         }
     }
     // First we process any arguments to see if any defaults have been overridden
     $options = ppl_parse_args($args);
     // Next we retrieve the stored options and use them unless a value has been overridden via the arguments
     $options = ppl_set_options('popular-posts', $options, $default_output_template);
     if (0 < $options['limit']) {
         $match_tags = $options['match_tags'] !== 'false' && $wp_version >= 2.3;
         $exclude_cats = $options['excluded_cats'] !== '';
         $include_cats = $options['included_cats'] !== '';
         $exclude_authors = $options['excluded_authors'] !== '';
         $include_authors = $options['included_authors'] !== '';
         $exclude_posts = trim($options['excluded_posts']) !== '';
         $include_posts = trim($options['included_posts']) !== '';
         $match_category = $options['match_cat'] === 'true';
         $match_author = $options['match_author'] === 'true';
         $use_tag_str = '' != $options['tag_str'] && $wp_version >= 2.3;
         $omit_current_post = $options['omit_current_post'] !== 'false';
         $hide_pass = $options['show_private'] === 'false';
         $check_age = 'none' !== $options['age']['direction'];
         $check_custom = trim($options['custom']['key']) !== '';
         $limit = $options['skip'] . ', ' . $options['limit'];
         //the workhorse...
         $sql = "SELECT *, meta_value + 0 AS viewcount FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON post_id = ID ";
         // build the 'WHERE' clause
         $where = array();
         $where[] = "meta_key = 'pvc_views'";
         if (!function_exists('get_post_type')) {
             $where[] = where_hide_future();
         } else {
             $where[] = where_show_status($options['status'], $options['show_attachments']);
         }
         if ($match_category) {
             $where[] = where_match_category();
         }
         if ($match_tags) {
             $where[] = where_match_tags($options['match_tags']);
         }
         if ($match_author) {
             $where[] = where_match_author();
         }
         $where[] = where_show_pages($options['show_pages'], $options['show_attachments']);
         if ($include_cats) {
             $where[] = where_included_cats($options['included_cats']);
         }
         if ($exclude_cats) {
             $where[] = where_excluded_cats($options['excluded_cats']);
         }
         if ($exclude_authors) {
             $where[] = where_excluded_authors($options['excluded_authors']);
         }
         if ($include_authors) {
             $where[] = where_included_authors($options['included_authors']);
         }
         if ($exclude_posts) {
             $where[] = where_excluded_posts(trim($options['excluded_posts']));
         }
         if ($include_posts) {
             $where[] = where_included_posts(trim($options['included_posts']));
         }
         if ($use_tag_str) {
             $where[] = where_tag_str($options['tag_str']);
         }
         if ($omit_current_post) {
             $where[] = where_omit_post($popular_posts_current_ID);
         }
         if ($hide_pass) {
             $where[] = where_hide_pass();
         }
         if ($check_age) {
             $where[] = where_check_age($options['age']['direction'], $options['age']['length'], $options['age']['duration']);
         }
         if ($check_custom) {
             $where[] = where_check_custom($options['custom']['key'], $options['custom']['op'], $options['custom']['value']);
         }
         $sql .= "WHERE " . implode(' AND ', $where);
         if ($check_custom) {
             $sql .= " GROUP BY {$wpdb->posts}.ID";
         }
         $sql .= " ORDER BY viewcount DESC LIMIT {$limit}";
         $results = $wpdb->get_results($sql);
     } else {
         $results = false;
     }
     if ($results) {
         $translations = ppl_prepare_template($options['output_template']);
         foreach ($results as $result) {
             $items[] = ppl_expand_template($result, $options['output_template'], $translations, 'popular-posts');
         }
         if ($options['sort']['by1'] !== '') {
             $items = ppl_sort_items($options['sort'], $results, 'popular-posts', $options['group_template'], $items);
         }
         $output = implode($options['divider'] ? $options['divider'] : "\n", $items);
         $output = $options['prefix'] . $output . $options['suffix'];
     } else {
         // if we reach here our query has produced no output ... so what next?
         if ($options['no_text'] !== 'false') {
             $output = '';
             // we display nothing at all
         } else {
             // we display the blank message, with tags expanded if necessary
             $translations = ppl_prepare_template($options['none_text']);
             $output = $options['prefix'] . ppl_expand_template(array(), $options['none_text'], $translations, 'popular-posts') . $options['suffix'];
         }
     }
     if (defined('POC_CACHE_4')) {
         poc_cache_store($cache_key, $output);
     }
     return $output ? $output . sprintf("<!-- popular Posts took %.3f ms -->", 1000 * (ppl_microtime() - $start_time)) : '';
 }
 function execute($args = '', $default_output_template = '<li>{commentlink}</li>')
 {
     if (!RecentComments::check_post_plugin_library(__('Post-Plugin Library missing', 'recent_comments_plugin'))) {
         return;
     }
     global $wpdb, $wp_version, $recent_comments_current_ID;
     $start_time = ppl_microtime();
     if (defined('POC_CACHE_4')) {
         $cache_key = 'recent-comments' . $args;
         $result = poc_cache_fetch($cache_key);
         if ($result !== false) {
             return $result . sprintf("<!-- Recent Comments took %.3f ms (cached) -->", 1000 * (ppl_microtime() - $start_time));
         }
     }
     // First we process any arguments to see if any defaults have been overridden
     $options = ppl_parse_args($args);
     // Next we retrieve the stored options and use them unless a value has been overridden via the arguments
     $options = ppl_set_options('recent-comments', $options, $default_output_template);
     if (0 < $options['limit']) {
         $match_tags = $options['match_tags'] !== 'false' && $wp_version >= 2.3;
         $exclude_cats = $options['excluded_cats'] !== '';
         $include_cats = $options['included_cats'] !== '';
         $exclude_authors = $options['excluded_authors'] !== '';
         $include_authors = $options['included_authors'] !== '';
         $exclude_posts = trim($options['excluded_posts']) !== '';
         $include_posts = trim($options['included_posts']) !== '';
         $match_category = $options['match_cat'] === 'true';
         $match_author = $options['match_author'] === 'true';
         $use_tag_str = '' != $options['tag_str'] && $wp_version >= 2.3;
         $omit_current_post = $options['omit_current_post'] !== 'false';
         $just_current_post = $options['just_current_post'] !== 'false';
         $hide_pass = $options['show_private'] === 'false';
         $check_age = 'none' !== $options['age']['direction'];
         $check_custom = trim($options['custom']['key']) !== '';
         $limit = $options['skip'] . ', ' . $options['limit'];
         $unique = $options['unique'] === 'true';
         $filter_comments = ($options['show_type'] === 'comments' or $options['show_type'] === 'trackbacks');
         $no_author_comments = $options['no_author_comments'] === 'true';
         $no_user_comments = $options['no_user_comments'] !== 'false';
         //the workhorse...
         $where = $groupby = array();
         if (!$unique) {
             $sql = "SELECT * FROM {$wpdb->comments} LEFT JOIN `{$wpdb->posts}` ON `comment_post_ID` = `ID` ";
         } else {
             $sql = "SELECT *, MAX(comment_date) AS comment_date FROM {$wpdb->comments} LEFT JOIN `{$wpdb->posts}` ON `comment_post_ID` = `ID` ";
             $groupby[] = "comment_post_ID";
         }
         if ($check_custom) {
             $sql .= "LEFT JOIN {$wpdb->postmeta} ON post_id = ID ";
             $groupby[] = "ID";
         }
         // build the 'WHERE' clause
         if (!function_exists('get_post_type')) {
             $where[] = where_hide_future();
         } else {
             $where[] = where_show_status($options['status'], $options['show_attachments']);
         }
         if ($match_category) {
             $where[] = where_match_category();
         }
         if ($match_tags) {
             $where[] = where_match_tags($options['match_tags']);
         }
         if ($match_author) {
             $where[] = where_match_author();
         }
         $where[] = where_show_pages($options['show_pages'], $options['show_attachments']);
         if ($include_cats) {
             $where[] = where_included_cats($options['included_cats']);
         }
         if ($exclude_cats) {
             $where[] = where_excluded_cats($options['excluded_cats']);
         }
         if ($exclude_authors) {
             $where[] = where_excluded_authors($options['excluded_authors']);
         }
         if ($include_authors) {
             $where[] = where_included_authors($options['included_authors']);
         }
         if ($exclude_posts) {
             $where[] = where_excluded_posts(trim($options['excluded_posts']));
         }
         if ($include_posts) {
             $where[] = where_included_posts(trim($options['included_posts']));
         }
         if ($use_tag_str) {
             $where[] = where_tag_str($options['tag_str']);
         }
         if ($omit_current_post) {
             $where[] = where_omit_post($recent_comments_current_ID);
         }
         if ($just_current_post) {
             $where[] = where_just_post();
         }
         if ($hide_pass) {
             $where[] = where_hide_pass();
         }
         if ($check_age) {
             $where[] = where_check_age($options['age']['direction'], $options['age']['length'], $options['age']['duration']);
         }
         if ($no_author_comments) {
             $where[] = where_author_comments();
         }
         if ($no_user_comments) {
             $where[] = where_user_comments();
         }
         if ($filter_comments) {
             $where[] = where_comment_type($options['show_type']);
         }
         if ($check_custom) {
             $where[] = where_check_custom($options['custom']['key'], $options['custom']['op'], $options['custom']['value']);
         }
         $where[] = "comment_approved = '1'";
         $sql .= "WHERE " . implode(' AND ', $where);
         if (count($groupby) > 0) {
             $sql .= " GROUP BY " . implode(', ', $groupby);
         }
         $sql .= " ORDER BY comment_date DESC LIMIT {$limit}";
         //echo $sql;
         $results = $wpdb->get_results($sql);
     } else {
         $results = false;
     }
     if ($results) {
         $translations = ppl_prepare_template($options['output_template']);
         if ($options['group_by'] === 'post') {
             $options['sort']['by1'] = '{date:raw}';
             $options['sort']['order1'] = SORT_DESC;
             $options['sort']['case1'] = 'false';
             $options['sort']['by2'] = '{commentdate:raw}';
             $options['sort']['order2'] = SORT_DESC;
             $options['sort']['case2'] = 'false';
         } elseif ($options['group_by'] === 'author') {
             $options['sort']['by1'] = '{commenter}';
             $options['sort']['order1'] = SORT_ASC;
             $options['sort']['case1'] = 'true';
             $options['sort']['by2'] = '{commentdate:raw}';
             $options['sort']['order2'] = SORT_DESC;
             $options['sort']['case2'] = 'false';
         }
         foreach ($results as $result) {
             $items[] = ppl_expand_template($result, $options['output_template'], $translations, 'recent-comments');
         }
         if ($options['sort']['by1'] !== '') {
             $items = ppl_sort_items($options['sort'], $results, 'recent-comments', $options['group_template'], $items);
         }
         $output = implode($options['divider'] ? $options['divider'] : "\n", $items);
         $output = $options['prefix'] . $output . $options['suffix'];
     } else {
         // if we reach here our query has produced no output ... so what next?
         if ($options['no_text'] !== 'false') {
             $output = '';
             // we display nothing at all
         } else {
             // we display the blank message, with tags expanded if necessary
             $translations = ppl_prepare_template($options['none_text']);
             $output = $options['prefix'] . ppl_expand_template(array(), $options['none_text'], $translations, 'recent_comments') . $options['suffix'];
         }
     }
     if (defined('POC_CACHE_4')) {
         poc_cache_store($cache_key, $output);
     }
     return $output ? $output . sprintf("<!-- Recent Comments took %.3f ms -->", 1000 * (ppl_microtime() - $start_time)) : '';
 }
Пример #3
0
 function execute($args = '', $default_output_template = '<li>{link}</li>', $option_key = 'similar-posts')
 {
     if (!SimilarPosts::check_post_plugin_library('<a href="http://downloads.wordpress.org/plugin/post-plugin-library.zip">' . __('Post-Plugin Library missing') . '</a>')) {
         return '';
     }
     global $table_prefix, $wpdb, $wp_version, $post;
     $start_time = ppl_microtime();
     if (defined('POC_CACHE_4')) {
         $cache_key = $option_key . $post->ID . $args;
         $result = poc_cache_fetch($cache_key);
         if ($result !== false) {
             return $result . sprintf("<!-- Similar Posts took %.3f ms (cached) -->", 1000 * (ppl_microtime() - $start_time));
         }
     }
     $table_name = $table_prefix . 'similar_posts';
     // First we process any arguments to see if any defaults have been overridden
     $options = ppl_parse_args($args);
     // Next we retrieve the stored options and use them unless a value has been overridden via the arguments
     $options = ppl_set_options($option_key, $options, $default_output_template);
     if (0 < $options['limit']) {
         $hide_future = true;
         $match_tags = $options['match_tags'] !== 'false' && $wp_version >= 2.3;
         $exclude_cats = $options['excluded_cats'] !== '';
         $include_cats = $options['included_cats'] !== '';
         $exclude_authors = $options['excluded_authors'] !== '';
         $include_authors = $options['included_authors'] !== '';
         $exclude_posts = trim($options['excluded_posts'] !== '');
         $include_posts = trim($options['included_posts']) !== '';
         $match_category = $options['match_cat'] === 'true';
         $use_tag_str = '' != $options['tag_str'] && $wp_version >= 2.3;
         $omit_current_post = $options['omit_current_post'] !== 'false' && isset($post) && $post->ID !== 0;
         $hide_pass = $options['show_private'] === 'false';
         $check_age = 'none' !== $options['age']['direction'];
         $check_custom = trim($options['custom']['key']) !== '';
         $limit = $options['skip'] . ', ' . $options['limit'];
         //get the terms to do the matching
         list($contentterms, $titleterms, $tagterms) = sp_terms_to_match($post->ID, $options['num_terms']);
         // these should add up to 1.0
         $weight_content = $options['weight_content'];
         $weight_title = $options['weight_title'];
         $weight_tags = $options['weight_tags'];
         // below a threshold we ignore the weight completely and save some effort
         if ($weight_content < 0.001) {
             $weight_content = (int) 0;
         }
         if ($weight_title < 0.001) {
             $weight_title = (int) 0;
         }
         if ($weight_tags < 0.001) {
             $weight_tags = (int) 0;
         }
         if ($options['crossmatch'] === 'true') {
             $combinedterms = $contentterms . ' ' . $titleterms . ' ' . $tagterms;
             $contentterms = $combinedterms;
             $titleterms = $combinedterms;
             $tagterms = $combinedterms;
             $count_combined = substr_count($combinedterms, ' ') + 1;
             // the weighting factors here and below are a rough attempt to get the score for
             // a perfect match to be roughly 100 for all combinations of content, title, and tags
             // MySQL fulltext search needs to be normalized by the number of search terms
             // The weighting is more successful when the terms are not combined
             if ($weight_content) {
                 $weight_content = 57.0 * $weight_content / $count_combined;
             }
             if ($weight_title) {
                 $weight_title = 220.0 * $weight_title / $count_combined;
             }
             if ($weight_tags) {
                 $weight_tags = 200.0 * $weight_tags / $count_combined;
             }
         } else {
             $count_content = substr_count($contentterms, ' ') + 1;
             $count_title = substr_count($titleterms, ' ') + 1;
             $count_tags = substr_count($tagterms, ' ') + 1;
             if ($weight_content) {
                 $weight_content = 57.0 * $weight_content / $count_content;
             }
             if ($weight_title) {
                 $weight_title = 18.0 * $weight_title / $count_title;
             }
             if ($weight_tags) {
                 $weight_tags = 24.0 * $weight_tags / $count_tags;
             }
         }
         // the workhorse...
         $sql = "SELECT *, ";
         $sql .= score_fulltext_match($table_name, $weight_title, $titleterms, $weight_content, $contentterms, $weight_tags, $tagterms);
         if ($check_custom) {
             $sql .= "LEFT JOIN {$wpdb->postmeta} ON post_id = ID ";
         }
         // build the 'WHERE' clause
         $where = array();
         $where[] = where_fulltext_match($weight_title, $titleterms, $weight_content, $contentterms, $weight_tags, $tagterms);
         if ($hide_future) {
             $where[] = where_hide_future();
         }
         if ($match_category) {
             $where[] = where_match_category();
         }
         if ($match_tags) {
             $where[] = where_match_tags($options['match_tags']);
         }
         $where[] = where_show_pages($options['show_pages']);
         if ($include_cats) {
             $where[] = where_included_cats($options['included_cats']);
         }
         if ($exclude_cats) {
             $where[] = where_excluded_cats($options['excluded_cats']);
         }
         if ($exclude_authors) {
             $where[] = where_excluded_authors($options['excluded_authors']);
         }
         if ($include_authors) {
             $where[] = where_included_authors($options['included_authors']);
         }
         if ($exclude_posts) {
             $where[] = where_excluded_posts(trim($options['excluded_posts']));
         }
         if ($include_posts) {
             $where[] = where_included_posts(trim($options['included_posts']));
         }
         if ($use_tag_str) {
             $where[] = where_tag_str($options['tag_str']);
         }
         if ($omit_current_post) {
             $where[] = where_omit_post();
         }
         if ($hide_pass) {
             $where[] = where_hide_pass();
         }
         if ($check_age) {
             $where[] = where_check_age($options['age']['direction'], $options['age']['length'], $options['age']['duration']);
         }
         if ($check_custom) {
             $where[] = where_check_custom($options['custom']['key'], $options['custom']['op'], $options['custom']['value']);
         }
         $sql .= "WHERE " . implode(' AND ', $where);
         if ($check_custom) {
             $sql .= " GROUP BY {$wpdb->posts}.ID";
         }
         $sql .= " ORDER BY score DESC LIMIT {$limit}";
         $results = $wpdb->get_results($sql);
     } else {
         $results = false;
     }
     if ($results) {
         $translations = ppl_prepare_template($options['output_template']);
         $output = '';
         foreach ($results as $result) {
             $output .= ppl_expand_template($result, $options['output_template'], $translations, $option_key);
         }
         // If stuff is to be trimmed off the front...
         if ($options['trim_before'] !== '' && strpos($output, $options['trim_before']) === 0) {
             $output = substr($output, strlen($options['trim_before']));
         }
         $output = $options['prefix'] . $output . $options['suffix'];
     } else {
         // if we reach here our query has produced no output ... so what next?
         if ($options['no_text'] !== 'false') {
             $output = '';
             // we display nothing at all
         } else {
             // we display the blank message, with tags expanded if necessary
             $translations = ppl_prepare_template($options['none_text']);
             $output = $options['prefix'] . ppl_expand_template(array(), $options['none_text'], $translations, $option_key) . $options['suffix'];
         }
     }
     if (defined('POC_CACHE_4')) {
         poc_cache_store($cache_key, $output);
     }
     return $output . sprintf("<!-- Similar Posts took %.3f ms -->", 1000 * (ppl_microtime() - $start_time));
 }