Advertisement
designbymerovingi

UM Charge for Messages

Mar 8th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. /**
  2.  * Store the Profile ID
  3.  * Since we can not ge the profile ID using um_user_permissions_filter filter
  4.  * we globalise it now so we can access it later.
  5.  * @version 1.0
  6.  */
  7. add_action( 'um_members_just_after_name', 'mycredpro_save_profile_id_global', 1 );
  8. function mycredpro_save_profile_id_global( $user_id ) {
  9.  
  10.     global $mycred_em_profile_id;
  11.  
  12.     $mycred_em_profile_id = $user_id;
  13.  
  14. }
  15.  
  16. /**
  17.  * Hide New Message Button
  18.  * Hide the new message button if we can not afford to start a new conversation
  19.  * with the profile.
  20.  * @version 1.0
  21.  */
  22. add_filter( 'um_user_permissions_filter', 'mycredpro_stop_um_message', 10, 2 );
  23. function mycredpro_stop_um_message( $roles, $user_id ) {
  24.  
  25.     if ( ! function_exists( 'mycred' ) ) return $roles;
  26.  
  27.     global $mycred_em_profile_id;
  28.  
  29.     // In case you use multiple point types and need to use a custom type,
  30.     // you enter your custom point type key here. For default leave as is.
  31.     $point_type = 'mycred_default';
  32.  
  33.     // Next lets load myCRED
  34.     $mycred = mycred( $point_type );
  35.  
  36.     // The cost of the message.
  37.     $cost = 5;
  38.  
  39.     if ( $mycred_em_profile_id !== NULL && $mycred_em_profile_id != $user_id ) {
  40.  
  41.         // Get user objects so we can check roles
  42.         $sender           = wp_get_current_user();
  43.         $senders_roles    = $sender->roles;
  44.         $recipient        = get_userdata( $mycred_em_profile_id );
  45.         $recipients_roles = $recipient->roles;
  46.  
  47.         // Check if this is a conversation between a buyer and seller (based of WP Roles)
  48.         if ( in_array( 'administrator', $senders_roles ) && in_array( 'subscriber', $recipients_roles ) ) {
  49.  
  50.             // Get senders balance
  51.             $balance = $mycred->get_users_balance( $user_id, $point_type );
  52.  
  53.             // Sorry but not enough funds, show error message
  54.             if ( $balance < $cost ) {
  55.  
  56.                 $roles['can_start_pm'] = false;
  57.  
  58.             }
  59.  
  60.         }
  61.  
  62.     }
  63.  
  64.     return $roles;
  65.  
  66. }
  67.  
  68. /**
  69.  * Charge Conversation
  70.  * Intercept the message ajax handler and charge or decline new messages
  71.  * before UM Messaging handles the conversation.
  72.  * @version 1.0
  73.  */
  74. add_action('wp_ajax_nopriv_um_messaging_send', 'mycredpro_stop_messages_on_no_funds', 1 );
  75. add_action('wp_ajax_um_messaging_send', 'mycredpro_stop_messages_on_no_funds', 1 );
  76. function mycredpro_stop_messages_on_no_funds() {
  77.  
  78.     if ( !isset( $_POST['message_to'] ) || !is_numeric( $_POST['message_to'] ) || !is_user_logged_in() ) die();
  79.     if ( !isset( $_POST['content'] ) || trim( $_POST['content'] ) == '' ) die();
  80.  
  81.     $sender           = wp_get_current_user();
  82.     $senders_roles    = $sender->roles;
  83.     $recipient        = get_userdata( absint( $_POST['message_to'] ) );
  84.     $recipients_roles = $recipient->roles;
  85.  
  86.     // Check if this is a conversation between a buyer and seller (based of WP Roles)
  87.     if ( in_array( 'administrator', $senders_roles ) && in_array( 'subscriber', $recipients_roles ) ) {
  88.  
  89.         $point_type = 'mycred_default';
  90.    
  91.         // Next lets load myCRED
  92.         $mycred = mycred( $point_type );
  93.  
  94.         // The cost of the message.
  95.         $cost = 5;
  96.  
  97.         // Get senders balance
  98.         $balance = $mycred->get_users_balance( $sender->ID, $point_type );
  99.  
  100.         // Sorry but not enough funds, show error message
  101.         if ( $balance < $cost ) {
  102.  
  103.             $output = array(
  104.                 'messages' => array(),
  105.                 'limit_hit' => 1
  106.             );
  107.  
  108.             $output=json_encode($output);
  109.             if(is_array($output)){print_r($output);}else{echo $output;}die;
  110.  
  111.         }
  112.  
  113.         else {
  114.  
  115.             // Now, lets charge!
  116.             $mycred->add_creds(
  117.                 'um_sent_message',
  118.                 $sender->ID,
  119.                 0 - $cost,
  120.                 'New message sent to %display_name%',
  121.                 $recipient->ID,
  122.                 array( 'ref_type' => 'user' ), // This allows the use of user related template tags
  123.                 $point_type
  124.             );
  125.    
  126.             // Now, lets give the seller the points
  127.             $mycred->add_creds(
  128.                 'um_received_message',
  129.                 $recipient->ID,
  130.                 $cost,
  131.                 'New message from %display_name%',
  132.                 $sender->ID,
  133.                 array( 'ref_type' => 'user' ), // This allows the use of user related template tags
  134.                 $point_type
  135.             );
  136.  
  137.         }
  138.  
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement