WordPress Related Posts - Version 3.0

Version Description

  • Opt-out for Related recommendations (articles)
  • Improved caching
Download this release

Release Info

Developer zemanta-panco
Plugin Icon wp plugin WordPress Related Posts
Version 3.0
Comparing to
See all releases

Code changes from version 2.9 to 3.0

config.php CHANGED
@@ -185,7 +185,7 @@ function wp_rp_install() {
185
  'show_statistics' => false,
186
  'show_traffic_exchange' => false,
187
  'show_zemanta_linky_option' => true,
188
- 'classic_user' => true
189
  );
190
 
191
  $wp_rp_options = array(
@@ -239,6 +239,16 @@ function wp_rp_is_classic() {
239
  return false;
240
  }
241
 
 
 
 
 
 
 
 
 
 
 
242
  function wp_rp_migrate_2_8() {
243
  global $wpdb;
244
 
185
  'show_statistics' => false,
186
  'show_traffic_exchange' => false,
187
  'show_zemanta_linky_option' => true,
188
+ 'classic_user' => strpos(get_bloginfo('language'), 'en') === 0 // Enable only if "any" english is the default language
189
  );
190
 
191
  $wp_rp_options = array(
239
  return false;
240
  }
241
 
242
+ function wp_rp_migrate_2_9() {
243
+ global $wpdb;
244
+
245
+ $wp_rp_meta = get_option('wp_rp_meta');
246
+ $wp_rp_meta['version'] = '3.0';
247
+ $wp_rp_meta['new_user'] = false;
248
+
249
+ update_option('wp_rp_meta', $wp_rp_meta);
250
+ }
251
+
252
  function wp_rp_migrate_2_8() {
253
  global $wpdb;
254
 
edit_related_posts.php CHANGED
@@ -24,6 +24,7 @@ function wp_rp_update_related_posts_callback() {
24
  }
25
 
26
  update_post_meta($post_id, '_wp_rp_selected_related_posts', $articles);
 
27
  die('ok');
28
  }
29
  add_action('wp_ajax_rp_update_related_posts', 'wp_rp_update_related_posts_callback');
24
  }
25
 
26
  update_post_meta($post_id, '_wp_rp_selected_related_posts', $articles);
27
+ delete_post_meta($post_id, '_wp_rp_related_posts_query_result_cache_'.$options['max_related_posts']); // Delete existing caches upon update, so that the new recs get calculated
28
  die('ok');
29
  }
30
  add_action('wp_ajax_rp_update_related_posts', 'wp_rp_update_related_posts_callback');
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: related,posts,post,related posts,plugin
4
  License: GPLv2
5
  Requires at least: 3.3
6
  Tested up to: 3.6
7
- Stable tag: 2.9
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
@@ -81,6 +81,10 @@ Fix for security vulnerability. Upgrade immediately.
81
 
82
  == Changelog ==
83
 
 
 
 
 
84
  = 2.9 =
85
  * Themes are now included with the plugin to simplify loading and customization
86
  * Simplified javascript loading
4
  License: GPLv2
5
  Requires at least: 3.3
6
  Tested up to: 3.6
7
+ Stable tag: 3.0
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
81
 
82
  == Changelog ==
83
 
84
+ = 3.0 =
85
+ * Opt-out for Related recommendations (articles)
86
+ * Improved caching
87
+
88
  = 2.9 =
89
  * Themes are now included with the plugin to simplify loading and customization
90
  * Simplified javascript loading
recommendations.php CHANGED
@@ -8,8 +8,10 @@ function wp_rp_update_tags($post_id) {
8
  $post = get_post($post->post_parent);
9
  }
10
 
 
 
11
  delete_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration');
12
- delete_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache');
13
 
