Cod:
<?php /*
Plugin Name: Related Posts Thumbnails
Plugin URI: http://wordpress.shaldybina.com/plugins/related-posts-thumbnails/
Description: Showing related posts thumbnails under the post.
Version: 1.3.1
Author: Maria Shaldybina
Author URI: http://shaldybina.com/
*/
/* Copyright 2010 Maria I Shaldybina
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
class RelatedPostsThumbnails {
/* Default values. PHP 4 compatible */
var $single_only = '1';
var $auto = '1';
var $top_text = '<h3>Related posts:</h3>';
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 RelatedPostsThumbnails() { // 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( 'relpoststh_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_relpoststh_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( 'relpoststh_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( 'relpoststh_relation', $this->relation );
$poststhname = get_option( 'relpoststh_poststhname', $this->poststhname );
$text_length = get_option( 'relpoststh_textlength', $this->text_length );
$excerpt_length = get_option( 'relpoststh_excerptlength', $this->excerpt_length );
$thsource = get_option( 'relpoststh_thsource', $this->thsource );
$categories_show_all = get_option( 'relpoststh_show_categoriesall', get_option( 'relpoststh_categoriesall', $this->categories_all ) );
$onlywiththumbs = ( current_theme_supports( 'post-thumbnails' ) && $thsource == 'post-thumbnails' ) ? get_option( 'relpoststh_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 != '0' || $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( 'relpoststh_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( 'relpoststh_show_categories',
get_option( 'relpoststh_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( 'relpoststh_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'";
}
Asta e prima parte a codului, dupa care urmeaza ceea ce deja am postat.