WordPress Related Posts - Version 2.8

Version Description

  • New thumbnailer
  • Edit Related Posts gets a visual upgrade, search functionality and a dose of awesome
  • Removed AB testing on mobile infinite stream
  • Bugfix in tag generator
Download this release

Release Info

Developer zemanta
Plugin Icon wp plugin WordPress Related Posts
Version 2.8
Comparing to
See all releases

Code changes from version 2.7 to 2.8

Files changed (5) hide show
  1. config.php +18 -3
  2. readme.txt +7 -1
  3. recommendations.php +1 -0
  4. thumbnailer.php +261 -143
  5. wp_related_posts.php +26 -137
config.php CHANGED
@@ -16,12 +16,15 @@ ul.related_post li a {
16
  ul.related_post li img {
17
  }");
18
 
 
 
19
  define('WP_RP_THUMBNAILS_WIDTH', 150);
20
  define('WP_RP_THUMBNAILS_HEIGHT', 150);
21
  define('WP_RP_THUMBNAILS_DEFAULTS_COUNT', 31);
22
 
23
- define("WP_RP_CTR_DASHBOARD_URL", "http://d.related-posts.com/");
24
- define("WP_RP_CTR_REPORT_URL", "http://t.related-posts.com/pageview/?");
 
25
  define("WP_RP_STATIC_CTR_PAGEVIEW_FILE", "js/pageview.js");
26
 
27
  define("WP_RP_STATIC_RECOMMENDATIONS_JS_FILE", "js/recommendations.js");
@@ -153,7 +156,7 @@ function wp_rp_related_posts_db_table_install() {
153
  $sql_tags = "CREATE TABLE $tags_table_name (
154
  post_id mediumint(9),
155
  post_date datetime NOT NULL,
156
- label VARCHAR(32) NOT NULL,
157
  weight float,
158
  INDEX post_id (post_id),
159
  INDEX label (label)
@@ -230,6 +233,18 @@ function wp_rp_install() {
230
  wp_rp_related_posts_db_table_install();
231
  }
232
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  function wp_rp_migrate_2_6() {
234
  $wp_rp_meta = get_option('wp_rp_meta');
235
  $wp_rp_meta['version'] = '2.7';
16
  ul.related_post li img {
17
  }");
18
 
19
+ define('WP_RP_THUMBNAILS_NAME', 'wp_rp_thumbnail');
20
+ define('WP_RP_THUMBNAILS_PROP_NAME', 'wp_rp_thumbnail_prop');
21
  define('WP_RP_THUMBNAILS_WIDTH', 150);
22
  define('WP_RP_THUMBNAILS_HEIGHT', 150);
23
  define('WP_RP_THUMBNAILS_DEFAULTS_COUNT', 31);
24
 
25
+ define("WP_RP_MAX_LABEL_LENGTH", 32);
26
+
27
+ define("WP_RP_CTR_DASHBOARD_URL", "http://d.zemanta.com/");
28
  define("WP_RP_STATIC_CTR_PAGEVIEW_FILE", "js/pageview.js");
29
 
30
  define("WP_RP_STATIC_RECOMMENDATIONS_JS_FILE", "js/recommendations.js");
156
  $sql_tags = "CREATE TABLE $tags_table_name (
157
  post_id mediumint(9),
158
  post_date datetime NOT NULL,
159
+ label VARCHAR(" . WP_RP_MAX_LABEL_LENGTH . ") NOT NULL,
160
  weight float,
161
  INDEX post_id (post_id),
162
  INDEX label (label)
233
  wp_rp_related_posts_db_table_install();
234
  }
235
 
236
+ function wp_rp_migrate_2_7() {
237
+ global $wpdb;
238
+
239
+ $wp_rp_meta = get_option('wp_rp_meta');
240
+ $wp_rp_meta['version'] = '2.8';
241
+ $wp_rp_meta['new_user'] = false;
242
+
243
+ $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key IN ('_wp_rp_extracted_image_url', '_wp_rp_extracted_image_url_full')");
244
+
245
+ update_option('wp_rp_meta', $wp_rp_meta);
246
+ }
247
+
248
  function wp_rp_migrate_2_6() {
249
  $wp_rp_meta = get_option('wp_rp_meta');
250
  $wp_rp_meta['version'] = '2.7';
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.7
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
@@ -75,6 +75,12 @@ Fix for security vulnerability. Upgrade immediately.
75
 
76
  == Changelog ==
77
 
 
 
 
 
 
 
78
  = 2.7 =
79
  * Improved thumbnailer
80
 
4
  License: GPLv2
5
  Requires at least: 3.3
6
  Tested up to: 3.6
7
+ Stable tag: 2.8
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
75
 
76
  == Changelog ==
77
 
78
+ = 2.8 =
79
+ * New thumbnailer
80
+ * Edit Related Posts gets a visual upgrade, search functionality and a dose of awesome
81
+ * Removed AB testing on mobile infinite stream
82
+ * Bugfix in tag generator
83
+
84
  = 2.7 =
85
  * Improved thumbnailer
86
 
recommendations.php CHANGED
@@ -128,6 +128,7 @@ function wp_rp_generate_tags($post) {
128
 
129
  foreach ($tag_obj['labels'] as $label) {
130
  $label = $tag_obj['prefix'] . strtolower($label);
 
131
 
132
  array_push($all_tags, $label);
133
 
128
 
129
  foreach ($tag_obj['labels'] as $label) {
130
  $label = $tag_obj['prefix'] . strtolower($label);
131
+ $label = substr($label, 0, WP_RP_MAX_LABEL_LENGTH);
132
 
133
  array_push($all_tags, $label);
134
 
thumbnailer.php CHANGED
@@ -1,167 +1,171 @@
1
  <?php
2
 
3
- function wp_rp_upload_default_thumbnail_file() {
4
- if (!empty($_FILES['wp_rp_default_thumbnail'])) {
5
- $file = $_FILES['wp_rp_default_thumbnail'];
6
- if(isset($file['error']) && $file['error'] === UPLOAD_ERR_NO_FILE) {
7
- return false;
8
- }
9
 
10
- $upload = wp_handle_upload($file, array('test_form' => false));
11
- if(isset($upload['error'])) {
12
- return new WP_Error('upload_error', $upload['error']);
13
- } else if(isset($upload['file'])) {
14
- $upload_dir = wp_upload_dir();
15
-
16
- if (function_exists('wp_get_image_editor')) { // WP 3.5+
17
- $image = wp_get_image_editor($upload['file']);
18
 
19
- $suffix = WP_RP_THUMBNAILS_WIDTH . 'x' . WP_RP_THUMBNAILS_HEIGHT;
20
- $resized_img_path = $image->generate_filename($suffix, $upload_dir['path'], 'jpg');
21
 
22
- $image->resize(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
23
- $image->save($resized_img_path, 'image/jpeg');
 
24
 
25
- return $upload_dir['url'] . '/' . urlencode(wp_basename($resized_img_path));
26
- } else {
27
- $path = image_resize($upload['file'], WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
28
- if (!is_wp_error($path)) {
29
- return $upload_dir['url'] . '/' . wp_basename($path);
30
- } else if (array_key_exists('error_getting_dimensions', $path->errors)) {
31
- return $upload['url'];
32
- }
33
- return $path;
34
- }
35
- }
36
  }
37
- return false;
38
- }
39
 
40
- function wp_rp_get_default_thumbnail_url($seed = false, $size = 'thumbnail') {
41
- $options = wp_rp_get_options();
42
- $upload_dir = wp_upload_dir();
43
 
44
- if ($options['default_thumbnail_path']) {
45
- return $options['default_thumbnail_path'];
46
- } else {
47
- if ($seed) {
48
- $next_seed = rand();
49
- srand($seed);
50
- }
51
- $file = rand(0, WP_RP_THUMBNAILS_DEFAULTS_COUNT - 1) . '.jpg';
52
- if ($seed) {
53
- srand($next_seed);
54
  }
55
- return plugins_url('/static/thumbs/' . $file, __FILE__);
56
  }
57
- }
58
 
59
- function wp_rp_direct_filesystem_method() {
60
- return 'direct';
61
  }
62
 
63
- function wp_rp_save_and_resize_image($url, $upload_dir, $wp_filesystem) {
64
- $http_response = wp_remote_get($url, array('timeout' => 10));
65
- if(is_wp_error($http_response)) {
66
- return false;
67
- }
68
- $img_data = wp_remote_retrieve_body($http_response);
69
 
70
- $img_name = wp_unique_filename($upload_dir['path'], wp_basename(parse_url($url, PHP_URL_PATH)));
71
- $img_path = $upload_dir['path'] . '/' . $img_name;
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- if(!$wp_filesystem->put_contents($img_path, $img_data, FS_CHMOD_FILE)) {
 
 
74
  return false;
75
  }
76
 
77
- if (function_exists('wp_get_image_editor')) { // WP 3.5+
78
- $image = wp_get_image_editor($img_path);
 
 
79
 
80
- $suffix = WP_RP_THUMBNAILS_WIDTH . 'x' . WP_RP_THUMBNAILS_HEIGHT;
81
- $resized_img_path = $image->generate_filename($suffix, $upload_dir['path'], 'jpg');
82
-
83
- $image->resize(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
84
- $image->save($resized_img_path, 'image/jpeg');
85
- } else {
86
- $resized_img_path = image_resize($img_path, WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
87
- if (is_wp_error($resized_img_path) && array_key_exists('error_getting_dimensions', $resized_img_path->errors)) {
88
- $resized_img_path = $img_path;
89
- }
90
  }
91
 
92
- if(is_wp_error($resized_img_path)) {
 
 
93
  return false;
94
  }
95
 
96
- $thumbnail_img_url = $upload_dir['url'] . '/' . urlencode(wp_basename($resized_img_path));
97
- $full_img_url = $upload_dir['url'] . '/' . urlencode(wp_basename($img_path));
98
 
99
- return array(
100
- 'thumbnail' => $thumbnail_img_url,
101
- 'full' => $full_img_url
102
- );
 
 
 
 
103
  }
104
 
105
- function wp_rp_actually_extract_images_from_post_html($post, $upload_dir, $wp_filesystem) {
106
  $content = $post->post_content;
107
- preg_match_all('/<img (?:[^>]+ )?src="([^"]+)"/', $content, $matches);
108
- $urls = $matches[1];
109
 
110
- $imgs = false;
 
 
 
 
 
111
 
112
- if(count($urls) == 0) {
113
- return $imgs;
114
  }
115
- array_splice($urls, 10);
 
 
 
 
116
 
117
- foreach ($urls as $url) {
118
- $imgs = wp_rp_save_and_resize_image(html_entity_decode($url), $upload_dir, $wp_filesystem);
119
- if ($imgs) {
120
- break;
121
  }
122
  }
123
 
124
- return $imgs;
 
 
 
 
 
 
 
 
 
 
 
 
125
  }
126
 
127
  function wp_rp_cron_do_extract_images_from_post($post_id, $attachment_id) {
 
 
 
128
  $post_id = (int) $post_id;
129
  $attachment_id = (int) $attachment_id;
130
  $post = get_post($post_id);
131
 
132
- $upload_dir = wp_upload_dir();
133
- if($upload_dir['error'] !== false) {
134
- return false;
135
- }
136
- require_once(ABSPATH . 'wp-admin/includes/file.php');
137
-
138
- global $wp_filesystem;
139
- add_filter('filesystem_method', 'wp_rp_direct_filesystem_method');
140
- WP_Filesystem();
141
-
142
  if ($attachment_id) {
143
- $imgs = wp_rp_save_and_resize_image(wp_get_attachment_url($attachment_id), $upload_dir, $wp_filesystem);
144
  } else {
145
- $imgs = wp_rp_actually_extract_images_from_post_html($post, $upload_dir, $wp_filesystem);
146
  }
147
 
148
- remove_filter('filesystem_method', 'wp_rp_direct_filesystem_method');
149
-
150
- if($imgs) {
151
- update_post_meta($post_id, '_wp_rp_extracted_image_url', $imgs['thumbnail']);
152
- update_post_meta($post_id, '_wp_rp_extracted_image_url_full', $imgs['full']);
153
  }
154
  }
155
  add_action('wp_rp_cron_extract_images_from_post', 'wp_rp_cron_do_extract_images_from_post', 10, 2);
156
 
157
  function wp_rp_extract_images_from_post($post, $attachment_id=null) {
158
- update_post_meta($post->ID, '_wp_rp_extracted_image_url', '');
159
- update_post_meta($post->ID, '_wp_rp_extracted_image_url_full', '');
160
  if(empty($post->post_content) && !$attachment_id) { return; }
161
 
 
162
  wp_schedule_single_event(time(), 'wp_rp_cron_extract_images_from_post', array($post->ID, $attachment_id));
163
  }
164
 
 
 
 
 
 
165
  function wp_rp_post_save_update_image($post_id) {
166
  $post = get_post($post_id);
167
 
@@ -169,33 +173,98 @@ function wp_rp_post_save_update_image($post_id) {
169
  return;
170
  }
171
 
172
- delete_post_meta($post->ID, '_wp_rp_extracted_image_url');
173
- delete_post_meta($post->ID, '_wp_rp_extracted_image_url_full');
174
 
175
  wp_rp_get_post_thumbnail_img($post);
176
  }
177
  add_action('save_post', 'wp_rp_post_save_update_image');
178
 
 
 
 
 
 
179
  function wp_rp_get_img_tag($src, $alt) {
180
  return '<img src="'. esc_attr($src) . '" alt="' . esc_attr($alt) . '" />';
181
  }
182
 
183
- function wp_rp_check_image_size($size, $img_src) {
184
- if (is_array($size) && ($img_src[1] !== $size[0] || $img_src[2] !== $size[1])) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  return false;
186
  }
187
- return true;
 
 
 
 
188
  }
189
 
190
  function wp_rp_get_attached_img_url($related_post, $size) {
191
- $image_id = null;
 
 
 
 
 
 
 
192
 
193
- if (has_post_thumbnail($related_post->ID)) {
194
- $image_id = get_post_thumbnail_id($related_post->ID);
195
  }
196
 
197
- if (!$image_id && function_exists('get_post_format_meta') && function_exists('img_html_to_post_id')) {
198
- // Image post format. Check wp-includes/media.php:get_the_post_format_image for the reference.
199
  $meta = get_post_format_meta($related_post->ID);
200
  if (!empty($meta['image'])) {
201
  if (is_numeric($meta['image'])) {
@@ -203,31 +272,40 @@ function wp_rp_get_attached_img_url($related_post, $size) {
203
  } else {
204
  $image_id = img_html_to_post_id($meta['image']);
205
  }
 
206
  }
207
  }
208
 
209
- if ($image_id === null) {
210
- return null;
 
211
  }
212
 
213
- $img_src = wp_get_attachment_image_src($image_id, $size); //[0] => url, [1] => width, [2] => height
214
-
215
- if (!wp_rp_check_image_size($size, $img_src)) {
216
- wp_rp_extract_images_from_post($related_post, $image_id);
217
- return false;
218
  }
219
 
220
- return $img_src[0];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  }
222
 
223
  function wp_rp_get_post_thumbnail_img($related_post, $size = null, $force = false) {
224
  $options = wp_rp_get_options();
225
  $platform_options = wp_rp_get_platform_options();
226
 
227
- if (!$size || $size === 'thumbnail') {
228
- $size = array(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT);
229
- }
230
-
231
  if (!($platform_options["display_thumbnail"] || $force)) {
232
  return false;
233
  }
@@ -238,6 +316,9 @@ function wp_rp_get_post_thumbnail_img($related_post, $size = null, $force = fals
238
  return wp_rp_get_img_tag($related_post->thumbnail, $post_title);
239
  }
240
 
 
 
 
241
  if ($options['thumbnail_use_custom']) {
242
  $thumbnail_src = get_post_meta($related_post->ID, $options["thumbnail_custom_field"], true);
243
 
@@ -246,25 +327,11 @@ function wp_rp_get_post_thumbnail_img($related_post, $size = null, $force = fals
246
  }
247
  }
248
 
249
- if($size == 'full') {
250
- $image_url = get_post_meta($related_post->ID, '_wp_rp_extracted_image_url_full', false);
251
- } else {
252
- $image_url = get_post_meta($related_post->ID, '_wp_rp_extracted_image_url', false);
253
- }
254
-
255
- if(!empty($image_url) && ($image_url[0] != '')) {
256
- return wp_rp_get_img_tag($image_url[0], $post_title);
257
- }
258
-
259
  $attached_img_url = wp_rp_get_attached_img_url($related_post, $size);
260
  if ($attached_img_url) {
261
  return wp_rp_get_img_tag($attached_img_url, $post_title);
262
  }
263
 
264
- if(empty($image_url) && $attached_img_url === null) {
265
- wp_rp_extract_images_from_post($related_post);
266
- }
267
-
268
  return wp_rp_get_img_tag(wp_rp_get_default_thumbnail_url($related_post->ID, $size), $post_title);
269
  }
270
 
@@ -274,3 +341,54 @@ function wp_rp_process_latest_post_thumbnails() {
274
  wp_rp_get_post_thumbnail_img($post);
275
  }
276
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ /**
4
+ * Init
5
+ */
 
 
 
6
 
7
+ function wp_rp_add_image_sizes() {
8
+ $platform_options = wp_rp_get_platform_options();
9
+ add_image_size(WP_RP_THUMBNAILS_NAME, WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
10
+ if ($platform_options['theme_name'] == 'pinterest.css') {
11
+ add_image_size(WP_RP_THUMBNAILS_PROP_NAME, WP_RP_THUMBNAILS_WIDTH, 0, false);
12
+ }
13
+ }
14
+ add_action('init', 'wp_rp_add_image_sizes');
15
 
 
 
16
 
17
+ /**
18
+ * Settings - replace default thumbnail
19
+ */
20
 
21
+ function wp_rp_upload_default_thumbnail_file() {
22
+ if (empty($_FILES['wp_rp_default_thumbnail'])) {
23
+ return new WP_Error('upload_error');
24
+ }
25
+ $file = $_FILES['wp_rp_default_thumbnail'];
26
+ if(isset($file['error']) && $file['error'] === UPLOAD_ERR_NO_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();
35
+ return $upload_dir['url'] . '/' . $image['file'];
 
 
 
 
 
36
  }
 
37
  }
 
38
 
39
+ return new WP_Error('upload_error');
 
40
  }
41
 
 
 
 
 
 
 
42
 
43
+ /**
44
+ * Cron - Thumbnail extraction
45
+ */
46
+
47
+ function wp_rp_upload_attachment($url, $post_id) {
48
+ /* Parts copied from wp-admin/includes/media.php:media_sideload_image */
49
+
50
+ include_once(ABSPATH . 'wp-admin/includes/file.php');
51
+ include_once(ABSPATH . 'wp-admin/includes/media.php');
52
+ include_once(ABSPATH . 'wp-admin/includes/image.php');
53
+
54
+ $tmp = download_url($url);
55
+ preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $url, $matches);
56
+ $file_array['name'] = sanitize_file_name(urldecode(basename($matches[0])));
57
 
58
+ $file_array['tmp_name'] = $tmp;
59
+ if (is_wp_error($tmp)) {
60
+ @unlink($file_array['tmp_name']);
61
  return false;
62
  }
63
 
64
+ $post_data = array(
65
+ 'guid' => $url,
66
+ 'post_title' => 'Zemanta Related Posts Thumbnail',
67
+ );
68
 
69
+ $attachment_id = media_handle_sideload($file_array, $post_id, null, $post_data);
70
+ if (is_wp_error($attachment_id)) {
71
+ @unlink($file_array['tmp_name']);
72
+ return false;
 
 
 
 
 
 
73
  }
74
 
75
+ $attach_data = wp_get_attachment_metadata($attachment_id);
76
+ if (!$attach_data || $attach_data['width'] < WP_RP_THUMBNAILS_WIDTH || $attach_data['height'] < WP_RP_THUMBNAILS_HEIGHT) {
77
+ wp_delete_attachment($attachment_id);
78
  return false;
79
  }
80
 
81
+ return $attachment_id;
82
+ }
83
 
84
+ function wp_rp_get_image_from_img_tag($post_id, $url, $img_tag) {
85
+ if (($attachment_id = wp_rp_attachment_url_to_postid($url)) || ($attachment_id = wp_rp_img_html_to_post_id($img_tag))) {
86
+ if (wp_rp_update_attachment_id($attachment_id)) {
87
+ return $attachment_id;
88
+ }
89
+ }
90
+
91
+ return wp_rp_upload_attachment($url, $post_id);
92
  }
93
 
94
+ function wp_rp_actually_extract_images_from_post_html($post) {
95
  $content = $post->post_content;
 
 
96
 
97
+ if (!preg_match_all('#' . wp_rp_get_tag_regex('img') . '#i', $content, $matches) || empty($matches)) {
98
+ return false;
99
+ }
100
+
101
+ $html_tags = $matches[0];
102
+ $attachment_id = false;
103
 
104
+ if(count($html_tags) == 0) {
105
+ return false;
106
  }
107
+ array_splice($html_tags, 10);
108
+
109
+ foreach ($html_tags as $html_tag) {
110
+ if (preg_match('#src=([\'"])(.+?)\1#is', $html_tag, $matches) && !empty($matches)) {
111
+ $url = $matches[2];
112
 
113
+ $attachment_id = wp_rp_get_image_from_img_tag($post->ID, $url, $html_tag);
114
+ if ($attachment_id) {
115
+ break;
116
+ }
117
  }
118
  }
119
 
120
+ return $attachment_id;
121
+ }
122
+
123
+ function wp_rp_update_attachment_id($attachment_id) {
124
+ include_once(ABSPATH . 'wp-admin/includes/image.php');
125
+
126
+ $img_path = get_attached_file($attachment_id);
127
+ if (!$img_path) { return false; }
128
+
129
+ $attach_data = wp_generate_attachment_metadata($attachment_id, $img_path);
130
+ wp_update_attachment_metadata($attachment_id, $attach_data);
131
+
132
+ return $attachment_id;
133
  }
134
 
135
  function wp_rp_cron_do_extract_images_from_post($post_id, $attachment_id) {
136
+ // Prevent multiple thumbnail extractions for a single post
137
+ if (get_post_meta($post_id, '_wp_rp_image', true) !== '') { return; }
138
+
139
  $post_id = (int) $post_id;
140
  $attachment_id = (int) $attachment_id;
141
  $post = get_post($post_id);
142
 
 
 
 
 
 
 
 
 
 
 
143
  if ($attachment_id) {
144
+ $new_attachment_id = wp_rp_update_attachment_id($attachment_id);
145
  } else {
146
+ $new_attachment_id = wp_rp_actually_extract_images_from_post_html($post);
147
  }
148
 
149
+ if ($new_attachment_id) {
150
+ update_post_meta($post_id, '_wp_rp_image', $new_attachment_id);
151
+ } else {
152
+ update_post_meta($post_id, '_wp_rp_image', 'empty');
 
153
  }
154
  }
155
  add_action('wp_rp_cron_extract_images_from_post', 'wp_rp_cron_do_extract_images_from_post', 10, 2);
156
 
157
  function wp_rp_extract_images_from_post($post, $attachment_id=null) {
 
 
158
  if(empty($post->post_content) && !$attachment_id) { return; }
159
 
160
+ delete_post_meta($post->ID, '_wp_rp_image');
161
  wp_schedule_single_event(time(), 'wp_rp_cron_extract_images_from_post', array($post->ID, $attachment_id));
162
  }
163
 
164
+
165
+ /**
166
+ * Update images on post save
167
+ */
168
+
169
  function wp_rp_post_save_update_image($post_id) {
170
  $post = get_post($post_id);
171
 
173
  return;
174
  }
175
 
176
+ delete_post_meta($post->ID, '_wp_rp_image');
 
177
 
178
  wp_rp_get_post_thumbnail_img($post);
179
  }
180
  add_action('save_post', 'wp_rp_post_save_update_image');
181
 
182
+
183
+ /**
184
+ * Get thumbnails when post is displayed
185
+ */
186
+
187
  function wp_rp_get_img_tag($src, $alt) {
188
  return '<img src="'. esc_attr($src) . '" alt="' . esc_attr($alt) . '" />';
189
  }
190
 
191
+ function wp_rp_get_default_thumbnail_url($seed = false, $size = 'thumbnail') {
192
+ $options = wp_rp_get_options();
193
+ $upload_dir = wp_upload_dir();
194
+
195
+ if ($options['default_thumbnail_path']) {
196
+ return $options['default_thumbnail_path'];
197
+ } else {
198
+ if ($seed) {
199
+ $next_seed = rand();
200
+ srand($seed);
201
+ }
202
+ $file = rand(0, WP_RP_THUMBNAILS_DEFAULTS_COUNT - 1) . '.jpg';
203
+ if ($seed) {
204
+ srand($next_seed);
205
+ }
206
+ return plugins_url('/static/thumbs/' . $file, __FILE__);
207
+ }
208
+ }
209
+
210
+ function wp_rp_get_image_with_exact_size($image_data, $size) {
211
+ # Partially copied from wp-include/media.php image_get_intermediate_size and image_downsize
212
+ if (!$image_data) { return false; }
213
+
214
+ $img_url = wp_get_attachment_url($image_data['id']);
215
+ $img_url_basename = wp_basename($img_url);
216
+
217
+ // Calculate exact dimensions for proportional images
218
+ if (!$size[0]) { $size[0] = (int) ($image_data['data']['width'] / $image_data['data']['height'] * $size[1]); }
219
+ if (!$size[1]) { $size[1] = (int) ($image_data['data']['height'] / $image_data['data']['width'] * $size[0]); }
220
+
221
+ foreach ($image_data['data']['sizes'] as $_size => $data) {
222
+ // width and height can be both string and integers. WordPress..
223
+ if (($size[0] == $data['width']) && ($size[1] == $data['height'])) {
224
+ $file = $data['file'];
225
+ $img_url = str_replace($img_url_basename, wp_basename($file), $img_url);
226
+ return array(
227
+ 'url' => $img_url,
228
+ 'file' => $data['file'],
229
+ 'width' => $data['width'],
230
+ 'height' => $data['height']
231
+ );
232
+ }
233
+ }
234
+
235
+ return false;
236
+ }
237
+
238
+ function wp_rp_get_image_data($image_id) {
239
+ if (!$image_id || is_wp_error($image_id)) { return false; }
240
+
241
+ $imagedata = wp_get_attachment_metadata($image_id);
242
+ if (!$imagedata || !is_array($imagedata) || !isset($imagedata['sizes']) || !is_array($imagedata['sizes'])) {
243
  return false;
244
  }
245
+
246
+ return array(
247
+ 'id' => $image_id,
248
+ 'data' => $imagedata
249
+ );
250
  }
251
 
252
  function wp_rp_get_attached_img_url($related_post, $size) {
253
+ $extracted_image = get_post_meta($related_post->ID, '_wp_rp_image', true);
254
+ if ($extracted_image === 'empty') { return false; }
255
+
256
+ $image_data = wp_rp_get_image_data((int)$extracted_image);
257
+ if (!$image_data && $extracted_image) {
258
+ // image_id in the db is incorrect
259
+ delete_post_meta($related_post->ID, '_wp_rp_image');
260
+ }
261
 
262
+ if (!$image_data && has_post_thumbnail($related_post->ID)) {
263
+ $image_data = wp_rp_get_image_data(get_post_thumbnail_id($related_post->ID));
264
  }
265
 
266
+ if (!$image_data && function_exists('get_post_format_meta') && function_exists('img_html_to_post_id')) {
267
+ // WP 3.6 Image post format. Check wp-includes/media.php:get_the_post_format_image for the reference.
268
  $meta = get_post_format_meta($related_post->ID);
269
  if (!empty($meta['image'])) {
270
  if (is_numeric($meta['image'])) {
272
  } else {
273
  $image_id = img_html_to_post_id($meta['image']);
274
  }
275
+ $image_data = wp_rp_get_image_data($image_id);
276
  }
277
  }
278
 
279
+ if (!$image_data) {
280
+ wp_rp_extract_images_from_post($related_post);
281
+ return false;
282
  }
283
 
284
+ if ($img_src = wp_rp_get_image_with_exact_size($image_data, $size)) {
285
+ return $img_src['url'];
 
 
 
286
  }
287
 
288
+ wp_rp_extract_images_from_post($related_post, $image_data['id']);
289
+ return false;
290
+ }
291
+
292
+ function wp_rp_get_thumbanil_size_array($size) {
293
+ if (!$size || $size === 'thumbnail') {
294
+ return array(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT);
295
+ }
296
+ if ($size == 'full') {
297
+ return array(WP_RP_THUMBNAILS_WIDTH, 0);
298
+ }
299
+ if (is_array($size)) {
300
+ return $size;
301
+ }
302
+ return false;
303
  }
304
 
305
  function wp_rp_get_post_thumbnail_img($related_post, $size = null, $force = false) {
306
  $options = wp_rp_get_options();
307
  $platform_options = wp_rp_get_platform_options();
308
 
 
 
 
 
309
  if (!($platform_options["display_thumbnail"] || $force)) {
310
  return false;
311
  }
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
 
327
  }
328
  }
329
 
 
 
 
 
 
 
 
 
 
 
330
  $attached_img_url = wp_rp_get_attached_img_url($related_post, $size);
331
  if ($attached_img_url) {
332
  return wp_rp_get_img_tag($attached_img_url, $post_title);
333
  }
334
 
 
 
 
 
335
  return wp_rp_get_img_tag(wp_rp_get_default_thumbnail_url($related_post->ID, $size), $post_title);
336
  }
337
 
341
  wp_rp_get_post_thumbnail_img($post);
342
  }
343
  }
344
+
345
+
346
+
347
+ /**
348
+ * Helpers
349
+ * Mostly! copied from WordPress 3.6 wp-includes/media.php and functions.php
350
+ */
351
+
352
+ function wp_rp_get_tag_regex( $tag ) {
353
+ if ( empty( $tag ) )
354
+ return;
355
+ return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/?>)', tag_escape( $tag ) ); // Added the last ?
356
+ }
357
+
358
+ function wp_rp_img_html_to_post_id( $html, &$matched_html = null ) {
359
+ $attachment_id = 0;
360
+
361
+ // Look for an <img /> tag
362
+ if ( ! preg_match( '#' . wp_rp_get_tag_regex( 'img' ) . '#i', $html, $matches ) || empty( $matches ) )
363
+ return $attachment_id;
364
+
365
+ $matched_html = $matches[0];
366
+
367
+ // Look for attributes.
368
+ if ( ! preg_match_all( '#class=([\'"])(.+?)\1#is', $matched_html, $matches ) || empty( $matches ) )
369
+ return $attachment_id;
370
+
371
+ $attr = array();
372
+ foreach ( $matches[1] as $key => $attribute_name )
373
+ $attr[ $attribute_name ] = $matches[2][ $key ];
374
+
375
+ if ( ! $attachment_id && ! empty( $attr['class'] ) && false !== strpos( $attr['class'], 'wp-image-' ) )
376
+ if ( preg_match( '#wp-image-([0-9]+)#i', $attr['class'], $matches ) )
377
+ $attachment_id = absint( $matches[1] );
378
+
379
+ return $attachment_id;
380
+ }
381
+
382
+ function wp_rp_attachment_url_to_postid( $url ) {
383
+ global $wpdb;
384
+ if ( preg_match( '#\.[a-zA-Z0-9]+$#', $url ) ) {
385
+ $id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' " .
386
+ "AND guid = %s", $url ) );
387
+
388
+ if ( ! empty( $id ) )
389
+ return (int) $id;
390
+ }
391
+
392
+ return 0;
393
+ }
394
+
wp_related_posts.php CHANGED
@@ -1,14 +1,14 @@
1
  <?php
2
  /*
3
  Plugin Name: WordPress Related Posts
4
- Version: 2.7
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.7');
12
 
13
  define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
14
 
@@ -93,13 +93,15 @@ function wp_rp_ajax_load_articles_callback() {
93
  }
94
 
95
  $post = get_post($getdata['post_id']);
96
- if (!$post) {
97
  die('error');
98
  }
99
 
100
  $from = (isset($getdata['from']) && is_numeric($getdata['from'])) ? intval($getdata['from']) : 0;
101
  $count = (isset($getdata['count']) && is_numeric($getdata['count'])) ? intval($getdata['count']) : 50;
102
 
 
 
103
  $image_size = isset($getdata['size']) ? $getdata['size'] : 'thumbnail';
104
  if(!($image_size == 'thumbnail' || $image_size == 'full')) {
105
  die('error');
@@ -107,11 +109,19 @@ function wp_rp_ajax_load_articles_callback() {
107
 
108
  $limit = $count + $from;
109
 
110
- $related_posts = array();
111
-
112
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
113
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
114
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
 
 
 
 
 
 
 
 
115
 
116
  if(function_exists('qtrans_postsFilter')) {
117
  $related_posts = qtrans_postsFilter($related_posts);
@@ -121,15 +131,14 @@ function wp_rp_ajax_load_articles_callback() {
121
 
122
  foreach (array_slice($related_posts, $from) as $related_post) {
123
  array_push($response_list, array(
124
- 'id' => $related_post->ID,
125
- 'url' => get_permalink($related_post->ID),
126
- 'title' => $related_post->post_title,
127
- 'img' => wp_rp_get_post_thumbnail_img($related_post, $image_size)
128
- ));
129
  }
130
 
131
  header('Content-Type: text/javascript');
132
-
133
  die(json_encode($response_list));
134
  }
135
  add_action('wp_ajax_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
@@ -388,7 +397,6 @@ function wp_rp_head_resources() {
388
  }
389
 
390
  $output_vars .= "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
391
- "\twindow._wp_rp_ajax_img_src_url = '" . esc_js(WP_RP_CTR_REPORT_URL) . "';\n" .
392
  "\twindow._wp_rp_thumbnails = " . ($platform_options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
393
  "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
394
  "\twindow._wp_rp_post_tags = {$post_tags};\n" .
@@ -398,9 +406,9 @@ function wp_rp_head_resources() {
398
  (current_user_can('edit_posts') ?
399
  "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" .
400
  "\twindow._wp_rp_plugin_static_base_url = '" . esc_js(plugins_url('static/' , __FILE__)) . "';\n" .
401
- "\twindow._wp_rp_ajax_nonce = '" . wp_create_nonce("wp_rp_ajax_nonce") . "';\n"
402
- : '') .
403
- wp_rp_render_head_script_variables();
404
  }
405
 
406
  $output .= "<script type=\"text/javascript\">\n" . $output_vars . "</script>\n";
@@ -528,122 +536,3 @@ function wp_rp_get_related_posts($before_title = '', $after_title = '') {
528
  return "\n" . $output . "\n";
529
  }
530
 
531
-
532
-
533
-
534
- // --------- mobile AB testing -----------
535
-
536
- define('WP_RP_AB_TEST_PARAM', 'wprptest2');
537
- define('WP_RP_AB_TEST_COOKIE', 'wprptest2');
538
- define('WP_RP_AB_TEST_DEBUG', 'wprpdebug2');
539
-
540
- global $wp_rp_session_id, $wp_rp_test_group;
541
- $wp_rp_session_id = false; $wp_rp_test_group = 0;
542
-
543
- function wp_rp_render_head_script_variables() {
544
- // used when rendering <head>
545
- global $wp_rp_session_id, $wp_rp_test_group;
546
-
547
- $output = '';
548
- if (wp_is_mobile() && !current_user_can('edit_posts')){
549
- //error_log("AB data appended in head <script>");
550
- $output = "\twindow._wp_rp_test_group = " . $wp_rp_test_group . ";\n" .
551
- "\twindow._wp_rp_sid = \"" . $wp_rp_session_id . "\";\n";
552
- }
553
- return $output;
554
- }
555
-
556
- function wp_rp_set_test_cookie() {
557
- global $wp_rp_session_id;
558
-
559
- //error_log("wp_rp_set_test_cookie");
560
- //error_log("session_id: " .$_COOKIE[WP_RP_AB_TEST_COOKIE]);
561
-
562
- $wp_rp_session_id = isset($_COOKIE[WP_RP_AB_TEST_COOKIE]) ? $_COOKIE[WP_RP_AB_TEST_COOKIE] : false;
563
- if ($wp_rp_session_id) {
564
- //error_log("cookie is set - type: " . gettype($wp_rp_session_id));
565
- return;
566
- }
567
-
568
- $wp_rp_session_id = (string)rand();
569
- //error_log("cookie is NOT set");
570
- setcookie(WP_RP_AB_TEST_COOKIE, $wp_rp_session_id, time() + 60 * 30);
571
- }
572
-
573
- function wp_rp_is_suitable_for_test() {
574
- if (current_user_can('edit_posts') && !isset($_GET[WP_RP_AB_TEST_DEBUG])) {
575
- return false;
576
- }
577
-
578
- $options = wp_rp_get_options();
579
- return $options['ctr_dashboard_enabled'] && wp_is_mobile();
580
- }
581
-
582
- function wp_rp_get_post_url($post_id) {
583
- global $wp_rp_test_group;
584
-
585
- //error_log("wp_rp_get_post_url");
586
-
587
- $post_url = get_permalink($post_id);
588
-
589
- if (!wp_rp_is_suitable_for_test()) {
590
- return $post_url;
591
- }
592
-
593
- if (strpos($post_url, '?') === false) {
594
- $post_url .= '?' .WP_RP_AB_TEST_PARAM. '=' . $wp_rp_test_group;
595
- } else {
596
- $post_url .= '&' .WP_RP_AB_TEST_PARAM. '=' . $wp_rp_test_group;
597
- }
598
- return $post_url;
599
- }
600
-
601
-
602
- function wp_rp_init_test() {
603
- $options = wp_rp_get_options();
604
- $platform_options = wp_rp_get_platform_options();
605
-
606
- if (!$options['enable_themes'] || $platform_options['theme_name'] !== 'm-stream.css') {
607
- //error_log("theme - not suitable for test");
608
- return;
609
- }
610
- global $wp_rp_session_id, $wp_rp_test_group, $post;
611
-
612
- //error_log("wp_rp_init_test");
613
-
614
- if ($wp_rp_session_id) {
615
- //error_log("session id set");
616
- return;
617
- }
618
-
619
- if (!wp_rp_is_suitable_for_test()) {
620
- //error_log("not suitable for test");
621
- return;
622
- }
623
-
624
- wp_rp_set_test_cookie();
625
-
626
- if (isset($_GET[WP_RP_AB_TEST_PARAM]) && isset($_GET[WP_RP_AB_TEST_DEBUG])) {
627
- $wp_rp_test_group = intval($_GET[WP_RP_AB_TEST_PARAM]);
628
- //error_log("wp rep test param is set: " . $wp_rp_test_group);
629
- return;
630
- }
631
-
632
- $wp_rp_test_group = abs(crc32($wp_rp_session_id) % 3);
633
-
634
- if(isset($_GET[WP_RP_AB_TEST_PARAM])){
635
- if(intval($_GET[WP_RP_AB_TEST_PARAM]) == $wp_rp_test_group){
636
- return;
637
- }
638
- }
639
-
640
- $options = wp_rp_get_options();
641
- if ($post && $post->post_type === 'post' && (($options["on_single_post"] && is_single()))) {
642
- wp_redirect(wp_rp_get_post_url($post->ID), 301);
643
- //error_log("redirect done");
644
- exit;
645
- }
646
- //error_log("skipped redirect");
647
- }
648
- add_action('template_redirect', 'wp_rp_init_test');
649
-
1
  <?php
2
  /*
3
  Plugin Name: WordPress Related Posts
4
+ Version: 2.8
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.8');
12
 
13
  define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
14
 
93
  }
94
 
95
  $post = get_post($getdata['post_id']);
96
+ if(!$post) {
97
  die('error');
98
  }
99
 
100
  $from = (isset($getdata['from']) && is_numeric($getdata['from'])) ? intval($getdata['from']) : 0;
101
  $count = (isset($getdata['count']) && is_numeric($getdata['count'])) ? intval($getdata['count']) : 50;
102
 
103
+ $search = isset($getdata['search']) && $getdata['search'] ? $getdata['search'] : false;
104
+
105
  $image_size = isset($getdata['size']) ? $getdata['size'] : 'thumbnail';
106
  if(!($image_size == 'thumbnail' || $image_size == 'full')) {
107
  die('error');
109
 
110
  $limit = $count + $from;
111
 
112
+ if ($search) {
113
+ $the_query = new WP_Query(array(
114
+ 's' => $search,
115
+ 'post_type' => 'post',
116
+ 'post_status'=>'publish',
117
+ 'post_count' => $limit));
118
+ $related_posts = $the_query->get_posts();
119
+ } else {
120
+ $related_posts = array();
121
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
122
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
123
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
124
+ }
125
 
126
  if(function_exists('qtrans_postsFilter')) {
127
  $related_posts = qtrans_postsFilter($related_posts);
131
 
132
  foreach (array_slice($related_posts, $from) as $related_post) {
133
  array_push($response_list, array(
134
+ 'id' => $related_post->ID,
135
+ 'url' => get_permalink($related_post->ID),
136
+ 'title' => $related_post->post_title,
137
+ 'img' => wp_rp_get_post_thumbnail_img($related_post, $image_size)
138
+ ));
139
  }
140
 
141
  header('Content-Type: text/javascript');
 
142
  die(json_encode($response_list));
143
  }
144
  add_action('wp_ajax_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
397
  }
398
 
399
  $output_vars .= "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
 
400
  "\twindow._wp_rp_thumbnails = " . ($platform_options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
401
  "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
402
  "\twindow._wp_rp_post_tags = {$post_tags};\n" .
406
  (current_user_can('edit_posts') ?
407
  "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" .
408
  "\twindow._wp_rp_plugin_static_base_url = '" . esc_js(plugins_url('static/' , __FILE__)) . "';\n" .
409
+ "\twindow._wp_rp_ajax_nonce = '" . wp_create_nonce("wp_rp_ajax_nonce") . "';\n" .
410
+ "\twindow._wp_rp_erp_search = true;\n"
411
+ : '');
412
  }
413
 
414
  $output .= "<script type=\"text/javascript\">\n" . $output_vars . "</script>\n";
536
  return "\n" . $output . "\n";
537
  }
538