Plugin Directory

Changeset 362839


Ignore:
Timestamp:
03/21/2011 12:06:19 PM (15 years ago)
Author:
esserq
Message:

Added 'Latest Badges' widget. The original author is now checked for badges instead of the approver. Corrected hard-coded table prefixes.

Location:
rockhoist-badges
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • rockhoist-badges/trunk/readme.txt

    r361587 r362839  
    23231. The Badge management screen
    24242. The badge condition management screen
    25 3. `<?php rhb_list_badges(); ?>` in action
     253. rhb_list_badges() in action
    2626
    2727== Changelog ==
    2828
     29= 1.1 =
     30* Added 'Latest Badges' widget.
     31* The original author is now checked for badges instead of the approver.
     32 
    2933= 1.0 =
    3034* First release.
  • rockhoist-badges/trunk/rh-badges.php

    r361580 r362839  
    22/*
    33Plugin Name: Rockhoist Badges
    4 Version: 1.0
     4Version: 1.1
    55Plugin URI: http://blarrr.com/wordpress-badges-plugin/
    66Description: A Stack Overflow inspired plugin which allows users to acquire badges. Badges are created and managed through the standard WordPress Dashboard.
     
    2828*/
    2929
    30 //Changelog
    31 $current_version = array('1.0');
    32 
    33 //Install the plugin.
     30// Change Log
     31$current_version = array('1.1');
     32
     33// Database schema version
     34global $rhb_db_version;
     35$rhb_db_version = "1.0";
     36
     37// Install the plugin.
    3438function rhb_installation() {
    3539
     
    8791        dbDelta( $sql );
    8892    }
    89 }
    90 
    91 
    92 // Hook for registering the install function upon plugin activation
     93 
     94    add_option("rhb_db_version", $rhb_db_version);
     95}
     96
     97
     98// Hook for registering the install function upon plugin activation.
    9399register_activation_hook(__FILE__,'rhb_installation');
    94100
    95101// Add the badge count to the Dashboard landing page.
    96 add_action('right_now_content_table_end', 'rh_rhb_add_badge_counts');
     102add_action('right_now_content_table_end', 'rhb_add_badge_counts');
    97103
    98104// Check for new badges after a post is published.
    99 add_action('publish_post', 'rhb_check_current_user');
     105add_action('publish_post', 'rhb_check_author');
    100106
    101107// Check for new badges after a comment is published.
    102108add_action('comment_post', 'rhb_check_current_user');
    103109
    104 function rh_rhb_add_badge_counts() {
     110function rhb_add_badge_counts() {
    105111       
    106112    $num = intval( rhb_count_badges() );
     
    112118            $text = "<a href='edit.php?page=badges'>$text</a>";
    113119        }
     120       
    114121        echo '<td class="first b b-badges">' . $num . '</td>';
    115122        echo '<td class="t badges">' . $text . '</td>';
     
    181188}
    182189
     190function rhb_list_recent_badges() {
     191
     192    print '<div id="recent-badges-table">
     193        <table>
     194            <tbody>';
     195   
     196            foreach (rhb_get_recent_badges() as $user_badge) {
     197           
     198            print '<tr>
     199                <td class="badge-cell">
     200                    <a class="badge">
     201                    <span class="';
     202
     203                    if ( 'gold' == $user_badge->type )
     204                        echo 'badge3';
     205                    elseif ( 'silver' == $user_badge->type )
     206                        echo 'badge2';
     207                    elseif ( 'bronze' == $user_badge->type )
     208                        echo 'badge1';
     209                   
     210                    print '"></span>
     211                        ' . $user_badge->name . '
     212                        </a>
     213                    </td>
     214                    <td>' . $user_badge->user_nicename. '</td>
     215                </tr>';
     216            }
     217        print '</tbody>
     218    </table>
     219    </div>';
     220}
     221
     222function rhb_get_recent_badges() {
     223   
     224    global $wpdb;
     225
     226    if ( empty($filter ) ) { $filter = array(); }
     227    $limit = ( isset( $filter['limit'] ) ? $filter['limit'] : 10 );
     228
     229    $sql = 'SELECT u.id user_id,
     230        u.user_nicename,
     231        b.badge_id,
     232        b.name,
     233        b.type,
     234        b.description
     235    FROM ' . $wpdb->prefix . 'rh_user_badges ub,
     236        ' . $wpdb->prefix . 'users u,
     237        ' . $wpdb->prefix . 'rh_badges b
     238    WHERE ub.badge_id = b.badge_id
     239        AND ub.user_id = u.id
     240    ORDER BY ub.time DESC
     241    LIMIT 0, ' . $limit;
     242
     243    $recent_badges = $wpdb->get_results( $wpdb->prepare( $sql ) );
     244
     245    return $recent_badges;
     246}
     247
    183248function rhb_get_badge_conditions( $filter = '' ) {
    184249
     
    213278            'type' => $args['type']),
    214279        array( '%s', '%s', '%s' ) );
    215    
    216280}
    217281
     
    226290            'count' => $args['count']),
    227291        array( '%d', '%s', '%s', '%d' ) );
    228 
    229292}
    230293
     
    267330
    268331    $comment_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
    269         FROM wp_comments
     332        FROM " . $wpdb->prefix . "comments
    270333        WHERE user_id = " . $args['user_ID'] . "
    271334        AND comment_approved = '1'" ) );
     
    279342
    280343    $post_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
    281         FROM wp_posts
     344        FROM " . $wpdb->prefix . "posts
    282345        WHERE post_author = " . $args['user_ID'] . "
    283346        AND post_status = 'publish'
     
    313376}
    314377
     378function rhb_check_author() {
     379   
     380    global $post;
     381
     382    $args = array('user_ID' => $post->post_author);
     383    rhb_check_user_badges( $args );
     384}
     385
    315386// Check whether an individual user has achieved any badges
    316387function rhb_check_user_badges( $args = '' ) {
     
    402473    // Tags which have not been used are not returned.
    403474    $sql = "SELECT 'post_tag' rh_object_type, trm.name rh_value, COUNT( * ) rh_count
    404         FROM wp_posts pst,
    405             wp_users usr,
    406             wp_term_taxonomy tax,
    407             wp_terms trm,
    408             wp_term_relationships rel
     475        FROM " . $wpdb->prefix . "posts pst,
     476            " . $wpdb->prefix . "users usr,
     477            " . $wpdb->prefix . "term_taxonomy tax,
     478            " . $wpdb->prefix . "terms trm,
     479            " . $wpdb->prefix . "term_relationships rel
    409480        WHERE pst.post_author = usr.ID
    410481            AND usr.ID = %d
     
    617688
    618689        // Check that the user can manage categories.
    619         if (!current_user_can('manage_categories'))
     690        if ( !current_user_can( 'manage_categories' ) )
    620691        {
    621692            wp_die( __('You do not have sufficient permissions to access this page.') );
     
    639710            <div class="updated"><p><strong><?php _e('Badge added successfully.', 'menu-badges' ); ?></strong></p></div>
    640711            <?php
    641 
    642712        }
    643713
     
    837907       
    838908    } // End rhb_badges_page function
    839    
    840 }
     909}
     910
     911/**
     912 * LatestBadgesWidget Class
     913 */
     914class LatestBadgesWidget extends WP_Widget {
     915    /** constructor */
     916    function LatestBadgesWidget() {
     917        parent::WP_Widget(false, $name = 'Latest Badges'); 
     918    }
     919
     920    /** @see WP_Widget::widget */
     921    function widget($args, $instance) {     
     922        extract( $args );
     923        $title = apply_filters('widget_title', $instance['title']);
     924        ?>
     925              <?php echo $before_widget; ?>
     926                  <?php if ( $title )
     927                            echo $before_title . $title . $after_title;
     928            else
     929                echo $before_title . 'Recent Badges' . $after_title; ?>
     930         
     931          <?php rhb_list_recent_badges(); ?>
     932         
     933              <?php echo $after_widget; ?>
     934        <?php
     935    }
     936
     937    /** @see WP_Widget::update */
     938    function update($new_instance, $old_instance) {             
     939    $instance = $old_instance;
     940    $instance['title'] = strip_tags($new_instance['title']);
     941        return $instance;
     942    }
     943
     944    /** @see WP_Widget::form */
     945    function form($instance) {             
     946        $title = esc_attr($instance['title']);
     947        ?>
     948         <p>
     949          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
     950          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
     951        </p>
     952        <?php
     953    }
     954
     955} // class LatestBadgesWidget
     956
     957// register LatestBadgesWidget widget
     958add_action('widgets_init', create_function('', 'return register_widget("LatestBadgesWidget");'));
Note: See TracChangeset for help on using the changeset viewer.