prefix . "rh_badges"; if( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) { $sql = "CREATE TABLE " . $table_name . " ( badge_id int(9) PRIMARY KEY AUTO_INCREMENT, description text NOT NULL, name varchar(255) NOT NULL, type varchar(10) NOT NULL );"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } // Create the rh_badge_conditions table. $table_name = $wpdb->prefix . "rh_badge_conditions"; if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { $sql = "CREATE TABLE " . $table_name . " ( badge_id int(9) NOT NULL, badge_condition_id int(9) PRIMARY KEY AUTO_INCREMENT, object_type varchar(25) NOT NULL, value varchar(255) NOT NULL, count int(9) NOT NULL );"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } // Create the rh_user_badges table. $table_name = $wpdb->prefix . "rh_user_badges"; if($wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) { $sql = "CREATE TABLE " . $table_name . " ( badge_id int(9) NOT NULL, user_id int(9) NOT NULL, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, CONSTRAINT rhub_pk UNIQUE KEY (badge_id, user_id) );"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } add_option("rhb_db_version", $rhb_db_version); } // Hook for registering the install function upon plugin activation. register_activation_hook(__FILE__,'rhb_installation'); // Add the badge count to the Dashboard landing page. add_action('right_now_content_table_end', 'rhb_add_badge_counts'); // Check for new badges after a post is published. add_action('publish_post', 'rhb_check_author'); // Check for new badges after a comment is published. add_action('comment_post', 'rhb_check_current_user'); function rhb_add_badge_counts() { $num = intval( rhb_count_badges() ); $text = _n( 'Badge', 'Badges', $num ); if ( current_user_can( 'edit_posts' ) ) { $num = "$num"; $text = "$text"; } echo '' . $num . ''; echo '' . $text . ''; echo ''; } function rhb_count_badges() { global $wpdb; $badge_count= $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $wpdb->prefix . 'rh_badges')); return $badge_count; } function rhb_get_badges( $filter = '' ) { global $wpdb; if ( empty($filter ) ) { $filter = array(); } // Select all rows by default $sql = 'SELECT badge_id, name, description, type FROM ' . $wpdb->prefix . 'rh_badges b WHERE 1=1 '; // If a user ID was entered. if ( array_key_exists('user_ID', $filter) ) { $user_ID = $filter['user_ID']; // Join the rh_user_badges table. $sql = 'SELECT b.badge_id, b.name, b.description, b.type FROM ' . $wpdb->prefix . 'rh_badges b, ' . $wpdb->prefix . 'rh_user_badges ub WHERE b.badge_id = ub.badge_id AND ub.user_id = ' . $user_ID; } // If a badge ID was entered. if ( array_key_exists('badge_ID', $filter) ) { $badge_ID = $filter['badge_ID']; // Append a WHERE clause to the SQL. $sql .= " AND b.badge_id = $badge_ID"; } $badges = $wpdb->get_results( $sql ); return $badges; } function rhb_list_badges( $filter = '' ) { if ( empty($filter ) ) { $filter = array(); } print ''; foreach (rhb_get_badges( $filter ) as $badge) { print '
' . $badge->name . '
'; } ; } function rhb_list_recent_badges() { print '
'; foreach (rhb_get_recent_badges() as $user_badge) { print ''; } print '
' . $user_badge->name . ' ' . $user_badge->user_nicename. '
'; } function rhb_get_recent_badges() { global $wpdb; if ( empty($filter ) ) { $filter = array(); } $limit = ( isset( $filter['limit'] ) ? $filter['limit'] : 10 ); $sql = 'SELECT u.id user_id, u.user_nicename, b.badge_id, b.name, b.type, b.description FROM ' . $wpdb->prefix . 'rh_user_badges ub, ' . $wpdb->prefix . 'users u, ' . $wpdb->prefix . 'rh_badges b WHERE ub.badge_id = b.badge_id AND ub.user_id = u.id ORDER BY ub.time DESC LIMIT 0, ' . $limit; $recent_badges = $wpdb->get_results( $wpdb->prepare( $sql ) ); return $recent_badges; } function rhb_get_badge_conditions( $filter = '' ) { global $wpdb; if ( empty( $filter ) ) { $filter = array(); } $sql = 'SELECT badge_condition_id, badge_id, object_type, value, count FROM ' . $wpdb->prefix . 'rh_badge_conditions'; // If a badge ID was entered. if ( array_key_exists( 'badge_ID', $filter ) ) { $badge_ID = $filter['badge_ID']; // Append a WHERE clause to the SQL. $sql .= " WHERE badge_id = $badge_ID"; } $badge_conditions = $wpdb->get_results($sql); return $badge_conditions; } function rhb_add_badge( $args = '' ) { global $wpdb; $wpdb->insert( $wpdb->prefix . 'rh_badges', array( 'name' => $args['name'], 'description' => $args['description'], 'type' => $args['type']), array( '%s', '%s', '%s' ) ); } function rhb_add_badge_condition( $args = '' ) { global $wpdb; $wpdb->insert( $wpdb->prefix . 'rh_badge_conditions', array('badge_id' => $args['badge_ID'], 'object_type' => $args['object_type'], 'value' => $args['value'], 'count' => $args['count']), array( '%d', '%s', '%s', '%d' ) ); } function rhb_remove_badge( $args = '' ) { global $wpdb; $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_user_badges' . ' WHERE badge_id = %d', $args['badge_ID'] ) ); $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_badge_conditions' . ' WHERE badge_id = %d', $args['badge_ID'] ) ); $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_badges' . ' WHERE badge_id = %d', $args['badge_ID'] ) ); } function rhb_update_badge($args = '') { global $wpdb; $wpdb->update( $wpdb->prefix . 'rh_badges', array( 'name' => $args['name'], 'description' => $args['description'], 'type' => $args['type'] ), array( 'badge_id' => $args['badge_ID'] ), array( '%s', '%s', '%s' ), array( '%d' ) ); } function rhb_remove_badge_condition( $args = '' ) { global $wpdb; $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_badge_conditions' . ' WHERE badge_condition_id = %d', $args['badge_condition_ID'] ) ); } function rhb_get_user_comment_count( $args = '' ) { global $wpdb; $comment_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM " . $wpdb->prefix . "comments WHERE user_id = " . $args['user_ID'] . " AND comment_approved = '1'" ) ); return $comment_count; } function rhb_get_user_post_count( $args = '' ) { global $wpdb; $post_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM " . $wpdb->prefix . "posts WHERE post_author = " . $args['user_ID'] . " AND post_status = 'publish' AND post_type = 'post'" ) ); return $post_count; } function rhb_award_badge( $args = '' ) { global $wpdb; $wpdb->insert( $wpdb->prefix . 'rh_user_badges', array('badge_id' => $args['badge_ID'], 'user_id' => $args['user_ID']), array( '%d','%d' ) ); } function rhb_revoke_badge( $args = '' ) { global $wpdb; $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_user_badges' . ' WHERE user_id = %d AND badge_id = %d', $args['user_ID'], $args['badge_ID'] ) ); } function rhb_check_current_user() { global $current_user; get_currentuserinfo(); $args = array('user_ID' => $current_user->ID); rhb_check_user_badges( $args ); } function rhb_check_author() { global $post; $args = array('user_ID' => $post->post_author); rhb_check_user_badges( $args ); } // Check whether an individual user has achieved any badges function rhb_check_user_badges( $args = '' ) { // Loop through each badge. foreach ( rhb_get_badges() as $badge ) { $award_badge = true; $filter = array( 'badge_ID' => $badge->badge_id ); // Loop through each badge condition. foreach( rhb_get_badge_conditions( $filter ) as $badge_condition ) { $condition_met = false; // Check the condition type switch( $badge_condition->object_type ) { case 'post_tag': // Get the user's count for each tag. $post_tags = rhb_get_post_tags( $args ); // Loop through each of those tags foreach ( $post_tags as $tag ) { // If we're comparing the same tag and the user has met the required count if ( ( strtolower( $badge_condition->value ) == strtolower( $tag->rh_value ) ) && ( $badge_condition->count <= $tag->rh_count ) ) { $condition_met = true; } } break; case 'comment_count': if ( $badge_condition->count <= rhb_get_user_comment_count( $args ) ) { $condition_met = true; } break; case 'post_count': if ( $badge_condition->count <= rhb_get_user_post_count( $args ) ) { $condition_met = true; } break; } // end switch // Award the badge if the conditions were met. $award_badge = $condition_met && $award_badge; } // end badge condition loop $args = array( 'badge_ID' => $badge->badge_id, 'user_ID' => $args['user_ID'] ); // If the user has met conditions for badge and doesn't already have the badge if ( 'yes' == $award_badge && !rhb_user_has_badge( $args ) ) { // award the badge to the user. rhb_award_badge( $args ); } } } function rhb_user_has_badge( $args = '' ) { global $wpdb; $badge_count = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $wpdb->prefix . 'rh_user_badges WHERE badge_id = %d AND user_id = $d;', $args['badge_ID'], $args['user_ID'])); return $badge_count; } function rhb_get_post_tags( $args = '' ) { global $wpdb; // This query selects the number of tags used across all posts by a specific user. // Tags which have not been used are not returned. $sql = "SELECT 'post_tag' rh_object_type, trm.name rh_value, COUNT( * ) rh_count FROM " . $wpdb->prefix . "posts pst, " . $wpdb->prefix . "users usr, " . $wpdb->prefix . "term_taxonomy tax, " . $wpdb->prefix . "terms trm, " . $wpdb->prefix . "term_relationships rel WHERE pst.post_author = usr.ID AND usr.ID = %d AND pst.post_status = 'publish' AND tax.taxonomy = 'post_tag' AND tax.term_id = trm.term_id AND rel.object_id = pst.ID AND rel.term_taxonomy_id = tax.term_taxonomy_id GROUP BY trm.name"; $post_tags = $wpdb->get_results( $wpdb->prepare( $sql, $args['user_ID'] ) ); return $post_tags; } add_action( 'admin_menu', 'rhb_add_pages' ); function rhb_add_pages() { add_posts_page( __('Badges','menu-badges'), __('Badges','menu-badges'), 'manage_options', 'badges', 'rhb_badges_page'); function rhb_edit_page() { if ( array_key_exists( 'add-badge-condition-posted', $_POST ) ) { ?>

