Related posts:'; var $number = 3; var $relation = 'categories'; var $poststhname = 'thumbnail'; var $background = '#FFFFFF'; var $hoverbackground = '#EEEEEF'; var $border_color = '#DDDDDD'; var $font_color = '#333333'; var $font_family = 'Arial'; var $font_size = '12'; var $text_length = '100'; var $excerpt_length = '0'; var $custom_field = ''; var $custom_height = '100'; var $custom_width = '100'; var $text_block_height = '75'; var $thsource = 'post-thumbnails'; var $categories_all = '1'; var $devmode = '0'; var $output_style = 'div'; var $post_types = array('post'); var $custom_taxonomies = array(); function RelatedPostsThumbnailsB() { // initialization load_plugin_textdomain( 'related-posts-thumbnails', false, basename( dirname( __FILE__ ) ) . '/locale' ); $this->default_image = WP_PLUGIN_URL . '/related-posts-thumbnails/img/default.png'; if ( get_option( 'relpoststhb_auto', $this->auto ) ) add_filter( 'the_content', array( $this, 'auto_show' ) ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_shortcode( 'related-posts-thumbnails' , array( $this, 'get_html' ) ); $this->wp_version = get_bloginfo('version'); } function auto_show( $content ) { // Automatically displaying related posts under post body return $content . $this->get_html( true ); } function get_html( $show_top = false ) { // Getting related posts HTML if ( $this->is_relpoststhb_show() ) return $this->get_thumbnails( $show_top ); return ''; } function get_thumbnails( $show_top = false ) { // Retrieve Related Posts HTML for output $output = ''; $debug = 'Developer mode initialisation; Version: 1.2.9;'; $time = microtime(true); $posts_number = get_option( 'relpoststhb_number', $this->number ); if ( $posts_number <= 0 ) // return nothing if this parameter was set to <= 0 return $this->finish_process( $output, $debug . 'Posts number is 0;', $time ); $id = get_the_ID(); $relation = get_option( 'relpoststhb_relation', $this->relation ); $poststhname = get_option( 'relpoststhb_poststhname', $this->poststhname ); $text_length = get_option( 'relpoststhb_textlength', $this->text_length ); $excerpt_length = get_option( 'relpoststhb_excerptlength', $this->excerpt_length ); $thsource = get_option( 'relpoststhb_thsource', $this->thsource ); $categories_show_all = get_option( 'relpoststhb_show_categoriesall', get_option( 'relpoststhb_categoriesall', $this->categories_all ) ); $onlywiththumbs = ( current_theme_supports( 'post-thumbnails' ) && $thsource == 'post-thumbnails' ) ? get_option( 'relpoststhb_onlywiththumbs', false) : false; $post_type = get_post_type(); global $wpdb; /* Get taxonomy terms */ $debug .= "Relation: $relation; All categories: $categories_show_all;"; $use_filter = ( $categories_show_all != '1' || $relation != 'no' ); if ( $use_filter ) { $query_objects = "SELECT distinct object_id FROM $wpdb->term_relationships WHERE 1=1 "; if ( $relation != 'no' ) { /* Get object terms */ if ( $relation == 'categories' ) $taxonomy = array( 'category' ); elseif ( $relation == 'tags' ) $taxonomy = array( 'post_tag' ); elseif ( $relation == 'custom') { $taxonomy = get_option( 'relpoststhb_custom_taxonomies', $this->custom_taxonomies ); } else { $taxonomy = array( 'category', 'post_tag' ); } $object_terms = wp_get_object_terms( $id, $taxonomy, array( 'fields' => 'ids' ) ); if ( empty( $object_terms ) || !is_array( $object_terms ) ) // no terms to get taxonomy return $this->finish_process( $output, $debug . 'No taxonomy terms to get posts;', $time ); $query = "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id in ('". implode( "', '", $object_terms ) . "')"; $object_taxonomy = $wpdb->get_results( $query ); $object_taxonomy_a = array(); if ( count( $object_taxonomy ) > 0 ) { foreach ( $object_taxonomy as $item ) $object_taxonomy_a[] = $item->term_taxonomy_id; } $query_objects .= " AND term_taxonomy_id IN ('". implode( "', '", $object_taxonomy_a ) . "') "; } if ( $categories_show_all != '1' ) { /* Get filter terms */ $select_terms = get_option( 'relpoststhb_show_categories', get_option( 'relpoststhb_categories' ) ); if ( empty( $select_terms ) || !is_array( $select_terms ) ) // if no categories were specified intentionally return nothing return $this->finish_process( $output, $debug . 'No categories were selected;', $time ); $query = "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id in ('". implode( "', '", $select_terms ) . "')"; $taxonomy = $wpdb->get_results( $query ); $filter_taxonomy_a = array(); if ( count( $taxonomy ) > 0 ) { foreach ($taxonomy as $item) $filter_taxonomy_a[] = $item->term_taxonomy_id; } if ($relation != 'no') { $query_objects .= " AND object_id IN (SELECT distinct object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ('". implode( "', '", $filter_taxonomy_a ) . "') )"; } else { $query_objects .= " AND term_taxonomy_id IN ('". implode( "', '", $filter_taxonomy_a ) . "')"; } } $relationships = $wpdb->get_results( $query_objects ); $related_objects = array(); if ( count( $relationships ) > 0 ) { foreach ($relationships as $item) $related_objects[] = $item->object_id; } } $query = "SELECT distinct ID FROM $wpdb->posts "; $where = " WHERE post_type = '" . $post_type . "' AND post_status = 'publish' AND ID<>" . $id; // not the current post $startdate = get_option( 'relpoststhb_startdate' ); if ( !empty( $startdate ) && preg_match( '/^\d\d\d\d-\d\d-\d\d$/', $startdate ) ) { // If startdate was set $debug .= "Startdate: $startdate;"; $where .= " AND post_date >= '" . $startdate . "'"; } if ( $use_filter ) { $where .= " AND ID IN ('". implode( "', '", $related_objects ) . "')"; } $join = ""; if ( $onlywiththumbs ) { $debug .= "Only with thumbnails;"; $join = " INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)"; $where .= " AND $wpdb->postmeta.meta_key = '_thumbnail_id'"; } $order = " ORDER BY rand() LIMIT " . $posts_number; $random_posts = $wpdb->get_results( $query . $join . $where . $order ); /* Get posts by their IDs */ if ( !is_array( $random_posts ) || count( $random_posts ) < 1 ) { return $this->finish_process( $output, $debug . 'No posts matching relationships criteria;', $time ); } $posts_in = array(); foreach ($random_posts as $random_post) $posts_in[] = $random_post->ID; $query = "SELECT ID, post_content, post_excerpt, post_title FROM $wpdb->posts WHERE ID IN ('". implode( "', '", $posts_in ) . "')"; $posts = $wpdb->get_results( $query ); if ( ! ( is_array( $posts ) && count( $posts ) > 0 ) ) { // no posts $debug .= 'No posts found;'; return $this->finish_process( $output, $debug, $time ); } else $debug .= 'Found ' . count( $posts ) . ' posts;'; /* Calculating sizes */ if ( $thsource == 'custom-field' ) { $debug .= 'Custom sizes;'; $width = get_option( 'relpoststhb_customwidth', $this->custom_width ); $height = get_option( 'relpoststhb_customheight', $this->custom_height ); } else { // post-thumbnails source if ( $poststhname == 'thumbnail' || $poststhname == 'medium' || $poststhname == 'large' ) { // get thumbnail size for basic sizes $debug .= 'Basic sizes;'; $width = get_option( "{$poststhname}_size_w" ); $height = get_option( "{$poststhname}_size_h" ); } elseif ( current_theme_supports( 'post-thumbnails' ) ) { // get sizes for theme supported thumbnails global $_wp_additional_image_sizes; if ( isset( $_wp_additional_image_sizes[ $poststhname ] ) ) { $debug .= 'Additional sizes;'; $width = $_wp_additional_image_sizes[ $poststhname ][ 'width' ]; $height = $_wp_additional_image_sizes[ $poststhname ][ 'height' ]; } else $debug .= 'No additional sizes;'; } } // displaying square if one size is not cropping if ( $height == 9999 ) $height = $width; if ( $width == 9999 ) $width = $height; // theme is not supporting but settings were not changed if ( empty( $width ) ) { $debug .= 'Using default width;'; $width = get_option( "thumbnail_size_w" ); } if ( empty( $height ) ) { $debug .= 'Using default height;'; $height = get_option( "thumbnail_size_h" ); } $debug .= 'Got sizes '.$width.'x'.$height.';'; // rendering related posts HTML if ( $show_top ) $output .= stripslashes( get_option( 'relpoststhb_top_text', $this->top_text ) ); $relpoststhb_output_style = get_option( 'relpoststhb_output_style', $this->output_style ); $relpoststhb_cleanhtml = get_option( 'relpoststhb_cleanhtml', 0 ); $text_height = get_option( 'relpoststhb_textblockheight', $this->text_block_height ); if ($relpoststhb_output_style == 'list') { $output .= ''; else $output .= ''; $output .= '
'; return $this->finish_process( $output, $debug, $time ); } function finish_process( $output, $debug, $time ) { $devmode = get_option( 'relpoststhb_devmode', $this->devmode ); if ( $devmode ) { $time = microtime(true) - $time; $debug .= "Plugin execution time: $time sec;"; $output .= ''; } return $output; } function process_text_cut( $text, $length ) { if ($length == 0) return ''; else { $text = htmlspecialchars( strip_tags( strip_shortcodes( $text ) ) ); if ( function_exists('mb_strlen') ) { return ( ( mb_strlen( $text ) > $length ) ? mb_substr( $text, 0, $length) . '...' : $text ); } else { return ( ( strlen( $text ) > $length ) ? substr( $text, 0, $length) . '...' : $text ); } } } function is_relpoststhb_show() { // Checking display options if ( !is_single() && get_option( 'relpoststhb_single_only', $this->single_only ) ) { // single only return false; } /* Check post type */ $post_types = get_option( 'relpoststhb_post_types', $this->post_types ); $post_type = get_post_type(); if ( !in_array($post_type, $post_types) ) { return false; } /* Check categories */ $id = get_the_ID(); $categories_all = get_option( 'relpoststhb_categoriesall', $this->categories_all ); if ( $categories_all != '1') { // only specific categories were selected $post_categories = wp_get_object_terms( $id, array( 'category' ), array( 'fields' => 'ids' ) ); $relpoststhb_categories = get_option( 'relpoststhb_categories' ); if ( !is_array( $relpoststhb_categories ) || !is_array( $post_categories ) ) // no categories were selcted or post doesn't belong to any return false; $common_categories = array_intersect( $relpoststhb_categories, $post_categories ); if ( empty( $common_categories ) ) // post doesn't belong to specified categories return false; } return true; } function admin_menu() { $page = add_options_page( __( 'Related Posts Thumbnails', 'related-posts-thumbnails' ), __( 'Related Posts Thumbs', 'related-posts-thumbnails' ), 'administrator', 'related-posts-thumbnails', array( $this, 'admin_interface' ) ); } function admin_interface() { // Admin interface if ( isset($_POST['action']) && ($_POST['action'] == 'update') ) { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'No access', 'related-posts-thumbnails' ) ); } check_admin_referer( 'related-posts-thumbnails' ); $validation = true; if ( !empty($_POST['relpoststhb_year']) || !empty($_POST['relpoststhb_month']) || !empty($_POST['relpoststhb_year']) ) { // check date $set_date = sprintf( '%04d-%02d-%02d', $_POST['relpoststhb_year'], $_POST['relpoststhb_month'], $_POST['relpoststhb_day'] ); if ( checkdate( intval($_POST['relpoststhb_month']), intval($_POST['relpoststhb_day']), intval($_POST['relpoststhb_year']) ) === false ) { $validation = false; $error = __( 'Wrong date', 'related-posts-thumbnails' ) . ': ' . sprintf( '%d/%d/%d', $_POST['relpoststhb_month'], $_POST['relpoststhb_day'], $_POST['relpoststhb_year'] ); } } else { $set_date = ''; } if ( $validation ) { update_option( 'relpoststhb_single_only', $_POST['relpoststhb_single_only'] ); update_option( 'relpoststhb_post_types', $_POST['relpoststhb_post_types'] ); update_option( 'relpoststhb_onlywiththumbs', $_POST['onlywiththumbs'] ); update_option( 'relpoststhb_output_style', $_POST['relpoststhb_output_style'] ); update_option( 'relpoststhb_cleanhtml', $_POST['relpoststhb_cleanhtml'] ); update_option( 'relpoststhb_auto', $_POST['relpoststhb_auto'] ); update_option( 'relpoststhb_top_text', $_POST['relpoststhb_top_text'] ); update_option( 'relpoststhb_number', $_POST['relpoststhb_number'] ); update_option( 'relpoststhb_relation', $_POST['relpoststhb_relation'] ); update_option( 'relpoststhb_default_image', $_POST['relpoststhb_default_image'] ); update_option( 'relpoststhb_poststhname', $_POST['relpoststhb_poststhname'] ); update_option( 'relpoststhb_background', $_POST['relpoststhb_background'] ); update_option( 'relpoststhb_hoverbackground', $_POST['relpoststhb_hoverbackground'] ); update_option( 'relpoststhb_bordercolor', $_POST['relpoststhb_bordercolor'] ); update_option( 'relpoststhb_fontcolor', $_POST['relpoststhb_fontcolor'] ); update_option( 'relpoststhb_fontsize', $_POST['relpoststhb_fontsize'] ); update_option( 'relpoststhb_fontfamily', $_POST['relpoststhb_fontfamily'] ); update_option( 'relpoststhb_textlength', $_POST['relpoststhb_textlength'] ); update_option( 'relpoststhb_excerptlength', $_POST['relpoststhb_excerptlength'] ); update_option( 'relpoststhb_thsource', $_POST['relpoststhb_thsource'] ); update_option( 'relpoststhb_customfield', $_POST['relpoststhb_customfield'] ); update_option( 'relpoststhb_theme_resize_url', $_POST['relpoststhb_theme_resize_url'] ); update_option( 'relpoststhb_customwidth', $_POST['relpoststhb_customwidth'] ); update_option( 'relpoststhb_customheight', $_POST['relpoststhb_customheight'] ); update_option( 'relpoststhb_textblockheight', $_POST['relpoststhb_textblockheight'] ); update_option( 'relpoststhb_categoriesall', $_POST['relpoststhb_categoriesall'] ); update_option( 'relpoststhb_categories', $_POST['relpoststhb_categories'] ); update_option( 'relpoststhb_show_categoriesall', $_POST['relpoststhb_show_categoriesall'] ); update_option( 'relpoststhb_show_categories', $_POST['relpoststhb_show_categories'] ); update_option( 'relpoststhb_devmode', $_POST['relpoststhb_devmode'] ); update_option( 'relpoststhb_startdate', $set_date ); update_option( 'relpoststhb_custom_taxonomies', $_POST['relpoststhb_custom_taxonomies'] ); echo "

