Related Posts Thumbnails Plugin for WordPress - Version 1.4.0

Version Description

Download this release

Release Info

Developer marynixie
Plugin Icon 128x128 Related Posts Thumbnails Plugin for WordPress
Version 1.4.0
Comparing to
See all releases

Code changes from version 1.3.1 to 1.4.0

Files changed (2) hide show
  1. readme.txt +9 -2
  2. related-posts-thumbnails.php +310 -252
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: marynixie
3
  Donate link: http://wordpress.shaldybina.com/donate
4
  Tags: related, posts, thumbnail
5
  Requires at least: 2.9
6
- Tested up to: 3.2.1
7
- Stable tag: 1.3.1
8
 
9
  Customizable plugin, that nicely displays related posts thumbnails under the post.
10
 
@@ -18,6 +18,10 @@ Thumbnails are arranged in columns under the post with related posts title/excer
18
 
19
  You can specify number of related posts to display, start date, categories to show on and to include, top text, style settings, default image URL. You can select type of relation - categories, tags, categories and tags, random or custom taxonomies. You can specify where to display related posts - choose page type (main or single), post type and select categories. You can turn off automatic appending to the end of post and use **<?php get_related_posts_thumbnails(); ?>** php tag in the Loop of your theme. Related Posts thumbnails sidebar widget is also available. You can use [related-posts-thumbnails] shortcode in the post body.
20
 
 
 
 
 
21
  Translations:
22
 
23
  * This plugin has Russian version
@@ -61,6 +65,9 @@ If there are no images of the specified size in the post, or file does not exist
61
 
62
  == Changelog ==
63
 
 
 
 
64
  = 1.3.1 =
65
  * backwards compatibility with WP 2.9
66
 
3
  Donate link: http://wordpress.shaldybina.com/donate
4
  Tags: related, posts, thumbnail
5
  Requires at least: 2.9
6
+ Tested up to: 4.4.2
7
+ Stable tag: 1.4.0
8
 
9
  Customizable plugin, that nicely displays related posts thumbnails under the post.
10
 
18
 
19
  You can specify number of related posts to display, start date, categories to show on and to include, top text, style settings, default image URL. You can select type of relation - categories, tags, categories and tags, random or custom taxonomies. You can specify where to display related posts - choose page type (main or single), post type and select categories. You can turn off automatic appending to the end of post and use **<?php get_related_posts_thumbnails(); ?>** php tag in the Loop of your theme. Related Posts thumbnails sidebar widget is also available. You can use [related-posts-thumbnails] shortcode in the post body.
20
 
21
+ Contributions:
22
+
23
+ * Thanks to <a href="http://adnan.pk/">Adnan</a> for updates to make it compatible with Wordpress 4.4
24
+
25
  Translations:
26
 
27
  * This plugin has Russian version
65
 
66
  == Changelog ==
67
 
68
+ = 1.4.0
69
+ * compatibility with WP 4.4
70
+
71
  = 1.3.1 =
72
  * backwards compatibility with WP 2.9
73
 
related-posts-thumbnails.php CHANGED
@@ -1,12 +1,15 @@
1
- <?php /*
2
- Plugin Name: Related Posts Thumbnails
3
- Plugin URI: http://wordpress.shaldybina.com/plugins/related-posts-thumbnails/
4
- Description: Showing related posts thumbnails under the post.
5
- Version: 1.3.1
6
- Author: Maria Shaldybina
7
- Author URI: http://shaldybina.com/
8
- */
9
- /* Copyright 2010 Maria I Shaldybina
 
 
 
10
 
11
  This program is free software; you can redistribute it and/or modify
12
  it under the terms of the GNU General Public License as published by
@@ -20,67 +23,98 @@
20
  */
21
  class RelatedPostsThumbnails {
22
  /* Default values. PHP 4 compatible */
23
- var $single_only = '1';
24
- var $auto = '1';
25
- var $top_text = '<h3>Related posts:</h3>';
26
- var $number = 3;
27
- var $relation = 'categories';
28
- var $poststhname = 'thumbnail';
29
- var $background = '#FFFFFF';
30
- var $hoverbackground = '#EEEEEF';
31
- var $border_color = '#DDDDDD';
32
- var $font_color = '#333333';
33
- var $font_family = 'Arial';
34
- var $font_size = '12';
35
- var $text_length = '100';
36
- var $excerpt_length = '0';
37
- var $custom_field = '';
38
- var $custom_height = '100';
39
- var $custom_width = '100';
40
  var $text_block_height = '75';
41
- var $thsource = 'post-thumbnails';
42
- var $categories_all = '1';
43
- var $devmode = '0';
44
- var $output_style = 'div';
45
- var $post_types = array('post');
46
  var $custom_taxonomies = array();
47
 
48
- function RelatedPostsThumbnails() { // initialization
 
49
  load_plugin_textdomain( 'related-posts-thumbnails', false, basename( dirname( __FILE__ ) ) . '/locale' );
50
- $this->default_image = WP_PLUGIN_URL . '/related-posts-thumbnails/img/default.png';
51
- if ( get_option( 'relpoststh_auto', $this->auto ) )
 
 
 
 
 
52
  add_filter( 'the_content', array( $this, 'auto_show' ) );
 
 
53
  add_action( 'admin_menu', array( $this, 'admin_menu' ) );
54
  add_shortcode( 'related-posts-thumbnails' , array( $this, 'get_html' ) );
55
- $this->wp_version = get_bloginfo('version');
 
56
  }
57
 
58
- function auto_show( $content ) { // Automatically displaying related posts under post body
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  return $content . $this->get_html( true );
60
  }
61
 
62
- function get_html( $show_top = false ) { // Getting related posts HTML
63
- if ( $this->is_relpoststh_show() )
64
- return $this->get_thumbnails( $show_top );
 
65
  return '';
66
  }
67
 
68
- function get_thumbnails( $show_top = false ) { // Retrieve Related Posts HTML for output
69
- $output = '';
70
- $debug = 'Developer mode initialisation; Version: 1.2.9;';
71
- $time = microtime(true);
72
- $posts_number = get_option( 'relpoststh_number', $this->number );
73
- if ( $posts_number <= 0 ) // return nothing if this parameter was set to <= 0
 
 
74
  return $this->finish_process( $output, $debug . 'Posts number is 0;', $time );
75
- $id = get_the_ID();
76
- $relation = get_option( 'relpoststh_relation', $this->relation );
77
- $poststhname = get_option( 'relpoststh_poststhname', $this->poststhname );
78
- $text_length = get_option( 'relpoststh_textlength', $this->text_length );
79
- $excerpt_length = get_option( 'relpoststh_excerptlength', $this->excerpt_length );
80
- $thsource = get_option( 'relpoststh_thsource', $this->thsource );
 
 
81
  $categories_show_all = get_option( 'relpoststh_show_categoriesall', get_option( 'relpoststh_categoriesall', $this->categories_all ) );
82
- $onlywiththumbs = ( current_theme_supports( 'post-thumbnails' ) && $thsource == 'post-thumbnails' ) ? get_option( 'relpoststh_onlywiththumbs', false) : false;
83
- $post_type = get_post_type();
84
 
85
  global $wpdb;
86
 
@@ -92,47 +126,46 @@ class RelatedPostsThumbnails {
92
  $query_objects = "SELECT distinct object_id FROM $wpdb->term_relationships WHERE 1=1 ";
93
 
94
  if ( $relation != 'no' ) { /* Get object terms */
95
- if ( $relation == 'categories' )
96
- $taxonomy = array( 'category' );
97
- elseif ( $relation == 'tags' )
98
- $taxonomy = array( 'post_tag' );
99
- elseif ( $relation == 'custom') {
100
- $taxonomy = get_option( 'relpoststh_custom_taxonomies', $this->custom_taxonomies );
101
- }
102
- else {
103
- $taxonomy = array( 'category', 'post_tag' );
104
- }
105
- $object_terms = wp_get_object_terms( $id, $taxonomy, array( 'fields' => 'ids' ) );
106
- if ( empty( $object_terms ) || !is_array( $object_terms ) ) // no terms to get taxonomy
107
- return $this->finish_process( $output, $debug . 'No taxonomy terms to get posts;', $time );
108
-
109
- $query = "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id in ('". implode( "', '", $object_terms ) . "')";
110
- $object_taxonomy = $wpdb->get_results( $query );
111
- $object_taxonomy_a = array();
112
- if ( count( $object_taxonomy ) > 0 ) {
113
- foreach ( $object_taxonomy as $item )
114
- $object_taxonomy_a[] = $item->term_taxonomy_id;
115
- }
116
- $query_objects .= " AND term_taxonomy_id IN ('". implode( "', '", $object_taxonomy_a ) . "') ";
117
  }
118
 
119
  if ( $categories_show_all != '1' ) { /* Get filter terms */
120
  $select_terms = get_option( 'relpoststh_show_categories',
121
- get_option( 'relpoststh_categories' ) );
122
- if ( empty( $select_terms ) || !is_array( $select_terms ) ) // if no categories were specified intentionally return nothing
123
  return $this->finish_process( $output, $debug . 'No categories were selected;', $time );
 
124
 
125
  $query = "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id in ('". implode( "', '", $select_terms ) . "')";
126
  $taxonomy = $wpdb->get_results( $query );
127
  $filter_taxonomy_a = array();
128
  if ( count( $taxonomy ) > 0 ) {
129
- foreach ($taxonomy as $item)
130
- $filter_taxonomy_a[] = $item->term_taxonomy_id;
131
  }
132
- if ($relation != 'no') {
133
  $query_objects .= " AND object_id IN (SELECT distinct object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ('". implode( "', '", $filter_taxonomy_a ) . "') )";
134
- }
135
- else {
136
  $query_objects .= " AND term_taxonomy_id IN ('". implode( "', '", $filter_taxonomy_a ) . "')";
137
  }
138
  }
@@ -140,149 +173,160 @@ class RelatedPostsThumbnails {
140
  $relationships = $wpdb->get_results( $query_objects );
141
  $related_objects = array();
142
  if ( count( $relationships ) > 0 ) {
143
- foreach ($relationships as $item)
144
  $related_objects[] = $item->object_id;
 
145
  }
146
  }
147
 
148
  $query = "SELECT distinct ID FROM $wpdb->posts ";
149
  $where = " WHERE post_type = '" . $post_type . "' AND post_status = 'publish' AND ID<>" . $id; // not the current post
150
  $startdate = get_option( 'relpoststh_startdate' );
151
- if ( !empty( $startdate ) && preg_match( '/^\d\d\d\d-\d\d-\d\d$/', $startdate ) ) { // If startdate was set
152
  $debug .= "Startdate: $startdate;";
153
  $where .= " AND post_date >= '" . $startdate . "'";
154
  }
155
  if ( $use_filter ) {
156
  $where .= " AND ID IN ('". implode( "', '", $related_objects ) . "')";
157
  }
158
- $join = "";
159
  if ( $onlywiththumbs ) {
160
- $debug .= "Only with thumbnails;";
161
  $join = " INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)";
162
- $where .= " AND $wpdb->postmeta.meta_key = '_thumbnail_id'";
163
  }
164
 
165
- $order = " ORDER BY rand() LIMIT " . $posts_number;
166
  $random_posts = $wpdb->get_results( $query . $join . $where . $order );
167
 
168
  /* Get posts by their IDs */
169
- if ( !is_array( $random_posts ) || count( $random_posts ) < 1 ) {
170
  return $this->finish_process( $output, $debug . 'No posts matching relationships criteria;', $time );
171
  }
172
 
173
  $posts_in = array();
174
- foreach ($random_posts as $random_post)
175
  $posts_in[] = $random_post->ID;
 
176
  $query = "SELECT ID, post_content, post_excerpt, post_title FROM $wpdb->posts WHERE ID IN ('". implode( "', '", $posts_in ) . "')";
177
  $posts = $wpdb->get_results( $query );
178
  if ( ! ( is_array( $posts ) && count( $posts ) > 0 ) ) { // no posts
179
  $debug .= 'No posts found;';
180
  return $this->finish_process( $output, $debug, $time );
181
- }
182
- else
183
  $debug .= 'Found ' . count( $posts ) . ' posts;';
 
184
 
185
  /* Calculating sizes */
186
  if ( $thsource == 'custom-field' ) {
187
  $debug .= 'Custom sizes;';
188
  $width = get_option( 'relpoststh_customwidth', $this->custom_width );
189
  $height = get_option( 'relpoststh_customheight', $this->custom_height );
190
- }
191
- else { // post-thumbnails source
192
  if ( $poststhname == 'thumbnail' || $poststhname == 'medium' || $poststhname == 'large' ) { // get thumbnail size for basic sizes
193
  $debug .= 'Basic sizes;';
194
  $width = get_option( "{$poststhname}_size_w" );
195
  $height = get_option( "{$poststhname}_size_h" );
196
- }
197
- elseif ( current_theme_supports( 'post-thumbnails' ) ) { // get sizes for theme supported thumbnails
198
  global $_wp_additional_image_sizes;
199
  if ( isset( $_wp_additional_image_sizes[ $poststhname ] ) ) {
200
  $debug .= 'Additional sizes;';
201
- $width = $_wp_additional_image_sizes[ $poststhname ][ 'width' ];
202
- $height = $_wp_additional_image_sizes[ $poststhname ][ 'height' ];
203
- }
204
- else
205
- $debug .= 'No additional sizes;';
206
  }
207
  }
208
  // displaying square if one size is not cropping
209
- if ( $height == 9999 )
210
  $height = $width;
211
- if ( $width == 9999 )
 
212
  $width = $height;
 
213
  // theme is not supporting but settings were not changed
214
  if ( empty( $width ) ) {
215
  $debug .= 'Using default width;';
216
- $width = get_option( "thumbnail_size_w" );
217
  }
218
  if ( empty( $height ) ) {
219
  $debug .= 'Using default height;';
220
- $height = get_option( "thumbnail_size_h" );
221
  }
222
  $debug .= 'Got sizes '.$width.'x'.$height.';';
223
  // rendering related posts HTML
224
- if ( $show_top )
225
  $output .= stripslashes( get_option( 'relpoststh_top_text', $this->top_text ) );
 
226
  $relpoststh_output_style = get_option( 'relpoststh_output_style', $this->output_style );
227
  $relpoststh_cleanhtml = get_option( 'relpoststh_cleanhtml', 0 );
228
  $text_height = get_option( 'relpoststh_textblockheight', $this->text_block_height );
229
- if ($relpoststh_output_style == 'list') {
 
230
  $output .= '<ul id="related_posts_thumbnails"';
231
- if (!$relpoststh_cleanhtml)
232
  $output .= ' style="list-style-type:none; list-style-position: inside; padding: 0; margin:0"';
 
233
  $output .= '>';
234
- }
235
- else
236
  $output .= '<div style="clear: both"></div><div style="border: 0pt none ; margin: 0pt; padding: 0pt;">';
237
- foreach( $posts as $post ) {
 
 
238
  $image = '';
239
  $url = '';
240
  if ( $thsource == 'custom-field' ) {
241
  $debug .= 'Using custom field;';
242
  $url = $basic_url = get_post_meta( $post->ID, get_option( 'relpoststh_customfield', $this->custom_field ), true );
243
- if (strpos($url, '/wp-content') !== false)
244
- $url = substr($url, strpos($url, '/wp-content'));
 
245
  $theme_resize_url = get_option( 'relpoststh_theme_resize_url', '' );
246
- if ( !empty( $theme_resize_url ) )
247
  $url = $theme_resize_url . '?src=' . $url . '&w=' . $width . '&h=' . $height . '&zc=1&q=90';
248
- }
249
- else {
250
  $from_post_body = true;
251
  if ( current_theme_supports( 'post-thumbnails' ) ) { // using built in Wordpress feature
252
  $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
253
  $debug .= 'Post-thumbnails enabled in theme;';
254
- if ( !( empty( $post_thumbnail_id ) || $post_thumbnail_id === false ) ) { // post has thumbnail
255
  $debug .= 'Post has thumbnail '.$post_thumbnail_id.';';
256
  $debug .= 'Postthname: '.$poststhname.';';
257
  $image = wp_get_attachment_image_src( $post_thumbnail_id, $poststhname );
258
  $url = $image[0];
259
  $from_post_body = false;
260
- }
261
- else
262
  $debug .= 'Post has no thumbnail;';
 
263
  }
264
  if ( $from_post_body ) { // Theme does not support post-thumbnails, or post does not have assigned thumbnail
265
  $debug .= 'Getting image from post body;';
266
  $wud = wp_upload_dir();
267
  preg_match_all( '|<img.*?src=[\'"](' . $wud['baseurl'] . '.*?)[\'"].*?>|i', $post->post_content, $matches ); // searching for the first uploaded image in text
268
- if ( isset( $matches ) ) $image = $matches[1][0];
269
- else
 
 
 
 
270
  $debug .= 'No image was found;';
 
 
271
  if ( strlen( trim( $image ) ) > 0 ) {
272
  $image_sizes = @getimagesize( $image );
273
- if ( $image_sizes === false )
274
  $debug .= 'Unable to determine parsed image size';
 
275
  if ( $image_sizes !== false && isset( $image_sizes[0] ) && $image_sizes[0] == $width ) { // if this image is the same size as we need
276
  $debug .= 'Image used is the required size;';
277
  $url = $image;
278
- }
279
- else { // if not, search for resized thumbnail according to Wordpress thumbnails naming function
280
  $debug .= 'Changing image according to Wordpress standards;';
281
  $url = preg_replace( '/(-[0-9]+x[0-9]+)?(\.[^\.]*)$/', '-' . $width . 'x' . $height . '$2', $image );
282
  }
283
- }
284
- else
285
  $debug .= 'Found wrong formatted image: '.$image.';';
 
286
  }
287
  $basic_url = $url;
288
  }
@@ -302,32 +346,34 @@ class RelatedPostsThumbnails {
302
  $post_excerpt = ( empty( $post->post_excerpt ) ) ? $post->post_content : $post->post_excerpt;
303
  $excerpt = $this->process_text_cut( $post_excerpt, $excerpt_length );
304
 
305
- if ( !empty( $title ) && !empty( $excerpt ) ) {
306
  $title = '<b>' . $title . '</b>';
307
  $excerpt = '<br/>' . $excerpt;
308
  }
309
 
 
 
310
  $debug .= 'Using title with size ' . $text_length . '. Using excerpt with size ' . $excerpt_length . ';';
311
- if ($relpoststh_output_style == 'list') {
312
  $link = get_permalink( $post->ID );
313
- $fontface = str_replace('"', "'", stripslashes( get_option( 'relpoststh_fontfamily', $this->font_family ) ) );
314
  $output .= '<li ';
315
- if ( !$relpoststh_cleanhtml )
316
  $output .= ' style="float: left; padding: 0; margin:0; padding: 5px; display: block; border-right: 1px solid ' . get_option( 'relpoststh_bordercolor', $this->border_color ) . '; background-color: ' . get_option( 'relpoststh_background', $this->background ) . '" onmouseout="this.style.backgroundColor=\'' . get_option( 'relpoststh_background', $this->background ) . '\'" onmouseover="this.style.backgroundColor=\'' . get_option( 'relpoststh_hoverbackground', $this->hoverbackground ) . '\'"';
 
317
  $output .= '>';
318
  $output .= '<a href="' . $link . '" ><img alt="' . $title . '" src="' . $url . '" width="' . $width . '" height="' . $height . '" ';
319
- if ( !$relpoststh_cleanhtml )
320
  $output .= 'style="padding: 0px; margin: 0px; border: 0pt none;"';
 
321
  $output .= '/></a>';
322
- if ($text_height != '0')
323
- {
324
  $output .= '<a href="' . $link . '"';
325
- if ( !$relpoststh_cleanhtml )
326
- $output .= ' style="display: block; width: ' . $width . 'px; overflow: hidden;height: ' . $text_height . 'px; font-family: ' . $fontface . '; font-style: normal; font-variant: normal; font-weight: normal; font-size: ' . get_option( 'relpoststh_fontsize', $this->font_size ) . 'px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; color: ' . get_option( 'relpoststh_fontcolor', $this->font_color ) . ';text-decoration: none;"';
327
  $output .= '><span>' . $title . $excerpt . '</span></a></li>';
328
  }
329
- }
330
- else {
331
  $output .= '<a onmouseout="this.style.backgroundColor=\'' . get_option( 'relpoststh_background', $this->background ) . '\'" onmouseover="this.style.backgroundColor=\'' . get_option( 'relpoststh_hoverbackground', $this->hoverbackground ) . '\'" style="background-color: ' . get_option( 'relpoststh_background', $this->background ) . '; border-right: 1px solid ' . get_option( 'relpoststh_bordercolor', $this->border_color ) . '; border-bottom: medium none; margin: 0pt; padding: 6px; display: block; float: left; text-decoration: none; text-align: left; cursor: pointer;" href="' . get_permalink( $post->ID ) . '">';
332
  $output .= '<div style="border: 0pt none ; margin: 0pt; padding: 0pt; width: ' . $width . 'px; height: ' . ( $height + $text_height ) . 'px;">';
333
  $output .= '<div style="border: 0pt none ; margin: 0pt; padding: 0pt; background: transparent url(' . $url . ') no-repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; width: ' . $width . 'px; height: ' . $height . 'px;"></div>';
@@ -335,12 +381,12 @@ class RelatedPostsThumbnails {
335
  $output .= '</div>';
336
  $output .= '</a>';
337
  }
338
-
339
  } // end foreach
340
- if ($relpoststh_output_style == 'list')
341
  $output .= '</ul>';
342
- else
343
  $output .= '</div>';
 
344
  $output .= '<div style="clear: both"></div>';
345
  return $this->finish_process( $output, $debug, $time );
346
  }
@@ -348,7 +394,7 @@ class RelatedPostsThumbnails {
348
  function finish_process( $output, $debug, $time ) {
349
  $devmode = get_option( 'relpoststh_devmode', $this->devmode );
350
  if ( $devmode ) {
351
- $time = microtime(true) - $time;
352
  $debug .= "Plugin execution time: $time sec;";
353
  $output .= '<!-- '.$debug.' -->';
354
  }
@@ -356,40 +402,42 @@ class RelatedPostsThumbnails {
356
  }
357
 
358
  function process_text_cut( $text, $length ) {
359
- if ($length == 0)
360
  return '';
361
- else {
362
  $text = htmlspecialchars( strip_tags( strip_shortcodes( $text ) ) );
363
- if ( function_exists('mb_strlen') ) {
364
- return ( ( mb_strlen( $text ) > $length ) ? mb_substr( $text, 0, $length) . '...' : $text );
365
- }
366
- else {
367
- return ( ( strlen( $text ) > $length ) ? substr( $text, 0, $length) . '...' : $text );
368
  }
369
  }
370
  }
371
 
372
- function is_relpoststh_show() { // Checking display options
373
- if ( !is_single() && get_option( 'relpoststh_single_only', $this->single_only ) ) { // single only
 
374
  return false;
375
  }
376
  /* Check post type */
377
  $post_types = get_option( 'relpoststh_post_types', $this->post_types );
378
  $post_type = get_post_type();
379
- if ( !in_array($post_type, $post_types) ) {
380
  return false;
381
  }
382
  /* Check categories */
383
  $id = get_the_ID();
384
  $categories_all = get_option( 'relpoststh_categoriesall', $this->categories_all );
385
- if ( $categories_all != '1') { // only specific categories were selected
386
  $post_categories = wp_get_object_terms( $id, array( 'category' ), array( 'fields' => 'ids' ) );
387
  $relpoststh_categories = get_option( 'relpoststh_categories' );
388
- if ( !is_array( $relpoststh_categories ) || !is_array( $post_categories ) ) // no categories were selcted or post doesn't belong to any
389
  return false;
 
390
  $common_categories = array_intersect( $relpoststh_categories, $post_categories );
391
- if ( empty( $common_categories ) ) // post doesn't belong to specified categories
392
  return false;
 
393
  }
394
  return true;
395
  }
@@ -398,27 +446,43 @@ class RelatedPostsThumbnails {
398
  $page = add_options_page( __( 'Related Posts Thumbnails', 'related-posts-thumbnails' ), __( 'Related Posts Thumbs', 'related-posts-thumbnails' ), 'administrator', 'related-posts-thumbnails', array( $this, 'admin_interface' ) );
399
  }
400
 
401
- function admin_interface() { // Admin interface
402
- if ( isset($_POST['action']) && ($_POST['action'] == 'update') ) {
403
- if ( !current_user_can( 'manage_options' ) ) {
 
404
  wp_die( __( 'No access', 'related-posts-thumbnails' ) );
405
  }
406
  check_admin_referer( 'related-posts-thumbnails' );
407
  $validation = true;
408
- if ( !empty($_POST['relpoststh_year']) || !empty($_POST['relpoststh_month']) || !empty($_POST['relpoststh_year']) ) { // check date
409
  $set_date = sprintf( '%04d-%02d-%02d', $_POST['relpoststh_year'], $_POST['relpoststh_month'], $_POST['relpoststh_day'] );
410
- if ( checkdate( intval($_POST['relpoststh_month']), intval($_POST['relpoststh_day']), intval($_POST['relpoststh_year']) ) === false ) {
411
  $validation = false;
412
  $error = __( 'Wrong date', 'related-posts-thumbnails' ) . ': ' . sprintf( '%d/%d/%d', $_POST['relpoststh_month'], $_POST['relpoststh_day'], $_POST['relpoststh_year'] );
413
  }
414
- }
415
- else {
416
  $set_date = '';
417
  }
418
  if ( $validation ) {
419
- update_option( 'relpoststh_single_only', $_POST['relpoststh_single_only'] );
420
- update_option( 'relpoststh_post_types', $_POST['relpoststh_post_types'] );
421
- update_option( 'relpoststh_onlywiththumbs', $_POST['onlywiththumbs'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  update_option( 'relpoststh_output_style', $_POST['relpoststh_output_style'] );
423
  update_option( 'relpoststh_cleanhtml', $_POST['relpoststh_cleanhtml'] );
424
  update_option( 'relpoststh_auto', $_POST['relpoststh_auto'] );
@@ -448,55 +512,49 @@ class RelatedPostsThumbnails {
448
  update_option( 'relpoststh_devmode', $_POST['relpoststh_devmode'] );
449
  update_option( 'relpoststh_startdate', $set_date );
450
  update_option( 'relpoststh_custom_taxonomies', $_POST['relpoststh_custom_taxonomies'] );
451
- echo "<div class='updated fade'><p>" . __( 'Settings updated', 'related-posts-thumbnails' ) ."</p></div>";
452
- }
453
- else {
454
- echo "<div class='error fade'><p>" . __( 'Settings update failed', 'related-posts-thumbnails' ) . '. '. $error . "</p></div>";
455
  }
456
  }
457
  $available_sizes = array( 'thumbnail' => 'thumbnail', 'medium' => 'medium' );
458
  if ( current_theme_supports( 'post-thumbnails' ) ) {
459
  global $_wp_additional_image_sizes;
460
- if ( is_array($_wp_additional_image_sizes ) ) {
461
  $available_sizes = array_merge( $available_sizes, $_wp_additional_image_sizes );
462
  }
463
  }
464
- $relpoststh_single_only = get_option( 'relpoststh_single_only', $this->single_only );
465
- $relpoststh_auto = get_option( 'relpoststh_auto', $this->auto );
466
- $relpoststh_cleanhtml = get_option( 'relpoststh_cleanhtml', 0 );
467
- $relpoststh_relation = get_option( 'relpoststh_relation', $this->relation );
468
- $relpoststh_thsource = get_option( 'relpoststh_thsource', $this->thsource );
469
- $relpoststh_devmode = get_option( 'relpoststh_devmode', $this->devmode );
470
- $relpoststh_categoriesall = get_option( 'relpoststh_categoriesall', $this->categories_all );
471
- $relpoststh_categories = get_option( 'relpoststh_categories' );
472
- $relpoststh_show_categories = get_option( 'relpoststh_show_categories', get_option( 'relpoststh_categories' ) );
473
  $relpoststh_show_categoriesall = get_option( 'relpoststh_show_categoriesall', $relpoststh_categoriesall );
474
- $onlywiththumbs = get_option( 'relpoststh_onlywiththumbs', false );
475
- $relpoststh_startdate = explode( '-', get_option( 'relpoststh_startdate' ) );
476
- $relpoststh_output_style = get_option( 'relpoststh_output_style', $this->output_style );
477
- $thsources = array( 'post-thumbnails' => __('Post thumbnails', 'related_posts_thumbnails'), 'custom-field' => __('Custom field', 'related_posts_thumbnails') );
478
- $categories = get_categories();
479
- if ($this->wp_version >= 3)
480
- {
481
  $post_types = get_post_types( array( 'public' => 1 ) );
482
- }
483
- else
484
- {
485
  $post_types = get_post_types();
486
  }
487
  $relpoststh_post_types = get_option( 'relpoststh_post_types', $this->post_types );
488
- $output_styles = array('div' => __( 'Blocks', 'related-posts-thumbnails' ), 'list' => __( 'List', 'related-posts-thumbnails' ) );
489
- $relation_options = array('categories' => __('Categories', 'related-posts-thumbnails'), 'tags' => __('Tags', 'related-posts-thumbnails'), 'both' => __('Categories and Tags', 'related-posts-thumbnails'), 'no' => __('Random', 'related-posts-thumbnails'), 'custom' => __('Custom', 'related-posts-thumbnails') );
490
- if ($this->wp_version >= 3)
491
- {
492
- $custom_taxonomies = get_taxonomies( array('public' => 1) );
493
  $relpoststh_custom_taxonomies = get_option( 'relpoststh_custom_taxonomies', $this->custom_taxonomies );
494
- if (!is_array($relpoststh_custom_taxonomies))
495
  $relpoststh_custom_taxonomies = array();
496
- }
497
- else
498
- {
499
- $relation_options['custom'] .= ' '. __('(This option is available for WP v3+ only)', 'related_posts_thumbnails');
500
  }
501
  ?>
502
  <script type="text/javascript">
@@ -544,36 +602,36 @@ class RelatedPostsThumbnails {
544
  <input type="hidden" name="action" value="update" />
545
  <?php wp_nonce_field( 'related-posts-thumbnails' ); ?>
546
  <div class="metabox-holder">
547
- <div class="postbox">
548
- <h3><?php _e( 'General Display Options', 'related-posts-thumbnails' ); ?>:</h3>
549
  <table class="form-table">
550
  <tr valign="top">
551
  <th scope="row"><?php _e( 'Automatically append to the post content', 'related-posts-thumbnails' ); ?>:</th>
552
  <td>
553
- <input type="checkbox" name="relpoststh_auto" id="relpoststh_auto" value="1" <?php if ( $relpoststh_auto ) echo 'checked="checked"'; ?>/>
554
  <label for="relpoststh_auto"><?php _e( 'Or use <b>&lt;?php get_related_posts_thumbnails(); ?&gt;</b> in the Loop', 'related-posts-thumbnails' ); ?></label><br />
555
  </td>
556
  </tr>
557
  <tr valign="top">
558
  <th scope="row"><?php _e( 'Developer mode', 'related-posts-thumbnails' ); ?>:</th>
559
  <td>
560
- <input type="checkbox" name="relpoststh_devmode" id="relpoststh_devmode" value="1" <?php if ( $relpoststh_devmode ) echo 'checked="checked"'; ?>/>
561
  <label for="relpoststh_devmode"><?php _e( 'This will add debugging information in HTML source', 'related-posts-thumbnails' ); ?></label><br />
562
  </td>
563
  </tr>
564
  <tr valign="top">
565
  <th scope="row"><?php _e( 'Page type', 'related-posts-thumbnails' ); ?>:</th>
566
  <td>
567
- <input type="checkbox" name="relpoststh_single_only" id="relpoststh_single_only" value="1" <?php if ( $relpoststh_single_only ) echo 'checked="checked"'; ?>/>
568
  <label for="relpoststh_single_only"><?php _e( 'Show on single posts only', 'related-posts-thumbnails' ); ?></label><br />
569
  </td>
570
  </tr>
571
  <tr valign="top">
572
  <th scope="row"><?php _e( 'Post types', 'related-posts-thumbnails' ); ?>:</th>
573
  <td>
574
- <?php if ( is_array($post_types) && count($post_types) ): ?>
575
- <?php foreach ($post_types as $post_type): ?>
576
- <input type="checkbox" name="relpoststh_post_types[]" id="pt_<?php echo $post_type; ?>" value="<?php echo $post_type; ?>" <?php if ( in_array( $post_type, $relpoststh_post_types ) ) echo 'checked="checked"'; ?>/>
577
  <label for="pt_<?php echo $post_type; ?>"><?php echo $post_type; ?></label>
578
  <?php endforeach; ?>
579
  <?php endif; ?>
@@ -594,7 +652,7 @@ class RelatedPostsThumbnails {
594
  <tr>
595
  <th scope="row"><?php _e( 'Include only posts after', 'related-posts-thumbnails' ); ?>:</th>
596
  <td>
597
- <?php _e( 'Year', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_year" size="4" value="<?php if (isset($relpoststh_startdate[0])) echo $relpoststh_startdate[0]; ?>"> <?php _e( 'Month', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_month" size="2" value="<?php if (isset($relpoststh_startdate[1])) echo $relpoststh_startdate[1]; ?>"> <?php _e( 'Day', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_day" size="2" value="<?php if (isset($relpoststh_startdate[2])) echo $relpoststh_startdate[2]; ?>"> <label for="relpoststh_excerptlength"><?php _e( 'Leave empty for all posts dates', 'related-posts-thumbnails' ); ?></label><br />
598
  </td>
599
  </tr>
600
  <tr>
@@ -612,7 +670,7 @@ class RelatedPostsThumbnails {
612
  <tr>
613
  <th scope="row"><?php _e( 'Default image URL', 'related-posts-thumbnails' ); ?>:</th>
614
  <td>
615
- <input type="text" name="relpoststh_default_image" value="<?php echo get_option('relpoststh_default_image', $this->default_image );?>" size="50"/>
616
  </td>
617
  </tr>
618
  <tr>
@@ -620,77 +678,78 @@ class RelatedPostsThumbnails {
620
  <td>
621
  <select name="relpoststh_thsource" id="relpoststh_thsource">
622
  <?php foreach ( $thsources as $name => $title ) : ?>
623
- <option value="<?php echo $name; ?>" <?php if ( $relpoststh_thsource == $name ) echo 'selected'; ?>><?php echo $title; ?></option>
624
  <?php endforeach; ?>
625
  </select>
626
  </td>
627
  </tr>
628
  </table>
629
  </div>
630
- <div class="postbox" id="relpoststh-post-thumbnails" <?php if ( $relpoststh_thsource != 'post-thumbnails' ) : ?> style="display:none" <?php endif; ?>>
631
- <h3><?php _e( 'Thumbnails source', 'related-posts-thumbnails' ); ?>:</h3>
632
  <table class="form-table">
633
  <tr valign="top">
634
  <th scope="row"><?php _e( 'Post-thumbnails name', 'related-posts-thumbnails' ); ?>:</th>
635
  <td>
636
  <select name="relpoststh_poststhname">
637
  <?php foreach ( $available_sizes as $size_name => $size ) : ?>
638
- <option <?php if ( $size_name == get_option('relpoststh_poststhname', $this->poststhname) ) echo 'selected'; ?>><?php echo $size_name; ?></option>
639
  <?php endforeach; ?>
640
  </select>
641
- <?php if ( !current_theme_supports( 'post-thumbnails' ) ) : ?>
642
  (<?php _e( 'Your theme has to support post-thumbnails to have more choices', 'related-posts-thumbnails' ); ?>)
643
  <?php endif; ?>
644
  </td>
645
  </tr>
646
- <?php if ( current_theme_supports( 'post-thumbnails' ) ): ?>
647
  <tr>
648
  <th scope="row"><?php _e( 'Show posts only with thumbnails', 'related-posts-thumbnails' ); ?>:</th>
649
  <td>
650
- <input type="checkbox" name="onlywiththumbs" id="onlywiththumbs" value="1" <?php if ( $onlywiththumbs ) echo 'checked="checked"'; ?>/>
651
  <label for="onlywiththumbs"><?php _e( 'Only posts with assigned Featured Image', 'related-posts-thumbnails' ); ?></label><br />
652
  </td>
653
  </tr>
654
  <?php endif; ?>
655
  </table>
656
  </div>
657
- <div class="postbox" id="relpoststh-custom-field" <?php if ( $relpoststh_thsource != 'custom-field' ) : ?> style="display:none" <?php endif; ?>>
658
- <h3><?php _e( 'Thumbnails source', 'related-posts-thumbnails' ); ?>:</h3>
659
  <table class="form-table">
660
  <tr valign="top">
661
  <th scope="row"><?php _e( 'Custom field name', 'related-posts-thumbnails' ); ?>:</th>
662
  <td>
663
- <input type="text" name="relpoststh_customfield" value="<?php echo get_option('relpoststh_customfield', $this->custom_field );?>" size="50"/>
664
  </td>
665
  </tr>
666
  <tr valign="top">
667
  <th scope="row"><?php _e( 'Size', 'related-posts-thumbnails' ); ?>:</th>
668
  <td>
669
- <?php _e( 'Width', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_customwidth" value="<?php echo get_option('relpoststh_customwidth', $this->custom_width );?>" size="3"/>px x
670
- <?php _e( 'Height', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_customheight" value="<?php echo get_option('relpoststh_customheight', $this->custom_height );?>" size="3"/>px
671
  </td>
672
  </tr>
673
  <tr valign="top">
674
  <th scope="row"><?php _e( 'Theme resize url', 'related-posts-thumbnails' ); ?>:</th>
675
  <td>
676
- <input type="text" name="relpoststh_theme_resize_url" value="<?php echo get_option('relpoststh_theme_resize_url', '' );?>" size="50"/>
677
  (<?php _e( 'If your theme resizes images, enter URL to its resizing PHP file', 'related-posts-thumbnails' ); ?>)
678
  </td>
679
  </tr>
680
  </table>
681
  </div>
682
- <div class="postbox">
683
- <h3><?php _e( 'Style options', 'related-posts-thumbnails' ); ?>:</h3>
684
  <table class="form-table">
685
  <tr>
686
  <th scope="row"><?php _e( 'Output style', 'related-posts-thumbnails' ); ?>:</th>
687
  <td>
688
  <select name="relpoststh_output_style" id="relpoststh_output_style">
689
  <?php foreach ( $output_styles as $name => $title ) : ?>
690
- <option value="<?php echo $name; ?>" <?php if ( $relpoststh_output_style == $name ) echo 'selected'; ?>><?php echo $title; ?></option>
691
  <?php endforeach; ?>
692
  </select>
693
- <span id="relpoststh_cleanhtml" style="display: <?php if ($relpoststh_output_style == 'list') echo 'inline'; else echo 'none';?>;"><?php _e( 'Turn off plugin styles', 'related-posts-thumbnails' ); ?> <input type="checkbox" name="relpoststh_cleanhtml" <?php if ( $relpoststh_cleanhtml ) echo 'checked="checked"'; ?> /></span>
 
694
  </td>
695
  </tr>
696
  <tr valign="top">
@@ -751,22 +810,23 @@ class RelatedPostsThumbnails {
751
  </tr>
752
  </table>
753
  </div>
754
- <div class="postbox">
755
- <h3><?php _e( 'Relation Builder Options', 'related-posts-thumbnails' ); ?>:</h3>
756
  <table class="form-table">
757
  <tr valign="top">
758
  <th scope="row"><?php _e( 'Relation based on', 'related-posts-thumbnails' ); ?>:</th>
759
  <td>
760
- <?php if (is_array($relation_options) && count($relation_options)): ?>
761
- <?php foreach ($relation_options as $ro_key => $ro_name): ?>
762
- <input type="radio" name="relpoststh_relation" id="relpoststh_relation_<?php echo $ro_key; ?>" value="<?php echo $ro_key; ?>" <?php if ( $relpoststh_relation == $ro_key ) echo 'checked="checked"'; ?>/>
763
  <label for="relpoststh_relation_<?php echo $ro_key; ?>"><?php echo $ro_name; ?></label><br />
764
  <?php endforeach; ?>
765
  <?php endif; ?>
766
- <div id="custom_taxonomies" style="display: <?php if ($relpoststh_relation == 'custom') echo 'inline'; else echo 'none';?>;">
767
- <?php if (is_array($custom_taxonomies) && count($custom_taxonomies)): ?>
768
- <?php foreach ($custom_taxonomies as $custom_taxonomy): ?>
769
- <input type="checkbox" name="relpoststh_custom_taxonomies[]" id="ct_<?php echo $custom_taxonomy; ?>" value="<?php echo $custom_taxonomy; ?>" <?php if ( in_array( $custom_taxonomy, $relpoststh_custom_taxonomies ) ) echo 'checked="checked"'; ?>/>
 
770
  <label for="ct_<?php echo $custom_taxonomy; ?>"><?php echo $custom_taxonomy; ?></label>
771
  <?php endforeach; ?>
772
  <?php endif; ?>
@@ -775,21 +835,21 @@ class RelatedPostsThumbnails {
775
  </tr>
776
  </table>
777
  </div>
778
- <input name="Submit" value="<?php _e( 'Save Changes', 'related-posts-thumbnails' ); ?>" type="submit">
779
  </div>
780
  </form>
781
  </div>
782
- <p style="margin-top: 40px;"><small><?php _e('If you experience some problems with this plugin please let me know about it on <a href="http://wordpress.shaldybina.com/plugins/related-posts-thumbnails/">Plugin\'s homepage</a>. If you think this plugin is awesome please vote on <a href="http://wordpress.org/extend/plugins/related-posts-thumbnails/">Wordpress plugin page</a>. Thanks!', 'related-posts-thumbnails' ); ?></small></p>
783
  <?php
784
  }
785
 
786
  function display_categories_list( $categoriesall, $categories, $selected_categories, $all_name, $specific_name ) {
787
  ?>
788
- <input id="<?php echo $all_name; ?>" class="select_all" type="checkbox" name="<?php echo $all_name; ?>" value="1" <?php if ( $categoriesall == '1' ) echo 'checked="checked"'; ?>/>
789
  <label for="<?php echo $all_name; ?>"><?php _e( 'All', 'related-posts-thumbnails' ); ?></label>
790
  <div class="select_specific" <?php if ( $categoriesall == '1' ) : ?> style="display:none" <?php endif; ?>>
791
  <?php foreach ( $categories as $category ) : ?>
792
- <input type="checkbox" name="<?php echo $specific_name; ?>[]" id="<?php echo $specific_name; ?>_<?php echo $category->category_nicename; ?>" value="<?php echo $category->cat_ID; ?>" <?php if ( in_array( $category->cat_ID, (array)$selected_categories ) ) echo 'checked="checked"'; ?>/>
793
  <label for="<?php echo $specific_name; ?>_<?php echo $category->category_nicename; ?>"><?php echo $category->cat_name; ?></label><br />
794
  <?php endforeach; ?>
795
  </div>
@@ -804,8 +864,7 @@ function related_posts_thumbnails() {
804
  $related_posts_thumbnails = new RelatedPostsThumbnails();
805
  }
806
 
807
- function get_related_posts_thumbnails()
808
- {
809
  global $related_posts_thumbnails;
810
  echo $related_posts_thumbnails->get_html();
811
  }
@@ -815,34 +874,33 @@ function get_related_posts_thumbnails()
815
  */
816
  class RelatedPostsThumbnailsWidget extends WP_Widget {
817
  function RelatedPostsThumbnailsWidget() {
818
- parent::WP_Widget(false, $name = 'Related Posts Thumbnails');
819
  }
820
 
821
- function widget($args, $instance) {
822
- if ( is_single() && !is_page() ) { // display on post page only
823
  extract( $args );
824
- $title = apply_filters('widget_title', $instance['title']);
825
  echo $before_widget;
826
- if ( $title )
827
- echo $before_title . $title . $after_title;
828
  get_related_posts_thumbnails();
829
  echo $after_widget;
830
  }
831
  }
832
 
833
- function update($new_instance, $old_instance) {
834
  $instance = $old_instance;
835
- $instance['title'] = strip_tags($new_instance['title']);
836
  return $instance;
837
  }
838
 
839
- function form($instance) {
840
- $title = esc_attr($instance['title']);
841
  ?>
842
- <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
843
  <?php
844
  }
845
-
846
  } // class RelatedPostsThumbnailsWidget
847
 
848
  add_action( 'widgets_init', create_function( '', 'return register_widget("RelatedPostsThumbnailsWidget");' ) );
1
+ <?php
2
+ /**
3
+ * Plugin Name: Related Posts Thumbnails
4
+ * Plugin URI: http://wordpress.shaldybina.com/plugins/related-posts-thumbnails/
5
+ * Description: Showing related posts thumbnails under the post.
6
+ * Version: 1.4.0
7
+ * Author: Maria Shaldybina
8
+ * Author URI: http://shaldybina.com/
9
+ */
10
+
11
+ /*
12
+ Copyright 2010 Maria I Shaldybina
13
 
14
  This program is free software; you can redistribute it and/or modify
15
  it under the terms of the GNU General Public License as published by
23
  */
24
  class RelatedPostsThumbnails {
25
  /* Default values. PHP 4 compatible */
26
+ var $single_only = '1';
27
+ var $auto = '1';
28
+ var $top_text = '<h3>Related posts:</h3>';
29
+ var $number = 3;
30
+ var $relation = 'categories';
31
+ var $poststhname = 'thumbnail';
32
+ var $background = '#FFFFFF';
33
+ var $hoverbackground = '#EEEEEF';
34
+ var $border_color = '#DDDDDD';
35
+ var $font_color = '#333333';
36
+ var $font_family = 'Arial';
37
+ var $font_size = '12';
38
+ var $text_length = '100';
39
+ var $excerpt_length = '0';
40
+ var $custom_field = '';
41
+ var $custom_height = '100';
42
+ var $custom_width = '100';
43
  var $text_block_height = '75';
44
+ var $thsource = 'post-thumbnails';
45
+ var $categories_all = '1';
46
+ var $devmode = '0';
47
+ var $output_style = 'div';
48
+ var $post_types = array( 'post' );
49
  var $custom_taxonomies = array();
50
 
51
+ function RelatedPostsThumbnails() {
52
+ // initialization
53
  load_plugin_textdomain( 'related-posts-thumbnails', false, basename( dirname( __FILE__ ) ) . '/locale' );
54
+ $this->default_image = esc_url( plugins_url( 'img/default.png', __FILE__ ) );
55
+
56
+ // Compatibility for old default image path.
57
+ if ( $this->is_old_default_img() )
58
+ update_option( 'relpoststh_default_image', $this->default_image );
59
+
60
+ if ( get_option( 'relpoststh_auto', $this->auto ) ) {
61
  add_filter( 'the_content', array( $this, 'auto_show' ) );
62
+ }
63
+
64
  add_action( 'admin_menu', array( $this, 'admin_menu' ) );
65
  add_shortcode( 'related-posts-thumbnails' , array( $this, 'get_html' ) );
66
+
67
+ $this->wp_version = get_bloginfo( 'version' );
68
  }
69
 
70
+ /**
71
+ * [is_old_default_img Check the compatibility for old default image path.]
72
+ * @return boolean Return true if path is old.
73
+ */
74
+ function is_old_default_img() {
75
+
76
+ if ( get_option( 'relpoststh_default_image') !== $this->default_image ) {
77
+
78
+ $chunks = explode( '/', get_option( 'relpoststh_default_image') );
79
+ if ( in_array('related-posts-thumbnails', $chunks) ) {
80
+ return true;
81
+ }
82
+ }
83
+
84
+ }
85
+
86
+ function auto_show( $content ) {
87
+ // Automatically displaying related posts under post body
88
  return $content . $this->get_html( true );
89
  }
90
 
91
+ function get_html( $show_top = false ) {
92
+ // Getting related posts HTML
93
+ if ( $this->is_relpoststh_show() ) {
94
+ return $this->get_thumbnails( $show_top ); }
95
  return '';
96
  }
97
 
98
+ function get_thumbnails( $show_top = false ) {
99
+ // Retrieve Related Posts HTML for output
100
+ $output = '';
101
+ $debug = 'Developer mode initialisation; Version: 1.2.9;';
102
+ $time = microtime( true );
103
+ $posts_number = get_option( 'relpoststh_number', $this->number );
104
+
105
+ if ( $posts_number <= 0 ) { // return nothing if this parameter was set to <= 0
106
  return $this->finish_process( $output, $debug . 'Posts number is 0;', $time );
107
+ }
108
+
109
+ $id = get_the_ID();
110
+ $relation = get_option( 'relpoststh_relation', $this->relation );
111
+ $poststhname = get_option( 'relpoststh_poststhname', $this->poststhname );
112
+ $text_length = get_option( 'relpoststh_textlength', $this->text_length );
113
+ $excerpt_length = get_option( 'relpoststh_excerptlength', $this->excerpt_length );
114
+ $thsource = get_option( 'relpoststh_thsource', $this->thsource );
115
  $categories_show_all = get_option( 'relpoststh_show_categoriesall', get_option( 'relpoststh_categoriesall', $this->categories_all ) );
116
+ $onlywiththumbs = ( current_theme_supports( 'post-thumbnails' ) && $thsource == 'post-thumbnails' ) ? get_option( 'relpoststh_onlywiththumbs', false ) : false;
117
+ $post_type = get_post_type();
118
 
119
  global $wpdb;
120
 
126
  $query_objects = "SELECT distinct object_id FROM $wpdb->term_relationships WHERE 1=1 ";
127
 
128
  if ( $relation != 'no' ) { /* Get object terms */
129
+ if ( $relation == 'categories' ) {
130
+ $taxonomy = array( 'category' ); } elseif ( $relation == 'tags' ) {
131
+ $taxonomy = array( 'post_tag' ); } elseif ( $relation == 'custom' ) {
132
+ $taxonomy = get_option( 'relpoststh_custom_taxonomies', $this->custom_taxonomies );
133
+ } else {
134
+ $taxonomy = array( 'category', 'post_tag' );
135
+ }
136
+ $object_terms = wp_get_object_terms( $id, $taxonomy, array( 'fields' => 'ids' ) );
137
+ if ( empty( $object_terms ) || ! is_array( $object_terms ) ) { // no terms to get taxonomy
138
+ return $this->finish_process( $output, $debug . 'No taxonomy terms to get posts;', $time );
139
+ }
140
+
141
+ $query = "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id in ('". implode( "', '", $object_terms ) . "')";
142
+ $object_taxonomy = $wpdb->get_results( $query );
143
+ $object_taxonomy_a = array();
144
+ if ( count( $object_taxonomy ) > 0 ) {
145
+ foreach ( $object_taxonomy as $item ) {
146
+ $object_taxonomy_a[] = $item->term_taxonomy_id;
147
+ }
148
+ }
149
+ $query_objects .= " AND term_taxonomy_id IN ('". implode( "', '", $object_taxonomy_a ) . "') ";
 
150
  }
151
 
152
  if ( $categories_show_all != '1' ) { /* Get filter terms */
153
  $select_terms = get_option( 'relpoststh_show_categories',
154
+ get_option( 'relpoststh_categories' ) );
155
+ if ( empty( $select_terms ) || ! is_array( $select_terms ) ) { // if no categories were specified intentionally return nothing
156
  return $this->finish_process( $output, $debug . 'No categories were selected;', $time );
157
+ }
158
 
159
  $query = "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id in ('". implode( "', '", $select_terms ) . "')";
160
  $taxonomy = $wpdb->get_results( $query );
161
  $filter_taxonomy_a = array();
162
  if ( count( $taxonomy ) > 0 ) {
163
+ foreach ( $taxonomy as $item ) {
164
+ $filter_taxonomy_a[] = $item->term_taxonomy_id; }
165
  }
166
+ if ( $relation != 'no' ) {
167
  $query_objects .= " AND object_id IN (SELECT distinct object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ('". implode( "', '", $filter_taxonomy_a ) . "') )";
168
+ } else {
 
169
  $query_objects .= " AND term_taxonomy_id IN ('". implode( "', '", $filter_taxonomy_a ) . "')";
170
  }
171
  }
173
  $relationships = $wpdb->get_results( $query_objects );
174
  $related_objects = array();
175
  if ( count( $relationships ) > 0 ) {
176
+ foreach ( $relationships as $item ) {
177
  $related_objects[] = $item->object_id;
178
+ }
179
  }
180
  }
181
 
182
  $query = "SELECT distinct ID FROM $wpdb->posts ";
183
  $where = " WHERE post_type = '" . $post_type . "' AND post_status = 'publish' AND ID<>" . $id; // not the current post
184
  $startdate = get_option( 'relpoststh_startdate' );
185
+ if ( ! empty( $startdate ) && preg_match( '/^\d\d\d\d-\d\d-\d\d$/', $startdate ) ) { // If startdate was set
186
  $debug .= "Startdate: $startdate;";
187
  $where .= " AND post_date >= '" . $startdate . "'";
188
  }
189
  if ( $use_filter ) {
190
  $where .= " AND ID IN ('". implode( "', '", $related_objects ) . "')";
191
  }
192
+ $join = '';
193
  if ( $onlywiththumbs ) {
194
+ $debug .= 'Only with thumbnails;';
195
  $join = " INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)";
196
+ $where .= " AND $wpdb->postmeta.meta_key = '_thumbnail_id'";
197
  }
198
 
199
+ $order = ' ORDER BY rand() LIMIT ' . $posts_number;
200
  $random_posts = $wpdb->get_results( $query . $join . $where . $order );
201
 
202
  /* Get posts by their IDs */
203
+ if ( ! is_array( $random_posts ) || count( $random_posts ) < 1 ) {
204
  return $this->finish_process( $output, $debug . 'No posts matching relationships criteria;', $time );
205
  }
206
 
207
  $posts_in = array();
208
+ foreach ( $random_posts as $random_post ) {
209
  $posts_in[] = $random_post->ID;
210
+ }
211
  $query = "SELECT ID, post_content, post_excerpt, post_title FROM $wpdb->posts WHERE ID IN ('". implode( "', '", $posts_in ) . "')";
212
  $posts = $wpdb->get_results( $query );
213
  if ( ! ( is_array( $posts ) && count( $posts ) > 0 ) ) { // no posts
214
  $debug .= 'No posts found;';
215
  return $this->finish_process( $output, $debug, $time );
216
+ } else {
 
217
  $debug .= 'Found ' . count( $posts ) . ' posts;';
218
+ }
219
 
220
  /* Calculating sizes */
221
  if ( $thsource == 'custom-field' ) {
222
  $debug .= 'Custom sizes;';
223
  $width = get_option( 'relpoststh_customwidth', $this->custom_width );
224
  $height = get_option( 'relpoststh_customheight', $this->custom_height );
225
+ } else { // post-thumbnails source
 
226
  if ( $poststhname == 'thumbnail' || $poststhname == 'medium' || $poststhname == 'large' ) { // get thumbnail size for basic sizes
227
  $debug .= 'Basic sizes;';
228
  $width = get_option( "{$poststhname}_size_w" );
229
  $height = get_option( "{$poststhname}_size_h" );
230
+ } elseif ( current_theme_supports( 'post-thumbnails' ) ) { // get sizes for theme supported thumbnails
 
231
  global $_wp_additional_image_sizes;
232
  if ( isset( $_wp_additional_image_sizes[ $poststhname ] ) ) {
233
  $debug .= 'Additional sizes;';
234
+ $width = $_wp_additional_image_sizes[ $poststhname ]['width'];
235
+ $height = $_wp_additional_image_sizes[ $poststhname ]['height'];
236
+ } else { $debug .= 'No additional sizes;'; }
 
 
237
  }
238
  }
239
  // displaying square if one size is not cropping
240
+ if ( $height == 9999 ) {
241
  $height = $width;
242
+ }
243
+ if ( $width == 9999 ) {
244
  $width = $height;
245
+ }
246
  // theme is not supporting but settings were not changed
247
  if ( empty( $width ) ) {
248
  $debug .= 'Using default width;';
249
+ $width = get_option( 'thumbnail_size_w' );
250
  }
251
  if ( empty( $height ) ) {
252
  $debug .= 'Using default height;';
253
+ $height = get_option( 'thumbnail_size_h' );
254
  }
255
  $debug .= 'Got sizes '.$width.'x'.$height.';';
256
  // rendering related posts HTML
257
+ if ( $show_top ) {
258
  $output .= stripslashes( get_option( 'relpoststh_top_text', $this->top_text ) );
259
+ }
260
  $relpoststh_output_style = get_option( 'relpoststh_output_style', $this->output_style );
261
  $relpoststh_cleanhtml = get_option( 'relpoststh_cleanhtml', 0 );
262
  $text_height = get_option( 'relpoststh_textblockheight', $this->text_block_height );
263
+
264
+ if ( $relpoststh_output_style == 'list' ) {
265
  $output .= '<ul id="related_posts_thumbnails"';
266
+ if ( ! $relpoststh_cleanhtml ) {
267
  $output .= ' style="list-style-type:none; list-style-position: inside; padding: 0; margin:0"';
268
+ }
269
  $output .= '>';
270
+ } else {
 
271
  $output .= '<div style="clear: both"></div><div style="border: 0pt none ; margin: 0pt; padding: 0pt;">';
272
+ }
273
+
274
+ foreach ( $posts as $post ) {
275
  $image = '';
276
  $url = '';
277
  if ( $thsource == 'custom-field' ) {
278
  $debug .= 'Using custom field;';
279
  $url = $basic_url = get_post_meta( $post->ID, get_option( 'relpoststh_customfield', $this->custom_field ), true );
280
+ if ( strpos( $url, '/wp-content' ) !== false ) {
281
+ $url = substr( $url, strpos( $url, '/wp-content' ) );
282
+ }
283
  $theme_resize_url = get_option( 'relpoststh_theme_resize_url', '' );
284
+ if ( ! empty( $theme_resize_url ) ) {
285
  $url = $theme_resize_url . '?src=' . $url . '&w=' . $width . '&h=' . $height . '&zc=1&q=90';
286
+ }
287
+ } else {
288
  $from_post_body = true;
289
  if ( current_theme_supports( 'post-thumbnails' ) ) { // using built in Wordpress feature
290
  $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
291
  $debug .= 'Post-thumbnails enabled in theme;';
292
+ if ( ! ( empty( $post_thumbnail_id ) || $post_thumbnail_id === false ) ) { // post has thumbnail
293
  $debug .= 'Post has thumbnail '.$post_thumbnail_id.';';
294
  $debug .= 'Postthname: '.$poststhname.';';
295
  $image = wp_get_attachment_image_src( $post_thumbnail_id, $poststhname );
296
  $url = $image[0];
297
  $from_post_body = false;
298
+ } else {
 
299
  $debug .= 'Post has no thumbnail;';
300
+ }
301
  }
302
  if ( $from_post_body ) { // Theme does not support post-thumbnails, or post does not have assigned thumbnail
303
  $debug .= 'Getting image from post body;';
304
  $wud = wp_upload_dir();
305
  preg_match_all( '|<img.*?src=[\'"](' . $wud['baseurl'] . '.*?)[\'"].*?>|i', $post->post_content, $matches ); // searching for the first uploaded image in text
306
+
307
+ //var_dump($matches);
308
+
309
+ if ( isset( $matches ) and isset( $matches[1][0] ) ) {
310
+ $image = $matches[1][0];
311
+ } else {
312
  $debug .= 'No image was found;';
313
+ }
314
+
315
  if ( strlen( trim( $image ) ) > 0 ) {
316
  $image_sizes = @getimagesize( $image );
317
+ if ( $image_sizes === false ) {
318
  $debug .= 'Unable to determine parsed image size';
319
+ }
320
  if ( $image_sizes !== false && isset( $image_sizes[0] ) && $image_sizes[0] == $width ) { // if this image is the same size as we need
321
  $debug .= 'Image used is the required size;';
322
  $url = $image;
323
+ } else { // if not, search for resized thumbnail according to Wordpress thumbnails naming function
 
324
  $debug .= 'Changing image according to Wordpress standards;';
325
  $url = preg_replace( '/(-[0-9]+x[0-9]+)?(\.[^\.]*)$/', '-' . $width . 'x' . $height . '$2', $image );
326
  }
327
+ } else {
 
328
  $debug .= 'Found wrong formatted image: '.$image.';';
329
+ }
330
  }
331
  $basic_url = $url;
332
  }
346
  $post_excerpt = ( empty( $post->post_excerpt ) ) ? $post->post_content : $post->post_excerpt;
347
  $excerpt = $this->process_text_cut( $post_excerpt, $excerpt_length );
348
 
349
+ if ( ! empty( $title ) && ! empty( $excerpt ) ) {
350
  $title = '<b>' . $title . '</b>';
351
  $excerpt = '<br/>' . $excerpt;
352
  }
353
 
354
+ $fontface = '';
355
+
356
  $debug .= 'Using title with size ' . $text_length . '. Using excerpt with size ' . $excerpt_length . ';';
357
+ if ( $relpoststh_output_style == 'list' ) {
358
  $link = get_permalink( $post->ID );
359
+ $fontface = str_replace( '"', "'", stripslashes( get_option( 'relpoststh_fontfamily', $this->font_family ) ) );
360
  $output .= '<li ';
361
+ if ( ! $relpoststh_cleanhtml ) {
362
  $output .= ' style="float: left; padding: 0; margin:0; padding: 5px; display: block; border-right: 1px solid ' . get_option( 'relpoststh_bordercolor', $this->border_color ) . '; background-color: ' . get_option( 'relpoststh_background', $this->background ) . '" onmouseout="this.style.backgroundColor=\'' . get_option( 'relpoststh_background', $this->background ) . '\'" onmouseover="this.style.backgroundColor=\'' . get_option( 'relpoststh_hoverbackground', $this->hoverbackground ) . '\'"';
363
+ }
364
  $output .= '>';
365
  $output .= '<a href="' . $link . '" ><img alt="' . $title . '" src="' . $url . '" width="' . $width . '" height="' . $height . '" ';
366
+ if ( ! $relpoststh_cleanhtml ) {
367
  $output .= 'style="padding: 0px; margin: 0px; border: 0pt none;"';
368
+ }
369
  $output .= '/></a>';
370
+ if ( $text_height != '0' ) {
 
371
  $output .= '<a href="' . $link . '"';
372
+ if ( ! $relpoststh_cleanhtml ) {
373
+ $output .= ' style="display: block; width: ' . $width . 'px; overflow: hidden;height: ' . $text_height . 'px; font-family: ' . $fontface . '; font-style: normal; font-variant: normal; font-weight: normal; font-size: ' . get_option( 'relpoststh_fontsize', $this->font_size ) . 'px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; color: ' . get_option( 'relpoststh_fontcolor', $this->font_color ) . ';text-decoration: none;"'; }
374
  $output .= '><span>' . $title . $excerpt . '</span></a></li>';
375
  }
376
+ } else {
 
377
  $output .= '<a onmouseout="this.style.backgroundColor=\'' . get_option( 'relpoststh_background', $this->background ) . '\'" onmouseover="this.style.backgroundColor=\'' . get_option( 'relpoststh_hoverbackground', $this->hoverbackground ) . '\'" style="background-color: ' . get_option( 'relpoststh_background', $this->background ) . '; border-right: 1px solid ' . get_option( 'relpoststh_bordercolor', $this->border_color ) . '; border-bottom: medium none; margin: 0pt; padding: 6px; display: block; float: left; text-decoration: none; text-align: left; cursor: pointer;" href="' . get_permalink( $post->ID ) . '">';
378
  $output .= '<div style="border: 0pt none ; margin: 0pt; padding: 0pt; width: ' . $width . 'px; height: ' . ( $height + $text_height ) . 'px;">';
379
  $output .= '<div style="border: 0pt none ; margin: 0pt; padding: 0pt; background: transparent url(' . $url . ') no-repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; width: ' . $width . 'px; height: ' . $height . 'px;"></div>';
381
  $output .= '</div>';
382
  $output .= '</a>';
383
  }
 
384
  } // end foreach
385
+ if ( $relpoststh_output_style == 'list' ) {
386
  $output .= '</ul>';
387
+ } else {
388
  $output .= '</div>';
389
+ }
390
  $output .= '<div style="clear: both"></div>';
391
  return $this->finish_process( $output, $debug, $time );
392
  }
394
  function finish_process( $output, $debug, $time ) {
395
  $devmode = get_option( 'relpoststh_devmode', $this->devmode );
396
  if ( $devmode ) {
397
+ $time = microtime( true ) - $time;
398
  $debug .= "Plugin execution time: $time sec;";
399
  $output .= '<!-- '.$debug.' -->';
400
  }
402
  }
403
 
404
  function process_text_cut( $text, $length ) {
405
+ if ( $length == 0 ) {
406
  return '';
407
+ } else {
408
  $text = htmlspecialchars( strip_tags( strip_shortcodes( $text ) ) );
409
+ if ( function_exists( 'mb_strlen' ) ) {
410
+ return ( ( mb_strlen( $text ) > $length ) ? mb_substr( $text, 0, $length ) . '...' : $text );
411
+ } else {
412
+ return ( ( strlen( $text ) > $length ) ? substr( $text, 0, $length ) . '...' : $text );
 
413
  }
414
  }
415
  }
416
 
417
+ function is_relpoststh_show() {
418
+ // Checking display options
419
+ if ( ! is_single() && get_option( 'relpoststh_single_only', $this->single_only ) ) { // single only
420
  return false;
421
  }
422
  /* Check post type */
423
  $post_types = get_option( 'relpoststh_post_types', $this->post_types );
424
  $post_type = get_post_type();
425
+ if ( ! in_array( $post_type, $post_types ) ) {
426
  return false;
427
  }
428
  /* Check categories */
429
  $id = get_the_ID();
430
  $categories_all = get_option( 'relpoststh_categoriesall', $this->categories_all );
431
+ if ( $categories_all != '1' ) { // only specific categories were selected
432
  $post_categories = wp_get_object_terms( $id, array( 'category' ), array( 'fields' => 'ids' ) );
433
  $relpoststh_categories = get_option( 'relpoststh_categories' );
434
+ if ( ! is_array( $relpoststh_categories ) || ! is_array( $post_categories ) ) { // no categories were selcted or post doesn't belong to any
435
  return false;
436
+ }
437
  $common_categories = array_intersect( $relpoststh_categories, $post_categories );
438
+ if ( empty( $common_categories ) ) { // post doesn't belong to specified categories
439
  return false;
440
+ }
441
  }
442
  return true;
443
  }
446
  $page = add_options_page( __( 'Related Posts Thumbnails', 'related-posts-thumbnails' ), __( 'Related Posts Thumbs', 'related-posts-thumbnails' ), 'administrator', 'related-posts-thumbnails', array( $this, 'admin_interface' ) );
447
  }
448
 
449
+ function admin_interface() {
450
+ // Admin interface
451
+ if ( isset( $_POST['action'] ) && ($_POST['action'] == 'update') ) {
452
+ if ( ! current_user_can( 'manage_options' ) ) {
453
  wp_die( __( 'No access', 'related-posts-thumbnails' ) );
454
  }
455
  check_admin_referer( 'related-posts-thumbnails' );
456
  $validation = true;
457
+ if ( ! empty( $_POST['relpoststh_year'] ) || ! empty( $_POST['relpoststh_month'] ) || ! empty( $_POST['relpoststh_year'] ) ) { // check date
458
  $set_date = sprintf( '%04d-%02d-%02d', $_POST['relpoststh_year'], $_POST['relpoststh_month'], $_POST['relpoststh_day'] );
459
+ if ( checkdate( intval( $_POST['relpoststh_month'] ), intval( $_POST['relpoststh_day'] ), intval( $_POST['relpoststh_year'] ) ) === false ) {
460
  $validation = false;
461
  $error = __( 'Wrong date', 'related-posts-thumbnails' ) . ': ' . sprintf( '%d/%d/%d', $_POST['relpoststh_month'], $_POST['relpoststh_day'], $_POST['relpoststh_year'] );
462
  }
463
+ } else {
 
464
  $set_date = '';
465
  }
466
  if ( $validation ) {
467
+
468
+ if( isset( $_POST['relpoststh_single_only'] ) ) {
469
+ update_option( 'relpoststh_single_only', sanitize_text_field( wp_unslash( $_POST['relpoststh_single_only'] ) ) );
470
+ } else {
471
+ update_option( 'relpoststh_single_only', '0' );
472
+ }
473
+
474
+ if( isset( $_POST['relpoststh_post_types'] ) ) {
475
+ update_option( 'relpoststh_post_types', array_map( 'sanitize_text_field', wp_unslash( $_POST['relpoststh_post_types'] ) ) );
476
+ } else {
477
+ update_option( 'relpoststh_post_types', array() );
478
+ }
479
+
480
+ if( isset( $_POST['onlywiththumbs'] ) ) {
481
+ update_option( 'relpoststh_onlywiththumbs', sanitize_text_field( wp_unslash( $_POST['onlywiththumbs'] ) ) );
482
+ } else {
483
+ update_option( 'relpoststh_onlywiththumbs', '0' );
484
+ }
485
+
486
  update_option( 'relpoststh_output_style', $_POST['relpoststh_output_style'] );
487
  update_option( 'relpoststh_cleanhtml', $_POST['relpoststh_cleanhtml'] );
488
  update_option( 'relpoststh_auto', $_POST['relpoststh_auto'] );
512
  update_option( 'relpoststh_devmode', $_POST['relpoststh_devmode'] );
513
  update_option( 'relpoststh_startdate', $set_date );
514
  update_option( 'relpoststh_custom_taxonomies', $_POST['relpoststh_custom_taxonomies'] );
515
+ echo "<div class='updated fade'><p>" . __( 'Settings updated', 'related-posts-thumbnails' ) .'</p></div>';
516
+ } else {
517
+ echo "<div class='error fade'><p>" . __( 'Settings update failed', 'related-posts-thumbnails' ) . '. '. $error . '</p></div>';
 
518
  }
519
  }
520
  $available_sizes = array( 'thumbnail' => 'thumbnail', 'medium' => 'medium' );
521
  if ( current_theme_supports( 'post-thumbnails' ) ) {
522
  global $_wp_additional_image_sizes;
523
+ if ( is_array( $_wp_additional_image_sizes ) ) {
524
  $available_sizes = array_merge( $available_sizes, $_wp_additional_image_sizes );
525
  }
526
  }
527
+ $relpoststh_single_only = get_option( 'relpoststh_single_only', $this->single_only );
528
+ $relpoststh_auto = get_option( 'relpoststh_auto', $this->auto );
529
+ $relpoststh_cleanhtml = get_option( 'relpoststh_cleanhtml', 0 );
530
+ $relpoststh_relation = get_option( 'relpoststh_relation', $this->relation );
531
+ $relpoststh_thsource = get_option( 'relpoststh_thsource', $this->thsource );
532
+ $relpoststh_devmode = get_option( 'relpoststh_devmode', $this->devmode );
533
+ $relpoststh_categoriesall = get_option( 'relpoststh_categoriesall', $this->categories_all );
534
+ $relpoststh_categories = get_option( 'relpoststh_categories' );
535
+ $relpoststh_show_categories = get_option( 'relpoststh_show_categories', get_option( 'relpoststh_categories' ) );
536
  $relpoststh_show_categoriesall = get_option( 'relpoststh_show_categoriesall', $relpoststh_categoriesall );
537
+ $onlywiththumbs = get_option( 'relpoststh_onlywiththumbs', false );
538
+ $relpoststh_startdate = explode( '-', get_option( 'relpoststh_startdate' ) );
539
+ $relpoststh_output_style = get_option( 'relpoststh_output_style', $this->output_style );
540
+ $thsources = array( 'post-thumbnails' => __( 'Post thumbnails', 'related_posts_thumbnails' ), 'custom-field' => __( 'Custom field', 'related_posts_thumbnails' ) );
541
+ $categories = get_categories();
542
+ if ( $this->wp_version >= 3 ) {
 
543
  $post_types = get_post_types( array( 'public' => 1 ) );
544
+ } else {
 
 
545
  $post_types = get_post_types();
546
  }
547
  $relpoststh_post_types = get_option( 'relpoststh_post_types', $this->post_types );
548
+ $output_styles = array( 'div' => __( 'Blocks', 'related-posts-thumbnails' ), 'list' => __( 'List', 'related-posts-thumbnails' ) );
549
+ $relation_options = array( 'categories' => __( 'Categories', 'related-posts-thumbnails' ), 'tags' => __( 'Tags', 'related-posts-thumbnails' ), 'both' => __( 'Categories and Tags', 'related-posts-thumbnails' ), 'no' => __( 'Random', 'related-posts-thumbnails' ), 'custom' => __( 'Custom', 'related-posts-thumbnails' ) );
550
+ if ( $this->wp_version >= 3 ) {
551
+ $custom_taxonomies = get_taxonomies( array( 'public' => 1 ) );
 
552
  $relpoststh_custom_taxonomies = get_option( 'relpoststh_custom_taxonomies', $this->custom_taxonomies );
553
+ if ( ! is_array( $relpoststh_custom_taxonomies ) ) {
554
  $relpoststh_custom_taxonomies = array();
555
+ }
556
+ } else {
557
+ $relation_options['custom'] .= ' '. __( '(This option is available for WP v3+ only)', 'related_posts_thumbnails' );
 
558
  }
559
  ?>
560
  <script type="text/javascript">
602
  <input type="hidden" name="action" value="update" />
603
  <?php wp_nonce_field( 'related-posts-thumbnails' ); ?>
604
  <div class="metabox-holder">
605
+ <div class="postbox" style="padding: 20px">
606
+ <h2><?php _e( 'General Display Options', 'related-posts-thumbnails' ); ?>:</h2>
607
  <table class="form-table">
608
  <tr valign="top">
609
  <th scope="row"><?php _e( 'Automatically append to the post content', 'related-posts-thumbnails' ); ?>:</th>
610
  <td>
611
+ <input type="checkbox" name="relpoststh_auto" id="relpoststh_auto" value="1" <?php if ( $relpoststh_auto ) { echo 'checked="checked"'; } ?>/>
612
  <label for="relpoststh_auto"><?php _e( 'Or use <b>&lt;?php get_related_posts_thumbnails(); ?&gt;</b> in the Loop', 'related-posts-thumbnails' ); ?></label><br />
613
  </td>
614
  </tr>
615
  <tr valign="top">
616
  <th scope="row"><?php _e( 'Developer mode', 'related-posts-thumbnails' ); ?>:</th>
617
  <td>
618
+ <input type="checkbox" name="relpoststh_devmode" id="relpoststh_devmode" value="1" <?php if ( $relpoststh_devmode ) { echo 'checked="checked"'; } ?>/>
619
  <label for="relpoststh_devmode"><?php _e( 'This will add debugging information in HTML source', 'related-posts-thumbnails' ); ?></label><br />
620
  </td>
621
  </tr>
622
  <tr valign="top">
623
  <th scope="row"><?php _e( 'Page type', 'related-posts-thumbnails' ); ?>:</th>
624
  <td>
625
+ <input type="checkbox" name="relpoststh_single_only" id="relpoststh_single_only" value="1" <?php if ( $relpoststh_single_only ) { echo 'checked="checked"'; } ?>/>
626
  <label for="relpoststh_single_only"><?php _e( 'Show on single posts only', 'related-posts-thumbnails' ); ?></label><br />
627
  </td>
628
  </tr>
629
  <tr valign="top">
630
  <th scope="row"><?php _e( 'Post types', 'related-posts-thumbnails' ); ?>:</th>
631
  <td>
632
+ <?php if ( is_array( $post_types ) && count( $post_types ) ) : ?>
633
+ <?php foreach ( $post_types as $post_type ) : ?>
634
+ <input type="checkbox" name="relpoststh_post_types[]" id="pt_<?php echo $post_type; ?>" value="<?php echo $post_type; ?>" <?php if ( in_array( $post_type, $relpoststh_post_types ) ) { echo 'checked="checked"'; } ?>/>
635
  <label for="pt_<?php echo $post_type; ?>"><?php echo $post_type; ?></label>
636
  <?php endforeach; ?>
637
  <?php endif; ?>
652
  <tr>
653
  <th scope="row"><?php _e( 'Include only posts after', 'related-posts-thumbnails' ); ?>:</th>
654
  <td>
655
+ <?php _e( 'Year', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_year" size="4" value="<?php if ( isset( $relpoststh_startdate[0] ) ) { echo $relpoststh_startdate[0]; } ?>"> <?php _e( 'Month', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_month" size="2" value="<?php if ( isset( $relpoststh_startdate[1] ) ) { echo $relpoststh_startdate[1]; } ?>"> <?php _e( 'Day', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_day" size="2" value="<?php if ( isset( $relpoststh_startdate[2] ) ) { echo $relpoststh_startdate[2]; } ?>"> <label for="relpoststh_excerptlength"><?php _e( 'Leave empty for all posts dates', 'related-posts-thumbnails' ); ?></label><br />
656
  </td>
657
  </tr>
658
  <tr>
670
  <tr>
671
  <th scope="row"><?php _e( 'Default image URL', 'related-posts-thumbnails' ); ?>:</th>
672
  <td>
673
+ <input type="text" name="relpoststh_default_image" value="<?php echo get_option( 'relpoststh_default_image', $this->default_image );?>" size="50"/>
674
  </td>
675
  </tr>
676
  <tr>
678
  <td>
679
  <select name="relpoststh_thsource" id="relpoststh_thsource">
680
  <?php foreach ( $thsources as $name => $title ) : ?>
681
+ <option value="<?php echo $name; ?>" <?php if ( $relpoststh_thsource == $name ) { echo 'selected'; } ?>><?php echo $title; ?></option>
682
  <?php endforeach; ?>
683
  </select>
684
  </td>
685
  </tr>
686
  </table>
687
  </div>
688
+ <div class="postbox" style="padding: 20px" id="relpoststh-post-thumbnails" <?php if ( $relpoststh_thsource != 'post-thumbnails' ) : ?> style="display:none" <?php endif; ?>>
689
+ <h2><?php _e( 'Thumbnails source', 'related-posts-thumbnails' ); ?>:</h2>
690
  <table class="form-table">
691
  <tr valign="top">
692
  <th scope="row"><?php _e( 'Post-thumbnails name', 'related-posts-thumbnails' ); ?>:</th>
693
  <td>
694
  <select name="relpoststh_poststhname">
695
  <?php foreach ( $available_sizes as $size_name => $size ) : ?>
696
+ <option <?php if ( $size_name == get_option( 'relpoststh_poststhname', $this->poststhname ) ) { echo 'selected'; } ?>><?php echo $size_name; ?></option>
697
  <?php endforeach; ?>
698
  </select>
699
+ <?php if ( ! current_theme_supports( 'post-thumbnails' ) ) : ?>
700
  (<?php _e( 'Your theme has to support post-thumbnails to have more choices', 'related-posts-thumbnails' ); ?>)
701
  <?php endif; ?>
702
  </td>
703
  </tr>
704
+ <?php if ( current_theme_supports( 'post-thumbnails' ) ) : ?>
705
  <tr>
706
  <th scope="row"><?php _e( 'Show posts only with thumbnails', 'related-posts-thumbnails' ); ?>:</th>
707
  <td>
708
+ <input type="checkbox" name="onlywiththumbs" id="onlywiththumbs" value="1" <?php if ( $onlywiththumbs ) { echo 'checked="checked"'; } ?>/>
709
  <label for="onlywiththumbs"><?php _e( 'Only posts with assigned Featured Image', 'related-posts-thumbnails' ); ?></label><br />
710
  </td>
711
  </tr>
712
  <?php endif; ?>
713
  </table>
714
  </div>
715
+ <div class="postbox" style="padding: 20px" id="relpoststh-custom-field" <?php if ( $relpoststh_thsource != 'custom-field' ) : ?> style="display:none" <?php endif; ?>>
716
+ <h2><?php _e( 'Thumbnails source', 'related-posts-thumbnails' ); ?>:</h2>
717
  <table class="form-table">
718
  <tr valign="top">
719
  <th scope="row"><?php _e( 'Custom field name', 'related-posts-thumbnails' ); ?>:</th>
720
  <td>
721
+ <input type="text" name="relpoststh_customfield" value="<?php echo get_option( 'relpoststh_customfield', $this->custom_field );?>" size="50"/>
722
  </td>
723
  </tr>
724
  <tr valign="top">
725
  <th scope="row"><?php _e( 'Size', 'related-posts-thumbnails' ); ?>:</th>
726
  <td>
727
+ <?php _e( 'Width', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_customwidth" value="<?php echo get_option( 'relpoststh_customwidth', $this->custom_width );?>" size="3"/>px x
728
+ <?php _e( 'Height', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststh_customheight" value="<?php echo get_option( 'relpoststh_customheight', $this->custom_height );?>" size="3"/>px
729
  </td>
730
  </tr>
731
  <tr valign="top">
732
  <th scope="row"><?php _e( 'Theme resize url', 'related-posts-thumbnails' ); ?>:</th>
733
  <td>
734
+ <input type="text" name="relpoststh_theme_resize_url" value="<?php echo get_option( 'relpoststh_theme_resize_url', '' );?>" size="50"/>
735
  (<?php _e( 'If your theme resizes images, enter URL to its resizing PHP file', 'related-posts-thumbnails' ); ?>)
736
  </td>
737
  </tr>
738
  </table>
739
  </div>
740
+ <div class="postbox" style="padding: 20px">
741
+ <h2><?php _e( 'Style options', 'related-posts-thumbnails' ); ?>:</h2>
742
  <table class="form-table">
743
  <tr>
744
  <th scope="row"><?php _e( 'Output style', 'related-posts-thumbnails' ); ?>:</th>
745
  <td>
746
  <select name="relpoststh_output_style" id="relpoststh_output_style">
747
  <?php foreach ( $output_styles as $name => $title ) : ?>
748
+ <option value="<?php echo $name; ?>" <?php if ( $relpoststh_output_style == $name ) { echo 'selected'; } ?>><?php echo $title; ?></option>
749
  <?php endforeach; ?>
750
  </select>
751
+ <span id="relpoststh_cleanhtml" style="display: <?php if ( $relpoststh_output_style == 'list' ) { echo 'inline';
752
+ } else { echo 'none'; }?>;"><?php _e( 'Turn off plugin styles', 'related-posts-thumbnails' ); ?> <input type="checkbox" name="relpoststh_cleanhtml" <?php if ( $relpoststh_cleanhtml ) { echo 'checked="checked"'; } ?> /></span>
753
  </td>
754
  </tr>
755
  <tr valign="top">
810
  </tr>
811
  </table>
812
  </div>
813
+ <div class="postbox" style="padding: 20px">
814
+ <h2><?php _e( 'Relation Builder Options', 'related-posts-thumbnails' ); ?>:</h2>
815
  <table class="form-table">
816
  <tr valign="top">
817
  <th scope="row"><?php _e( 'Relation based on', 'related-posts-thumbnails' ); ?>:</th>
818
  <td>
819
+ <?php if ( is_array( $relation_options ) && count( $relation_options ) ) : ?>
820
+ <?php foreach ( $relation_options as $ro_key => $ro_name ) : ?>
821
+ <input type="radio" name="relpoststh_relation" id="relpoststh_relation_<?php echo $ro_key; ?>" value="<?php echo $ro_key; ?>" <?php if ( $relpoststh_relation == $ro_key ) { echo 'checked="checked"'; } ?>/>
822
  <label for="relpoststh_relation_<?php echo $ro_key; ?>"><?php echo $ro_name; ?></label><br />
823
  <?php endforeach; ?>
824
  <?php endif; ?>
825
+ <div id="custom_taxonomies" style="display: <?php if ( $relpoststh_relation == 'custom' ) { echo 'inline';
826
+ } else { echo 'none'; }?>;">
827
+ <?php if ( is_array( $custom_taxonomies ) && count( $custom_taxonomies ) ) : ?>
828
+ <?php foreach ( $custom_taxonomies as $custom_taxonomy ) : ?>
829
+ <input type="checkbox" name="relpoststh_custom_taxonomies[]" id="ct_<?php echo $custom_taxonomy; ?>" value="<?php echo $custom_taxonomy; ?>" <?php if ( in_array( $custom_taxonomy, $relpoststh_custom_taxonomies ) ) { echo 'checked="checked"'; } ?>/>
830
  <label for="ct_<?php echo $custom_taxonomy; ?>"><?php echo $custom_taxonomy; ?></label>
831
  <?php endforeach; ?>
832
  <?php endif; ?>
835
  </tr>
836
  </table>
837
  </div>
838
+ <input name="Submit" value="<?php _e( 'Save Changes', 'related-posts-thumbnails' ); ?>" type="submit" class="button-primary">
839
  </div>
840
  </form>
841
  </div>
842
+ <p style="margin-top: 40px;"><small><?php _e( 'If you experience some problems with this plugin please let me know about it on <a href="http://wordpress.shaldybina.com/plugins/related-posts-thumbnails/">Plugin\'s homepage</a>. If you think this plugin is awesome please vote on <a href="http://wordpress.org/extend/plugins/related-posts-thumbnails/">Wordpress plugin page</a>. Thanks!', 'related-posts-thumbnails' ); ?></small></p>
843
  <?php
844
  }
845
 
846
  function display_categories_list( $categoriesall, $categories, $selected_categories, $all_name, $specific_name ) {
847
  ?>
848
+ <input id="<?php echo $all_name; ?>" class="select_all" type="checkbox" name="<?php echo $all_name; ?>" value="1" <?php if ( $categoriesall == '1' ) { echo 'checked="checked"'; } ?>/>
849
  <label for="<?php echo $all_name; ?>"><?php _e( 'All', 'related-posts-thumbnails' ); ?></label>
850
  <div class="select_specific" <?php if ( $categoriesall == '1' ) : ?> style="display:none" <?php endif; ?>>
851
  <?php foreach ( $categories as $category ) : ?>
852
+ <input type="checkbox" name="<?php echo $specific_name; ?>[]" id="<?php echo $specific_name; ?>_<?php echo $category->category_nicename; ?>" value="<?php echo $category->cat_ID; ?>" <?php if ( in_array( $category->cat_ID, (array) $selected_categories ) ) { echo 'checked="checked"'; } ?>/>
853
  <label for="<?php echo $specific_name; ?>_<?php echo $category->category_nicename; ?>"><?php echo $category->cat_name; ?></label><br />
854
  <?php endforeach; ?>
855
  </div>
864
  $related_posts_thumbnails = new RelatedPostsThumbnails();
865
  }
866
 
867
+ function get_related_posts_thumbnails() {
 
868
  global $related_posts_thumbnails;
869
  echo $related_posts_thumbnails->get_html();
870
  }
874
  */
875
  class RelatedPostsThumbnailsWidget extends WP_Widget {
876
  function RelatedPostsThumbnailsWidget() {
877
+ parent::__construct( false, $name = 'Related Posts Thumbnails' );
878
  }
879
 
880
+ function widget( $args, $instance ) {
881
+ if ( is_single() && ! is_page() ) { // display on post page only
882
  extract( $args );
883
+ $title = apply_filters( 'widget_title', $instance['title'] );
884
  echo $before_widget;
885
+ if ( $title ) {
886
+ echo $before_title . $title . $after_title; }
887
  get_related_posts_thumbnails();
888
  echo $after_widget;
889
  }
890
  }
891
 
892
+ function update( $new_instance, $old_instance ) {
893
  $instance = $old_instance;
894
+ $instance['title'] = strip_tags( $new_instance['title'] );
895
  return $instance;
896
  }
897
 
898
+ function form( $instance ) {
899
+ $title = esc_attr( $instance['title'] );
900
  ?>
901
+ <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
902
  <?php
903
  }
 
904
  } // class RelatedPostsThumbnailsWidget
905
 
906
  add_action( 'widgets_init', create_function( '', 'return register_widget("RelatedPostsThumbnailsWidget");' ) );