WordPress Related Posts - Version 2.7

Version Description

New thumbnailer might break backwards compatibility for blogs with custom thumbnail sizes since it resizes all thumbnails to 150x150.

Download this release

Release Info

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

Code changes from version 2.6.3 to 2.7

Files changed (4) hide show
  1. config.php +7 -0
  2. readme.txt +14 -6
  3. thumbnailer.php +124 -105
  4. wp_related_posts.php +2 -2
config.php CHANGED
@@ -230,6 +230,13 @@ function wp_rp_install() {
230
  wp_rp_related_posts_db_table_install();
231
  }
232
 
 
 
 
 
 
 
 
233
  function wp_rp_migrate_2_5() {
234
  $wp_rp_meta = get_option('wp_rp_meta');
235
  $wp_rp_options = get_option('wp_rp_options');
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';
236
+ $wp_rp_meta['new_user'] = false;
237
+ update_option('wp_rp_meta', $wp_rp_meta);
238
+ }
239
+
240
  function wp_rp_migrate_2_5() {
241
  $wp_rp_meta = get_option('wp_rp_meta');
242
  $wp_rp_options = get_option('wp_rp_options');
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.6.3
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
@@ -47,16 +47,18 @@ We adopted the principle of having three "release channels". You can try the mos
47
  = Via admin: =
48
  1. Go to Plugins -> Add New
49
  2. Search for WordPress Related Posts
50
- 3. Install and activate the first result
51
- 4. After installation go to Settings -> Related Posts in your plugins list to turn on Thumbnails & Statistics
52
- 5. Done!
 
53
 
54
  = Via upload: =
55
  1. Download the plugin .zip file
56
  2. Log in to yourdomain.com/wp-admin
57
  3. Click Plugins -> Add New -> Upload
58
- 4. After installation go to Settings -> Related Posts in your plugins list to turn on Thumbnails & Statistics
59
- 5. You're finished!
 
60
 
61
  == Screenshots ==
62
  1. WordPress Related Posts Default Theme.
@@ -65,11 +67,17 @@ We adopted the principle of having three "release channels". You can try the mos
65
 
66
  == Upgrade Notice ==
67
 
 
 
 
68
  = 2.6.2 =
69
  Fix for security vulnerability. Upgrade immediately.
70
 
71
  == Changelog ==
72
 
 
 
 
73
  = 2.6.3 =
74
  * Due to popular demand, "Edit related posts" is back in action. Send us more feedback to support@zemanta.com. Thanks!
75
 
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
 
47
  = Via admin: =
48
  1. Go to Plugins -> Add New
49
  2. Search for WordPress Related Posts
50
+ 3. Install the plugin called "WordPress Related Posts" and activate it
51
+ 4. After activation click Turn on to get advanced features
52
+ 5. If you use any caching plugin please clear the cache
53
+ 6. Done!
54
 
55
  = Via upload: =
56
  1. Download the plugin .zip file
57
  2. Log in to yourdomain.com/wp-admin
58
  3. Click Plugins -> Add New -> Upload
59
+ 4. After installation activate the plugin and click Turn on to get advanced features
60
+ 5. If you use any caching plugin please clear the cache
61
+ 6. You're finished!
62
 
63
  == Screenshots ==
64
  1. WordPress Related Posts Default Theme.
67
 
68
  == Upgrade Notice ==
69
 
70
+ = 2.7 =
71
+ New thumbnailer might break backwards compatibility for blogs with custom thumbnail sizes since it resizes all thumbnails to 150x150.
72
+
73
  = 2.6.2 =
74
  Fix for security vulnerability. Upgrade immediately.
75
 
76
  == Changelog ==
77
 
78
+ = 2.7 =
79
+ * Improved thumbnailer
80
+
81
  = 2.6.3 =
82
  * Due to popular demand, "Edit related posts" is back in action. Send us more feedback to support@zemanta.com. Thanks!
83
 
thumbnailer.php CHANGED
@@ -56,36 +56,53 @@ function wp_rp_get_default_thumbnail_url($seed = false, $size = 'thumbnail') {
56
  }
57
  }
58
 
