Plugin Directory

Changeset 816887


Ignore:
Timestamp:
12/08/2013 02:38:50 PM (12 years ago)
Author:
tomsinger
Message:

Version 1.0 see readme.txt for release notes

Location:
featured-posts-widget/trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • featured-posts-widget/trunk/featured-posts-widget.php

    r573986 r816887  
    55Description: Add featured posts to a widget
    66Author: Tom Singer
    7 Version: 0.1
     7Version: 1.0
    88Author URI: http://www.89pies.com
    99License: MIT
     
    2323
    2424        $this->WP_Widget('featured_posts_widget', 'Featured Posts', $widget_ops, $control_ops );
     25
     26
    2527    }
    2628 
    2729    function form ($instance) {
     30        if ( isset( $instance[ 'title' ] ) ) {
     31            $title = $instance[ 'title' ];
     32        }
     33        else {
     34            $title = __( 'New title', 'text_domain' );
     35        }
     36        $limit = $instance['limit'];
     37        $thumbnail_width = $instance['thumbnail_width'];
     38        $thumbnail_height = $instance['thumbnail_height'];
     39        $thumbnail_options = array('None' => 'none', 'Left' => 'left', 'Right' => 'right', 'Above' => 'above', 'Below' => 'below');
     40        ?>
     41        <p>
     42            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
     43            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
     44        </p>
     45        <p>
     46            <label for="<?php echo $this->get_field_id( 'show_thumbnail' ); ?>"><?php _e( 'Show Thumbnail:' )?></label>
     47            <select name="<?php echo $this->get_field_name( 'show_thumbnail' ); ?>" id="<?php echo $this->get_field_id( 'show_thumbnail' ); ?>">
     48            <?php foreach( $thumbnail_options as $description => $option ) { ?>
     49                <option value="<?php echo $option ?>" <?php echo ( $instance['show_thumbnail'] == $option ? ' selected="selected"' : '' ) ?> >
     50                    <?php echo $description ?>
     51                </option>
     52            <?php } ?>
     53            </select>
     54        </p>
     55        <p>
     56            <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Category:' )?></label>
     57            <select name="<?php echo $this->get_field_name( 'category' ); ?>" id="<?php echo $this->get_field_id( 'category' ); ?>">
     58            <option value=""><?php echo esc_attr( __('All Categories') ); ?></option>
     59            <?php
     60            $cat_args = array(
     61                'orderby' => 'term_group',
     62                'hide_empty' => true
     63            );
     64            $categories = get_categories( $cat_args );
     65            foreach( $categories as $category ) {
     66                $option = '<option value="' . $category->cat_ID . '"' . ( $instance['category'] == $category->cat_ID ? ' selected="selected"' : '' ) . '>';
     67                if( $category->parent )
     68                    $option .= ' - ';
     69                $option .= $category->cat_name;
     70                $option .= '</option>';
     71                echo $option;
     72            }
     73            ?>
     74            </select>
     75        </p>
     76        <p>
     77            <label for="<?php echo $this->get_field_id( 'thumbnail_width' ); ?>"><?php _e( 'Thumbnail Width:' ); ?></label>
     78            <input class="widefat" id="<?php echo $this->get_field_id( 'thumbnail_width' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail_width' ); ?>" type="text" value="<?php echo esc_attr( $thumbnail_width ); ?>" />
     79        </p>
     80        <p>
     81            <label for="<?php echo $this->get_field_id( 'thumbnail_height' ); ?>"><?php _e( 'Thumbnail Height:' ); ?></label>
     82            <input class="widefat" id="<?php echo $this->get_field_id( 'thumbnail_height' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail_height' ); ?>" type="text" value="<?php echo esc_attr( $thumbnail_height ); ?>" />
     83        </p>
     84        <p>
     85            <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Limit:' ); ?></label>
     86            <input class="widefat" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo esc_attr( $limit ); ?>" />
     87        </p>
     88        <?php
    2889    }
    2990
    3091    function update ($new_instance, $old_instance) {
    31     }
    32 
    33     function widget ($args,$instance) {
     92        $instance = array();
     93        $instance['title'] = strip_tags( $new_instance['title'] );
     94        $instance['show_thumbnail'] = strip_tags( $new_instance['show_thumbnail'] );
     95        $instance['thumbnail_width'] = strip_tags( $new_instance['thumbnail_width'] );
     96        $instance['thumbnail_height'] = strip_tags( $new_instance['thumbnail_height'] );
     97        $instance['category'] = $new_instance['category'];
     98        $instance['limit'] = strip_tags( $new_instance['limit'] );
     99        return $instance;
     100    }
     101
     102    function widget ( $args, $instance ) {
    34103        extract($args);
    35104
    36         $title = "Featured Posts";
    37 
    38         $post_args = array(
     105        if (count($instance) > 0) {
     106            extract( $instance );
     107        }
     108
     109        if (function_exists('add_image_size')) {
     110            if (is_null($thumbnail_width)) {
     111                $thumbnail_width = 0;
     112            }
     113            if (is_null($thumbnail_height)) {
     114                $thumbnail_height = 0;
     115            }
     116            add_image_size('featured-posts-widget-thumbnail', $instance['thumbnail_width'], $instance['thumbnail_height'], true);
     117        }
     118
     119        if (is_null($show_thumbnail)) {
     120            $show_thumbnail = 'none';
     121        }
     122
     123        if( ! $title )
     124            $title = "Featured Posts";
     125       
     126        $query_args = array(
    39127            'meta_query' => array(
    40128                array(
     
    42130                    'value' => 'true',
    43131                )
    44             )
     132            ),
     133            'posts_per_page'=> -1,
     134            'ignore_sticky_posts' => true
    45135        );
    46  
    47         $posts = get_posts($post_args);
    48 
    49         if ( count($posts) > 0 ) {
    50             $out = '<ul>';
    51                 foreach($posts as $post) {
    52                     $out .= '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
    53                 }
     136        if( $limit )
     137            $query_args['posts_per_page'] = intval( $limit );
     138        if( $category ) {
     139            $query_args['tax_query'] = array(
     140                array(
     141                    'taxonomy' => 'category',
     142                    'field' => 'id',
     143                    'terms' => intval( $category ),
     144                    'include_children' => false
     145                )
     146            );
     147        }
     148       
     149        $query = new WP_Query( $query_args );
     150        if( $query->post_count ) {
     151            $out = '<ul class="featured-posts-widget-thumbnail-' . $instance['show_thumbnail']  . '">';
     152            while( $query->have_posts() ) {
     153                $query->the_post();
     154                $out .= '<li>';
     155                if( ('above' == $show_thumbnail || 'left' == $show_thumbnail || 'right' == $show_thumbnail) && has_post_thumbnail( get_the_ID() ) ) {
     156                    $out .= '<a href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), "featured-posts-widget-thumbnail" ) . '</a>';
     157                }
     158                if ('above' == $show_thumbnail) {
     159                    $out .= '<br />';
     160                }
     161                $out .= '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
     162                if ('below' == $show_thumbnail) {
     163                    $out .= '<br />';
     164                }
     165                if( ('below' == $show_thumbnail) && has_post_thumbnail( get_the_ID() ) ) {
     166                    $out .= '<a href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), "featured-posts-widget-thumbnail" ) . '</a>';
     167                }
     168                $out .= '</li>';
     169            }
    54170            $out .= '</ul>';
    55171
    56172            echo $before_widget;
    57             echo $before_title.$title.$after_title;
     173            echo $before_title . $title . $after_title;
    58174            echo $out;
    59175            echo $after_widget;
     
    100216}
    101217
     218function featured_posts_widget_scripts() {
     219    wp_register_style( 'featured-posts-widget-stylesheet', plugins_url('css/featured-posts-widget.css', __FILE__), array(), '1.0' );
     220    wp_enqueue_style( 'featured-posts-widget-stylesheet' );
     221}
     222
     223add_action( 'wp_enqueue_scripts', 'featured_posts_widget_scripts' );
     224
    102225function featured_posts_widget_load_widgets() {
    103226    register_widget('featured_posts_widget');
     
    105228
    106229add_action('widgets_init', 'featured_posts_widget_load_widgets');
     230
  • featured-posts-widget/trunk/readme.txt

    r573986 r816887  
    11=== Plugin Name ===
    22Contributors: tomsinger
    3 Tags: featured, posts
     3Tags: featured, posts,featured posts, featured posts widget
    44Requires at least: 3.4.1
    5 Tested up to: 3.4.1
    6 Stable tag: 0.1
     5Tested up to: 3.7.1
     6Stable tag: 1.0
    77License: MIT
    88License URI: http://www.opensource.org/licenses/mit-license.php
    99
    10 A quick and dirty Wordpress plugin to create a Featured Posts widget
     10A Wordpress plugin to create a Featured Posts widget
    1111
    1212== Description ==
    1313
    14 A quick and dirty Wordpress plugin to create a Featured Posts widget
     14A Wordpress plugin to create a Featured Posts widget
     15
     16Features:
     17* Customisable title
     18* Select categories to choose posts from
     19* Customisable thumbnails (none, above, below, left, right)
     20* Customisable thumbnail sizes
     21
     22=== Thanks ===
     23
     24Initial image code, customisations and WP_Query refactor from https://twitter.com/_ericholmes to help create a real 1.0 version
     25
     26== Changelog ==
     27
     28= 1.0 =
     29* Customisable title
     30* Select categories to choose posts from
     31* Customisable thumbnails (none, above, below, left, right)
     32* Customisable thumbnail sizes
     33* Better use of WP_Query
Note: See TracChangeset for help on using the changeset viewer.