daily pastebin goal
29%
SHARE
TWEET

Wordpress Plugin for extending myCRED as a Time Bank

AJDesigns Dec 30th, 2013 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *  Plugin Name: myCRED Time Bank Helper
  3. *  Description: Modifications to the myCRED plugin by Gabriel Merovingi to optimize myCRED for use as a Time Bank. This code was written by Gabriel Merovingi and his documented version will probably be more upto date: http://mycred.me/support/tutorials/add-transfer-messages/
  4. *  
  5. */
  6.  
  7.  
  8.  
  9.     /**
  10.      * Step 1 : Insert Message Field
  11.      * @since 0.1
  12.      * @version 1.0
  13.      */
  14.     add_action( 'mycred_transfer_form_to', 'mycred_pro_transfer_message_field' );
  15.     function mycred_pro_transfer_message_field() {
  16.             echo '<label>Message</label><input type="text" name="transfer-message" id="mycred-transfer-message" value="" placeholder="Optional message to recipient" />';
  17.     }
  18.      
  19.     /**
  20.      * Step 2 : Replace the Transfer Script
  21.      * @since 0.1
  22.      * @version 1.0
  23.      */
  24.     add_action( 'mycred_front_enqueue', 'mycred_pro_adjust_transfer_script', 99 );
  25.     function mycred_pro_adjust_transfer_script() {
  26.             wp_deregister_script( 'mycred-transfer-ajax' );
  27.             wp_register_script(
  28.                     'mycred-transfer-ajax',
  29.                     get_stylesheet_directory_uri() .  '/js/transfer.js',
  30.                     array( 'jquery', 'jquery-ui-autocomplete' ),
  31.                     myCRED_TRANSFER_VERSION . '.2'
  32.             );
  33.     }
  34.      
  35.  /**
  36.  * Step 3 : Add Message
  37.  * @since 0.1
  38.  * @version 1.0
  39.  */
  40. add_filter( 'mycred_add', 'mycred_pro_add_message', 98, 3 );
  41. function mycred_pro_add_message( $reply, $request, $mycred ) {
  42.         if ( $reply === false || $request['ref'] != 'transfer' ) return $reply;
  43.  
  44.         // If message is set
  45.         if ( isset( $_POST['tmessage'] ) ) {
  46.  
  47.                 // Sanitize things so people dont sent "bad" messages
  48.                 $message = sanitize_text_field( $_POST['tmessage'] );
  49.  
  50.                 // Replace our custom template tag with the message
  51.                 $log_entry = str_replace( '%message%', $message, $request['entry'] );
  52.  
  53.                 // Update the balance now
  54.                 $mycred->update_users_balance( $request['user_id'], $request['amount'] );
  55.  
  56.                 // Add to the log
  57.                 $mycred->add_to_log(
  58.                         'transfer',
  59.                         $request['user_id'],
  60.                         $request['amount'],
  61.                         $log_entry,
  62.                         $request['ref_id'],
  63.                         $request['data'],
  64.                         $request['type']
  65.                 );
  66.  
  67.                 // Tell myCRED not to duplicate this transaction
  68.                 // This should be 'done' but due to a 1.3 bug we must return false
  69.                 // in order to avoid duplicate entries.
  70.                 return false;
  71.         }
  72.  
  73.         return $reply;
  74. }
  75.      
  76.     /**
  77.      * Step 4 : Notifications
  78.      * @since 0.1
  79.      * @version 1.0
  80.      */
  81.     add_filter( 'mycred_notifications_note', 'mycred_pro_parse_notifications', 10, 3 );
  82.     function mycred_pro_parse_notifications( $template, $request, $mycred ) {
  83.            
  84.             if ( isset( $_POST['tmessage'] ) ) {
  85.      
  86.                     $message = sanitize_text_field( $_POST['tmessage'] );
  87.                     if ( ! empty( $message ) ) {
  88.      
  89.                             $template = str_replace( '%message%', $message, $template );
  90.      
  91.                     }
  92.      
  93.             }
  94.      
  95.             return $template;
  96.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top