" . __( 'Settings updated', 'related-posts-thumbnails' ) ."

"; } else { echo "

" . __( 'Settings update failed', 'related-posts-thumbnails' ) . '. '. $error . "

"; } } $available_sizes = array( 'thumbnail' => 'thumbnail', 'medium' => 'medium' ); if ( current_theme_supports( 'post-thumbnails' ) ) { global $_wp_additional_image_sizes; if ( is_array($_wp_additional_image_sizes ) ) { $available_sizes = array_merge( $available_sizes, $_wp_additional_image_sizes ); } } $relpoststhb_single_only = get_option( 'relpoststhb_single_only', $this->single_only ); $relpoststhb_auto = get_option( 'relpoststhb_auto', $this->auto ); $relpoststhb_cleanhtml = get_option( 'relpoststhb_cleanhtml', 0 ); $relpoststhb_relation = get_option( 'relpoststhb_relation', $this->relation ); $relpoststhb_thsource = get_option( 'relpoststhb_thsource', $this->thsource ); $relpoststhb_devmode = get_option( 'relpoststhb_devmode', $this->devmode ); $relpoststhb_categoriesall = get_option( 'relpoststhb_categoriesall', $this->categories_all ); $relpoststhb_categories = get_option( 'relpoststhb_categories' ); $relpoststhb_show_categories = get_option( 'relpoststhb_show_categories', get_option( 'relpoststhb_categories' ) ); $relpoststhb_show_categoriesall = get_option( 'relpoststhb_show_categoriesall', $relpoststhb_categoriesall ); $onlywiththumbs = get_option( 'relpoststhb_onlywiththumbs', false ); $relpoststhb_startdate = explode( '-', get_option( 'relpoststhb_startdate' ) ); $relpoststhb_output_style = get_option( 'relpoststhb_output_style', $this->output_style ); $thsources = array( 'post-thumbnails' => __('Post thumbnails', 'related_posts_thumbnails'), 'custom-field' => __('Custom field', 'related_posts_thumbnails') ); $categories = get_categories(); if ($this->wp_version >= 3) { $post_types = get_post_types( array( 'public' => 1 ) ); } else { $post_types = get_post_types(); } $relpoststhb_post_types = get_option( 'relpoststhb_post_types', $this->post_types ); $output_styles = array('div' => __( 'Blocks', 'related-posts-thumbnails' ), 'list' => __( 'List', 'related-posts-thumbnails' ) ); $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') ); if ($this->wp_version >= 3) { $custom_taxonomies = get_taxonomies( array('public' => 1) ); $relpoststhb_custom_taxonomies = get_option( 'relpoststhb_custom_taxonomies', $this->custom_taxonomies ); if (!is_array($relpoststhb_custom_taxonomies)) $relpoststhb_custom_taxonomies = array(); } else { $relation_options['custom'] .= ' '. __('(This option is available for WP v3+ only)', 'related_posts_thumbnails'); } ?>

:

: />
: />
: />
: />
: display_categories_list( $relpoststhb_categoriesall, $categories, $relpoststhb_categories, 'relpoststhb_categoriesall', 'relpoststhb_categories' ); ?>
: display_categories_list( $relpoststhb_show_categoriesall, $categories, $relpoststhb_show_categories, 'relpoststhb_show_categoriesall', 'relpoststhb_show_categories' ); ?>
: : : :
:
:
:
:
style="display:none" >

:

: ()
: />
style="display:none" >

:

:
: : px x : px
: ()

:

: />
:
:
:
:
:
:
:
:
: px

:

: $ro_name): ?> />
/>

Plugin\'s homepage. If you think this plugin is awesome please vote on Wordpress plugin page. Thanks!', 'related-posts-thumbnails' ); ?>

/>
style="display:none" > cat_ID, (array)$selected_categories ) ) echo 'checked="checked"'; ?>/>
get_html(); } /** * Related Posts Widget, will be displayed on post page */ class RelatedPostsThumbnailsWidgetB extends WP_Widget { function RelatedPostsThumbnailsWidgetB() { parent::WP_Widget(false, $name = 'Related Posts Thumbnails B'); } function widget($args, $instance) { if ( is_single() && !is_page() ) { // display on post page only extract( $args ); $title = apply_filters('widget_title', $instance['title']); echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; get_related_posts_thumbnails(); echo $after_widget; } } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); return $instance; } function form($instance) { $title = esc_attr($instance['title']); ?>