$_POST['badge-condition-type'], 'value' => $_POST['badge-condition-value'], 'count' => $_POST['badge-condition-count'], 'badge_ID' => $_GET['badge_ID']); // Insert the badge into the database. rhb_add_badge_condition( $args ); } if ( array_key_exists( 'update-badge-posted', $_POST) ) { ?>

$_GET['badge_ID'], 'name' => $_POST['badge-name'], 'description' => $_POST['badge-desc'], 'type' => $_POST['badge-type'] ); // Update the badge. rhb_update_badge( $args ); } if ( isset( $_GET['action'] ) && $_GET['action'] == 'deletecondition' ) { ?>

$_GET['badge_condition_ID'] ); rhb_remove_badge_condition( $args ); } // Fetch the badge details. $filter = array( 'badge_ID' => $_GET['badge_ID'] ); $badges = rhb_get_badges( $filter ); $badge = $badges[0]; ?>

$_GET['badge_ID']); $badge_conditions = rhb_get_badge_conditions($filter); foreach ($badge_conditions as $badge_condition) { $badge_condition_count++; ?>
Object Type Value Count
object_type; ?>
value; ?> count; ?>

Add Condition

This should match the name of the object e.g. a tag name.

The quantity of the value required for the condition to be met.

The name is how it appears on your site.

A description of the badge criteria. This also appears on your site.

5); rhb_check_user_badges( $args ); if ( array_key_exists('add-badge-posted', $_POST) ) { // Get posted values for new badge. $args = array( 'name' => $_POST['badge-name'], 'description' => $_POST['badge-desc'], 'type' => $_POST['badge-type']); // Insert the badge into the database. rhb_add_badge($args); ?>

$badge_ID){ // Build the arguments array based on the user ID and current badge ID. $args = array('user_ID' => $user->ID, 'badge_ID' => $badge_ID); if ( $_POST['awardrevoke-action-type'] == 'award' ) { // Award the specified user the badge. rhb_award_badge($args); } elseif ( $_POST['awardrevoke-action-type'] == 'revoke' ) { // Revoke the badge from the specified user. rhb_revoke_badge($args); } } ?>

$_GET['badge_ID']); // Insert the badge into the database. rhb_remove_badge($args); ?>

Name Description Type
name; ?>
description; ?> type; ?>

Award/ Revoke Badges

Select one or more badges from the table on the right and click the Apply Changes button below.

Add New Badge

The name is how it appears on your site.

A description of the badge criteria. This also appears on your site.

'."\n"; } add_action('widgets_init', create_function('', 'return register_widget("LatestBadgesWidget");')); // register LatestBadgesWidget widget add_action('wp_print_styles', 'rhb_css'); // Rockhoist Badges stylesheet