14
  $wpdb->query(
15
  $wpdb->prepare('DELETE from ' . $wpdb->prefix . 'wp_rp_tags WHERE post_id=%d', $post->ID)
@@ -156,16 +158,15 @@ function wp_rp_generate_tags($post) {
156
 
157
  function wp_rp_fetch_related_posts_v2($limit = 10, $exclude_ids = array()) {
158
  global $wpdb, $post;
 
159
 
160
  $timestamp = time();
161
  $related_posts_query_result_cache_expiration = (int) get_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration', true);
162
- $related_posts_query_result_cache = get_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache', true);
163
 
164
  if (!$related_posts_query_result_cache || !$related_posts_query_result_cache_expiration || $related_posts_query_result_cache_expiration < $timestamp) { // Cache empty or never cached or cache expired
165
 
166
  $related_post_ids = null;
167
-
168
- $options = wp_rp_get_options();
169
  $exclude_ids_str = wp_rp_get_exclude_ids_list_string($exclude_ids);
170
 
171
  $tags_query = "SELECT label FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID;";
@@ -229,8 +230,13 @@ function wp_rp_fetch_related_posts_v2($limit = 10, $exclude_ids = array()) {
229
  }
230
 
231
  // Update the cache
232
- update_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration', $timestamp + 24 * 60 * 60); // one day
233
- update_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache', $related_posts_query_result_cache);
 
 
 
 
 
234
  }
235
 
236
  $related_posts_with_score_map = array();
8
  $post = get_post($post->post_parent);
9
  }
10
 
11
+ $options = wp_rp_get_options();
12
+
13
  delete_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration');
14
+ delete_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_'.$options['max_related_posts']);
15
 
16
  $wpdb->query(
17
  $wpdb->prepare('DELETE from ' . $wpdb->prefix . 'wp_rp_tags WHERE post_id=%d', $post->ID)
158
 
159
  function wp_rp_fetch_related_posts_v2($limit = 10, $exclude_ids = array()) {
160
  global $wpdb, $post;
161
+ $options = wp_rp_get_options();
162
 
163
  $timestamp = time();
164
  $related_posts_query_result_cache_expiration = (int) get_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration', true);
165
+ $related_posts_query_result_cache = get_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_'.$options['max_related_posts'], true);
166
 
167
  if (!$related_posts_query_result_cache || !$related_posts_query_result_cache_expiration || $related_posts_query_result_cache_expiration < $timestamp) { // Cache empty or never cached or cache expired
168
 
169
  $related_post_ids = null;
 
 
170
  $exclude_ids_str = wp_rp_get_exclude_ids_list_string($exclude_ids);
171
 
172
  $tags_query = "SELECT label FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID;";
230
  }
231
 
232
  // Update the cache
233
+ if ($timestamp - $post->post_date > 30 * 24 * 60 * 60) { // Post is older than one month
234
+ update_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration', $timestamp + 30 * 24 * 60 * 60); // Cache for one month
235
+ } else {
236
+ update_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_expiration', $timestamp + 24 * 60 * 60); // Cache for one day
237
+ }
238
+
239
+ update_post_meta($post->ID, '_wp_rp_related_posts_query_result_cache_'.$options['max_related_posts'], $related_posts_query_result_cache);
240
  }
241
 
242
  $related_posts_with_score_map = array();
settings.php CHANGED
@@ -207,6 +207,13 @@ function wp_rp_settings_page() {
207
  }
208
  }
209
 
 
 
 
 
 
 
 
210
  if (isset($postdata['wp_rp_turn_on_button_pressed'])) {
211
  $meta['show_turn_on_button'] = false;
212
  $meta['turn_on_button_pressed'] = $postdata['wp_rp_turn_on_button_pressed'];
@@ -518,8 +525,12 @@ function wp_rp_settings_page() {
518
  </td>
519
  </tr>
520
  <tr valign="top">
521
- <td colspan="2">
522
-
 
 
 
 
523
  <br/>
524
  <label>
525
  <input name="wp_rp_on_single_post" type="checkbox" id="wp_rp_on_single_post" value="yes" <?php checked($options['on_single_post']); ?>>
@@ -548,14 +559,12 @@ function wp_rp_settings_page() {
548
  <input name="wp_rp_promoted_content_enabled" type="checkbox" id="wp_rp_promoted_content_enabled" value="yes" <?php checked($options['promoted_content_enabled']); ?> />
549
  <?php _e('Promoted Content', 'wp_related_posts');?>*
550
  </label>
551
- </div>
552
- <?php if($meta['show_zemanta_linky_option']): ?>
553
  <label>
554
  <input name="wp_rp_display_zemanta_linky" type="checkbox" id="wp_rp_display_zemanta_linky" value="yes" <?php checked($options['display_zemanta_linky']); ?> />
555
  <?php _e("Support us (show our logo)",'wp_related_posts');?>
556
- </label>
557
  </td>
558
- <?php endif; ?>
559
  </tr>
560
  </table>
561
  <p class="submit"><input type="submit" value="<?php _e('Save changes', 'wp_related_posts'); ?>" class="button-primary" /></p>
207
  }
208
  }
209
 
210
+ if (isset($postdata['wp_rp_classic_state'])) {
211
+ $meta['classic_user'] = true;
212
+ } else {
213
+ $meta['classic_user'] = false;
214
+ }
215
+ wp_rp_update_meta($meta);
216
+
217
  if (isset($postdata['wp_rp_turn_on_button_pressed'])) {
218
  $meta['show_turn_on_button'] = false;
219
  $meta['turn_on_button_pressed'] = $postdata['wp_rp_turn_on_button_pressed'];
525
  </td>
526
  </tr>
527
  <tr valign="top">
528
+ <td colspan="2"><?php if(strpos(get_bloginfo('language'), 'en') === 0): ?>
529
+ <br/>
530
+ <label>
531
+ <input name="wp_rp_classic_state" type="checkbox" id="wp_rp_classic_state" value="yes" <?php checked($meta['classic_user']); ?>>
532
+ <?php _e("Display Related Posts Recommendations on Compose Screen",'wp_related_posts');?>
533
+ </label><?php endif; ?>
534
  <br/>
535
  <label>
536
  <input name="wp_rp_on_single_post" type="checkbox" id="wp_rp_on_single_post" value="yes" <?php checked($options['on_single_post']); ?>>
559
  <input name="wp_rp_promoted_content_enabled" type="checkbox" id="wp_rp_promoted_content_enabled" value="yes" <?php checked($options['promoted_content_enabled']); ?> />
560
  <?php _e('Promoted Content', 'wp_related_posts');?>*
561
  </label>
562
+ </div><?php if($meta['show_zemanta_linky_option']): ?>
 
563
  <label>
564
  <input name="wp_rp_display_zemanta_linky" type="checkbox" id="wp_rp_display_zemanta_linky" value="yes" <?php checked($options['display_zemanta_linky']); ?> />
565
  <?php _e("Support us (show our logo)",'wp_related_posts');?>
566
+ </label><?php endif; ?>
567
  </td>
 
568
  </tr>
569
  </table>
570
  <p class="submit"><input type="submit" value="<?php _e('Save changes', 'wp_related_posts'); ?>" class="button-primary" /></p>
thumbnailer.php CHANGED
@@ -27,8 +27,8 @@ function wp_rp_upload_default_thumbnail_file() {
27
  return false;
28
  }
29
 
30
-
31
- if ($image_id = media_handle_upload('wp_rp_default_thumbnail', 0)) {
32
  $image_data = wp_rp_get_image_data($image_id);
33
  if ($image = wp_rp_get_image_with_exact_size($image_data, array(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT))) {
34
  $upload_dir = wp_upload_dir();
@@ -311,14 +311,12 @@ function wp_rp_get_post_thumbnail_img($related_post, $size = null, $force = fals
311
  }
312
 
313
  $post_title = wptexturize($related_post->post_title);
314
-
315
  if (property_exists($related_post, 'thumbnail')) {
316
  return wp_rp_get_img_tag($related_post->thumbnail, $post_title);
317
  }
318
 
319
  $size = wp_rp_get_thumbanil_size_array($size);
320
  if (!$size) { return false; }
321
-
322
  if ($options['thumbnail_use_custom']) {
323
  $thumbnail_src = get_post_meta($related_post->ID, $options["thumbnail_custom_field"], true);
324
 
27
  return false;
28
  }
29
 
30
+ $image_id = media_handle_upload('wp_rp_default_thumbnail', 0);
31
+ if ($image_id) {
32
  $image_data = wp_rp_get_image_data($image_id);
33
  if ($image = wp_rp_get_image_with_exact_size($image_data, array(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT))) {
34
  $upload_dir = wp_upload_dir();
311
  }
312
 
313
  $post_title = wptexturize($related_post->post_title);
 
314
  if (property_exists($related_post, 'thumbnail')) {
315
  return wp_rp_get_img_tag($related_post->thumbnail, $post_title);
316
  }
317
 
318
  $size = wp_rp_get_thumbanil_size_array($size);
319
  if (!$size) { return false; }
 
320
  if ($options['thumbnail_use_custom']) {
321
  $thumbnail_src = get_post_meta($related_post->ID, $options["thumbnail_custom_field"], true);
322
 
wp_related_posts.php CHANGED
@@ -1,544 +1,544 @@
1
- <?php
2
- /*
3
- Plugin Name: WordPress Related Posts
4
- Version: 2.9
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. Click on <a href="admin.php?page=wordpress-related-posts">Related Posts tab</a> to configure your settings.
7
- Author: Zemanta Ltd.
8
- Author URI: http://www.zemanta.com
9
- */
10
-
11
- define('WP_RP_VERSION', '2.9');
12
-
13
- define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
14
-
15
- include_once(dirname(__FILE__) . '/config.php');
16
- include_once(dirname(__FILE__) . '/lib/stemmer.php');
17
- include_once(dirname(__FILE__) . '/lib/mobile_detect.php');
18
-
19
- include_once(dirname(__FILE__) . '/admin_notices.php');
20
- include_once(dirname(__FILE__) . '/notifications.php');
21
- include_once(dirname(__FILE__) . '/widget.php');
22
- include_once(dirname(__FILE__) . '/thumbnailer.php');
23
- include_once(dirname(__FILE__) . '/settings.php');
24
- include_once(dirname(__FILE__) . '/recommendations.php');
25
- include_once(dirname(__FILE__) . '/dashboard_widget.php');
26
- include_once(dirname(__FILE__) . '/edit_related_posts.php');
27
- include_once(dirname(__FILE__) . '/compatibility.php');
28
-
29
- register_activation_hook(__FILE__, 'wp_rp_activate_hook');
30
- register_deactivation_hook(__FILE__, 'wp_rp_deactivate_hook');
31
-
32
- add_action('wp_head', 'wp_rp_head_resources');
33
- add_action('wp_before_admin_bar_render', 'wp_rp_extend_adminbar');
34
-
35
- add_action('plugins_loaded', 'wp_rp_init_zemanta');
36
- function wp_rp_init_zemanta() {
37
- include_once(dirname(__FILE__) . '/zemanta/zemanta.php');
38
- if (wp_rp_is_classic()) {
39
- $wprp_zemanta = new WPRPZemanta();
40
- }
41
- }
42
-
43
- function wp_rp_extend_adminbar() {
44
- global $wp_admin_bar;
45
-
46
- if(!is_super_admin() || !is_admin_bar_showing())
47
- return;
48
-
49
- $wp_admin_bar->add_menu(array(
50
- 'id' => 'wp_rp_adminbar_menu',
51
- 'title' => __('Related Posts', 'wp_related_posts'),
52
- 'href' => admin_url('admin.php?page=wordpress-related-posts&ref=adminbar')
53
- ));
54
- }
55
-
56
- global $wp_rp_output;
57
- $wp_rp_output = array();
58
- function wp_rp_add_related_posts_hook($content) {
59
- global $wp_rp_output, $post;
60
-
61
- $options = wp_rp_get_options();
62
-
63
- if ($post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
64
- if (!isset($wp_rp_output[$post->ID])) {
65
- $wp_rp_output[$post->ID] = wp_rp_get_related_posts();
66
- }
67
- $content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
68
- $content = $content . $wp_rp_output[$post->ID];
69
- }
70
-
71
- return $content;
72
- }
73
- add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
74
-
75
- global $wp_rp_is_phone;
76
- function wp_rp_is_phone() {
77
- global $wp_rp_is_phone;
78
-
79
- if (!isset($wp_rp_is_phone)) {
80
- $detect = new WpRpMobileDetect();
81
- $wp_rp_is_phone = $detect->isMobile() && !$detect->isTablet();
82
- }
83
-
84
- return $wp_rp_is_phone;
85
- }
86
-
87
- function wp_rp_get_platform_options() {
88
- $options = wp_rp_get_options();
89
-
90
- if (wp_rp_is_phone()) {
91
- return $options['mobile'];
92
- }
93
- return $options['desktop'];
94
- }
95
-
96
- function wp_rp_ajax_load_articles_callback() {
97
- global $post;
98
-
99
- $getdata = stripslashes_deep($_GET);
100
- if (!isset($getdata['post_id'])) {
101
- die('error');
102
- }
103
-
104
- $post = get_post($getdata['post_id']);
105
- if(!$post) {
106
- die('error');
107
- }
108
-
109
- $from = (isset($getdata['from']) && is_numeric($getdata['from'])) ? intval($getdata['from']) : 0;
110
- $count = (isset($getdata['count']) && is_numeric($getdata['count'])) ? intval($getdata['count']) : 50;
111
-
112
- $search = isset($getdata['search']) && $getdata['search'] ? $getdata['search'] : false;
113
-
114
- $image_size = isset($getdata['size']) ? $getdata['size'] : 'thumbnail';
115
- if(!($image_size == 'thumbnail' || $image_size == 'full')) {
116
- die('error');
117
- }
118
-
119
- $limit = $count + $from;
120
-
121
- if ($search) {
122
- $the_query = new WP_Query(array(
123
- 's' => $search,
124
- 'post_type' => 'post',
125
- 'post_status'=>'publish',
126
- 'post_count' => $limit));
127
- $related_posts = $the_query->get_posts();
128
- } else {
129
- $related_posts = array();
130
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
131
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
132
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
133
- }
134
-
135
- if(function_exists('qtrans_postsFilter')) {
136
- $related_posts = qtrans_postsFilter($related_posts);
137
- }
138
-
139
- $response_list = array();
140
-
141
- foreach (array_slice($related_posts, $from) as $related_post) {
142
- array_push($response_list, array(
143
- 'id' => $related_post->ID,
144
- 'url' => get_permalink($related_post->ID),
145
- 'title' => $related_post->post_title,
146
- 'img' => wp_rp_get_post_thumbnail_img($related_post, $image_size)
147
- ));
148
- }
149
-
150
- header('Content-Type: text/javascript');
151
- die(json_encode($response_list));
152
- }
153
- add_action('wp_ajax_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
154
- add_action('wp_ajax_nopriv_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
155
-
156
- function wp_rp_append_posts(&$related_posts, $fetch_function_name, $limit) {
157
- $options = wp_rp_get_options();
158
-
159
- $len = sizeof($related_posts);
160
- $num_missing_posts = $limit - $len;
161
- if ($num_missing_posts > 0) {
162
- $exclude_ids = array_map(create_function('$p', 'return $p->ID;'), $related_posts);
163
-
164
- $posts = call_user_func($fetch_function_name, $num_missing_posts, $exclude_ids);
165
- if ($posts) {
166
- $related_posts = array_merge($related_posts, $posts);
167
- }
168
- }
169
- }
170
-
171
- function wp_rp_fetch_posts_and_title() {
172
- $options = wp_rp_get_options();
173
-
174
- $limit = $options['max_related_posts'];
175
- $title = $options["related_posts_title"];
176
-
177
- $related_posts = array();
178
-
179
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
180
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
181
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
182
-
183
- if(function_exists('qtrans_postsFilter')) {
184
- $related_posts = qtrans_postsFilter($related_posts);
185
- }
186
-
187
- return array(
188
- "posts" => $related_posts,
189
- "title" => $title
190
- );
191
- }
192
-
193
- function wp_rp_get_next_post(&$related_posts, &$selected_related_posts, &$inserted_urls, &$special_urls, $default_post_type) {
194
- $post = false;
195
-
196
- while (!($post && $post->ID) && !(empty($related_posts) && empty($selected_related_posts))) {
197
- $post_type = $default_post_type;
198
-
199
- $post = array_shift($selected_related_posts);
200
-
201
- if ($post && $post->type) {
202
- $post_type = $post->type;
203
- }
204
-
205
- if (!$post || !$post->ID) {
206
- while (!empty($related_posts) && (!($post = array_shift($related_posts)) || isset($special_urls[get_permalink($post->ID)])));
207
- }
208
-
209
- if ($post && $post->ID) {
210
- $post_url = property_exists($post, 'post_url') ? $post->post_url : get_permalink($post->ID);
211
- if (isset($inserted_urls[$post_url])) {
212
- $post = false;
213
- } else {
214
- $post->type = $post_type;
215
- }
216
- }
217
- }
218
-
219
- if (!$post || !$post->ID) {
220
- return false;
221
- }
222
-
223
- $inserted_urls[$post_url] = true;
224
-
225
- return $post;
226
- }
227
-
228
- function wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts) {
229
- $options = wp_rp_get_options();
230
- $platform_options = wp_rp_get_platform_options();
231
- $output = "";
232
-
233
- $statistics_enabled = $options['ctr_dashboard_enabled'];
234
-
235
- $limit = $options['max_related_posts'];
236
-
237
- $inserted_urls = array(); // Used to prevent duplicates
238
- $special_urls = array();
239
-
240
- foreach ($selected_related_posts as $post) {
241
- if (property_exists($post, 'post_url') && $post->post_url) {
242
- $special_urls[$post->post_url] = true;
243
- }
244
- }
245
-
246
- $default_post_type = empty($selected_related_posts) ? 'none' : 'empty';
247
-
248
- $image_size = ($platform_options['theme_name'] == 'pinterest.css') ? 'full' : 'thumbnail';
249
-
250
- for ($i = 0; $i < $limit; $i++) {
251
- $related_post = wp_rp_get_next_post($related_posts, $selected_related_posts, $inserted_urls, $special_urls, $default_post_type);
252
-
253
- if (!$related_post) {
254
- break;
255
- }
256
-
257
- if (property_exists($related_post, 'type')) {
258
- $post_type = $related_post->type;
259
- } else {
260
- $post_type = $default_post_type;
261
- }
262
-
263
- if (in_array($post_type, array('empty', 'none'))) {
264
- $post_id = 'in-' . $related_post->ID;
265
- } else {
266
- $post_id = 'ex-' . $related_post->ID;
267
- }
268
-
269
- $data_attrs = '';
270
- if ($statistics_enabled) {
271
- $data_attrs .= 'data-position="' . $i . '" data-poid="' . $post_id . '" data-post-type="' . $post_type . '" ';
272
- }
273
-
274
- $output .= '<li ' . $data_attrs . '>';
275
-
276
- $post_url = property_exists($related_post, 'post_url') ? $related_post->post_url : get_permalink($related_post->ID);
277
-
278
- $img = wp_rp_get_post_thumbnail_img($related_post, $image_size);
279
- if ($img) {
280
- $output .= '<a href="' . $post_url . '" class="wp_rp_thumbnail">' . $img . '</a>';
281
- }
282
-
283
- if ($platform_options["display_publish_date"]){
284
- $dateformat = get_option('date_format');
285
- $output .= mysql2date($dateformat, $related_post->post_date) . " -- ";
286
- }
287
-
288
- $output .= '<a href="' . $post_url . '" class="wp_rp_title">' . wptexturize($related_post->post_title) . '</a>';
289
-
290
- if ($platform_options["display_comment_count"] && property_exists($related_post, 'comment_count')){
291
- $output .= " (" . $related_post->comment_count . ")";
292
- }
293
-
294
- if ($platform_options["display_excerpt"]){
295
- $excerpt_max_length = $platform_options["excerpt_max_length"];
296
- $excerpt = '';
297
-
298
- if ($related_post->post_excerpt){
299
- $excerpt = strip_shortcodes(strip_tags($related_post->post_excerpt));
300
- }
301
- if (!$excerpt) {
302
- $excerpt = strip_shortcodes(strip_tags($related_post->post_content));
303
- }
304
-
305
- if ($excerpt) {
306
- if (strlen($excerpt) > $excerpt_max_length) {
307
- $excerpt = mb_substr($excerpt, 0, $excerpt_max_length - 3) . '...';
308
- }
309
- $output .= '<br /><small>' . $excerpt . '</small>';
310
- }
311
- }
312
- $output .= '</li>';
313
- }
314
-
315
- return $output;
316
- }
317
-
318
- function wp_rp_should_exclude() {
319
- global $wpdb, $post;
320
-
321
- if (!$post || !$post->ID) {
322
- return true;
323
- }
324
-
325
- $options = wp_rp_get_options();
326
-
327
- if(!$options['exclude_categories']) { return false; }
328
-
329
- $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'] . ')';
330
-
331
- $result = $wpdb->get_col($q);
332
-
333
- $count = (int) $result[0];
334
-
335
- return $count > 0;
336
- }
337
-
338
- function wp_rp_ajax_blogger_network_blacklist_callback() {
339
- check_ajax_referer('wp_rp_ajax_nonce');
340
- if (!current_user_can('delete_users')) {
341
- die();
342
- }
343
-
344
- $sourcefeed = (int) $_GET['sourcefeed'];
345
-
346
- $meta = wp_rp_get_meta();
347
-
348
- $blog_id = $meta['blog_id'];
349
- $auth_key = $meta['auth_key'];
350
- $req_options = array(
351
- 'timeout' => 5
352
- );
353
- $url = WP_RP_CTR_DASHBOARD_URL . "blacklist/?blog_id=$blog_id&auth_key=$auth_key&sfid=$sourcefeed";
354
- $response = wp_remote_get($url, $req_options);
355
-
356
- if (wp_remote_retrieve_response_code($response) == 200) {
357
- $body = wp_remote_retrieve_body($response);
358
- if ($body) {
359
- $doc = json_decode($body);
360
- if ($doc && $doc->status === 'ok') {
361
- header('Content-Type: text/javascript');
362
- echo "if(window['_wp_rp_blacklist_callback$sourcefeed']) window._wp_rp_blacklist_callback$sourcefeed();";
363
- }
364
- }
365
- }
366
- die();
367
- }
368
-
369
- add_action('wp_ajax_rp_blogger_network_blacklist', 'wp_rp_ajax_blogger_network_blacklist_callback');
370
-
371
- function wp_rp_head_resources() {
372
- global $post, $wpdb;
373
-
374
- //error_log("call to wp_rp_head_resources");
375
-
376
- if (wp_rp_should_exclude()) {
377
- return;
378
- }
379
-
380
- $meta = wp_rp_get_meta();
381
- $options = wp_rp_get_options();
382
- $platform_options = wp_rp_get_platform_options();
383
- //error_log('theme name 1: ' . $platform_options['theme_name']);
384
- $statistics_enabled = false;
385
- $remote_recommendations = false;
386
- $output = '';
387
-
388
- if (is_single()) {
389
- $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
390
- $remote_recommendations = $meta['remote_recommendations'] && $statistics_enabled;
391
- }
392
-
393
- $output_vars = "\twindow._wp_rp_static_base_url = '" . esc_js(WP_RP_STATIC_BASE_URL) . "';\n" .
394
- "\twindow._wp_rp_wp_ajax_url = \"" . admin_url('admin-ajax.php') . "\";\n" .
395
- "\twindow._wp_rp_plugin_version = '" . WP_RP_VERSION . "';\n" .
396
- "\twindow._wp_rp_post_id = '" . esc_js($post->ID) . "';\n" .
397
- "\twindow._wp_rp_num_rel_posts = '" . $options['max_related_posts'] . "';\n";
398
-
399
-
400
- if ($statistics_enabled) {
401
- $tags = $wpdb->get_col("SELECT DISTINCT(label) FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID ORDER BY weight desc;", 0);
402
- if (!empty($tags)) {
403
- $post_tags = '[' . implode(', ', array_map(create_function('$v', 'return "\'" . urlencode(substr($v, strpos($v, \'_\') + 1)) . "\'";'), $tags)) . ']';
404
- } else {
405
- $post_tags = '[]';
406
- }
407
-
408
- $output_vars .= "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
409
- "\twindow._wp_rp_thumbnails = " . ($platform_options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
410
- "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
411
- "\twindow._wp_rp_post_tags = {$post_tags};\n" .
412
- "\twindow._wp_rp_remote_recommendations = " . ($remote_recommendations ? 'true' : 'false') . ";\n" .
413
- "\twindow._wp_rp_promoted_content = " . ($options['promoted_content_enabled'] ? 'true' : 'false') . ";\n" .
414
- "\twindow._wp_rp_traffic_exchange = " . ($options['traffic_exchange_enabled'] ? 'true' : 'false') . ";\n" .
415
- (current_user_can('edit_posts') ?
416
- "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" .
417
- "\twindow._wp_rp_plugin_static_base_url = '" . esc_js(plugins_url('static/' , __FILE__)) . "';\n" .
418
- "\twindow._wp_rp_ajax_nonce = '" . wp_create_nonce("wp_rp_ajax_nonce") . "';\n" .
419
- "\twindow._wp_rp_erp_search = true;\n"
420
- : '');
421
- }
422
-
423
- $output .= "<script type=\"text/javascript\">\n" . $output_vars . "</script>\n";
424
-
425
- if($statistics_enabled) {
426
- $output .= '<script type="text/javascript" src="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_LOADER_FILE . '?version=' . WP_RP_VERSION . '" async></script>' . "\n";
427
- }
428
-
429
- if ($options['enable_themes']) {
430
- $theme_url = plugins_url(WP_RP_STATIC_THEMES_PATH, __FILE__);
431
-
432
- if ($platform_options['theme_name'] !== 'plain.css' && $platform_options['theme_name'] !== 'm-plain.css') {
433
- $output .= '<link rel="stylesheet" href="' . $theme_url . $platform_options['theme_name'] . '?version=' . WP_RP_VERSION . '" />' . "\n";
434
- }
435
-
436
- if ($platform_options['custom_theme_enabled']) {
437
- $output .= '<style type="text/css">' . "\n" . $platform_options['theme_custom_css'] . "</style>\n";
438
- }
439
-
440
- if ($platform_options['theme_name'] === 'm-stream.css') {
441
- wp_enqueue_script('wp_rp_infiniterecs', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_INFINITE_RECS_JS_FILE, array('jquery'), WP_RP_VERSION);
442
- }
443
-
444
- if ($platform_options['theme_name'] === 'pinterest.css') {
445
- wp_enqueue_script('wp_rp_pinterest', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_PINTEREST_JS_FILE, array('jquery'), WP_RP_VERSION);
446
- }
447
- }
448
-
449
- if (current_user_can('edit_posts') && $statistics_enabled) {
450
- wp_enqueue_style('wp_rp_edit_related_posts_css', WP_RP_STATIC_BASE_URL . 'wp-rp-css/edit_related_posts.css', array(), WP_RP_VERSION);
451
- wp_enqueue_script('wp_rp_edit_related_posts_js', WP_RP_STATIC_BASE_URL . 'js/edit_related_posts.js', array('jquery'), WP_RP_VERSION);
452
- }
453
-
454
- echo $output;
455
- }
456
-
457
- function wp_rp_get_selected_posts() {
458
- global $post;
459
-
460
- $selected_related_posts = get_post_meta($post->ID, '_wp_rp_selected_related_posts');
461
- if (empty($selected_related_posts)) {
462
- return array();
463
- }
464
-
465
- $selected_related_posts = $selected_related_posts[0];
466
- if (empty($selected_related_posts)) {
467
- return array();
468
- }
469
-
470
- $options = wp_rp_get_options();
471
- $limit = $options['max_related_posts'];
472
-
473
- return array_slice((array)$selected_related_posts, 0, $limit);
474
- }
475
-
476
- global $wp_rp_is_first_widget;
477
- $wp_rp_is_first_widget = true;
478
- function wp_rp_get_related_posts($before_title = '', $after_title = '') {
479
- if (wp_rp_should_exclude()) {
480
- return;
481
- }
482
-
483
- global $post, $wp_rp_is_first_widget;
484
- global $wp_rp_test_group; // used for AB testing on mobile
485
-
486
- $options = wp_rp_get_options();
487
- $platform_options = wp_rp_get_platform_options();
488
- $meta = wp_rp_get_meta();
489
-
490
- $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
491
- $remote_recommendations = is_single() && $meta['remote_recommendations'] && $statistics_enabled;
492
-
493
- $posts_and_title = wp_rp_fetch_posts_and_title();
494
- $related_posts = $posts_and_title['posts'];
495
- $title = $posts_and_title['title'];
496
-
497
- $selected_related_posts = wp_rp_get_selected_posts();
498
-
499
- $related_posts_content = "";
500
-
501
- if (!$related_posts) {
502
- return;
503
- }
504
-
505
- $posts_footer = '';
506
- if (current_user_can('edit_posts') && $statistics_enabled) {
507
- $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_edit" href="#" id="wp_rp_edit_related_posts">Edit Related Posts</a></div>';
508
- }
509
- if ($options['display_zemanta_linky']) {
510
- $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_backlink" target="_blank" href="http://www.zemanta.com/?wp-related-posts">Zemanta</a></div>';
511
- }
512
-
513
- $css_classes = 'related_post wp_rp';
514
- $css_classes_wrap = ' ' . str_replace(array('.css', '-'), array('', '_'), esc_attr('wp_rp_' . $platform_options['theme_name']));
515
-
516
- $related_posts_lis = wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts);
517
- $related_posts_ul = '<ul class="' . $css_classes . '" style="visibility: ' . ($remote_recommendations ? 'hidden' : 'visible') . '">' . $related_posts_lis . '</ul>';
518
-
519
- $related_posts_title = $title ? ($before_title ? $before_title . $title . $after_title : '<h3 class="related_post_title">' . $title . '</h3>') : '';
520
-
521
- $first_id_attr = '';
522
- if($wp_rp_is_first_widget) {
523
- $wp_rp_is_first_widget = false;
524
- $first_id_attr = 'id="wp_rp_first"';
525
- }
526
-
527
- $wrap_style = '';
528
- //error_log('test group when content:' . $wp_rp_test_group);
529
- if ($wp_rp_test_group == 2) {
530
- $wrap_style = ' style="display:none;"';
531
- }
532
-
533
- $output = '<div class="wp_rp_wrap ' . $css_classes_wrap . '" ' . $first_id_attr . $wrap_style . '>' .
534
- '<div class="wp_rp_content">' .
535
- $related_posts_title .
536
- $related_posts_ul .
537
- $posts_footer .
538
- '</div>' .
539
- ($remote_recommendations ? '<script type="text/javascript">window._wp_rp_callback_widget_exists ? window._wp_rp_callback_widget_exists() : false;</script>' : '') .
540
- '</div>';
541
-
542
- return "\n" . $output . "\n";
543
- }
544
-
1
+ <?php
2
+ /*
3
+ Plugin Name: WordPress Related Posts
4
+ Version: 3.0
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. Click on <a href="admin.php?page=wordpress-related-posts">Related Posts tab</a> to configure your settings.
7
+ Author: Zemanta Ltd.
8
+ Author URI: http://www.zemanta.com
9
+ */
10
+
11
+ define('WP_RP_VERSION', '3.0');
12
+
13
+ define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
14
+
15
+ include_once(dirname(__FILE__) . '/config.php');
16
+ include_once(dirname(__FILE__) . '/lib/stemmer.php');
17
+ include_once(dirname(__FILE__) . '/lib/mobile_detect.php');
18
+
19
+ include_once(dirname(__FILE__) . '/admin_notices.php');
20
+ include_once(dirname(__FILE__) . '/notifications.php');
21
+ include_once(dirname(__FILE__) . '/widget.php');
22
+ include_once(dirname(__FILE__) . '/thumbnailer.php');
23
+ include_once(dirname(__FILE__) . '/settings.php');
24
+ include_once(dirname(__FILE__) . '/recommendations.php');
25
+ include_once(dirname(__FILE__) . '/dashboard_widget.php');
26
+ include_once(dirname(__FILE__) . '/edit_related_posts.php');
27
+ include_once(dirname(__FILE__) . '/compatibility.php');
28
+
29
+ register_activation_hook(__FILE__, 'wp_rp_activate_hook');
30
+ register_deactivation_hook(__FILE__, 'wp_rp_deactivate_hook');
31
+
32
+ add_action('wp_head', 'wp_rp_head_resources');
33
+ add_action('wp_before_admin_bar_render', 'wp_rp_extend_adminbar');
34
+
35
+ add_action('plugins_loaded', 'wp_rp_init_zemanta');
36
+ function wp_rp_init_zemanta() {
37
+ include_once(dirname(__FILE__) . '/zemanta/zemanta.php');
38
+ if (wp_rp_is_classic()) {
39
+ $wprp_zemanta = new WPRPZemanta();
40
+ }
41
+ }
42
+
43
+ function wp_rp_extend_adminbar() {
44
+ global $wp_admin_bar;
45
+
46
+ if(!is_super_admin() || !is_admin_bar_showing())
47
+ return;
48
+
49
+ $wp_admin_bar->add_menu(array(
50
+ 'id' => 'wp_rp_adminbar_menu',
51
+ 'title' => __('Related Posts', 'wp_related_posts'),
52
+ 'href' => admin_url('admin.php?page=wordpress-related-posts&ref=adminbar')
53
+ ));
54
+ }
55
+
56
+ global $wp_rp_output;
57
+ $wp_rp_output = array();
58
+ function wp_rp_add_related_posts_hook($content) {
59
+ global $wp_rp_output, $post;
60
+
61
+ $options = wp_rp_get_options();
62
+
63
+ if ($post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
64
+ if (!isset($wp_rp_output[$post->ID])) {
65
+ $wp_rp_output[$post->ID] = wp_rp_get_related_posts();
66
+ }
67
+ $content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
68
+ $content = $content . $wp_rp_output[$post->ID];
69
+ }
70
+
71
+ return $content;
72
+ }
73
+ add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
74
+
75
+ global $wp_rp_is_phone;
76
+ function wp_rp_is_phone() {
77
+ global $wp_rp_is_phone;
78
+
79
+ if (!isset($wp_rp_is_phone)) {
80
+ $detect = new WpRpMobileDetect();
81
+ $wp_rp_is_phone = $detect->isMobile() && !$detect->isTablet();
82
+ }
83
+
84
+ return $wp_rp_is_phone;
85
+ }
86
+
87
+ function wp_rp_get_platform_options() {
88
+ $options = wp_rp_get_options();
89
+
90
+ if (wp_rp_is_phone()) {
91
+ return $options['mobile'];
92
+ }
93
+ return $options['desktop'];
94
+ }
95
+
96
+ function wp_rp_ajax_load_articles_callback() {
97
+ global $post;
98
+
99
+ $getdata = stripslashes_deep($_GET);
100
+ if (!isset($getdata['post_id'])) {
101
+ die('error');
102
+ }
103
+
104
+ $post = get_post($getdata['post_id']);
105
+ if(!$post) {
106
+ die('error');
107
+ }
108
+
109
+ $from = (isset($getdata['from']) && is_numeric($getdata['from'])) ? intval($getdata['from']) : 0;
110
+ $count = (isset($getdata['count']) && is_numeric($getdata['count'])) ? intval($getdata['count']) : 50;
111
+
112
+ $search = isset($getdata['search']) && $getdata['search'] ? $getdata['search'] : false;
113
+
114
+ $image_size = isset($getdata['size']) ? $getdata['size'] : 'thumbnail';
115
+ if(!($image_size == 'thumbnail' || $image_size == 'full')) {
116
+ die('error');
117
+ }
118
+
119
+ $limit = $count + $from;
120
+
121
+ if ($search) {
122
+ $the_query = new WP_Query(array(
123
+ 's' => $search,
124
+ 'post_type' => 'post',
125
+ 'post_status'=>'publish',
126
+ 'post_count' => $limit));
127
+ $related_posts = $the_query->get_posts();
128
+ } else {
129
+ $related_posts = array();
130
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
131
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
132
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
133
+ }
134
+
135
+ if(function_exists('qtrans_postsFilter')) {
136
+ $related_posts = qtrans_postsFilter($related_posts);
137
+ }
138
+
139
+ $response_list = array();
140
+
141
+ foreach (array_slice($related_posts, $from) as $related_post) {
142
+ array_push($response_list, array(
143
+ 'id' => $related_post->ID,
144
+ 'url' => get_permalink($related_post->ID),
145
+ 'title' => $related_post->post_title,
146
+ 'img' => wp_rp_get_post_thumbnail_img($related_post, $image_size)
147
+ ));
148
+ }
149
+
150
+ header('Content-Type: text/javascript');
151
+ die(json_encode($response_list));
152
+ }
153
+ add_action('wp_ajax_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
154
+ add_action('wp_ajax_nopriv_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
155
+
156
+ function wp_rp_append_posts(&$related_posts, $fetch_function_name, $limit) {
157
+ $options = wp_rp_get_options();
158
+
159
+ $len = sizeof($related_posts);
160
+ $num_missing_posts = $limit - $len;
161
+ if ($num_missing_posts > 0) {
162
+ $exclude_ids = array_map(create_function('$p', 'return $p->ID;'), $related_posts);
163
+
164
+ $posts = call_user_func($fetch_function_name, $num_missing_posts, $exclude_ids);
165
+ if ($posts) {
166
+ $related_posts = array_merge($related_posts, $posts);
167
+ }
168
+ }
169
+ }
170
+
171
+ function wp_rp_fetch_posts_and_title() {
172
+ $options = wp_rp_get_options();
173
+
174
+ $limit = $options['max_related_posts'];
175
+ $title = $options["related_posts_title"];
176
+
177
+ $related_posts = array();
178
+
179
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
180
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
181
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
182
+
183
+ if(function_exists('qtrans_postsFilter')) {
184
+ $related_posts = qtrans_postsFilter($related_posts);
185
+ }
186
+
187
+ return array(
188
+ "posts" => $related_posts,
189
+ "title" => $title
190
+ );
191
+ }
192
+
193
+ function wp_rp_get_next_post(&$related_posts, &$selected_related_posts, &$inserted_urls, &$special_urls, $default_post_type) {
194
+ $post = false;
195
+
196
+ while (!($post && $post->ID) && !(empty($related_posts) && empty($selected_related_posts))) {
197
+ $post_type = $default_post_type;
198
+
199
+ $post = array_shift($selected_related_posts);
200
+
201
+ if ($post && $post->type) {
202
+ $post_type = $post->type;
203
+ }
204
+
205
+ if (!$post || !$post->ID) {
206
+ while (!empty($related_posts) && (!($post = array_shift($related_posts)) || isset($special_urls[get_permalink($post->ID)])));
207
+ }
208
+
209
+ if ($post && $post->ID) {
210
+ $post_url = property_exists($post, 'post_url') ? $post->post_url : get_permalink($post->ID);
211
+ if (isset($inserted_urls[$post_url])) {
212
+ $post = false;
213
+ } else {
214
+ $post->type = $post_type;
215
+ }
216
+ }
217
+ }
218
+
219
+ if (!$post || !$post->ID) {
220
+ return false;
221
+ }
222
+
223
+ $inserted_urls[$post_url] = true;
224
+
225
+ return $post;
226
+ }
227
+
228
+ function wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts) {
229
+ $options = wp_rp_get_options();
230
+ $platform_options = wp_rp_get_platform_options();
231
+ $output = "";
232
+
233
+ $statistics_enabled = $options['ctr_dashboard_enabled'];
234
+
235
+ $limit = $options['max_related_posts'];
236
+
237
+ $inserted_urls = array(); // Used to prevent duplicates
238
+ $special_urls = array();
239
+
240
+ foreach ($selected_related_posts as $post) {
241
+ if (property_exists($post, 'post_url') && $post->post_url) {
242
+ $special_urls[$post->post_url] = true;
243
+ }
244
+ }
245
+
246
+ $default_post_type = empty($selected_related_posts) ? 'none' : 'empty';
247
+
248
+ $image_size = ($platform_options['theme_name'] == 'pinterest.css') ? 'full' : 'thumbnail';
249
+
250
+ for ($i = 0; $i < $limit; $i++) {
251
+ $related_post = wp_rp_get_next_post($related_posts, $selected_related_posts, $inserted_urls, $special_urls, $default_post_type);
252
+
253
+ if (!$related_post) {
254
+ break;
255
+ }
256
+
257
+ if (property_exists($related_post, 'type')) {
258
+ $post_type = $related_post->type;
259
+ } else {
260
+ $post_type = $default_post_type;
261
+ }
262
+
263
+ if (in_array($post_type, array('empty', 'none'))) {
264
+ $post_id = 'in-' . $related_post->ID;
265
+ } else {
266
+ $post_id = 'ex-' . $related_post->ID;
267
+ }
268
+
269
+ $data_attrs = '';
270
+ if ($statistics_enabled) {
271
+ $data_attrs .= 'data-position="' . $i . '" data-poid="' . $post_id . '" data-post-type="' . $post_type . '" ';
272
+ }
273
+
274
+ $output .= '<li ' . $data_attrs . '>';
275
+
276
+ $post_url = property_exists($related_post, 'post_url') ? $related_post->post_url : get_permalink($related_post->ID);
277
+
278
+ $img = wp_rp_get_post_thumbnail_img($related_post, $image_size);
279
+ if ($img) {
280
+ $output .= '<a href="' . $post_url . '" class="wp_rp_thumbnail">' . $img . '</a>';
281
+ }
282
+
283
+ if ($platform_options["display_publish_date"]){
284
+ $dateformat = get_option('date_format');
285
+ $output .= mysql2date($dateformat, $related_post->post_date) . " -- ";
286
+ }
287
+
288
+ $output .= '<a href="' . $post_url . '" class="wp_rp_title">' . wptexturize($related_post->post_title) . '</a>';
289
+
290
+ if ($platform_options["display_comment_count"] && property_exists($related_post, 'comment_count')){
291
+ $output .= " (" . $related_post->comment_count . ")";
292
+ }
293
+
294
+ if ($platform_options["display_excerpt"]){
295
+ $excerpt_max_length = $platform_options["excerpt_max_length"];
296
+ $excerpt = '';
297
+
298
+ if ($related_post->post_excerpt){
299
+ $excerpt = strip_shortcodes(strip_tags($related_post->post_excerpt));
300
+ }
301
+ if (!$excerpt) {
302
+ $excerpt = strip_shortcodes(strip_tags($related_post->post_content));
303
+ }
304
+
305
+ if ($excerpt) {
306
+ if (strlen($excerpt) > $excerpt_max_length) {
307
+ $excerpt = mb_substr($excerpt, 0, $excerpt_max_length - 3) . '...';
308
+ }
309
+ $output .= '<br /><small>' . $excerpt . '</small>';
310
+ }
311
+ }
312
+ $output .= '</li>';
313
+ }
314
+
315
+ return $output;
316
+ }
317
+
318
+ function wp_rp_should_exclude() {
319
+ global $wpdb, $post;
320
+
321
+ if (!$post || !$post->ID) {
322
+ return true;
323
+ }
324
+
325
+ $options = wp_rp_get_options();
326
+
327
+ if(!$options['exclude_categories']) { return false; }
328
+
329
+ $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'] . ')';
330
+
331
+ $result = $wpdb->get_col($q);
332
+
333
+ $count = (int) $result[0];
334
+
335
+ return $count > 0;
336
+ }
337
+
338
+ function wp_rp_ajax_blogger_network_blacklist_callback() {
339
+ check_ajax_referer('wp_rp_ajax_nonce');
340
+ if (!current_user_can('delete_users')) {
341
+ die();
342
+ }
343
+
344
+ $sourcefeed = (int) $_GET['sourcefeed'];
345
+
346
+ $meta = wp_rp_get_meta();
347
+
348
+ $blog_id = $meta['blog_id'];
349
+ $auth_key = $meta['auth_key'];
350
+ $req_options = array(
351
+ 'timeout' => 5
352
+ );
353
+ $url = WP_RP_CTR_DASHBOARD_URL . "blacklist/?blog_id=$blog_id&auth_key=$auth_key&sfid=$sourcefeed";
354
+ $response = wp_remote_get($url, $req_options);
355
+
356
+ if (wp_remote_retrieve_response_code($response) == 200) {
357
+ $body = wp_remote_retrieve_body($response);
358
+ if ($body) {
359
+ $doc = json_decode($body);
360
+ if ($doc && $doc->status === 'ok') {
361
+ header('Content-Type: text/javascript');
362
+ echo "if(window['_wp_rp_blacklist_callback$sourcefeed']) window._wp_rp_blacklist_callback$sourcefeed();";
363
+ }
364
+ }
365
+ }
366
+ die();
367
+ }
368
+
369
+ add_action('wp_ajax_rp_blogger_network_blacklist', 'wp_rp_ajax_blogger_network_blacklist_callback');
370
+
371
+ function wp_rp_head_resources() {
372
+ global $post, $wpdb;
373
+
374
+ //error_log("call to wp_rp_head_resources");
375
+
376
+ if (wp_rp_should_exclude()) {
377
+ return;
378
+ }
379
+
380
+ $meta = wp_rp_get_meta();
381
+ $options = wp_rp_get_options();
382
+ $platform_options = wp_rp_get_platform_options();
383
+ //error_log('theme name 1: ' . $platform_options['theme_name']);
384
+ $statistics_enabled = false;
385
+ $remote_recommendations = false;
386
+ $output = '';
387
+
388
+ if (is_single()) {
389
+ $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
390
+ $remote_recommendations = $meta['remote_recommendations'] && $statistics_enabled;
391
+ }
392
+
393
+ $output_vars = "\twindow._wp_rp_static_base_url = '" . esc_js(WP_RP_STATIC_BASE_URL) . "';\n" .
394
+ "\twindow._wp_rp_wp_ajax_url = \"" . admin_url('admin-ajax.php') . "\";\n" .
395
+ "\twindow._wp_rp_plugin_version = '" . WP_RP_VERSION . "';\n" .
396
+ "\twindow._wp_rp_post_id = '" . esc_js($post->ID) . "';\n" .
397
+ "\twindow._wp_rp_num_rel_posts = '" . $options['max_related_posts'] . "';\n";
398
+
399
+
400
+ if ($statistics_enabled) {
401
+ $tags = $wpdb->get_col("SELECT DISTINCT(label) FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID ORDER BY weight desc;", 0);
402
+ if (!empty($tags)) {
403
+ $post_tags = '[' . implode(', ', array_map(create_function('$v', 'return "\'" . urlencode(substr($v, strpos($v, \'_\') + 1)) . "\'";'), $tags)) . ']';
404
+ } else {
405
+ $post_tags = '[]';
406
+ }
407
+
408
+ $output_vars .= "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
409
+ "\twindow._wp_rp_thumbnails = " . ($platform_options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
410
+ "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
411
+ "\twindow._wp_rp_post_tags = {$post_tags};\n" .
412
+ "\twindow._wp_rp_remote_recommendations = " . ($remote_recommendations ? 'true' : 'false') . ";\n" .
413
+ "\twindow._wp_rp_promoted_content = " . ($options['promoted_content_enabled'] ? 'true' : 'false') . ";\n" .
414
+ "\twindow._wp_rp_traffic_exchange = " . ($options['traffic_exchange_enabled'] ? 'true' : 'false') . ";\n" .
415
+ (current_user_can('edit_posts') ?
416
+ "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" .
417
+ "\twindow._wp_rp_plugin_static_base_url = '" . esc_js(plugins_url('static/' , __FILE__)) . "';\n" .
418
+ "\twindow._wp_rp_ajax_nonce = '" . wp_create_nonce("wp_rp_ajax_nonce") . "';\n" .
419
+ "\twindow._wp_rp_erp_search = true;\n"
420
+ : '');
421
+ }
422
+
423
+ $output .= "<script type=\"text/javascript\">\n" . $output_vars . "</script>\n";
424
+
425
+ if($statistics_enabled) {
426
+ $output .= '<script type="text/javascript" src="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_LOADER_FILE . '?version=' . WP_RP_VERSION . '" async></script>' . "\n";
427
+ }
428
+
429
+ if ($options['enable_themes']) {
430
+ $theme_url = plugins_url(WP_RP_STATIC_THEMES_PATH, __FILE__);
431
+
432
+ if ($platform_options['theme_name'] !== 'plain.css' && $platform_options['theme_name'] !== 'm-plain.css') {
433
+ $output .= '<link rel="stylesheet" href="' . $theme_url . $platform_options['theme_name'] . '?version=' . WP_RP_VERSION . '" />' . "\n";
434
+ }
435
+
436
+ if ($platform_options['custom_theme_enabled']) {
437
+ $output .= '<style type="text/css">' . "\n" . $platform_options['theme_custom_css'] . "</style>\n";
438
+ }
439
+
440
+ if ($platform_options['theme_name'] === 'm-stream.css') {
441
+ wp_enqueue_script('wp_rp_infiniterecs', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_INFINITE_RECS_JS_FILE, array('jquery'), WP_RP_VERSION);
442
+ }
443
+
444
+ if ($platform_options['theme_name'] === 'pinterest.css') {
445
+ wp_enqueue_script('wp_rp_pinterest', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_PINTEREST_JS_FILE, array('jquery'), WP_RP_VERSION);
446
+ }
447
+ }
448
+
449
+ if (current_user_can('edit_posts') && $statistics_enabled) {
450
+ wp_enqueue_style('wp_rp_edit_related_posts_css', WP_RP_STATIC_BASE_URL . 'wp-rp-css/edit_related_posts.css', array(), WP_RP_VERSION);
451
+ wp_enqueue_script('wp_rp_edit_related_posts_js', WP_RP_STATIC_BASE_URL . 'js/edit_related_posts.js', array('jquery'), WP_RP_VERSION);
452
+ }
453
+
454
+ echo $output;
455
+ }
456
+
457
+ function wp_rp_get_selected_posts() {
458
+ global $post;
459
+
460
+ $selected_related_posts = get_post_meta($post->ID, '_wp_rp_selected_related_posts');
461
+ if (empty($selected_related_posts)) {
462
+ return array();
463
+ }
464
+
465
+ $selected_related_posts = $selected_related_posts[0];
466
+ if (empty($selected_related_posts)) {
467
+ return array();
468
+ }
469
+
470
+ $options = wp_rp_get_options();
471
+ $limit = $options['max_related_posts'];
472
+
473
+ return array_slice((array)$selected_related_posts, 0, $limit);
474
+ }
475
+
476
+ global $wp_rp_is_first_widget;
477
+ $wp_rp_is_first_widget = true;
478
+ function wp_rp_get_related_posts($before_title = '', $after_title = '') {
479
+ if (wp_rp_should_exclude()) {
480
+ return;
481
+ }
482
+
483
+ global $post, $wp_rp_is_first_widget;
484
+ global $wp_rp_test_group; // used for AB testing on mobile
485
+
486
+ $options = wp_rp_get_options();
487
+ $platform_options = wp_rp_get_platform_options();
488
+ $meta = wp_rp_get_meta();
489
+
490
+ $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
491
+ $remote_recommendations = is_single() && $meta['remote_recommendations'] && $statistics_enabled;
492
+
493
+ $posts_and_title = wp_rp_fetch_posts_and_title();
494
+ $related_posts = $posts_and_title['posts'];
495
+ $title = $posts_and_title['title'];
496
+
497
+ $selected_related_posts = wp_rp_get_selected_posts();
498
+
499
+ $related_posts_content = "";
500
+
501
+ if (!$related_posts) {
502
+ return;
503
+ }
504
+
505
+ $posts_footer = '';
506
+ if (current_user_can('edit_posts') && $statistics_enabled) {
507
+ $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_edit" href="#" id="wp_rp_edit_related_posts">Edit Related Posts</a></div>';
508
+ }
509
+ if ($options['display_zemanta_linky']) {
510
+ $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_backlink" target="_blank" href="http://www.zemanta.com/?wp-related-posts" rel="nofollow">Zemanta</a></div>';
511
+ }
512
+
513
+ $css_classes = 'related_post wp_rp';
514
+ $css_classes_wrap = ' ' . str_replace(array('.css', '-'), array('', '_'), esc_attr('wp_rp_' . $platform_options['theme_name']));
515
+
516
+ $related_posts_lis = wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts);
517
+ $related_posts_ul = '<ul class="' . $css_classes . '" style="visibility: ' . ($remote_recommendations ? 'hidden' : 'visible') . '">' . $related_posts_lis . '</ul>';
518
+
519
+ $related_posts_title = $title ? ($before_title ? $before_title . $title . $after_title : '<h3 class="related_post_title">' . $title . '</h3>') : '';
520
+
521
+ $first_id_attr = '';
522
+ if($wp_rp_is_first_widget) {
523
+ $wp_rp_is_first_widget = false;
524
+ $first_id_attr = 'id="wp_rp_first"';
525
+ }
526
+
527
+ $wrap_style = '';
528
+ //error_log('test group when content:' . $wp_rp_test_group);
529
+ if ($wp_rp_test_group == 2) {
530
+ $wrap_style = ' style="display:none;"';
531
+ }
532
+
533
+ $output = '<div class="wp_rp_wrap ' . $css_classes_wrap . '" ' . $first_id_attr . $wrap_style . '>' .
534
+ '<div class="wp_rp_content">' .
535
+ $related_posts_title .
536
+ $related_posts_ul .
537
+ $posts_footer .
538
+ '</div>' .
539
+ ($remote_recommendations ? '<script type="text/javascript">window._wp_rp_callback_widget_exists ? window._wp_rp_callback_widget_exists() : false;</script>' : '') .
540
+ '</div>';
541
+
542
+ return "\n" . $output . "\n";
543
+ }
544
+