Advertisement
designbymerovingi

Example, water intake report shortcode

Jun 11th, 2016
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. add_shortcode( 'water_consumption', 'mycredpro_click_for_points' );
  2. function mycredpro_click_for_points( $atts, $content = '' ) {
  3.  
  4. extract( shortcode_atts( array(
  5. 'ref' => '',
  6. 'message' => '',
  7. 'hours' => 2,
  8. 'points' => 10
  9. ), $atts ) );
  10.  
  11. // Must be logged in to see this shortcode
  12. if ( ! is_user_logged_in() ) return;
  13.  
  14. // Get current time
  15. $now = current_time( 'timestamp' );
  16. $user_id = get_current_user_id();
  17.  
  18. // Get last entry
  19. $log = new myCRED_Query_Log( array(
  20. 'user_id' => $user_id,
  21. 'ref' => $ref,
  22. 'number' => 1
  23. ) );
  24.  
  25. // User has an entry
  26. if ( $log->have_entries() ) {
  27.  
  28. $last_entry = $log->results[0];
  29.  
  30. // Make sure enough time has passed by removing x hours from the current time
  31. if ( $log_entry->time <= ( ( $hours * HOUR_IN_SECONDS ) - $now ) ) {
  32.  
  33. // Intercept button clicks
  34. if ( isset( $_GET['water-intake'] ) && isset( $_GET['token'] ) && wp_verify_nonce( $_GET['token'], 'new-water-intake' ) ) {
  35.  
  36. // User clicked on button, award points
  37. mycred_add(
  38. $ref,
  39. $user_id,
  40. $points,
  41. 'Report of water intake'
  42. );
  43.  
  44. // Show users a message showing they got points or something
  45. return '<p>' . $message . '</p>';
  46.  
  47. }
  48.  
  49. // Show the user the button they need to click on
  50. return '<a href="' . add_query_arg( array(
  51. 'water-intake' => $now,
  52. 'token' => wp_create_token( 'new-water-intake' )
  53. ) ) . '" class="btn btn-primary">Click to report water intake</a>';
  54.  
  55. }
  56.  
  57. }
  58.  
  59. // Else just show somethig for users
  60. return '<p>' . $content . '</p>';
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement