Advertisement
alchymyth

rel posts

Feb 11th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.81 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WordPress Related Posts
  4. Version: 2.4
  5. Plugin URI: http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/
  6. Description: Quickly increase your readers' engagement with your posts by adding Related Posts in the footer of your content.
  7. Author: Zemanta Ltd.
  8. Author URI: http://www.zemanta.com
  9. */
  10.  
  11. define('WP_RP_VERSION', '2.4');
  12.  
  13. include_once(dirname(__FILE__) . '/config.php');
  14. include_once(dirname(__FILE__) . '/lib/stemmer.php');
  15.  
  16. include_once(dirname(__FILE__) . '/admin_notices.php');
  17. include_once(dirname(__FILE__) . '/notifications.php');
  18. include_once(dirname(__FILE__) . '/widget.php');
  19. include_once(dirname(__FILE__) . '/thumbnailer.php');
  20. include_once(dirname(__FILE__) . '/settings.php');
  21. include_once(dirname(__FILE__) . '/recommendations.php');
  22. include_once(dirname(__FILE__) . '/dashboard_widget.php');
  23. include_once(dirname(__FILE__) . '/compatibility.php');
  24.  
  25. register_activation_hook(__FILE__, 'wp_rp_activate_hook');
  26. register_deactivation_hook(__FILE__, 'wp_rp_deactivate_hook');
  27.  
  28. add_action('wp_head', 'wp_rp_head_resources');
  29. add_action('wp_before_admin_bar_render', 'wp_rp_extend_adminbar');
  30.  
  31. function wp_rp_extend_adminbar() {
  32.     global $wp_admin_bar;
  33.  
  34.     if(!is_super_admin() || !is_admin_bar_showing())
  35.         return;
  36.  
  37.     $wp_admin_bar->add_menu(array(
  38.         'id' => 'wp_rp_adminbar_menu',
  39.         'title' => __('Related Posts', 'wp_related_posts'),
  40.         'href' => admin_url('admin.php?page=wordpress-related-posts&ref=adminbar')
  41.     ));
  42. }
  43.  
  44. global $wp_rp_output;
  45. $wp_rp_output = array();
  46. function wp_rp_add_related_posts_hook($content) {
  47.     global $wp_rp_output, $post;
  48.     $options = wp_rp_get_options();
  49.  
  50.     if ($post->post_type === 'post' && ($options["on_single_post"] || (is_feed() && $options["on_rss"]))) {
  51.         if (!isset($wp_rp_output[$post->ID])) {
  52.             $wp_rp_output[$post->ID] = wp_rp_get_related_posts();
  53.         }
  54.         $content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
  55.         $content = $content . $wp_rp_output[$post->ID];
  56.     }
  57.  
  58.     return $content;
  59. }
  60. add_filter('the_content', 'wp_rp_add_related_posts_hook', 101);
  61.  
  62. function wp_rp_append_posts(&$related_posts, $fetch_function_name) {
  63.     $options = wp_rp_get_options();
  64.  
  65.     $limit = $options['max_related_posts'];
  66.  
  67.     $len = sizeof($related_posts);
  68.     $num_missing_posts = $limit - $len;
  69.     if ($num_missing_posts > 0) {
  70.         $exclude_ids = array_map(create_function('$p', 'return $p->ID;'), $related_posts);
  71.  
  72.         $posts = call_user_func($fetch_function_name, $num_missing_posts, $exclude_ids);
  73.         if ($posts) {
  74.             $related_posts = array_merge($related_posts, $posts);
  75.         }
  76.     }
  77. }
  78.  
  79. function wp_rp_fetch_posts_and_title() {
  80.     $options = wp_rp_get_options();
  81.  
  82.     $limit = $options['max_related_posts'];
  83.     $title = $options["related_posts_title"];
  84.  
  85.     $related_posts = array();
  86.  
  87.     wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2');
  88.     wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts');
  89.     wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts');
  90.  
  91.     if(function_exists('qtrans_postsFilter')) {
  92.         $related_posts = qtrans_postsFilter($related_posts);
  93.     }
  94.  
  95.     return array(
  96.         "posts" => $related_posts,
  97.         "title" => $title
  98.     );
  99. }
  100.  
  101. function wp_rp_generate_related_posts_list_items($related_posts) {
  102.     $options = wp_rp_get_options();
  103.     $output = "";
  104.     $i = 0;
  105.  
  106.     $statistics_enabled = $options['ctr_dashboard_enabled'];
  107.  
  108.     foreach ($related_posts as $related_post ) {
  109.         $data_attrs = '';
  110.         if ($statistics_enabled) {
  111.             $data_attrs .= 'data-position="' . $i++ . '" data-poid="in-' . $related_post->ID . '" ';
  112.         }
  113.  
  114.         $output .= '<li ' . $data_attrs . '>';
  115.  
  116.         $img = wp_rp_get_post_thumbnail_img($related_post);
  117.         if ($img) {
  118.             $output .=  '<a href="' . get_permalink($related_post->ID) . '" class="wp_rp_thumbnail">' . $img . '</a>';
  119.         }
  120.  
  121.         if (!$options["display_thumbnail"] || ($options["display_thumbnail"] && ($options["thumbnail_display_title"] || !$img))) {
  122.             if ($options["display_publish_date"]){
  123.                 $dateformat = get_option('date_format');
  124.                 $output .= mysql2date($dateformat, $related_post->post_date) . " -- ";
  125.             }
  126.  
  127.             $output .= '<a href="' . get_permalink($related_post->ID) . '" class="wp_rp_title">' . wptexturize($related_post->post_title) . '</a>';
  128.  
  129.             if ($options["display_comment_count"]){
  130.                 $output .=  " (" . $related_post->comment_count . ")";
  131.             }
  132.  
  133.             if ($options["display_excerpt"]){
  134.                 $excerpt_max_length = $options["excerpt_max_length"];
  135.                 if($related_post->post_excerpt){
  136.                     $output .= '<br /><small>' . (mb_substr(strip_shortcodes(strip_tags($related_post->post_excerpt)), 0, $excerpt_max_length)) . '...</small>';
  137.                 } else {
  138.                     $output .= '<br /><small>' . (mb_substr(strip_shortcodes(strip_tags($related_post->post_content)), 0, $excerpt_max_length)) . '...</small>';
  139.                 }
  140.             }
  141.         }
  142.         $output .=  '</li>';
  143.     }
  144.  
  145.     return $output;
  146. }
  147.  
  148. function wp_rp_should_exclude() {
  149.     global $wpdb, $post;
  150.  
  151.     if (!$post || !$post->ID) {
  152.         return true;
  153.     }
  154.  
  155.     $options = wp_rp_get_options();
  156.  
  157.     if(!$options['exclude_categories']) { return false; }
  158.  
  159.     $q = 'SELECT COUNT(tt.term_id) FROM '. $wpdb->term_taxonomy.' tt, ' . $wpdb->term_relationships.' tr WHERE tt.taxonomy = \'category\' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = '. $post->ID . ' AND tt.term_id IN (' . $options['exclude_categories'] . ')';
  160.  
  161.     $result = $wpdb->get_col($q);
  162.  
  163.     $count = (int) $result[0];
  164.  
  165.     return $count > 0;
  166. }
  167.  
  168. function wp_rp_ajax_blogger_network_blacklist_callback() {
  169.     if (!current_user_can('delete_users')) {
  170.         die();
  171.     }
  172.  
  173.     $sourcefeed = (int) $_GET['sourcefeed'];
  174.  
  175.     $meta = wp_rp_get_meta();
  176.  
  177.     $blog_id = $meta['blog_id'];
  178.     $auth_key = $meta['auth_key'];
  179.     $req_options = array(
  180.         'timeout' => 5
  181.     );
  182.     $url = WP_RP_CTR_DASHBOARD_URL . "blacklist/?blog_id=$blog_id&auth_key=$auth_key&sfid=$sourcefeed";
  183.     $response = wp_remote_get($url, $req_options);
  184.  
  185.     if (wp_remote_retrieve_response_code($response) == 200) {
  186.         $body = wp_remote_retrieve_body($response);
  187.         if ($body) {
  188.             $doc = json_decode($body);
  189.             if ($doc && $doc->status === 'ok') {
  190.                 header_remove();
  191.                 header('Content-Type: text/javascript');
  192.                 echo "if(window['_wp_rp_blacklist_callback$sourcefeed']) window._wp_rp_blacklist_callback$sourcefeed();";
  193.             }
  194.         }
  195.     }
  196.     die();
  197. }
  198.  
  199. add_action('wp_ajax_rp_blogger_network_blacklist', 'wp_rp_ajax_blogger_network_blacklist_callback');
  200.  
  201. function wp_rp_head_resources() {
  202.     global $post, $wpdb;
  203.  
  204.     if (wp_rp_should_exclude()) {
  205.         return;
  206.     }
  207.  
  208.     $meta = wp_rp_get_meta();
  209.     $options = wp_rp_get_options();
  210.     $statistics_enabled = false;
  211.     $remote_recommendations = false;
  212.     $output = '';
  213.  
  214.     if (is_single()) {
  215.         $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
  216.         $remote_recommendations = $meta['remote_recommendations'] && $statistics_enabled;
  217.     }
  218.  
  219.     if ($statistics_enabled) {
  220.         $tags = $wpdb->get_col("SELECT label FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID ORDER BY weight desc;", 0);
  221.         if (!empty($tags)) {
  222.             $post_tags = '[' . implode(', ', array_map(create_function('$v', 'return "\'" . urlencode(substr($v, strpos($v, \'_\') + 1)) . "\'";'), $tags)) . ']';
  223.         } else {
  224.             $post_tags = '[]';
  225.         }
  226.  
  227.         $output .= "<script type=\"text/javascript\">\n" .
  228.             "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
  229.             "\twindow._wp_rp_ajax_img_src_url = '" . esc_js(WP_RP_CTR_REPORT_URL) . "';\n" .
  230.             "\twindow._wp_rp_post_id = '" . esc_js($post->ID) . "';\n" .
  231.             "\twindow._wp_rp_thumbnails = " . ($options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
  232.             "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
  233.             "\twindow._wp_rp_post_tags = {$post_tags};\n" .
  234.             "\twindow._wp_rp_static_base_url = '" . esc_js(WP_RP_STATIC_BASE_URL) . "';\n" .
  235.             "\twindow._wp_rp_promoted_content = " . ($options['promoted_content_enabled'] ? 'true' : 'false') . ";\n" .
  236.             "\twindow._wp_rp_plugin_version = '" . WP_RP_VERSION . "';\n" .
  237.             "\twindow._wp_rp_traffic_exchange = " . ($options['traffic_exchange_enabled'] ? 'true' : 'false') . ";\n" .
  238.             (current_user_can('delete_users') ? "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" : '') .
  239.             "\twindow._wp_rp_num_rel_posts = '" . $options['max_related_posts'] . "';\n" .
  240.             "</script>\n";
  241.     }
  242.  
  243.     if ($remote_recommendations) {
  244.         $output .= '<script type="text/javascript" src="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_RECOMMENDATIONS_JS_FILE . '?version=' . WP_RP_VERSION . '"></script>' . "\n";
  245.         $output .= '<link rel="stylesheet" href="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_RECOMMENDATIONS_CSS_FILE . '?version=' . WP_RP_VERSION . '" />' . "\n";
  246.     }
  247.  
  248.     if($statistics_enabled) {
  249.         $output .= '<script type="text/javascript" src="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_CTR_PAGEVIEW_FILE . '?version=' . WP_RP_VERSION . '" async></script>' . "\n";
  250.     }
  251.  
  252.     if ($options['enable_themes']) {
  253.         if ($options["display_thumbnail"]) {
  254.             $theme_url = WP_RP_STATIC_BASE_URL . WP_RP_STATIC_THEMES_THUMBS_PATH;
  255.         } else {
  256.             $theme_url = WP_RP_STATIC_BASE_URL . WP_RP_STATIC_THEMES_PATH;
  257.         }
  258.  
  259.         $output .= '<link rel="stylesheet" href="' . $theme_url . $options['theme_name'] . '?version=' . WP_RP_VERSION . '" />' . "\n";
  260.         if ($options['custom_theme_enabled']) {
  261.             $output .= '<style type="text/css">' . "\n" . $options['theme_custom_css'] . "</style>\n";
  262.         }
  263.     }
  264.  
  265.     echo $output;
  266. }
  267.  
  268. function wp_rp_get_related_posts($before_title = '', $after_title = '') {
  269.     if (wp_rp_should_exclude()) {
  270.         return;
  271.     }
  272.  
  273.     $options = wp_rp_get_options();
  274.     $meta = wp_rp_get_meta();
  275.  
  276.     $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
  277.     $remote_recommendations = is_single() && $meta['remote_recommendations'] && $statistics_enabled;
  278.  
  279.     $output = "";
  280.     $promotional_link = '';
  281.  
  282.     $posts_and_title = wp_rp_fetch_posts_and_title();
  283.  
  284.     $related_posts = $posts_and_title['posts'];
  285.     $title = $posts_and_title['title'];
  286.  
  287.     if (!$related_posts) {
  288.         return;
  289.     }
  290.  
  291.     $css_classes = 'related_post wp_rp';
  292.     if ($options['enable_themes']) {
  293.         $css_classes .= ' ' . str_replace(array('.css', '-'), array('', '_'), esc_attr('wp_rp_' . $options['theme_name']));
  294.     }
  295.  
  296.     $output = wp_rp_generate_related_posts_list_items($related_posts);
  297.     $output = '<ul class="' . $css_classes . '" style="visibility: ' . ($remote_recommendations ? 'hidden' : 'visible') . '">' . $output . '</ul>';
  298.     if($remote_recommendations) {
  299.         $output = $output . '<script type="text/javascript">window._wp_rp_callback_widget_exists && window._wp_rp_callback_widget_exists();</script>';
  300.     }
  301.  
  302.     if ($title != '') {
  303.         if ($before_title) {
  304.             $output = $before_title . $title . $after_title . $output;
  305.         } else {
  306.             $title_tag = $options["related_posts_title_tag"];
  307.             $output =  '<' . $title_tag . ' class="related_post_title">' . $title . $promotional_link . '</' . $title_tag . '>' . $output;
  308.         }
  309.     }
  310.  
  311.     return "\n" . $output . "\n";
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement