Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Message Payment
- * Stop messages from being sent from "buyers" to "sellers" if the buyer does
- * not have enough myCRED points. If they do, charge the buyer the points and transfer
- * it to the seller.
- * @see http://codex.mycred.me/category/template-tags/temp-user/
- * @version 1.0
- */
- add_action( 'wp_ajax_userpro_start_chat', 'mycredme_intercept_ajax_new_message', 0 );
- function mycredme_intercept_ajax_new_message() {
- // Lets mimic the real ajax handler that comes after this
- global $userpro, $userpro_msg;
- $output = '';
- $chat_from = $_POST['chat_from'];
- $chat_with = $_POST['chat_with'];
- if ( ! userpro_is_logged_in() || $chat_from != get_current_user_id() ) die();
- if ( ! $userpro_msg->can_chat_with( $chat_with ) ) die();
- // Ok, if we got this far, we can send messages. Lets make sure myCRED is installed.
- if ( ! function_exists( 'mycred' ) ) die();
- // In case you use multiple point types and need to use a custom type,
- // you enter your custom point type key here. For default leave as is.
- $point_type = 'mycred_default';
- // Next lets load myCRED
- $mycred = mycred( $point_type );
- // The cost of the message.
- $cost = 5;
- // Make sure user is not excluded from using points
- if ( ! $mycred->exclude_user( $chat_from ) ) {
- // Get user objects so we can check roles
- $sender = get_userdata( $chat_from );
- $senders_roles = $sender->roles;
- $recipient = get_userdata( $chat_with );
- $recipients_roles = $recipient->roles;
- // Check if this is a conversation between a buyer and seller (based of WP Roles)
- if ( in_array( 'verkoper', $senders_roles ) && in_array( 'koper', $recipients_roles ) ) {
- // Get senders balance
- $balance = $mycred->get_users_balance( $chat_from, $point_type );
- // Sorry but not enough funds, show error message
- if ( $balance < $minimum ) {
- // Shown by the UserPro Messaging plugin
- $output['message'] = '<div class="userpro-msg-notice">Insufficient Funds.</div>';
- ob_start();
- // Not sure if this is neaded but it's what the plugin does so...
- require_once userpro_msg_path . 'templates/conversation.php';
- $output['html'] = ob_get_contents();
- ob_end_clean();
- $output = json_encode( $output );
- if ( is_array( $output ) ) { print_r( $output ); } else { echo $output; } die;
- }
- // Now, lets charge!
- $mycred->add_creds(
- 'userpro_sent_message',
- $chat_from,
- 0 - $cost,
- 'New message sent to %display_name%',
- $chat_with,
- array( 'ref_type' => 'user' ), // This allows the use of user related template tags
- $point_type
- );
- // Now, lets give the seller the points
- $mycred->add_creds(
- 'userpro_received_message',
- $chat_with,
- $cost,
- 'New message from %display_name%',
- $chat_from,
- array( 'ref_type' => 'user' ), // This allows the use of user related template tags
- $point_type
- );
- }
- }
- // If we come this far, all is well.
- // Do nothing in order for the real ajax handler to
- // do it's thing.
- }
- /**
- * Add myCRED References
- * Allows you to create badges in myCRED based on the above type of transactions, like
- * a badge for paying for 100 messages or for receiving 500 payments from new messages.
- * @see http://codex.mycred.me/reference-guide/log-references/
- * @version 1.0
- */
- add_filter( 'mycred_all_references', 'mycredpro_add_custom_references' );
- function mycredpro_add_custom_references( $list ) {
- // The list is an associative array so we just need to provide the
- // unique reference and a label. Can also be used to relable existing references.
- // Labels are mainly used in the wp-admin area seen by those who can setup myCRED badges.
- $list['userpro_sent_message'] = 'Paid for Message';
- $list['userpro_received_message'] = 'Received Message Payment';
- return $list;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement