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
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  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( 'relpoststhb_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_relpoststhb_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( 'relpoststhb_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( 'relpoststhb_relation', $this->relation );
  77.         $poststhname = get_option( 'relpoststhb_poststhname', $this->poststhname );
  78.         $text_length = get_option( 'relpoststhb_textlength', $this->text_length );
  79.         $excerpt_length = get_option( 'relpoststhb_excerptlength', $this->excerpt_length );
  80.         $thsource = get_option( 'relpoststhb_thsource', $this->thsource );
  81.         $categories_show_all = get_option( 'relpoststhb_show_categoriesall', get_option( 'relpoststhb_categoriesall', $this->categories_all ) );
  82.         $onlywiththumbs = ( current_theme_supports( 'post-thumbnails' ) && $thsource == 'post-thumbnails' ) ? get_option( 'relpoststhb_onlywiththumbs', false) : false;
  83.         $post_type = get_post_type();
  84.  
  85.         global $wpdb;
  86.  
  87.         /* Get taxonomy terms */
  88.         $debug .= "Relation: $relation; All categories: $categories_show_all;";
  89.         $use_filter = ( $categories_show_all != '1' || $relation != 'no' );
  90.  
  91.         if ( $use_filter ) {
  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( 'relpoststhb_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( 'relpoststhb_show_categories',
  121.                                             get_option( 'relpoststhb_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.             }
  139.  
  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( 'relpoststhb_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( 'relpoststhb_customwidth', $this->custom_width );
  189.             $height = get_option( 'relpoststhb_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( 'relpoststhb_top_text', $this->top_text ) );
  226.         $relpoststhb_output_style = get_option( 'relpoststhb_output_style', $this->output_style );
  227.         $relpoststhb_cleanhtml = get_option( 'relpoststhb_cleanhtml', 0 );
  228.         $text_height = get_option( 'relpoststhb_textblockheight', $this->text_block_height );
  229.         if ($relpoststhb_output_style == 'list') {
  230.             $output .= '<ul id="related_posts_thumbnails"';
  231.             if (!$relpoststhb_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( 'relpoststhb_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( 'relpoststhb_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.             }
  289.  
  290.             if ( strpos( $url, '/' ) === 0 ) {
  291.                 $debug .= 'Relative url: ' . $url . ';';
  292.                 $url = $basic_url = get_bloginfo( 'url' ) . $url;
  293.             }
  294.  
  295.             $debug .= 'Image URL: '.$url.';';
  296.             if ( empty( $basic_url ) ) { // parsed URL is empty or no file if can check
  297.                 $debug .= 'Image is empty or no file. Using default image;';
  298.                 $url = get_option( 'relpoststhb_default_image', $this->default_image );
  299.             }
  300.  
  301.             $title = $this->process_text_cut( $post->post_title, $text_length );
  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 ($relpoststhb_output_style == 'list') {
  312.                 $link = get_permalink( $post->ID );
  313.                 $fontface = str_replace('"', "'", stripslashes( get_option( 'relpoststhb_fontfamily', $this->font_family ) ) );
  314.                 $output .= '<li ';
  315.                 if ( !$relpoststhb_cleanhtml )
  316.                     $output .= ' style="float: left; padding: 0; margin:0; padding: 5px; display: block; border-right: 1px solid ' . get_option( 'relpoststhb_bordercolor', $this->border_color ) . '; background-color: ' . get_option( 'relpoststhb_background', $this->background ) . '" onmouseout="this.style.backgroundColor=\'' . get_option( 'relpoststhb_background', $this->background ) . '\'" onmouseover="this.style.backgroundColor=\'' . get_option( 'relpoststhb_hoverbackground', $this->hoverbackground ) . '\'"';
  317.                 $output .= '>';
  318.                 $output .= '<a href="' . $link . '" ><img alt="' . $title . '" src="' . $url . '" width="' . $width . '" height="' . $height . '" ';
  319.                 if ( !$relpoststhb_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 ( !$relpoststhb_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( 'relpoststhb_fontsize', $this->font_size ) . 'px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; color: ' . get_option( 'relpoststhb_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( 'relpoststhb_background', $this->background ) . '\'" onmouseover="this.style.backgroundColor=\'' . get_option( 'relpoststhb_hoverbackground', $this->hoverbackground ) . '\'" style="background-color: ' . get_option( 'relpoststhb_background', $this->background ) . '; border-right: 1px solid ' . get_option( 'relpoststhb_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>';
  334.                 $output .= '<div style="border: 0pt none; margin: 3px 0pt 0pt; padding: 0pt; font-family: ' . $fontface . '; font-style: normal; font-variant: normal; font-weight: normal; font-size: ' . get_option( 'relpoststhb_fontsize', $this->font_size ) . 'px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; color: ' . get_option( 'relpoststhb_fontcolor', $this->font_color ) . ';">' . $title . $excerpt . '</div>';
  335.                 $output .= '</div>';
  336.                 $output .= '</a>';
  337.             }
  338.  
  339.         } // end foreach
  340.         if ($relpoststhb_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.     }
  347.  
  348.     function finish_process( $output, $debug, $time ) {
  349.         $devmode = get_option( 'relpoststhb_devmode', $this->devmode );
  350.         if ( $devmode ) {
  351.             $time = microtime(true) - $time;
  352.             $debug .= "Plugin execution time: $time sec;";
  353.             $output .= '<!-- '.$debug.' -->';
  354.         }
  355.         return $output;
  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_relpoststhb_show() { // Checking display options
  373.         if ( !is_single() && get_option( 'relpoststhb_single_only', $this->single_only ) ) { // single only
  374.             return false;
  375.         }
  376.         /* Check post type */
  377.         $post_types = get_option( 'relpoststhb_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( 'relpoststhb_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.             $relpoststhb_categories = get_option( 'relpoststhb_categories' );
  388.             if ( !is_array( $relpoststhb_categories ) || !is_array( $post_categories ) ) // no categories were selcted or post doesn't belong to any
  389.                 return false;
  390.             $common_categories = array_intersect( $relpoststhb_categories, $post_categories );
  391.             if ( empty( $common_categories ) ) // post doesn't belong to specified categories
  392.                 return false;
  393.         }
  394.         return true;
  395.     }
  396.  
  397.     function admin_menu() {
  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['relpoststhb_year']) || !empty($_POST['relpoststhb_month']) || !empty($_POST['relpoststhb_year']) ) { // check date
  409.                 $set_date = sprintf( '%04d-%02d-%02d', $_POST['relpoststhb_year'], $_POST['relpoststhb_month'], $_POST['relpoststhb_day'] );
  410.                 if ( checkdate( intval($_POST['relpoststhb_month']), intval($_POST['relpoststhb_day']), intval($_POST['relpoststhb_year']) ) === false ) {
  411.                     $validation = false;
  412.                     $error = __( 'Wrong date', 'related-posts-thumbnails' ) . ': ' . sprintf( '%d/%d/%d', $_POST['relpoststhb_month'], $_POST['relpoststhb_day'], $_POST['relpoststhb_year'] );
  413.                 }
  414.             }
  415.             else {
  416.                 $set_date = '';
  417.             }
  418.             if ( $validation ) {
  419.                 update_option( 'relpoststhb_single_only', $_POST['relpoststhb_single_only'] );
  420.                 update_option( 'relpoststhb_post_types', $_POST['relpoststhb_post_types'] );
  421.                 update_option( 'relpoststhb_onlywiththumbs', $_POST['onlywiththumbs'] );
  422.                 update_option( 'relpoststhb_output_style', $_POST['relpoststhb_output_style'] );
  423.                 update_option( 'relpoststhb_cleanhtml', $_POST['relpoststhb_cleanhtml'] );
  424.                 update_option( 'relpoststhb_auto', $_POST['relpoststhb_auto'] );
  425.                 update_option( 'relpoststhb_top_text', $_POST['relpoststhb_top_text'] );
  426.                 update_option( 'relpoststhb_number', $_POST['relpoststhb_number'] );
  427.                 update_option( 'relpoststhb_relation', $_POST['relpoststhb_relation'] );
  428.                 update_option( 'relpoststhb_default_image', $_POST['relpoststhb_default_image'] );
  429.                 update_option( 'relpoststhb_poststhname', $_POST['relpoststhb_poststhname'] );
  430.                 update_option( 'relpoststhb_background', $_POST['relpoststhb_background'] );
  431.                 update_option( 'relpoststhb_hoverbackground', $_POST['relpoststhb_hoverbackground'] );
  432.                 update_option( 'relpoststhb_bordercolor', $_POST['relpoststhb_bordercolor'] );
  433.                 update_option( 'relpoststhb_fontcolor', $_POST['relpoststhb_fontcolor'] );
  434.                 update_option( 'relpoststhb_fontsize', $_POST['relpoststhb_fontsize'] );
  435.                 update_option( 'relpoststhb_fontfamily', $_POST['relpoststhb_fontfamily'] );
  436.                 update_option( 'relpoststhb_textlength', $_POST['relpoststhb_textlength'] );
  437.                 update_option( 'relpoststhb_excerptlength', $_POST['relpoststhb_excerptlength'] );
  438.                 update_option( 'relpoststhb_thsource', $_POST['relpoststhb_thsource'] );
  439.                 update_option( 'relpoststhb_customfield', $_POST['relpoststhb_customfield'] );
  440.                 update_option( 'relpoststhb_theme_resize_url', $_POST['relpoststhb_theme_resize_url'] );
  441.                 update_option( 'relpoststhb_customwidth', $_POST['relpoststhb_customwidth'] );
  442.                 update_option( 'relpoststhb_customheight', $_POST['relpoststhb_customheight'] );
  443.                 update_option( 'relpoststhb_textblockheight', $_POST['relpoststhb_textblockheight'] );
  444.                 update_option( 'relpoststhb_categoriesall', $_POST['relpoststhb_categoriesall'] );
  445.                 update_option( 'relpoststhb_categories', $_POST['relpoststhb_categories'] );
  446.                 update_option( 'relpoststhb_show_categoriesall', $_POST['relpoststhb_show_categoriesall'] );
  447.                 update_option( 'relpoststhb_show_categories', $_POST['relpoststhb_show_categories'] );
  448.                 update_option( 'relpoststhb_devmode', $_POST['relpoststhb_devmode'] );
  449.                 update_option( 'relpoststhb_startdate', $set_date );
  450.                 update_option( 'relpoststhb_custom_taxonomies', $_POST['relpoststhb_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.         $relpoststhb_single_only = get_option( 'relpoststhb_single_only', $this->single_only );
  465.         $relpoststhb_auto = get_option( 'relpoststhb_auto', $this->auto );
  466.         $relpoststhb_cleanhtml = get_option( 'relpoststhb_cleanhtml', 0 );
  467.         $relpoststhb_relation = get_option( 'relpoststhb_relation', $this->relation );
  468.         $relpoststhb_thsource = get_option( 'relpoststhb_thsource', $this->thsource );
  469.         $relpoststhb_devmode = get_option( 'relpoststhb_devmode', $this->devmode );
  470.         $relpoststhb_categoriesall = get_option( 'relpoststhb_categoriesall', $this->categories_all );
  471.         $relpoststhb_categories = get_option( 'relpoststhb_categories' );
  472.         $relpoststhb_show_categories = get_option( 'relpoststhb_show_categories', get_option( 'relpoststhb_categories' ) );
  473.         $relpoststhb_show_categoriesall = get_option( 'relpoststhb_show_categoriesall', $relpoststhb_categoriesall );
  474.         $onlywiththumbs = get_option( 'relpoststhb_onlywiththumbs', false );
  475.         $relpoststhb_startdate = explode( '-', get_option( 'relpoststhb_startdate' ) );
  476.         $relpoststhb_output_style = get_option( 'relpoststhb_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.         $relpoststhb_post_types = get_option( 'relpoststhb_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.             $relpoststhb_custom_taxonomies = get_option( 'relpoststhb_custom_taxonomies', $this->custom_taxonomies );
  494.             if (!is_array($relpoststhb_custom_taxonomies))
  495.                 $relpoststhb_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">
  503.     jQuery(document).ready(function($) {
  504.         $(".select_all").click(function(){
  505.             if (this.checked) {
  506.                 $(this).parent().find("div.select_specific").hide();
  507.             }
  508.             else {
  509.                 $(this).parent().find("div.select_specific").show();
  510.             }
  511.         });
  512.         $('#relpoststhb_thsource').change(function(){
  513.             if (this.value == 'post-thumbnails') {
  514.                 $('#relpoststh-post-thumbnails').show();
  515.                 $('#relpoststh-custom-field').hide();
  516.             }
  517.             else {
  518.                 $('#relpoststh-post-thumbnails').hide();
  519.                 $('#relpoststh-custom-field').show();
  520.             }
  521.         });
  522.         $('#relpoststhb_output_style').change(function(){
  523.             if (this.value == 'list') {
  524.                 $('#relpoststhb_cleanhtml').show();
  525.             }
  526.             else {
  527.                 $('#relpoststhb_cleanhtml').hide();
  528.             }
  529.         });
  530.         $("input[name='relpoststhb_relation']").change(function(){
  531.             if ($("input[name='relpoststhb_relation']:checked").val() == 'custom') {
  532.                 $('#custom_taxonomies').show();
  533.             }
  534.             else {
  535.                 $('#custom_taxonomies').hide();
  536.             }
  537.         });
  538.     });
  539. </script>
  540. <div class="wrap">
  541.     <div class="icon32" id="icon-options-general"><br></div>
  542.     <h2><?php _e( 'Related Posts Thumbnails Settings', 'related-posts-thumbnails' ); ?></h2>
  543.     <form action="?page=related-posts-thumbnails" method="POST">
  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="relpoststhb_auto" id="relpoststhb_auto" value="1" <?php if ( $relpoststhb_auto ) echo 'checked="checked"'; ?>/>
  554.                             <label for="relpoststhb_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="relpoststhb_devmode" id="relpoststhb_devmode" value="1" <?php if ( $relpoststhb_devmode ) echo 'checked="checked"'; ?>/>
  561.                             <label for="relpoststhb_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="relpoststhb_single_only" id="relpoststhb_single_only" value="1" <?php if ( $relpoststhb_single_only ) echo 'checked="checked"'; ?>/>
  568.                             <label for="relpoststhb_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="relpoststhb_post_types[]" id="pt_<?php echo $post_type; ?>" value="<?php echo $post_type; ?>" <?php if ( in_array( $post_type, $relpoststhb_post_types ) ) echo 'checked="checked"'; ?>/>
  577.                             <label for="pt_<?php echo $post_type; ?>"><?php echo $post_type; ?></label>
  578.                             <?php endforeach; ?>
  579.                             <?php endif; ?>
  580.                         </td>
  581.                     </tr>
  582.                     <tr valign="top">
  583.                         <th scope="row"><?php _e( 'Categories on which related thumbnails will appear', 'related-posts-thumbnails' ); ?>:</th>
  584.                         <td>
  585.                             <?php $this->display_categories_list( $relpoststhb_categoriesall, $categories, $relpoststhb_categories, 'relpoststhb_categoriesall', 'relpoststhb_categories' ); ?>
  586.                         </td>
  587.                     </tr>
  588.                     <tr valign="top">
  589.                         <th scope="row"><?php _e( 'Categories that will appear in related thumbnails', 'related-posts-thumbnails' ); ?>:</th>
  590.                         <td>
  591.                             <?php $this->display_categories_list( $relpoststhb_show_categoriesall, $categories, $relpoststhb_show_categories, 'relpoststhb_show_categoriesall', 'relpoststhb_show_categories' ); ?>
  592.                         </td>
  593.                     </tr>
  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="relpoststhb_year" size="4" value="<?php if (isset($relpoststhb_startdate[0])) echo $relpoststhb_startdate[0]; ?>"> <?php _e( 'Month', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststhb_month" size="2" value="<?php if (isset($relpoststhb_startdate[1])) echo $relpoststhb_startdate[1]; ?>"> <?php _e( 'Day', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststhb_day" size="2" value="<?php if (isset($relpoststhb_startdate[2])) echo $relpoststhb_startdate[2]; ?>"> <label for="relpoststhb_excerptlength"><?php _e( 'Leave empty for all posts dates', 'related-posts-thumbnails' ); ?></label><br />
  598.                         </td>
  599.                     </tr>
  600.                     <tr>
  601.                         <th scope="row"><?php _e( 'Top text', 'related-posts-thumbnails' ); ?>:</th>
  602.                         <td>
  603.                             <input type="text" name="relpoststhb_top_text" value="<?php echo stripslashes( htmlspecialchars( get_option( 'relpoststhb_top_text', $this->top_text ) ) ); ?>" size="50"/>
  604.                         </td>
  605.                     </tr>
  606.                     <tr>
  607.                         <th scope="row"><?php _e( 'Number of similar posts to display', 'related-posts-thumbnails' ); ?>:</th>
  608.                         <td>
  609.                             <input type="text" name="relpoststhb_number" value="<?php echo get_option( 'relpoststhb_number', $this->number ); ?>" size="2"/>
  610.                         </td>
  611.                     </tr>
  612.                     <tr>
  613.                         <th scope="row"><?php _e( 'Default image URL', 'related-posts-thumbnails' ); ?>:</th>
  614.                         <td>
  615.                             <input type="text" name="relpoststhb_default_image" value="<?php echo get_option('relpoststhb_default_image', $this->default_image );?>" size="50"/>
  616.                         </td>
  617.                     </tr>
  618.                     <tr>
  619.                         <th scope="row"><?php _e( 'Thumbnails source', 'related-posts-thumbnails' ); ?>:</th>
  620.                         <td>
  621.                             <select name="relpoststhb_thsource"  id="relpoststhb_thsource">
  622.                                 <?php foreach ( $thsources as $name => $title ) : ?>
  623.                                 <option value="<?php echo $name; ?>" <?php if ( $relpoststhb_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 ( $relpoststhb_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="relpoststhb_poststhname">
  637.                                 <?php foreach ( $available_sizes as $size_name => $size ) : ?>
  638.                                 <option <?php if ( $size_name == get_option('relpoststhb_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 ( $relpoststhb_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="relpoststhb_customfield" value="<?php echo get_option('relpoststhb_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="relpoststhb_customwidth" value="<?php echo get_option('relpoststhb_customwidth', $this->custom_width );?>" size="3"/>px x
  670.                             <?php _e( 'Height', 'related-posts-thumbnails' ); ?>: <input type="text" name="relpoststhb_customheight" value="<?php echo get_option('relpoststhb_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="relpoststhb_theme_resize_url" value="<?php echo get_option('relpoststhb_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="relpoststhb_output_style"  id="relpoststhb_output_style">
  689.                                 <?php foreach ( $output_styles as $name => $title ) : ?>
  690.                                 <option value="<?php echo $name; ?>" <?php if ( $relpoststhb_output_style == $name ) echo 'selected'; ?>><?php echo $title; ?></option>
  691.                                 <?php endforeach; ?>
  692.                             </select>
  693.                             <span id="relpoststhb_cleanhtml" style="display: <?php if ($relpoststhb_output_style == 'list') echo 'inline'; else echo 'none';?>;"><?php _e( 'Turn off plugin styles', 'related-posts-thumbnails' ); ?> <input type="checkbox" name="relpoststhb_cleanhtml" <?php if ( $relpoststhb_cleanhtml ) echo 'checked="checked"'; ?> /></span>
  694.                         </td>
  695.                     </tr>
  696.                     <tr valign="top">
  697.                         <th scope="row"><?php _e( 'Background color', 'related-posts-thumbnails' ); ?>:</th>
  698.                         <td>
  699.                             <input type="text" name="relpoststhb_background" value="<?php echo get_option( 'relpoststhb_background', $this->background ); ?>" size="7"/>
  700.                         </td>
  701.                     </tr>
  702.                     <tr valign="top">
  703.                         <th scope="row"><?php _e( 'Background color on mouse over', 'related-posts-thumbnails' ); ?>:</th>
  704.                         <td>
  705.                             <input type="text" name="relpoststhb_hoverbackground" value="<?php echo get_option( 'relpoststhb_hoverbackground', $this->hoverbackground ); ?>" size="7"/>
  706.                         </td>
  707.                     </tr>
  708.                     <tr valign="top">
  709.                         <th scope="row"><?php _e( 'Border color', 'related-posts-thumbnails' ); ?>:</th>
  710.                         <td>
  711.                             <input type="text" name="relpoststhb_bordercolor" value="<?php echo get_option( 'relpoststhb_bordercolor', $this->border_color )?>" size="7"/>
  712.                         </td>
  713.                     </tr>
  714.                     <tr valign="top">
  715.                         <th scope="row"><?php _e( 'Font color', 'related-posts-thumbnails' ); ?>:</th>
  716.                         <td>
  717.                             <input type="text" name="relpoststhb_fontcolor" value="<?php echo get_option( 'relpoststhb_fontcolor', $this->font_color ); ?>" size="7"/>
  718.                         </td>
  719.                     </tr>
  720.                     <tr valign="top">
  721.                         <th scope="row"><?php _e( 'Font family', 'related-posts-thumbnails' ); ?>:</th>
  722.                         <td>
  723.                             <input type="text" name="relpoststhb_fontfamily" value="<?php echo stripslashes( htmlspecialchars( get_option( 'relpoststhb_fontfamily', $this->font_family ) ) ); ?>" size="50"/>
  724.                         </td>
  725.                     </tr>
  726.                     <tr valign="top">
  727.                         <th scope="row"><?php _e( 'Font size', 'related-posts-thumbnails' ); ?>:</th>
  728.                         <td>
  729.                             <input type="text" name="relpoststhb_fontsize" value="<?php echo get_option( 'relpoststhb_fontsize', $this->font_size )?>" size="7"/>
  730.                         </td>
  731.                     </tr>
  732.                     <tr valign="top">
  733.                         <th scope="row"><?php _e( 'Text maximum length', 'related-posts-thumbnails' ); ?>:</th>
  734.                         <td>
  735.                             <input type="text" name="relpoststhb_textlength" value="<?php echo get_option( 'relpoststhb_textlength', $this->text_length )?>" size="7"/>
  736.                             <label for="relpoststhb_textlength"><?php _e( 'Set 0 for no title', 'related-posts-thumbnails' ); ?></label><br />
  737.                         </td>
  738.                     </tr>
  739.                     <tr valign="top">
  740.                         <th scope="row"><?php _e( 'Excerpt maximum length', 'related-posts-thumbnails' ); ?>:</th>
  741.                         <td>
  742.                             <input type="text" name="relpoststhb_excerptlength" value="<?php echo get_option( 'relpoststhb_excerptlength', $this->excerpt_length )?>" size="7"/>
  743.                             <label for="relpoststhb_excerptlength"><?php _e( 'Set 0 for no excerpt', 'related-posts-thumbnails' ); ?></label><br />
  744.                         </td>
  745.                     </tr>
  746.                     <tr valign="top">
  747.                         <th scope="row"><?php _e( 'Text block height', 'related-posts-thumbnails' ); ?>:</th>
  748.                         <td>
  749.                             <input type="text" name="relpoststhb_textblockheight" value="<?php echo get_option( 'relpoststhb_textblockheight', $this->text_block_height )?>" size="7"/> px
  750.                         </td>
  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="relpoststhb_relation" id="relpoststhb_relation_<?php echo $ro_key; ?>" value="<?php echo $ro_key; ?>" <?php if ( $relpoststhb_relation == $ro_key ) echo 'checked="checked"'; ?>/>
  763.                             <label for="relpoststhb_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 ($relpoststhb_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="relpoststhb_custom_taxonomies[]" id="ct_<?php echo $custom_taxonomy; ?>" value="<?php echo $custom_taxonomy; ?>" <?php if ( in_array( $custom_taxonomy, $relpoststhb_custom_taxonomies ) ) echo 'checked="checked"'; ?>/>
  770.                                 <label for="ct_<?php echo $custom_taxonomy; ?>"><?php echo $custom_taxonomy; ?></label>
  771.                                 <?php endforeach; ?>
  772.                                 <?php endif; ?>
  773.                             </div>
  774.                         </td>
  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>
  796.     <?php
  797.     }
  798. }
  799.  
  800. add_action( 'init', 'related_posts_thumbnails' );
  801.  
  802. function related_posts_thumbnails() {
  803.     global $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. }
  812.  
  813. /**
  814.  * Related Posts Widget, will be displayed on post page
  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");' ) );
  849. ?>