Advertisement
designbymerovingi

UM - myCRED Charge first message (custom)

Mar 4th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 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_after_follow_button_profile', 'mycredpro_save_profile_id_global', 1 );
  8. function mycredpro_save_profile_id_global( $user_id ) {
  9.  
  10.     global $mycred_em_profile_id, $mycred_em_charge;
  11.  
  12.     $mycred_em_profile_id = $user_id;
  13.     $mycred_em_charge     = false;
  14.  
  15. }
  16.  
  17. /**
  18.  * Stop Message
  19.  * Stop a user from sending a message if the parties
  20.  * do not fulfill our requirements.
  21.  * @version 1.0
  22.  */
  23. add_filter( 'um_user_permissions_filter', 'mycredpro_stop_um_message', 10, 2 );
  24. function mycredpro_stop_um_message( $roles, $user_id ) {
  25.  
  26.     global $mycred_em_profile_id, $mycred_em_charge;
  27.  
  28.     if ( ! function_exists( 'mycred' ) ) return $roles;
  29.  
  30.     // In case you use multiple point types and need to use a custom type,
  31.     // you enter your custom point type key here. For default leave as is.
  32.     $point_type = 'mycred_default';
  33.  
  34.     // Next lets load myCRED
  35.     $mycred = mycred( $point_type );
  36.  
  37.     // The cost of the message.
  38.     $cost = 5;
  39.  
  40.     if ( $mycred_em_profile_id !== NULL && $mycred_em_profile_id != $user_id ) {
  41.  
  42.         // Get user objects so we can check roles
  43.         $sender           = get_userdata( $user_id );
  44.         $senders_roles    = $sender->roles;
  45.         $recipient        = get_userdata( $mycred_em_profile_id );
  46.         $recipients_roles = $recipient->roles;
  47.  
  48.         // Check if this is a conversation between a buyer and seller (based of WP Roles)
  49.         if ( in_array( 'verkoper', $senders_roles ) && in_array( 'koper', $recipients_roles ) ) {
  50.  
  51.             // Get senders balance
  52.             $balance = $mycred->get_users_balance( $user_id, $point_type );
  53.  
  54.             // Sorry but not enough funds, show error message
  55.             if ( $balance < $cost ) {
  56.  
  57.                 $roles['can_start_pm'] = false;
  58.  
  59.             }
  60.             else {
  61.  
  62.                 $mycred_em_charge = true;
  63.  
  64.             }
  65.  
  66.         }
  67.  
  68.     }
  69.  
  70.     return $roles;
  71.  
  72. }
  73.  
  74. /**
  75.  * Charge Conversation
  76.  * Charges the conversaion between two parties.
  77.  * @version 1.0
  78.  */
  79. add_action( 'um_after_new_conversation', 'mycredpro_charge_um_conversation', 10, 3 );
  80. function mycredpro_charge_um_conversation( $user1, $user2, $conversation_id ) {
  81.  
  82.     global $mycred_em_charge;
  83.  
  84.     // See if we should charge this instance.
  85.     if ( $mycred_em_charge !== true ) return;
  86.  
  87.     // Ok, if we got this far, we can send messages. Lets make sure myCRED is installed.
  88.     if ( ! function_exists( 'mycred' ) ) return;
  89.  
  90.     // In case you use multiple point types and need to use a custom type,
  91.     // you enter your custom point type key here. For default leave as is.
  92.     $point_type = 'mycred_default';
  93.  
  94.     // Next lets load myCRED
  95.     $mycred = mycred( $point_type );
  96.  
  97.     // The cost of the message.
  98.     $cost = 5;
  99.  
  100.     // Now, lets charge!
  101.     $mycred->add_creds(
  102.         'um_sent_message',
  103.         $user1,
  104.         0 - $cost,
  105.         'New message sent to %display_name%',
  106.         $user2,
  107.         array( 'ref_type' => 'user' ), // This allows the use of user related template tags
  108.         $point_type
  109.     );
  110.  
  111.     // Now, lets give the seller the points
  112.     $mycred->add_creds(
  113.         'um_received_message',
  114.         $user2,
  115.         $cost,
  116.         'New message from %display_name%',
  117.         $user1,
  118.         array( 'ref_type' => 'user' ), // This allows the use of user related template tags
  119.         $point_type
  120.     );
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement