AJDesigns

myCRED custom field for Transfers

Jan 1st, 2014
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //add a new field to the transfer add-on widget via the 'transfer_description_to' hook.//
  3. /////////////////////////////////////////////////////////////////////////////////////////
  4.  
  5. //$atts – The shortcode attributes used
  6. //$prefs – The transfer add-on settings.
  7.  
  8. add_action ('mycred_transfer_form_to', 'mycred_add_transfer_description_field', 10, 3 );
  9. function mycred_add_transfer_description_field ($atts, $prefs)
  10. {
  11.     $description_input = '<input type="text" id="tinfo" name="tinfo" value="" class="mycred-transfer-description" placeholder="Description" />';
  12.     echo $description_input;
  13.    
  14. }
  15.  
  16.  
  17. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Add the customized Javacript to handle the new field 'tinfo' in the Transfer add-on.              //
  19. // Hook into myCRED AFTER myCRED has registered the transfer script and de-register it and register our own version.//
  20. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21.  
  22. add_action( 'mycred_front_enqueue', 'mycred_pro_custom_transfer_script', 99 );
  23. function mycred_pro_custom_transfer_script() {
  24.     // De-register Transfer script
  25.     wp_deregister_script( 'mycred-transfer-ajax' );
  26.  
  27.     // Register our own version fromt the plugin /js/ folder
  28.     wp_register_script(
  29.         'mycred-transfer-ajax',
  30.         // get_template_directory_uri() . '/js/transfer.js', //use this line if transfer.js is in theme's js folder
  31.         plugins_url() . '/tb-extend-mycred/js/transfer.js',
  32.         array( 'jquery', 'jquery-ui-autocomplete' ),
  33.         '1.0'
  34.     );
  35. }
  36.  
  37. //////////////////////////////////////////////////////////////
  38. //insert the new 'tinfo" field into the logs.//
  39. //////////////////////////////////////////////////////////////
  40.  
  41.  
  42.  
  43. add_filter( 'mycred_add', 'adjust_transfer_log_entries', 1, 3 );
  44. function adjust_transfer_log_entries( $reply, $request, $mycred ) {
  45.     // Bail if this is not a transfer
  46.     if ( $request['ref'] != 'transfer' ) return $reply;
  47.  
  48.     // Get transfer message
  49.     $transfer_message = '';
  50.     if ( isset( $_POST['tinfo'] ) )
  51.         $transfer_message = sanitize_text_field( $_POST['tinfo'] );
  52.    
  53.     if ( empty( $transfer_message ) ) return $reply;
  54.  
  55.     $log_entry = str_replace( '%message%', $transfer_message, $request['entry'] );
  56.  
  57.     $mycred->update_users_balance( $request['user_id'], $request['amount'] );
  58.     $mycred->add_to_log(
  59.         $request['ref'],
  60.         $request['user_id'],
  61.         $request['amount'],
  62.         $log_entry,
  63.         $request['ref_id'],
  64.         $request['data'],
  65.         $request['type']
  66.     );
  67. }
RAW Paste Data