WordPress.org

Plugin Directory

Changeset 611266


Ignore:
Timestamp:
10/12/12 06:30:28 (19 months ago)
Author:
c3mdigital
Message:

Added randomize display to the widget option + bug fix

Location:
favorite-plugins-widget/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • favorite-plugins-widget/trunk/favorite-plugins-widget.php

    r605093 r611266  
    44Plugin URI: http://plugins.wordpress.org/favorite-plugin-widget 
    55Description: Show your favorite plugins from your WordPress.org profile along with author and star ratings 
    6 Version: 1.0 
     6Version: 1.1 
    77Author: Chris Olbekson 
    88Author URI: http://c3mdigital.com/ 
     
    2929/** 
    3030 * Special thanks to Otto and Japh for getting the plugin favorites into core!! 
    31  * In 3.5 will also be able to access your favorites directly from a tab in the plugin installer screen 
     31 * In 3.5 will also be able to access your favorites directly 
     32 * from a tab in the plugin installer screen 
     33 * 
     34 * @class C3M_Favorite_Plugins 
     35 * 
    3236 */ 
    3337 
    3438class C3M_Favorite_Plugins { 
    3539    static $instance; 
    36     const VERSION = '1.0'; 
     40    const VERSION = '1.1'; 
    3741    const CRON_HOOK = 'c3m_favorite_plugins'; 
    3842 
     
    4751            $args = (object) $args; 
    4852 
    49         $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array('action' => $action, 'request' => serialize($args))) ); 
     53        $request = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array( 'action' => $action, 'request' => serialize($args) ) ) ); 
    5054        if ( is_wp_error($request) ) { 
    5155            $res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() ); 
     
    6064 
    6165    function css() { 
    62         ?> 
    63         <style type="text/css"> 
    64             div.star-holder { 
    65                 position:   relative; 
    66                 height:     17px; 
    67                 width:      100px; 
    68                 background: url(<?php echo admin_url( '/images/stars.png?ver=20120307' ); ?>) repeat-x bottom left; 
    69                 } 
    70  
    71             div.star-holder .star-rating { 
    72                 background: url(<?php echo admin_url( '/images/stars.png?ver=20120307' ); ?>) repeat-x top left; 
    73                 height:     17px; 
    74                 float:      left; 
    75                 } 
    76         </style> 
    77             <?php 
    78  
    79     } 
     66    $c3m_css = '<style type="text/css"> 
     67    div.star-holder { 
     68        position:   relative; 
     69        height:     17px; 
     70        width:      100px; 
     71        background: url('.esc_url( admin_url( '/images/stars.png?ver=20120307' ) ). ') repeat-x bottom left; 
     72        margin:     4px 0; 
     73        } 
     74 
     75        div.star-holder .star-rating { 
     76        background: url('.esc_url( admin_url( '/images/stars.png?ver=20120307' ) ). ') repeat-x top left; 
     77        height:     17px; 
     78        float:      left; 
     79        } 
     80        .c3m-favorites, 
     81         .c3m-favorites li { 
     82        margin: 4px 0; 
     83        </style>'; 
     84 
     85        echo apply_filters( 'c3m_css', $c3m_css ); 
     86    } 
     87 
     88    static function shuffle_results( $list ) { 
     89                if (!is_array($list)) return $list; 
     90 
     91                $keys = array_keys($list); 
     92                shuffle($keys); 
     93                $random = array(); 
     94                foreach ($keys as $key) 
     95                    $random[$key] = $list[$key]; 
     96 
     97                return $random; 
     98            } 
    8099} 
    81100 
    82101$c3m_plugins  = new C3M_Favorite_Plugins(); 
    83102 
    84  
    85103class C3M_My_Favs extends WP_Widget { 
    86104 
    87105    function __construct() { 
    88         $widget_ops = array ( 'classname' => 'my_plugins', 'description' => 'Displays favorite plugins from your WordPress.org profile' ); 
     106        $widget_ops = array( 'classname' => 'my_plugins', 'description' => 'Displays favorite plugins from your WordPress.org profile' ); 
    89107        $this->WP_Widget( 'my_plugins', 'Favorite Plugins', $widget_ops ); 
    90108 
     
    95113        global $c3m_plugins; 
    96114 
    97             /** The plugins API returns an object of your favorite plugins.  Lets store it as a transient so we don't kill .org and slow down your pages  */ 
     115        /** The plugins API returns an object of your favorite plugins.  Lets store it as a transient so we don't kill .org and slow down your pages  */ 
    98116 
    99117            if ( false == get_transient( '_c3m_favorite_plugins' ) ) { 
    100118                $wp_user = $instance['wp_user']; 
    101119                $api_data = $c3m_plugins->api( 'query_plugins', array( 'user' => $wp_user ) ); 
    102  
    103120                set_transient( '_c3m_favorite_plugins', $api_data, 60*60*12 ); 
    104121            } 
     
    108125        $stars = (bool)$instance['stars']; 
    109126        $authors = (bool)$instance['authors']; 
     127        $random = (bool)$instance['random']; 
    110128 
    111129        $api_data = get_transient( '_c3m_favorite_plugins' ); 
    112130 
    113         $api_plugins = $api_data->plugins; 
    114131 
    115132        /** 
     
    123140         * @var string $after_title 
    124141         */ 
    125         echo $before_title . apply_filters( 'widget_title', $title ) . $after_title; 
    126         echo '<ul class="c3m-favorites">'; 
     142        echo $before_title . apply_filters( 'widget_title', $title ) . $after_title ?> 
     143 
     144        <ul class="c3m-favorites"> 
     145        <?php 
    127146        $c = 0; 
    128         foreach( $api_plugins as $plugin ) { 
     147 
     148        if ( is_wp_error( $api_data ) ) : 
     149 
     150            printf( '<li>Error: %s ', $api_data->get_error_message() ); 
     151 
     152        else : 
     153 
     154        if( $random ) { 
     155            $results = C3M_Favorite_Plugins::shuffle_results( $api_data->plugins ); 
     156        } else { 
     157            $results = $api_data->plugins; 
     158        } 
     159 
     160        foreach( (array)$results as $plugin ) { 
    129161            $c++; 
    130162            if ( $c > $per_page ) 
     
    132164 
    133165            $name = $plugin->name; ?> 
    134             <li><strong><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $plugin->slug ?>/"><?php echo esc_html( $name ); ?></a></strong><br> 
    135             <?php if( $stars ) : ?> 
    136                 <div class="star-holder" title="<?php printf( _n( '(based on %s rating)', '(based on %s ratings)', $plugin->num_ratings ), number_format_i18n( $plugin->num_ratings ) ); ?>"> 
    137                 <div class="star star-rating" style="width: <?php echo esc_attr( str_replace( ',', '.', $plugin->rating ) ); ?>px"></div></div> 
     166            <li> 
     167            <?php $result = '<strong><a target="_blank" href="http://wordpress.org/extend/plugins/'. esc_url( $plugin->slug ). '/">'.esc_html( $name ). '</a></strong><br>' ?> 
     168 
     169                <?php if( $stars ) : ?> 
     170 
     171            <?php $result .= '<div class="star-holder" title="'.sprintf( _n( '(based on %s rating)', '(based on %s ratings)', esc_attr( $plugin->num_ratings ) ), esc_attr( number_format_i18n( $plugin->num_ratings ) ) ).'">' ?> 
     172            <?php $result .= '<div class="star star-rating" style="width: '. esc_attr( str_replace( ',', '.', $plugin->rating ) ). 'px"></div></div>' ?> 
     173 
    138174            <?php endif; ?> 
    139175            <?php if( $authors ) : ?> 
    140                 <em><?php _e('By: ') ?></em> <?php echo links_add_target( $plugin->author, '_blank' ). '<br>'; 
    141             endif; ?> 
    142             </li><?php 
    143         } 
     176 
     177            <?php $result .= '<em>'. __('By: '). '</em>'.links_add_target( $plugin->author, '_blank' ). '<br>'; 
     178 
     179            endif; 
     180 
     181            $result = apply_filters('c3m_fav_output', $result ); 
     182 
     183            printf( '<li>%s</li>', $result ); 
     184        } 
     185        endif; 
     186 
    144187        echo '</ul>'; 
    145188 
     
    159202        $instance['stars'] = (bool)$new_instance['stars']; 
    160203        $instance['authors'] = (bool)$new_instance['authors']; 
     204        $instance['random'] = (bool)$new_instance['random']; 
    161205 
    162206        delete_transient( '_c3m_favorite_plugins' ); 
     
    167211 
    168212    function form( $instance ) { 
    169  
    170213        $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 
    171214        $wp_user = isset( $instance['wp_user'] ) ? esc_attr( strtolower( $instance['wp_user']) ) : ''; 
     
    173216        $stars = isset( $instance['stars'] ) ? (bool)$instance['stars'] : true; 
    174217        $authors = isset( $instance['authors'] ) ? (bool)$instance['authors'] : true; 
    175         ?> 
    176  
    177         <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
    178         <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 ); ?>"/></p> 
    179  
    180         <p><label for="<?php echo $this->get_field_id( 'wp_user' ); ?>"><?php _e( 'WordPress.org username:' ); ?></label> 
    181         <input class="widefat" id="<?php echo $this->get_field_id( 'wp_user' ); ?>" name="<?php echo $this->get_field_name( 'wp_user' ); ?>" type="text" value="<?php echo esc_attr( $wp_user ); ?>"/></p> 
    182  
    183         <p><label for="<?php echo $this->get_field_name( 'per_page' ); ?>"><?php _e( 'Number of plugins to show:' ); ?></label> 
    184         <input id="<?php echo esc_attr( $this->get_field_id( 'per_page' ) ); ?>" name="<?php echo $this->get_field_name( 'per_page' ); ?>" type="text" value="<?php echo  (int)$per_page; ?>" size="3" /></p> 
    185  
    186         <p><input class="checkbox" type="checkbox" <?php checked( $stars ); ?> id="<?php echo $this->get_field_id( 'stars' ); ?>" name="<?php echo $this->get_field_name( 'stars' ); ?>" /> 
    187         <label for="<?php echo $this->get_field_id( 'stars' ); ?>"><?php _e( 'Display Star Ratings?' ); ?></label></p> 
    188  
    189         <p><input class="checkbox" type="checkbox" <?php checked( $authors ); ?> id="<?php echo $this->get_field_id( 'authors' ); ?>" name="<?php echo $this->get_field_name( 'authors' ); ?>"/> 
    190         <label for="<?php echo $this->get_field_id( 'authors' ); ?>"><?php _e( 'Display Plugin Authors?' ); ?></label></p> 
     218        $random = isset( $instance['random'] ) ? (bool)$instance['random'] : false; 
     219    ?> 
     220 
     221        <p> 
     222            <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
     223                <?php _e( 'Title:' ); ?> 
     224            </label> 
     225            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) )?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ) ?>" type="text" value="<?php echo esc_attr( $title ); ?>"/> 
     226        </p> 
     227 
     228        <p> 
     229            <label for="<?php echo esc_attr( $this->get_field_id( 'wp_user' ) ) ?>"> 
     230                <?php _e( 'WordPress.org username:' ); ?> 
     231            </label> 
     232            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'wp_user' ) )?>" name="<?php echo esc_attr( $this->get_field_name( 'wp_user' ) ) ?>" type="text" value="<?php echo esc_attr( $wp_user ); ?>"/> 
     233        </p> 
     234 
     235        <p> 
     236            <label for="<?php echo esc_attr( $this->get_field_name( 'per_page' ) ) ?>"> 
     237                <?php _e( 'Number of plugins to show:' ); ?> 
     238            </label> 
     239            <input id="<?php echo esc_attr( $this->get_field_id( 'per_page' ) )  ?>" name="<?php echo esc_attr( $this->get_field_name( 'per_page' ) ) ?>" type="text" value="<?php echo  (int)$per_page; ?>" size="3" /> 
     240        </p> 
     241 
     242        <p> 
     243            <input class="checkbox" type="checkbox" <?php checked( $stars ); ?> id="<?php echo esc_attr( $this->get_field_id( 'stars' ) ) ?>" name="<?php echo esc_attr( $this->get_field_name( 'stars' ) ) ?>" /> 
     244            <label for="<?php echo esc_attr( $this->get_field_id( 'stars' ) ) ?>"> 
     245                <?php _e( 'Display Star Ratings?' ); ?> 
     246            </label> 
     247        </p> 
     248 
     249        <p> 
     250            <input class="checkbox" type="checkbox" <?php checked( $authors ) ?> id="<?php echo esc_attr( $this->get_field_id( 'authors' ) )?>" name="<?php echo esc_attr( $this->get_field_name( 'authors' ) ) ?>"/> 
     251            <label for="<?php echo esc_attr( $this->get_field_id( 'authors' ) ) ?>"> 
     252                <?php _e( 'Display Plugin Authors?' ); ?> 
     253            </label> 
     254        </p> 
     255        <p> 
     256            <input class="checkbox" type="checkbox" <?php checked( $random ); ?> id="<?php echo esc_attr( $this->get_field_id( 'random' ) ) ?>" name="<?php echo esc_attr( $this->get_field_name( 'random' ) ) ?>"/> 
     257            <label for="<?php echo esc_attr( $this->get_field_id( 'random' ) ) ?>"> 
     258                <?php _e( 'Randomize Results?' ); ?> 
     259            </label> 
     260        </p> 
    191261 
    192262    <?php } 
    193263 
    194  } 
     264} 
    195265 
    196266function c3m_register() { 
  • favorite-plugins-widget/trunk/readme.txt

    r605641 r611266  
    44Requires at least: 3.4.1 
    55Tested up to: 3.5 beta 1 
    6 Stable tag: 1.0 
     6Stable tag: 1.1 
    77License: GPLv2 
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html 
     
    1919The styling is minimal and easily adapts to your theme design with few hooks added for extension if needed. 
    2020 
     21Now includes option to shuffle results.  Only want to show 4 of your favorites but you have more?  Just choose the shuffle option and the results will be randomized on each page load (Does not decrease performance). 
     22 
    2123**How does it work?** 
    2224 
     
    2729 
    2830 
    29 ### Take a second to show off your favorite plugins to all your site visitors. 
     31### How to filter the output 
    3032 
     33To add additonal CSS: 
    3134 
     35` 
     36function my_star_css( $css ) { 
     37    $css =. '.c3m-favorites li { 
     38             padding: 10px 4px 0; 
     39             background: pink; 
     40             color: purple; 
     41            }'; 
     42    return $css; 
     43} 
     44` 
     45You can also remove it entirely by passing __return_false to the filter: 
    3246 
    3347== Installation == 
     
    43571. The Widget ops 
    44582. The Widget on a sidebar 
     59 
     60== Changelog == 
     61 
     62= 1.1 = 
     63 
     64* Added randomize option based on user feedback. 
     65* Added additional margins to the li tag 
     66* Added additional output filters for css and display. 
     67* Fixed bug to better handle error when api unavailable or no connection. 
     68* Doesn't store the error as a transient 
Note: See TracChangeset for help on using the changeset viewer.