Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode( 'water_consumption', 'mycredpro_click_for_points' );
- function mycredpro_click_for_points( $atts, $content = '' ) {
- extract( shortcode_atts( array(
- 'ref' => '',
- 'message' => '',
- 'hours' => 2,
- 'points' => 10
- ), $atts ) );
- // Must be logged in to see this shortcode
- if ( ! is_user_logged_in() ) return;
- // Get current time
- $now = current_time( 'timestamp' );
- $user_id = get_current_user_id();
- // Get last entry
- $log = new myCRED_Query_Log( array(
- 'user_id' => $user_id,
- 'ref' => $ref,
- 'number' => 1
- ) );
- // User has an entry
- if ( $log->have_entries() ) {
- $last_entry = $log->results[0];
- // Make sure enough time has passed by removing x hours from the current time
- if ( $log_entry->time <= ( ( $hours * HOUR_IN_SECONDS ) - $now ) ) {
- // Intercept button clicks
- if ( isset( $_GET['water-intake'] ) && isset( $_GET['token'] ) && wp_verify_nonce( $_GET['token'], 'new-water-intake' ) ) {
- // User clicked on button, award points
- mycred_add(
- $ref,
- $user_id,
- $points,
- 'Report of water intake'
- );
- // Show users a message showing they got points or something
- return '<p>' . $message . '</p>';
- }
- // Show the user the button they need to click on
- return '<a href="' . add_query_arg( array(
- 'water-intake' => $now,
- 'token' => wp_create_token( 'new-water-intake' )
- ) ) . '" class="btn btn-primary">Click to report water intake</a>';
- }
- }
- // Else just show somethig for users
- return '<p>' . $content . '</p>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement