Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2012
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.96 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Rockhoist Badges MODIFIED BY MAC
  4. Version: 1.2
  5. Plugin URI: http://blarrr.com/wordpress-badges-plugin/
  6. Description: A Stack Overflow inspired plugin which allows users to acquire badges. Badges are created and managed through the standard WordPress Dashboard.
  7. Author: B. Jordan MODIFIED BY MAC
  8. Author URI: http://www.blarrr.com/
  9. */
  10.  
  11. // Change Log
  12. $current_version = array('1.2');
  13.  
  14. // Database schema version
  15. global $rhb_db_version;
  16. $rhb_db_version = "1.0";
  17.  
  18. // Install the plugin.
  19. function rhb_installation() {
  20.  
  21. global $wpdb;
  22.  
  23. // Create the rh_badges table.
  24.  
  25. $table_name = $wpdb->prefix . "rh_badges";
  26.  
  27. if( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
  28.  
  29. $sql = "CREATE TABLE " . $table_name . " (
  30. badge_id int(9) PRIMARY KEY AUTO_INCREMENT,
  31. description text NOT NULL,
  32. name varchar(255) NOT NULL,
  33. type varchar(10) NOT NULL
  34. );";
  35.  
  36. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  37. dbDelta( $sql );
  38. }
  39.  
  40. // Create the rh_badge_conditions table.
  41.  
  42. $table_name = $wpdb->prefix . "rh_badge_conditions";
  43.  
  44. if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
  45.  
  46. $sql = "CREATE TABLE " . $table_name . " (
  47. badge_id int(9) NOT NULL,
  48. badge_condition_id int(9) PRIMARY KEY AUTO_INCREMENT,
  49. object_type varchar(25) NOT NULL,
  50. value varchar(255) NOT NULL,
  51. count int(9) NOT NULL
  52. );";
  53.  
  54. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  55. dbDelta( $sql );
  56. }
  57.  
  58. // Create the rh_user_badges table.
  59.  
  60. $table_name = $wpdb->prefix . "rh_user_badges";
  61.  
  62. if($wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
  63.  
  64. $sql = "CREATE TABLE " . $table_name . " (
  65. badge_id int(9) NOT NULL,
  66. user_id int(9) NOT NULL,
  67. time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  68. CONSTRAINT rhub_pk UNIQUE KEY (badge_id, user_id)
  69. );";
  70.  
  71. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  72. dbDelta( $sql );
  73. }
  74.  
  75. add_option("rhb_db_version", $rhb_db_version);
  76. }
  77.  
  78.  
  79. // Hook for registering the install function upon plugin activation.
  80. register_activation_hook(__FILE__,'rhb_installation');
  81.  
  82. // Add the badge count to the Dashboard landing page.
  83. add_action('right_now_content_table_end', 'rhb_add_badge_counts');
  84.  
  85. // Check for new badges after a post is published.
  86. add_action('publish_post', 'rhb_check_author');
  87.  
  88. // Check for new badges after a comment is published.
  89. add_action('comment_post', 'rhb_check_current_user');
  90.  
  91. function rhb_add_badge_counts() {
  92.  
  93. $num = intval( rhb_count_badges() );
  94.  
  95. $text = _n( 'Badge', 'Badges', $num );
  96.  
  97. if ( current_user_can( 'edit_posts' ) ) {
  98. $num = "<a href='edit.php?page=badges'>$num</a>";
  99. $text = "<a href='edit.php?page=badges'>$text</a>";
  100. }
  101.  
  102. echo '<td class="first b b-badges">' . $num . '</td>';
  103. echo '<td class="t badges">' . $text . '</td>';
  104.  
  105. echo '</tr>';
  106. }
  107.  
  108. function rhb_count_badges() {
  109.  
  110. global $wpdb;
  111.  
  112. $badge_count= $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $wpdb->prefix . 'rh_badges'));
  113.  
  114. return $badge_count;
  115. }
  116.  
  117. function rhb_get_badges( $filter = '' ) {
  118.  
  119. global $wpdb;
  120.  
  121. if ( empty($filter ) ) { $filter = array(); }
  122.  
  123. // Select all rows by default
  124. $sql = 'SELECT badge_id, name, description, type FROM ' . $wpdb->prefix . 'rh_badges b WHERE 1=1 ';
  125.  
  126. // If a user ID was entered.
  127. if ( array_key_exists('user_ID', $filter) ) {
  128.  
  129. $user_ID = $filter['user_ID'];
  130.  
  131. // Join the rh_user_badges table.
  132. $sql = 'SELECT b.badge_id, b.name, b.description, b.type
  133. FROM ' . $wpdb->prefix . 'rh_badges b,
  134. ' . $wpdb->prefix . 'rh_user_badges ub
  135. WHERE b.badge_id = ub.badge_id
  136. AND ub.user_id = ' . $user_ID;
  137.  
  138. }
  139.  
  140. // If a badge ID was entered.
  141. if ( array_key_exists('badge_ID', $filter) ) {
  142.  
  143. $badge_ID = $filter['badge_ID'];
  144.  
  145. // Append a WHERE clause to the SQL.
  146. $sql .= " AND b.badge_id = $badge_ID";
  147. }
  148.  
  149. $badges = $wpdb->get_results( $sql );
  150.  
  151. return $badges;
  152. }
  153.  
  154. function rhb_list_badges( $filter = '' ) {
  155.  
  156.  
  157. if ( empty($filter ) ) { $filter = array(); }
  158.  
  159. print '';
  160.  
  161. foreach (rhb_get_badges( $filter ) as $badge) {
  162.  
  163. print '<div class="biobadge"><div class="';
  164.  
  165. if ( 'gold' == $badge->type )
  166. echo 'bgold';
  167. elseif ( 'silver' == $badge->type )
  168. echo 'bsilver';
  169. elseif ( 'bronze' == $badge->type )
  170. echo 'bbronze';
  171.  
  172. print '"></div>
  173. ' . $badge->name . '
  174. </div>';
  175. }
  176. ;
  177. }
  178.  
  179. function rhb_list_recent_badges() {
  180.  
  181. print '<div id="recent-badges-table">
  182. <table>
  183. <tbody>';
  184.  
  185. foreach (rhb_get_recent_badges() as $user_badge) {
  186.  
  187. print '<tr>
  188. <td class="badge-cell">
  189. <a class="badge">
  190. <span class="';
  191.  
  192. if ( 'gold' == $user_badge->type )
  193. echo 'badge3';
  194. elseif ( 'silver' == $user_badge->type )
  195. echo 'badge2';
  196. elseif ( 'bronze' == $user_badge->type )
  197. echo 'badge1';
  198.  
  199. print '"></span>
  200. ' . $user_badge->name . '
  201. </a>
  202. </td>
  203. <td>' . $user_badge->user_nicename. '</td>
  204. </tr>';
  205. }
  206. print '</tbody>
  207. </table>
  208. </div>';
  209. }
  210.  
  211. function rhb_get_recent_badges() {
  212.  
  213. global $wpdb;
  214.  
  215. if ( empty($filter ) ) { $filter = array(); }
  216. $limit = ( isset( $filter['limit'] ) ? $filter['limit'] : 10 );
  217.  
  218. $sql = 'SELECT u.id user_id,
  219. u.user_nicename,
  220. b.badge_id,
  221. b.name,
  222. b.type,
  223. b.description
  224. FROM ' . $wpdb->prefix . 'rh_user_badges ub,
  225. ' . $wpdb->prefix . 'users u,
  226. ' . $wpdb->prefix . 'rh_badges b
  227. WHERE ub.badge_id = b.badge_id
  228. AND ub.user_id = u.id
  229. ORDER BY ub.time DESC
  230. LIMIT 0, ' . $limit;
  231.  
  232. $recent_badges = $wpdb->get_results( $wpdb->prepare( $sql ) );
  233.  
  234. return $recent_badges;
  235. }
  236.  
  237. function rhb_get_badge_conditions( $filter = '' ) {
  238.  
  239. global $wpdb;
  240.  
  241. if ( empty( $filter ) ) { $filter = array(); }
  242.  
  243. $sql = 'SELECT badge_condition_id, badge_id, object_type, value, count FROM ' . $wpdb->prefix . 'rh_badge_conditions';
  244.  
  245. // If a badge ID was entered.
  246. if ( array_key_exists( 'badge_ID', $filter ) ) {
  247.  
  248. $badge_ID = $filter['badge_ID'];
  249.  
  250. // Append a WHERE clause to the SQL.
  251. $sql .= " WHERE badge_id = $badge_ID";
  252.  
  253. }
  254.  
  255. $badge_conditions = $wpdb->get_results($sql);
  256.  
  257. return $badge_conditions;
  258. }
  259.  
  260. function rhb_add_badge( $args = '' ) {
  261.  
  262. global $wpdb;
  263.  
  264. $wpdb->insert( $wpdb->prefix . 'rh_badges',
  265. array( 'name' => $args['name'],
  266. 'description' => $args['description'],
  267. 'type' => $args['type']),
  268. array( '%s', '%s', '%s' ) );
  269. }
  270.  
  271. function rhb_add_badge_condition( $args = '' ) {
  272.  
  273. global $wpdb;
  274.  
  275. $wpdb->insert( $wpdb->prefix . 'rh_badge_conditions',
  276. array('badge_id' => $args['badge_ID'],
  277. 'object_type' => $args['object_type'],
  278. 'value' => $args['value'],
  279. 'count' => $args['count']),
  280. array( '%d', '%s', '%s', '%d' ) );
  281. }
  282.  
  283. function rhb_remove_badge( $args = '' ) {
  284.  
  285. global $wpdb;
  286.  
  287. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_user_badges' . ' WHERE badge_id = %d', $args['badge_ID'] ) );
  288.  
  289. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_badge_conditions' . ' WHERE badge_id = %d', $args['badge_ID'] ) );
  290.  
  291. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_badges' . ' WHERE badge_id = %d', $args['badge_ID'] ) );
  292. }
  293.  
  294. function rhb_update_badge($args = '') {
  295.  
  296. global $wpdb;
  297.  
  298. $wpdb->update( $wpdb->prefix . 'rh_badges',
  299. array( 'name' => $args['name'],
  300. 'description' => $args['description'],
  301. 'type' => $args['type'] ),
  302. array( 'badge_id' => $args['badge_ID'] ),
  303. array( '%s', '%s', '%s' ),
  304. array( '%d' )
  305. );
  306. }
  307.  
  308. function rhb_remove_badge_condition( $args = '' ) {
  309.  
  310. global $wpdb;
  311.  
  312. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'rh_badge_conditions' . ' WHERE badge_condition_id = %d', $args['badge_condition_ID'] ) );
  313.  
  314. }
  315.  
  316. function rhb_get_user_comment_count( $args = '' ) {
  317.  
  318. global $wpdb;
  319.  
  320. $comment_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
  321. FROM " . $wpdb->prefix . "comments
  322. WHERE user_id = " . $args['user_ID'] . "
  323. AND comment_approved = '1'" ) );
  324.  
  325. return $comment_count;
  326. }
  327.  
  328. function rhb_get_user_post_count( $args = '' ) {
  329.  
  330. global $wpdb;
  331.  
  332. $post_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
  333. FROM " . $wpdb->prefix . "posts
  334. WHERE post_author = " . $args['user_ID'] . "
  335. AND post_status = 'publish'
  336. AND post_type = 'post'" ) );
  337.  
  338. return $post_count;
  339. }
  340.  
  341. function rhb_award_badge( $args = '' ) {
  342.  
  343. global $wpdb;
  344.  
  345. $wpdb->insert( $wpdb->prefix . 'rh_user_badges',
  346. array('badge_id' => $args['badge_ID'],
  347. 'user_id' => $args['user_ID']),
  348. array( '%d','%d' ) );
  349. }
  350.  
  351. function rhb_revoke_badge( $args = '' ) {
  352.  
  353. global $wpdb;
  354.  
  355. $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'] ) );
  356. }
  357.  
  358. function rhb_check_current_user() {
  359.  
  360. global $current_user;
  361. get_currentuserinfo();
  362.  
  363. $args = array('user_ID' => $current_user->ID);
  364. rhb_check_user_badges( $args );
  365. }
  366.  
  367. function rhb_check_author() {
  368.  
  369. global $post;
  370.  
  371. $args = array('user_ID' => $post->post_author);
  372. rhb_check_user_badges( $args );
  373. }
  374.  
  375. // Check whether an individual user has achieved any badges
  376. function rhb_check_user_badges( $args = '' ) {
  377.  
  378. // Loop through each badge.
  379. foreach ( rhb_get_badges() as $badge ) {
  380.  
  381. $award_badge = true;
  382.  
  383. $filter = array( 'badge_ID' => $badge->badge_id );
  384.  
  385. // Loop through each badge condition.
  386. foreach( rhb_get_badge_conditions( $filter ) as $badge_condition ) {
  387.  
  388. $condition_met = false;
  389.  
  390. // Check the condition type
  391. switch( $badge_condition->object_type ) {
  392.  
  393. case 'post_tag':
  394.  
  395. // Get the user's count for each tag.
  396. $post_tags = rhb_get_post_tags( $args );
  397.  
  398. // Loop through each of those tags
  399. foreach ( $post_tags as $tag ) {
  400.  
  401. // If we're comparing the same tag and the user has met the required count
  402. if ( ( strtolower( $badge_condition->value ) == strtolower( $tag->rh_value ) )
  403. && ( $badge_condition->count <= $tag->rh_count ) ) {
  404.  
  405. $condition_met = true;
  406.  
  407. }
  408. }
  409.  
  410. break;
  411.  
  412. case 'comment_count':
  413.  
  414. if ( $badge_condition->count <= rhb_get_user_comment_count( $args ) ) {
  415. $condition_met = true;
  416. }
  417.  
  418. break;
  419.  
  420. case 'post_count':
  421.  
  422. if ( $badge_condition->count <= rhb_get_user_post_count( $args ) ) {
  423. $condition_met = true;
  424. }
  425.  
  426. break;
  427.  
  428. } // end switch
  429.  
  430. // Award the badge if the conditions were met.
  431. $award_badge = $condition_met && $award_badge;
  432.  
  433.  
  434. } // end badge condition loop
  435.  
  436. $args = array( 'badge_ID' => $badge->badge_id,
  437. 'user_ID' => $args['user_ID'] );
  438.  
  439. // If the user has met conditions for badge and doesn't already have the badge
  440. if ( 'yes' == $award_badge && !rhb_user_has_badge( $args ) ) {
  441.  
  442. // award the badge to the user.
  443. rhb_award_badge( $args );
  444. }
  445. }
  446. }
  447.  
  448. function rhb_user_has_badge( $args = '' ) {
  449.  
  450. global $wpdb;
  451.  
  452. $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']));
  453.  
  454. return $badge_count;
  455. }
  456.  
  457. function rhb_get_post_tags( $args = '' ) {
  458.  
  459. global $wpdb;
  460.  
  461. // This query selects the number of tags used across all posts by a specific user.
  462. // Tags which have not been used are not returned.
  463. $sql = "SELECT 'post_tag' rh_object_type, trm.name rh_value, COUNT( * ) rh_count
  464. FROM " . $wpdb->prefix . "posts pst,
  465. " . $wpdb->prefix . "users usr,
  466. " . $wpdb->prefix . "term_taxonomy tax,
  467. " . $wpdb->prefix . "terms trm,
  468. " . $wpdb->prefix . "term_relationships rel
  469. WHERE pst.post_author = usr.ID
  470. AND usr.ID = %d
  471. AND pst.post_status = 'publish'
  472. AND tax.taxonomy = 'post_tag'
  473. AND tax.term_id = trm.term_id
  474. AND rel.object_id = pst.ID
  475. AND rel.term_taxonomy_id = tax.term_taxonomy_id
  476. GROUP BY trm.name";
  477.  
  478. $post_tags = $wpdb->get_results( $wpdb->prepare( $sql, $args['user_ID'] ) );
  479.  
  480. return $post_tags;
  481. }
  482.  
  483. add_action( 'admin_menu', 'rhb_add_pages' );
  484.  
  485. function rhb_add_pages() {
  486.  
  487. add_posts_page( __('Badges','menu-badges'), __('Badges','menu-badges'), 'manage_options', 'badges', 'rhb_badges_page');
  488.  
  489. function rhb_edit_page() {
  490.  
  491. if ( array_key_exists( 'add-badge-condition-posted', $_POST ) ) {
  492.  
  493. ?>
  494. <div class="updated"><p><strong><?php _e('Condition added successfully.', 'menu-badges' ); ?></strong></p></div>
  495. <?php
  496.  
  497. // Get posted values for new badge.
  498. $args = array( 'object_type' => $_POST['badge-condition-type'],
  499. 'value' => $_POST['badge-condition-value'],
  500. 'count' => $_POST['badge-condition-count'],
  501. 'badge_ID' => $_GET['badge_ID']);
  502.  
  503. // Insert the badge into the database.
  504. rhb_add_badge_condition( $args );
  505. }
  506.  
  507. if ( array_key_exists( 'update-badge-posted', $_POST) ) {
  508.  
  509. ?>
  510. <div class="updated"><p><strong><?php _e('Badge updated successfully.', 'menu-badges' ); ?></strong></p></div>
  511. <?php
  512.  
  513. // Get posted values for new badge.
  514. $args = array( 'badge_ID' => $_GET['badge_ID'],
  515. 'name' => $_POST['badge-name'],
  516. 'description' => $_POST['badge-desc'],
  517. 'type' => $_POST['badge-type'] );
  518.  
  519. // Update the badge.
  520. rhb_update_badge( $args );
  521. }
  522.  
  523. if ( isset( $_GET['action'] ) && $_GET['action'] == 'deletecondition' ) {
  524.  
  525. ?>
  526. <div class="updated"><p><strong><?php _e( 'Condition removed successfully.', 'menu-badges' ); ?></strong></p></div>
  527. <?php
  528.  
  529. // Remove the condition.
  530. $args = array( 'badge_condition_ID' => $_GET['badge_condition_ID'] );
  531. rhb_remove_badge_condition( $args );
  532. }
  533.  
  534. // Fetch the badge details.
  535. $filter = array( 'badge_ID' => $_GET['badge_ID'] );
  536. $badges = rhb_get_badges( $filter );
  537. $badge = $badges[0];
  538.  
  539. ?>
  540.  
  541. <div class="wrap">
  542. <h2><?php echo __( 'Badges', 'menu-badges' ); ?></h2>
  543. <div id="col-container">
  544.  
  545. <div id="col-right">
  546. <div class="col-wrap">
  547. <div class="form-wrap">
  548. <form method="post" action="" id="object-filter">
  549.  
  550. <table class="wp-list-table widefat fixed">
  551. <thead>
  552. <tr>
  553. <th class="manage-column column-title" id="criteria-type" scope="col"><span>Object Type</span></th>
  554.  
  555. <th class="manage-column column-title" id="criteria-value" scope="col"><span>Value</span></th>
  556.  
  557. <th class="manage-column column-title" id="criteria-count" scope="col"><span>Count</span></th>
  558. </tr>
  559. </thead>
  560. <tbody>
  561.  
  562. <?php
  563.  
  564. $badge_condition_count = 0;
  565. $filter = array('badge_ID' => $_GET['badge_ID']);
  566. $badge_conditions = rhb_get_badge_conditions($filter);
  567.  
  568. foreach ($badge_conditions as $badge_condition) {
  569.  
  570. $badge_condition_count++;
  571. ?>
  572. <tr class="<?php if ( $badge_condition_count%2 == 0 ) { echo 'alternate'; } ?>">
  573. <td>
  574. <strong><?php echo $badge_condition->object_type; ?></strong>
  575. <br />
  576. <div class="row-actions">
  577. <span class="delete"><a href="?page=badges&action=deletecondition&badge_ID=<?php echo $_GET['badge_ID']; ?>&badge_condition_ID=<?php echo $badge_condition->badge_condition_id; ?>" class="delete-tag">Delete</a></span>
  578. </div>
  579. </td>
  580. <td><?php echo $badge_condition->value; ?></td>
  581. <td><?php echo $badge_condition->count; ?></td>
  582. </tr>
  583. <?php
  584. }
  585. ?>
  586. </tbody>
  587. </table>
  588.  
  589. </form>
  590. </div> <!-- /formwrap -->
  591.  
  592. <div id="poststuff">
  593. <div class="form-wrap">
  594. <div class="postbox" id="postcustom">
  595. <div title="Click to toggle" class="handlediv"><br></div>
  596. <h3 class="hndle"><span>Add Condition</span></h3>
  597. <div class="inside">
  598.  
  599.  
  600. <form class="validate" action="edit.php?page=badges&action=edit&badge_ID=<?php echo $badge->badge_id; ?>" method="post" id="add-badge-condition">
  601. <input type="hidden" value="1" name="add-badge-condition-posted" id="add-badge-condition-posted">
  602.  
  603. <div class="form-field">
  604. <label for="badge-condition-type" style="display:inline;">Object Type</label>
  605. <select id="badge-condition-type" name="badge-condition-type">
  606. <option value="post_tag" selected="selected">Post Tag</option>
  607. <option value="post_count">Post Count</option>
  608. <option value="comment_count">Comment Count</option>
  609. </select>
  610. </div>
  611.  
  612. <div class="form-field form-required">
  613. <label for="badge-condition-value">Value</label>
  614. <input type="text" size="40" value="" id="badge-condition-value" name="badge-condition-value">
  615. <p>This should match the name of the object e.g. a tag name.</p>
  616. </div>
  617.  
  618. <div class="form-field">
  619. <label for="badge-condition-count">Count</label>
  620. <input type="text" size="40" value="" id="badge-condition-count" name="badge-condition-count">
  621. <p>The quantity of the value required for the condition to be met.</p>
  622. </div>
  623.  
  624. <p class="submit"><input type="submit" value="Add Condition" class="button" id="submit" name="submit"></p>
  625.  
  626. </form>
  627.  
  628. </div><!-- /inside-->
  629. </div><!-- /postbox -->
  630. </div><!-- /form-wrap -->
  631. </div><!-- /poststuff -->
  632.  
  633. </div><!-- /col-wrap -->
  634. </div><!-- /col-right -->
  635.  
  636. <div id="col-left">
  637. <div class="col-wrap">
  638.  
  639. <div class="form-wrap">
  640. <form class="validate" action="edit.php?page=badges&action=edit&badge_ID=<?php echo $badge->badge_id; ?>" method="post" id="update-badge">
  641. <input type="hidden" value="1" name="update-badge-posted" id="add-badge-posted">
  642. <div class="form-field form-required">
  643. <label for="badge-name">Name</label>
  644. <input type="text" size="40" value="<?php echo $badge->name; ?>" id="badge-name" name="badge-name">
  645. <p>The name is how it appears on your site.</p>
  646. </div>
  647.  
  648. <div class="form-field">
  649. <label for="badge-description">Description</label>
  650. <textarea cols="40" rows="5" id="badge-desc" name="badge-desc"><?php echo $badge->description; ?></textarea>
  651. <p>A description of the badge criteria. This also appears on your site.</p>
  652. </div>
  653.  
  654. <div class="form-field">
  655. <label for="badge-type" style="display:inline;">Badge Type</label>
  656. <select id="badge-type" name="badge-type">
  657. <option value="gold" <?php if ($badge->type == 'gold') { echo 'selected="selected"'; } ?>>Gold</option>
  658. <option value="silver" <?php if ($badge->type == 'silver') { echo 'selected="selected"'; } ?>>Silver</option>
  659. <option value="bronze" <?php if ($badge->type == 'bronze') { echo 'selected="selected"'; } ?>>Bronze</option>
  660. </select>
  661. </div>
  662.  
  663. <p class="submit"><input type="submit" value="Update" class="button" id="submit" name="submit"></p>
  664.  
  665. </form>
  666. </div> <!-- /form-wrap -->
  667.  
  668. </div><!-- /col-wrap -->
  669. </div><!-- /col-left -->
  670. </div><!-- /col-container -->
  671. </div><!-- /wrap -->
  672. <?php
  673.  
  674. }
  675.  
  676. function rhb_badges_page() {
  677.  
  678. // Check that the user can manage categories.
  679. if ( !current_user_can( 'manage_categories' ) )
  680. {
  681. wp_die( __('You do not have sufficient permissions to access this page.') );
  682. }
  683.  
  684. // debug shit
  685. $args = array('user_ID' => 5);
  686. rhb_check_user_badges( $args );
  687.  
  688. if ( array_key_exists('add-badge-posted', $_POST) ) {
  689.  
  690. // Get posted values for new badge.
  691. $args = array( 'name' => $_POST['badge-name'],
  692. 'description' => $_POST['badge-desc'],
  693. 'type' => $_POST['badge-type']);
  694.  
  695. // Insert the badge into the database.
  696. rhb_add_badge($args);
  697.  
  698. ?>
  699. <div class="updated"><p><strong><?php _e('Badge added successfully.', 'menu-badges' ); ?></strong></p></div>
  700. <?php
  701. }
  702.  
  703. if ( array_key_exists('awardrevoke-posted', $_POST) ) {
  704.  
  705. // Search the user by the given username.
  706. $user = get_userdatabylogin($_POST['awardrevoke-username']);
  707.  
  708. // If a user was returned and badges were checked.
  709. if ( $user && isset( $_POST['awardrhb_revoke_badges'] ) ) {
  710.  
  711. // Loop through each selected badge.
  712. foreach ($_POST['awardrhb_revoke_badges'] as $k => $badge_ID){
  713.  
  714. // Build the arguments array based on the user ID and current badge ID.
  715. $args = array('user_ID' => $user->ID,
  716. 'badge_ID' => $badge_ID);
  717.  
  718. if ( $_POST['awardrevoke-action-type'] == 'award' ) {
  719.  
  720. // Award the specified user the badge.
  721. rhb_award_badge($args);
  722.  
  723. } elseif ( $_POST['awardrevoke-action-type'] == 'revoke' ) {
  724.  
  725. // Revoke the badge from the specified user.
  726. rhb_revoke_badge($args);
  727.  
  728. }
  729. }
  730.  
  731. ?>
  732. <div class="updated"><p><strong><?php _e('Changes applied successfully.', 'menu-badges' ); ?></strong></p></div>
  733. <?php
  734.  
  735. }
  736. }
  737.  
  738. if ( array_key_exists('action', $_GET)
  739. && $_GET['action'] == 'delete'
  740. && isset($_GET['badge_ID']) ) {
  741.  
  742. // Get posted values for new badge.
  743. $args = array( 'badge_ID' => $_GET['badge_ID']);
  744.  
  745. // Insert the badge into the database.
  746. rhb_remove_badge($args);
  747.  
  748. ?>
  749. <div class="updated"><p><strong><?php _e('Badge removed successfully.', 'menu-badges' ); ?></strong></p></div>
  750. <?php
  751. }
  752.  
  753. // If we are editing the badge or performing an action on the edit page.
  754. if (isset( $_GET['action'] )
  755. && ( $_GET['action'] == 'edit'
  756. || $_GET['action'] == 'deletecondition' ) ) {
  757.  
  758. // Display the badge editing page.
  759. rhb_edit_page();
  760.  
  761. } else {
  762.  
  763. // Otherwise, display the main badges page.
  764. ?>
  765.  
  766. <div class="wrap">
  767. <h2><?php echo __('Badges','menu-badges'); ?></h2>
  768. <div id="col-container">
  769.  
  770. <div id="col-right">
  771. <div class="col-wrap">
  772.  
  773. <form class="validate" action="edit.php?page=badges" method="post" id="awardrevoke">
  774.  
  775. <div class="form-wrap">
  776. <table class="wp-list-table widefat fixed">
  777. <thead>
  778. <tr>
  779. <th class="check-column"></th>
  780. <th class="manage-column column-title" id="badge-name" scope="col"><span>Name</span></th>
  781.  
  782. <th class="manage-column column-title" id="badge-desc" scope="col"><span>Description</span></th>
  783.  
  784. <th class="manage-column column-title" id="badge-class" scope="col"><span>Type</span></th>
  785. </tr>
  786. </thead>
  787. <tbody>
  788. <?php
  789.  
  790. // Controls the alternating row style.
  791. $badge_count = 0;
  792.  
  793. foreach (rhb_get_badges() as $badge) {
  794.  
  795. $badge_count++;
  796.  
  797. ?>
  798.  
  799. <tr class="<?php if ($badge_count%2 == 0) { echo 'alternate'; } ?>">
  800. <th scope="row" class="check-column"><input type="checkbox" name="awardrhb_revoke_badges[]" value="<?php echo $badge->badge_id; ?>"></th>
  801. <td>
  802. <strong><a href="?page=badges&action=edit&badge_ID=<?php echo $badge->badge_id; ?>"><?php echo $badge->name; ?></a></strong>
  803. <br />
  804. <div class="row-actions">
  805. <span class="edit"><a href="?page=badges&action=edit&badge_ID=<?php echo $badge->badge_id; ?>">Edit</a> | </span>
  806. <span class="delete"><a href="?page=badges&action=delete&badge_ID=<?php echo $badge->badge_id; ?>" class="delete-tag">Delete</a></span>
  807. </div>
  808. </td>
  809. <td><?php echo $badge->description; ?></td>
  810. <td><?php echo $badge->type; ?></td>
  811. </tr>
  812.  
  813. <?php
  814. }
  815. ?>
  816. </tbody>
  817. </table>
  818.  
  819. </div> <!-- /form-wrap -->
  820.  
  821. <div id="poststuff">
  822. <div class="form-wrap">
  823. <div class="postbox" id="postcustom">
  824. <h3 class="hndle"><span>Award/ Revoke Badges</span></h3>
  825. <div class="inside">
  826.  
  827. <input type="hidden" value="1" name="awardrevoke-posted" id="add-badge-condition-posted">
  828.  
  829. <div class="form-field">
  830. <label for="awardrevoke-action-type" style="display: inline;">Action Type</label>
  831. <select id="awardrevoke-action-type" name="awardrevoke-action-type">
  832. <option value="award" selected="selected">Award</option>
  833. <option value="revoke">Revoke</option>
  834. </select>
  835. </div>
  836.  
  837. <div class="form-field form-required">
  838. <label for="awardrevoke-username">Username</label>
  839. <input type="text" size="40" value="" id="awardrevoke-username" name="awardrevoke-username">
  840. </div>
  841.  
  842. <p>Select one or more badges from the table on the right and click the <b>Apply Changes</b> button below.</p>
  843.  
  844. <p class="submit"><input type="submit" value="Apply Changes" class="button" id="submit" name="submit"></p>
  845.  
  846.  
  847. </div><!-- /inside-->
  848. </div><!-- /postbox -->
  849. </div><!-- /form-wrap -->
  850. </div><!-- /poststuff -->
  851. </form>
  852. </div><!-- /col-wrap-->
  853. </div><!-- /col-right -->
  854.  
  855. <div id="col-left">
  856. <div class="col-wrap">
  857.  
  858. <h3>Add New Badge</h3>
  859.  
  860. <div class="form-wrap">
  861. <form class="validate" action="edit.php?page=badges" method="post" id="add-badge">
  862.  
  863. <input type="hidden" value="1" name="add-badge-posted" id="add-badge-posted">
  864.  
  865. <div class="form-field form-required">
  866. <label for="badge-name">Name</label>
  867. <input type="text" size="40" value="" id="badge-name" name="badge-name">
  868. <p>The name is how it appears on your site.</p>
  869. </div>
  870.  
  871. <div class="form-field">
  872. <label for="badge-description">Description</label>
  873. <textarea cols="40" rows="5" id="badge-desc" name="badge-desc"></textarea>
  874. <p>A description of the badge criteria. This also appears on your site.</p>
  875. </div>
  876.  
  877. <div class="form-field">
  878. <label for="badge-type" style="display:inline;">Badge Type</label>
  879. <select id="badge-type" name="badge-type">
  880. <option selected="selected" value="gold">Gold</option>
  881. <option value="silver">Silver</option>
  882. <option value="bronze">Bronze</option>
  883. </select>
  884. </div>
  885.  
  886. <p class="submit"><input type="submit" value="Add New Badge" class="button" id="submit" name="submit"></p>
  887. </form>
  888. </div><!-- /form-wrap -->
  889. </div><!-- /col-wrap -->
  890. </div><!-- /col-left -->
  891. </div><!-- /col-container -->
  892. </div><!-- /wrap -->
  893.  
  894. <?php
  895. } // End Post page
  896.  
  897. } // End rhb_badges_page function
  898. }
  899.  
  900. /**
  901. * LatestBadgesWidget Class
  902. */
  903. class LatestBadgesWidget extends WP_Widget {
  904. /** constructor */
  905. function LatestBadgesWidget() {
  906. parent::WP_Widget(false, $name = 'Latest Badges');
  907. }
  908.  
  909. /** @see WP_Widget::widget */
  910. function widget($args, $instance) {
  911. extract( $args );
  912. $title = apply_filters('widget_title', $instance['title']);
  913. ?>
  914. <?php echo $before_widget; ?>
  915. <?php if ( $title )
  916. echo $before_title . $title . $after_title;
  917. else
  918. echo $before_title . 'Recent Badges' . $after_title; ?>
  919.  
  920. <?php rhb_list_recent_badges(); ?>
  921.  
  922. <?php echo $after_widget; ?>
  923. <?php
  924. }
  925.  
  926. /** @see WP_Widget::update */
  927. function update($new_instance, $old_instance) {
  928. $instance = $old_instance;
  929. $instance['title'] = strip_tags($new_instance['title']);
  930. return $instance;
  931. }
  932.  
  933. /** @see WP_Widget::form */
  934. function form($instance) {
  935. $title = esc_attr($instance['title']);
  936. ?>
  937. <p>
  938. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  939. <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; ?>" />
  940. </p>
  941. <?php
  942. }
  943.  
  944. } // class LatestBadgesWidget
  945.  
  946. // Link to Rockhoist Badges stylesheet and apply some custom styles
  947. function rhb_css() {
  948. echo "\n".'<link rel="stylesheet" href="'. WP_PLUGIN_URL . '/rockhoist-badges/badges.css" type="text/css" media="screen" />'."\n";
  949. }
  950.  
  951. add_action('widgets_init', create_function('', 'return register_widget("LatestBadgesWidget");')); // register LatestBadgesWidget widget
  952. add_action('wp_print_styles', 'rhb_css'); // Rockhoist Badges stylesheet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement