Advertisement
designbymerovingi

myCRED Points for correct answers

Dec 18th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. add_shortcode( 'points_for_correct_answer', 'render_points_for_correct_answer' );
  2. function render_points_for_correct_answer( $atts = array(), $content = NULL ) {
  3.     extract( shortcode_atts( array(
  4.         'answer'   => '',
  5.         'amount'   => '',
  6.         'ok_text'  => '',
  7.         'ok_img'   => '',
  8.         'bad_text' => '',
  9.         'bad_img'  => ''
  10.     ), $atts ) );
  11.  
  12.     // User must be logged in
  13.     if ( ! is_user_logged_in() || ! function_exists( 'mycred_get_settings' ) ) return;
  14.  
  15.     // Prep
  16.     $user_id = get_current_user_id();
  17.     $post_id = $GLOBALS['post']->ID;
  18.  
  19.     // Load myCRED
  20.     $mycred = mycred_get_settings();
  21.  
  22.     // Answer must be set and so does the amount!
  23.     if ( empty( $answer ) || empty( $amount ) ) return;
  24.  
  25.     // Check if form has been submitted
  26.     if ( isset( $_POST['token'] ) && wp_verify_nonce( $_POST['token'], 'answering-question' ) ) {
  27.            
  28.         $given_answer = sanitize_text_field( $_POST['answer'] );
  29.            
  30.         if ( $given_answer == $answer ) {
  31.             // Data
  32.             $data = array(
  33.                 'ref_type' => 'post',
  34.                 'answer'   => $answer
  35.             );
  36.  
  37.             // Make this unique
  38.             if ( $mycred->has_entry( 'answer_questions', $post_id, $user_id, $data ) )
  39.                     return 'You have already received points for this question.';
  40.                
  41.             // Add points
  42.             $mycred->add_creds(
  43.                 'answer_questions',
  44.                 $user_id,
  45.                 $amount,
  46.                 '%plural% for correct answer on post %link_with_title%',
  47.                 $post_id,
  48.                 $data
  49.             );
  50.                
  51.             $output = $ok_text;
  52.                
  53.             if ( ! empty( $ok_img ) )
  54.                 $output .= '<img src="' . $ok_img . '" alt="" />';
  55.                
  56.             return $output;
  57.         }
  58.         else {
  59.             $output = $bad_text;
  60.                
  61.             if ( ! empty( $bad_image ) )
  62.                 $output .= '<img src="' . $bad_image . '" alt="" />';
  63.                
  64.             return $output;
  65.         }
  66.     }
  67.     // No form submission
  68.     else {
  69.            
  70.         return '
  71. <form action="" method="post">
  72.     <label>Answer</label>
  73.     <input type="text" name="answer" id="answer" value="" />
  74.     <input type="hidden" name="token" value="' . wp_create_nonce( 'answering-question' ) . '" />
  75.     <input type="submit" name="submit" value="Send" />
  76. </form>';
  77.            
  78.     }
  79. }
  80. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement