Advertisement
Guest User

Untitled

a guest
Nov 26th, 2010
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.07 KB | None | 0 0
  1. <?php
  2. /*
  3.  * @package Featured Posts
  4.  * @author Nando Pappalardo e Giustino Borzacchiello
  5.  * @version 1.4
  6.  */
  7. /*
  8. Plugin Name: Featured Post with thumbnail
  9. Plugin URI: http://www.yourinspirationweb.com/en/wordpress-plugin-featured-posts-with-thumbnails-highlighting-your-best-articles/
  10. Description: This widget allows you to add in your blog's sidebar a list of featured post with thumbanil.
  11. Author: Nando Pappalardo e Giustino Borzacchiello
  12. Version: 1.4
  13. Author URI: http://en.yourinspirationweb.com/
  14.  
  15. USAGE:
  16.  
  17.     LICENCE:
  18.  
  19.     This program is free software: you can redistribute it and/or modify
  20.     it under the terms of the GNU General Public License as published by
  21.     the Free Software Foundation, either version 3 of the License, or
  22.     (at your option) any later version.
  23.  
  24.     This program is distributed in the hope that it will be useful,
  25.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.     GNU General Public License for more details.
  28.  
  29.     You should have received a copy of the GNU General Public License
  30.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  31.    
  32. */
  33.  
  34. /**
  35.  * Determina il percorso del plugin
  36.  * Determine plugin path
  37.  */
  38. $featured_post_plugin_path = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)).'/';
  39.  
  40. if(function_exists('add_theme_support')) {
  41.    add_theme_support( 'post-thumbnails' );
  42. }
  43.  
  44.  
  45. /**
  46.  * Aggiunge il CSS del plugin
  47.  * Enqueue plugin CSS file
  48.  */
  49. function featured_post_css() {
  50.    global $featured_post_plugin_path;
  51.    wp_enqueue_style('featured-post-css',$featured_post_plugin_path.'featured-post.css');
  52. }
  53. add_action('wp_print_styles', 'featured_post_css');
  54.  
  55.  
  56. /**
  57.  * Recupera la prima immagine del post
  58.  * Returns the first image in the post
  59.  *
  60.  */
  61. function catch_that_image() {
  62.    global $post, $posts, $featured_post_plugin_path;
  63.    $first_img = '';
  64.    ob_start();
  65.    ob_end_clean();
  66.    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  67.    $first_img = $matches [1] [0];
  68.  
  69.    if(empty($first_img)) { //Defines a default image
  70.       $first_img = $featured_post_plugin_path . "images/default.gif";
  71.    }
  72.    return $first_img;
  73. }
  74.  
  75.  
  76. /**
  77.  *
  78.  * Mostra i post in evidenza
  79.  * Show featured posts using unordered list
  80.  * @param mixed $args
  81.  */
  82. function featured_posts_YIW( $args = null ) {
  83.  
  84.    global $featured_post_plugin_path;
  85.    $defaults = array(
  86.        'title'        => 'Featured Posts',
  87.        'numberposts'  => 5,
  88.        'orderby'      => 'DESC',
  89.        'widththumb'   => 73,
  90.        'heightthumb'  => 73,
  91.        'beforetitle'  => '<h3>',
  92.        'aftertitle'   => '</h3>'
  93.    );
  94.  
  95.    $fp = wp_parse_args($args, $defaults);
  96.    /* User-selected settings. */
  97.    $title    = $fp['title'];
  98.    $showposts    = $fp['numberposts'];
  99.    $orderby      = $fp['orderby'];
  100.    $width_thumb  = $fp['widththumb'];
  101.    $height_thumb = $fp['heightthumb'];
  102.    $before_title = $fp['beforetitle'];
  103.    $after_title  = $fp['aftertitle'];
  104.  
  105.  
  106.  
  107.  
  108.    /* Titolo del widget (before e after definiti dal tema). */
  109.    if ( ! empty($title) ) {
  110.       echo $before_title . $title . $after_title;
  111.    }
  112.  
  113.    /*
  114.     * Modificare i parametri di questa funzione per mostrare/escludere categorie, pagine.
  115.     * If you want to exclude categories and/or pages modify this function's arguments properly
  116.     * Info: http://codex.wordpress.org/Template_Tags/get_posts
  117.     */
  118.    global $post;
  119.    $featured_posts = get_posts('meta_key=featured&meta_value=1&numberposts='.$showposts.'&orderby='.$orderby);
  120.  
  121.    echo '<div id="fCon">';
  122.    foreach ($featured_posts as $post):
  123.       setup_postdata($post);
  124.       ?>
  125.  
  126.    
  127.      <?php if ( (function_exists('the_post_thumbnail')) && (has_post_thumbnail()) ) :
  128.         the_post_thumbnail(array($width_thumb,$height_thumb,true));
  129.         else: ?>
  130.  
  131.         <a href="<?php the_permalink(); ?>"><img src=<?php echo $featured_post_plugin_path ?>scripts/timthumb.php?src=<?php echo catch_that_image(); ?>&a=t&h=<?php echo $height_thumb ?>&w=<?php echo $width_thumb ?> /></a>
  132.         <?php endif; ?>
  133.    <h4 class="fConTitle"><?php the_title(); ?></h4>
  134.     <div id="fConE"> <?php the_excerpt(); ?></div>
  135.    <?php
  136.    endforeach;
  137.    echo "</div>";
  138. }
  139.  
  140.  
  141. /**
  142.  * WIDGET SECTION
  143.  *-----------------------------------------------------------------------*/
  144.  
  145. /*
  146.  * Aggiunge file per la traduzione
  147.  * add translate language
  148.  */
  149. load_plugin_textdomain('featured-post','wp-content/plugins/featured-posts/language/');
  150.  
  151. /*
  152.  * Aggiungiamo la nostra funzione al gancio widgets_init
  153.  * Add our function to widgets_init hook
  154.  */
  155. add_action( 'widgets_init', 'my_widget_featured_posts' );
  156.  
  157. /*
  158.  * Funzione che registra il nostro widget
  159.  * Register our widget
  160.  */
  161. function my_widget_featured_posts() {
  162.    register_widget( 'Featured_posts' );
  163. }
  164.  
  165. class Featured_posts extends WP_Widget {
  166.  
  167.    function Featured_posts() {
  168.         /* Impostazione del widget */
  169.       $widget_ops = array( 'classname' => 'widget_featured-posts', 'description' => __('This widget allows you to add in your blog\'s sidebar a list of featured posts.','featured-post') );
  170.  
  171.         /* Impostazioni di controllo del widget */
  172.       $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'widget_featured-posts' );
  173.  
  174.         /* Creiamo il widget */
  175.       $this->WP_Widget( 'widget_featured-posts', __('Featured Posts','featured-post'), $widget_ops, $control_ops );
  176.    }
  177.  
  178.    function widget( $args, $instance ) {
  179.       extract( $args );
  180.       $arguments = array(
  181.       'title'        => $instance['title'],
  182.       'numberposts'  => $instance['showposts'],
  183.       'orderby'      => $instance['orderby'],
  184.       'widththumb'   => $instance['width-thumb'],
  185.       'heightthumb'  => $instance['height-thumb'],
  186.       'beforetitle'  => $before_title,
  187.       'aftertitle'   => $after_title
  188.       );
  189.       global $featured_post_plugin_path;
  190.       /* Before widget (definito dal tema). */
  191.       echo $before_widget;
  192.       featured_posts_YIW($arguments);
  193.       echo $after_widget;
  194.    }
  195.  
  196.    function update( $new_instance, $old_instance ) {
  197.       $instance = $old_instance;
  198.       /* Strip tags (if needed) and update the widget settings. */
  199.       $instance['title'] = strip_tags( $new_instance['title'] );
  200.       $instance['showposts'] = $new_instance['showposts'];
  201.       $instance['orderby'] = $new_instance['orderby'];
  202.       $instance['width-thumb'] = $new_instance['width-thumb'];
  203.       $instance['height-thumb'] = $new_instance['height-thumb'];
  204.  
  205.       return $instance;
  206.    }
  207.  
  208.    function form( $instance ) {
  209.       /* Impostazioni di default del nostro widget */
  210.       $defaults = array( 'title' => __('Featured Posts','featured-post'), 'showposts' => '', 'orderby' => '', 'width-thumb' => '73', 'height-thumb' => '73' );
  211.  
  212.       $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  213. <p>
  214.    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:','featured-post') ?></label>
  215.    <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
  216. </p>
  217.  
  218. <!-- numero di post da visualizzare -->
  219. <p>
  220.    <select name="<?php echo $this->get_field_name( 'showposts' ); ?>" id="<?php echo $this->get_field_id( 'showposts' ); ?>" >
  221.         <?php
  222.         for ($i=0; $i<=100; $i++) {
  223.            if ($instance['showposts'] == $i) {
  224.           $selected = " selected='selected'";
  225.            } else {
  226.           $selected = "";
  227.            }
  228.            echo '<option class="level-0" value="'.$i.'"'.$selected.'>'.$i.'</option>';
  229.         }
  230.         ?>
  231.    </select>
  232.    <label for="<?php echo $this->get_field_id( 'showposts' ); ?>"><?php _e('How many posts do you want to display?','featured-post') ?></label>
  233. </p>
  234.  
  235. <!-- scelta del tipo di ordinamento -->
  236. <p>
  237.    <select name="<?php echo $this->get_field_name( 'orderby' ); ?>" id="<?php echo $this->get_field_id( 'orderby' ); ?>" >
  238.       <option class="level-0" value="rand" <?php if ($instance['orderby'] == "random") echo " selected='selected'" ?>><?php _e('Random','featured-post') ?></option>
  239.       <option class="level-0" value="title" <?php if ($instance['orderby'] == "title") echo " selected='selected'" ?>><?php _e('Title','featured-post') ?></option>
  240.       <option class="level-0" value="date" <?php if ($instance['orderby'] == "date") echo " selected='selected'" ?>><?php _e('Date','featured-post') ?></option>
  241.       <option class="level-0" value="author" <?php if ($instance['orderby'] == "author") echo " selected='selected'" ?>><?php _e('Author','featured-post') ?></option>
  242.       <option class="level-0" value="modified" <?php if ($instance['orderby'] == "modified") echo " selected='selected'" ?>><?php _e('Modified','featured-post') ?></option>
  243.       <option class="level-0" value="ID" <?php if ($instance['orderby'] == "ID") echo " selected='selected'" ?>><?php _e('ID','featured-post') ?></option>
  244.    </select>
  245.    <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Choose type of order:','featured-post') ?></label>
  246. </p>
  247.  
  248. <p>
  249.    <input id="<?php echo $this->get_field_id( 'width-thumb' ); ?>" name="<?php echo $this->get_field_name( 'width-thumb' ); ?>" value="<?php echo $instance['width-thumb']; ?>" style="width:20%;" />
  250.    <label for="<?php echo $this->get_field_id( 'width-thumb' ); ?>"><?php _e('Width Thumbnail','featured-post') ?></label>
  251. </p>
  252.  
  253. <p>
  254.    <input id="<?php echo $this->get_field_id( 'height-thumb' ); ?>" name="<?php echo $this->get_field_name( 'height-thumb' ); ?>" value="<?php echo $instance['height-thumb']; ?>" style="width:20%;" />
  255.    <label for="<?php echo $this->get_field_id( 'height-thumb' ); ?>"><?php _e('Height Thumbnail','featured-post') ?></label>
  256. </p>
  257.    <?php
  258.  
  259.    }
  260. }//end class Featured_posts
  261.  
  262. /**
  263.  * Aggiunge/rimuove il campo personalizzato featured
  264.  * Add/remove featured custom field
  265.  *
  266.  * @param integer $post_ID
  267.  */
  268. function add_featured($post_ID) {
  269.    $articolo = get_post($post_ID);
  270.  
  271.    if ($_POST['insert_featured_post'] == 'yes') {
  272.       add_post_meta($articolo->ID, 'featured', 1, TRUE) or update_post_meta($articolo->ID, 'featured', 1);
  273.    }
  274.    elseif ( $_POST['insert_featured_post'] == 'no' ) {
  275.       delete_post_meta($articolo->ID, 'featured');
  276.    }
  277. }
  278.  
  279. /**
  280.  *
  281.  * Mostra il form featured nella sezione "Scrivi Post"
  282.  * Shows featured form in "Write Post" section
  283.  */
  284. function post_box() {
  285.    global $post;
  286.    $featured = get_post_meta($post->ID,featured,1);
  287.    ?>
  288. <label for="insert_featured_post"><?php _e('Featured post?','featured-post') ?></label>
  289. <select name="insert_featured_post" id="insert_featured_post">
  290.    <option value="yes" <?php if ($featured) echo 'selected="selected"'?>><?php _e('Yes','featured-post') ?>&nbsp;</option>
  291.    <option value="no" <?php if (!$featured) echo 'selected="selected"'?>><?php _e('No ','featured-post') ?>&nbsp;</option>
  292. </select>
  293. <?php
  294. }
  295. function my_post_options_box() {
  296.    add_meta_box('post_info', __('Featured','featured-post'), 'post_box', 'post', 'side', 'high');
  297. }
  298.  
  299.  
  300. add_action('admin_menu', 'my_post_options_box');
  301.  
  302. add_action('new_to_publish', 'add_featured');
  303. add_action('save_post', 'add_featured');
  304.  
  305. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement