Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Plugin Name: myCRED Time Bank Helper
- * 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/
- *
- */
- /**
- * Step 1 : Insert Message Field
- * @since 0.1
- * @version 1.0
- */
- add_action( 'mycred_transfer_form_to', 'mycred_pro_transfer_message_field' );
- function mycred_pro_transfer_message_field() {
- echo '<label>Message</label><input type="text" name="transfer-message" id="mycred-transfer-message" value="" placeholder="Optional message to recipient" />';
- }
- /**
- * Step 2 : Replace the Transfer Script
- * @since 0.1
- * @version 1.0
- */
- add_action( 'mycred_front_enqueue', 'mycred_pro_adjust_transfer_script', 99 );
- function mycred_pro_adjust_transfer_script() {
- wp_deregister_script( 'mycred-transfer-ajax' );
- wp_register_script(
- 'mycred-transfer-ajax',
- get_stylesheet_directory_uri() . '/js/transfer.js',
- array( 'jquery', 'jquery-ui-autocomplete' ),
- myCRED_TRANSFER_VERSION . '.2'
- );
- }
- /**
- * Step 3 : Add Message
- * @since 0.1
- * @version 1.0
- */
- add_filter( 'mycred_add', 'mycred_pro_add_message', 98, 3 );
- function mycred_pro_add_message( $reply, $request, $mycred ) {
- if ( $reply === false || $request['ref'] != 'transfer' ) return $reply;
- // If message is set
- if ( isset( $_POST['tmessage'] ) ) {
- // Sanitize things so people dont sent "bad" messages
- $message = sanitize_text_field( $_POST['tmessage'] );
- // Replace our custom template tag with the message
- $log_entry = str_replace( '%message%', $message, $request['entry'] );
- // Update the balance now
- $mycred->update_users_balance( $request['user_id'], $request['amount'] );
- // Add to the log
- $mycred->add_to_log(
- 'transfer',
- $request['user_id'],
- $request['amount'],
- $log_entry,
- $request['ref_id'],
- $request['data'],
- $request['type']
- );
- // Tell myCRED not to duplicate this transaction
- // This should be 'done' but due to a 1.3 bug we must return false
- // in order to avoid duplicate entries.
- return false;
- }
- return $reply;
- }
- /**
- * Step 4 : Notifications
- * @since 0.1
- * @version 1.0
- */
- add_filter( 'mycred_notifications_note', 'mycred_pro_parse_notifications', 10, 3 );
- function mycred_pro_parse_notifications( $template, $request, $mycred ) {
- if ( isset( $_POST['tmessage'] ) ) {
- $message = sanitize_text_field( $_POST['tmessage'] );
- if ( ! empty( $message ) ) {
- $template = str_replace( '%message%', $message, $template );
- }
- }
- return $template;
- }
Add Comment
Please, Sign In to add comment