Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Store the Profile ID
- * Since we can not ge the profile ID using um_user_permissions_filter filter
- * we globalise it now so we can access it later.
- * @version 1.0
- */
- add_action( 'um_members_just_after_name', 'mycredpro_save_profile_id_global', 1 );
- function mycredpro_save_profile_id_global( $user_id ) {
- global $mycred_em_profile_id;
- $mycred_em_profile_id = $user_id;
- }
- /**
- * Hide New Message Button
- * Hide the new message button if we can not afford to start a new conversation
- * with the profile.
- * @version 1.0
- */
- add_filter( 'um_user_permissions_filter', 'mycredpro_stop_um_message', 10, 2 );
- function mycredpro_stop_um_message( $roles, $user_id ) {
- if ( ! function_exists( 'mycred' ) ) return $roles;
- global $mycred_em_profile_id;
- // 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;
- if ( $mycred_em_profile_id !== NULL && $mycred_em_profile_id != $user_id ) {
- // Get user objects so we can check roles
- $sender = wp_get_current_user();
- $senders_roles = $sender->roles;
- $recipient = get_userdata( $mycred_em_profile_id );
- $recipients_roles = $recipient->roles;
- // Check if this is a conversation between a buyer and seller (based of WP Roles)
- if ( in_array( 'administrator', $senders_roles ) && in_array( 'subscriber', $recipients_roles ) ) {
- // Get senders balance
- $balance = $mycred->get_users_balance( $user_id, $point_type );
- // Sorry but not enough funds, show error message
- if ( $balance < $cost ) {
- $roles['can_start_pm'] = false;
- }
- }
- }
- return $roles;
- }
- /**
- * Charge Conversation
- * Intercept the message ajax handler and charge or decline new messages
- * before UM Messaging handles the conversation.
- * @version 1.0
- */
- add_action('wp_ajax_nopriv_um_messaging_send', 'mycredpro_stop_messages_on_no_funds', 1 );
- add_action('wp_ajax_um_messaging_send', 'mycredpro_stop_messages_on_no_funds', 1 );
- function mycredpro_stop_messages_on_no_funds() {
- if ( !isset( $_POST['message_to'] ) || !is_numeric( $_POST['message_to'] ) || !is_user_logged_in() ) die();
- if ( !isset( $_POST['content'] ) || trim( $_POST['content'] ) == '' ) die();
- $sender = wp_get_current_user();
- $senders_roles = $sender->roles;
- $recipient = get_userdata( absint( $_POST['message_to'] ) );
- $recipients_roles = $recipient->roles;
- // Check if this is a conversation between a buyer and seller (based of WP Roles)
- if ( in_array( 'administrator', $senders_roles ) && in_array( 'subscriber', $recipients_roles ) ) {
- $point_type = 'mycred_default';
- // Next lets load myCRED
- $mycred = mycred( $point_type );
- // The cost of the message.
- $cost = 5;
- // Get senders balance
- $balance = $mycred->get_users_balance( $sender->ID, $point_type );
- // Sorry but not enough funds, show error message
- if ( $balance < $cost ) {
- $output = array(
- 'messages' => array(),
- 'limit_hit' => 1
- );
- $output=json_encode($output);
- if(is_array($output)){print_r($output);}else{echo $output;}die;
- }
- else {
- // Now, lets charge!
- $mycred->add_creds(
- 'um_sent_message',
- $sender->ID,
- 0 - $cost,
- 'New message sent to %display_name%',
- $recipient->ID,
- 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(
- 'um_received_message',
- $recipient->ID,
- $cost,
- 'New message from %display_name%',
- $sender->ID,
- array( 'ref_type' => 'user' ), // This allows the use of user related template tags
- $point_type
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement