Пример #1
0
function similar_posts_options_page()
{
    echo '<div class="wrap"><h2>';
    _e('Similar Posts ', 'post_plugins');
    echo '<a href="http://rmarsh.com/plugins/post-options/" style="font-size: 0.8em;">';
    _e('help and instructions');
    echo '</a></h2></div>';
    if (!SimilarPosts::check_post_plugin_library(__('<h1>Please install the <a href="http://downloads.wordpress.org/plugin/post-plugin-library.zip">Post Plugin Library</a> plugin.</h1>'))) {
        return;
    }
    $m = new admin_subpages();
    $m->add_subpage('General', 'general', 'similar_posts_general_options_subpage');
    $m->add_subpage('Output', 'output', 'similar_posts_output_options_subpage');
    $m->add_subpage('Filter', 'filter', 'similar_posts_filter_options_subpage');
    $m->add_subpage('Other', 'other', 'similar_posts_other_options_subpage');
    $m->add_subpage('Manage the Index', 'index', 'similar_posts_index_options_subpage');
    $m->add_subpage('Report a Bug', 'bug', 'similar_posts_bug_subpage');
    $m->add_subpage('Remove this Plugin', 'remove', 'similar_posts_remove_subpage');
    $m->display();
}
Пример #2
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));
 }