Advertisement
Guest User

Untitled

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