59
- function wp_rp_extract_post_image($post_id, $size = 'thumbnail') {
60
- // We don't have an image stored for this post yet - find the first uploaded image and save it
61
- $args = array(
62
- 'post_type' => 'attachment',
63
- 'numberposts' => 1,
64
- 'post_status' => null,
65
- 'post_parent' => $post_id,
66
- 'orderby' => 'id',
67
- 'order' => 'ASC',
68
- );
69
 
70
- $attachments = get_posts($args);
71
- $image_id = '-1';
72
- if ( $attachments ) {
73
- foreach ( $attachments as $attachment ) {
74
- $img = wp_get_attachment_image($attachment->ID, $size);
75
- if($img) {
76
- $image_id = $attachment->ID;
77
- break;
78
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  }
80
  }
81
- return $image_id;
82
- }
83
 
84
- function wp_rp_direct_filesystem_method() {
85
- return 'direct';
 
 
 
 
 
 
 
 
 
86
  }
87
 
88
- function wp_rp_actually_extract_images_from_post_html($post) {
89
  $content = $post->post_content;
90
  preg_match_all('/<img (?:[^>]+ )?src="([^"]+)"/', $content, $matches);
91
  $urls = $matches[1];
@@ -97,9 +114,24 @@ function wp_rp_actually_extract_images_from_post_html($post) {
97
  }
98
  array_splice($urls, 10);
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  $upload_dir = wp_upload_dir();
101
  if($upload_dir['error'] !== false) {
102
- return $imgs;
103
  }
104
  require_once(ABSPATH . 'wp-admin/includes/file.php');
105
 
@@ -107,76 +139,27 @@ function wp_rp_actually_extract_images_from_post_html($post) {
107
  add_filter('filesystem_method', 'wp_rp_direct_filesystem_method');
108
  WP_Filesystem();
109
 
110
- foreach ($urls as $url) {
111
- $url = html_entity_decode($url);
112
-
113
- $http_response = wp_remote_get($url, array('timeout' => 10));
114
- if(is_wp_error($http_response)) {
115
- continue;
116
- }
117
- $img_data = wp_remote_retrieve_body($http_response);
118
-
119
- $img_name = wp_unique_filename($upload_dir['path'], wp_basename(parse_url($url, PHP_URL_PATH)));
120
- $img_path = $upload_dir['path'] . '/' . $img_name;
121
-
122
- if(!$wp_filesystem->put_contents($img_path, $img_data, FS_CHMOD_FILE)) {
123
- continue;
124
- }
125
-
126
- if (function_exists('wp_get_image_editor')) { // WP 3.5+
127
- $image = wp_get_image_editor($img_path);
128
-
129
- $suffix = WP_RP_THUMBNAILS_WIDTH . 'x' . WP_RP_THUMBNAILS_HEIGHT;
130
- $resized_img_path = $image->generate_filename($suffix, $upload_dir['path'], 'jpg');
131
-
132
- $image->resize(WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
133
- $image->save($resized_img_path, 'image/jpeg');
134
- } else {
135
- $resized_img_path = image_resize($img_path, WP_RP_THUMBNAILS_WIDTH, WP_RP_THUMBNAILS_HEIGHT, true);
136
- if (is_wp_error($resized_img_path) && array_key_exists('error_getting_dimensions', $resized_img_path->errors)) {
137
- $resized_img_path = $img_path;
138
- }
139
- }
140
-
141
- if(is_wp_error($resized_img_path)) {
142
- continue;
143
- }
144
-
145
- $thumbnail_img_url = $upload_dir['url'] . '/' . urlencode(wp_basename($resized_img_path));
146
- $full_img_url = $upload_dir['url'] . '/' . urlencode(wp_basename($img_path));
147
-
148
- $imgs = array(
149
- 'thumbnail' => $thumbnail_img_url,
150
- 'full' => $full_img_url
151
- );
152
-
153
- break;
154
  }
155
 
156
  remove_filter('filesystem_method', 'wp_rp_direct_filesystem_method');
157
-
158
- return $imgs;
159
- }
160
-
161
- function wp_rp_cron_do_extract_images_from_post_html($post_id) {
162
- $post_id = (int) $post_id;
163
- $post = get_post($post_id);
164
-
165
- $imgs = wp_rp_actually_extract_images_from_post_html($post);
166
 
167
  if($imgs) {
168
  update_post_meta($post_id, '_wp_rp_extracted_image_url', $imgs['thumbnail']);
169
  update_post_meta($post_id, '_wp_rp_extracted_image_url_full', $imgs['full']);
170
  }
171
  }
172
- add_action('wp_rp_cron_extract_images_from_post_html', 'wp_rp_cron_do_extract_images_from_post_html');
173
 
174
- function wp_rp_extract_images_from_post_html($post) {
175
  update_post_meta($post->ID, '_wp_rp_extracted_image_url', '');
176
  update_post_meta($post->ID, '_wp_rp_extracted_image_url_full', '');
177
- if(empty($post->post_content)) { return; }
178
 
179
- wp_schedule_single_event(time(), 'wp_rp_cron_extract_images_from_post_html', array($post->ID));
180
  }
181
 
182
  function wp_rp_post_save_update_image($post_id) {
@@ -193,37 +176,76 @@ function wp_rp_post_save_update_image($post_id) {
193
  }
194
  add_action('save_post', 'wp_rp_post_save_update_image');
195
 
 
 
 
196
 
197
- function wp_rp_get_post_thumbnail_img($related_post, $size = 'thumbnail', $force = false) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  $options = wp_rp_get_options();
199
  $platform_options = wp_rp_get_platform_options();
200
 
 
 
 
 
201
  if (!($platform_options["display_thumbnail"] || $force)) {
202
  return false;
203
  }
204
 
 
 
205
  if (property_exists($related_post, 'thumbnail')) {
206
- return '<img src="'. esc_attr($related_post->thumbnail) . '" alt="' . esc_attr(wptexturize($related_post->post_title)) . '" />';
207
  }
208
 
209
  if ($options['thumbnail_use_custom']) {
210
  $thumbnail_src = get_post_meta($related_post->ID, $options["thumbnail_custom_field"], true);
211
 
212
  if ($thumbnail_src) {
213
- $img = '<img src="' . esc_attr($thumbnail_src) . '" alt="' . esc_attr(wptexturize($related_post->post_title)) . '" />';
214
- return $img;
215
  }
216
  }
217
 
218
- if (has_post_thumbnail($related_post->ID)) {
219
- $attr = array(
220
- 'alt' => esc_attr(wptexturize($related_post->post_title)),
221
- 'title' => false
222
- );
223
- $img = get_the_post_thumbnail($related_post->ID, $size, $attr);
224
- return $img;
225
- }
226
-
227
  if($size == 'full') {
228
  $image_url = get_post_meta($related_post->ID, '_wp_rp_extracted_image_url_full', false);
229
  } else {
@@ -231,22 +253,19 @@ function wp_rp_get_post_thumbnail_img($related_post, $size = 'thumbnail', $force
231
  }
232
 
233
  if(!empty($image_url) && ($image_url[0] != '')) {
234
- $img = '<img src="' . esc_attr($image_url[0]) . '" alt="' . esc_attr(wptexturize($related_post->post_title)) . '" />';
235
- return $img;
236
  }
237
 
238
- $image_id = wp_rp_extract_post_image($related_post->ID, $size);
239
- if ($image_id !== '-1') {
240
- $img = wp_get_attachment_image($image_id, $size);
241
- return $img;
242
  }
243
 
244
- if(empty($image_url)) {
245
- wp_rp_extract_images_from_post_html($related_post);
246
  }
247
 
248
- $img = '<img src="'. esc_attr(wp_rp_get_default_thumbnail_url($related_post->ID, $size)) . '" alt="' . esc_attr(wptexturize($related_post->post_title)) . '" />';
249
- return $img;
250
  }
251
 
252
  function wp_rp_process_latest_post_thumbnails() {
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];
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
 
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) {
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'])) {
202
+ $image_id = absint($meta['image']);
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
  }
234
 
235
+ $post_title = wptexturize($related_post->post_title);
236
+
237
  if (property_exists($related_post, 'thumbnail')) {
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
 
244
  if ($thumbnail_src) {
245
+ return wp_rp_get_img_tag($thumbnail_src, $post_title);
 
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 {
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
 
271
  function wp_rp_process_latest_post_thumbnails() {
wp_related_posts.php CHANGED
@@ -1,14 +1,14 @@
1
  <?php
2
  /*
3
  Plugin Name: WordPress Related Posts
4
- Version: 2.6.3
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.6');
12
 
13
  define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
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