Advertisement
designbymerovingi

myCRED Charge Example for UserPro Messaging

Feb 28th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. /**
  2.  * Message Payment
  3.  * Stop messages from being sent from "buyers" to "sellers" if the buyer does
  4.  * not have enough myCRED points. If they do, charge the buyer the points and transfer
  5.  * it to the seller.
  6.  * @see http://codex.mycred.me/category/template-tags/temp-user/
  7.  * @version 1.0
  8.  */
  9. add_action( 'wp_ajax_userpro_start_chat', 'mycredme_intercept_ajax_new_message', 0 );
  10. function mycredme_intercept_ajax_new_message() {
  11.  
  12.     // Lets mimic the real ajax handler that comes after this
  13.     global $userpro, $userpro_msg;
  14.  
  15.     $output    = '';
  16.     $chat_from = $_POST['chat_from'];
  17.     $chat_with = $_POST['chat_with'];
  18.     if ( ! userpro_is_logged_in() || $chat_from != get_current_user_id() ) die();
  19.     if ( ! $userpro_msg->can_chat_with( $chat_with ) ) die();
  20.  
  21.     // Ok, if we got this far, we can send messages. Lets make sure myCRED is installed.
  22.     if ( ! function_exists( 'mycred' ) ) die();
  23.  
  24.     // In case you use multiple point types and need to use a custom type,
  25.     // you enter your custom point type key here. For default leave as is.
  26.     $point_type = 'mycred_default';
  27.  
  28.     // Next lets load myCRED
  29.     $mycred = mycred( $point_type );
  30.  
  31.     // The cost of the message.
  32.     $cost = 5;
  33.  
  34.     // Make sure user is not excluded from using points
  35.     if ( ! $mycred->exclude_user( $chat_from ) ) {
  36.  
  37.         // Get user objects so we can check roles
  38.         $sender           = get_userdata( $chat_from );
  39.         $senders_roles    = $sender->roles;
  40.         $recipient        = get_userdata( $chat_with );
  41.         $recipients_roles = $recipient->roles;
  42.  
  43.         // Check if this is a conversation between a buyer and seller (based of WP Roles)
  44.         if ( in_array( 'verkoper', $senders_roles ) && in_array( 'koper', $recipients_roles ) ) {
  45.  
  46.             // Get senders balance
  47.             $balance = $mycred->get_users_balance( $chat_from, $point_type );
  48.  
  49.             // Sorry but not enough funds, show error message
  50.             if ( $balance < $minimum ) {
  51.  
  52.                 // Shown by the UserPro Messaging plugin
  53.                 $output['message'] = '<div class="userpro-msg-notice">Insufficient Funds.</div>';
  54.  
  55.                 ob_start();
  56.  
  57.                 // Not sure if this is neaded but it's what the plugin does so...
  58.                 require_once userpro_msg_path . 'templates/conversation.php';
  59.                 $output['html'] = ob_get_contents();
  60.                 ob_end_clean();
  61.  
  62.                 $output = json_encode( $output );
  63.                 if ( is_array( $output ) ) { print_r( $output ); } else { echo $output; } die;
  64.  
  65.             }
  66.  
  67.             // Now, lets charge!
  68.             $mycred->add_creds(
  69.                 'userpro_sent_message',
  70.                 $chat_from,
  71.                 0 - $cost,
  72.                 'New message sent to %display_name%',
  73.                 $chat_with,
  74.                 array( 'ref_type' => 'user' ), // This allows the use of user related template tags
  75.                 $point_type
  76.             );
  77.  
  78.             // Now, lets give the seller the points
  79.             $mycred->add_creds(
  80.                 'userpro_received_message',
  81.                 $chat_with,
  82.                 $cost,
  83.                 'New message from %display_name%',
  84.                 $chat_from,
  85.                 array( 'ref_type' => 'user' ), // This allows the use of user related template tags
  86.                 $point_type
  87.             );
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93.     // If we come this far, all is well.
  94.     // Do nothing in order for the real ajax handler to
  95.     // do it's thing.
  96.  
  97. }
  98.  
  99. /**
  100.  * Add myCRED References
  101.  * Allows you to create badges in myCRED based on the above type of transactions, like
  102.  * a badge for paying for 100 messages or for receiving 500 payments from new messages.
  103.  * @see http://codex.mycred.me/reference-guide/log-references/
  104.  * @version 1.0
  105.  */
  106. add_filter( 'mycred_all_references', 'mycredpro_add_custom_references' );
  107. function mycredpro_add_custom_references( $list ) {
  108.  
  109.     // The list is an associative array so we just need to provide the
  110.     // unique reference and a label. Can also be used to relable existing references.
  111.     // Labels are mainly used in the wp-admin area seen by those who can setup myCRED badges.
  112.     $list['userpro_sent_message']     = 'Paid for Message';
  113.     $list['userpro_received_message'] = 'Received Message Payment';
  114.  
  115.     return $list;
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement