SommerSyi Posted July 1, 2015 Share Posted July 1, 2015 Hello I'm using the mycred-usersultra script on wordpress It allows you to award points to members who follow or following or for friendships. I am trying I am trying to modify it to give a point to a member when the member that follows them is not following anyone else. Members can follow as many as they like but only the first person they ever follow gets the point. I call it a first connection. Any suggestions or help will be appreciated. Below is the original code. I tried to adjust the user is following block but it stopped awarding points totally. <?php if ( ! defined( 'MYCRED_USERSULTRA_VERSION' ) ) exit; /** * myCRED_UsersUltra_Hook_Social class * @since 1.0 * @version 1.0 */ if ( class_exists( 'myCRED_Hook' ) ) : class myCRED_UsersUltra_Hook_Social extends myCRED_Hook { /** * Construct */ function __construct( $hook_prefs, $type = 'mycred_default' ) { parent::__construct( array( 'id' => 'usersultra_social', 'defaults' => array( 'new_friend' => array( 'creds' => 1, 'log' => '%plural% for new friendship with %display_name%' ), 'ended_friendship' => array( 'creds' => -1, 'log' => '%plural% deduction for ended friendship with %display_name%' ), 'new_follow' => array( 'creds' => 1, 'log' => '%plural% for following a member', 'limit' => 1 ), 'new_follower' => array( 'creds' => 1, 'log' => '%plural% for new follower', 'limit' => 1 ), 'rate' => array( 'creds' => 1, 'log' => '%plural% for rating' ), 'rating' => array( 'creds' => 1, 'log' => '%plural% for receiving a rating' ) ) ), $hook_prefs, $type ); } /** * Run * @since 1.0 * @version 1.0 */ public function run() { if ( ! is_user_logged_in() ) return; // Friendship & Blocking if ( $this->prefs['new_friend']['creds'] != $this->core->zero() || $this->prefs['ended_friendship']['creds'] != $this->core->zero() ) add_action( 'wp_ajax_friend_request_action', array( $this, 'friendships' ), 1 ); // Likes //if ( $this->prefs['like']['creds'] != $this->core->zero() || $this->prefs['liked']['creds'] != $this->core->zero() ) // add_action( 'wp_ajax_like_item', array( $this, 'likes' ), 1 ); // Follow if ( $this->prefs['new_follow']['creds'] != $this->core->zero() || $this->prefs['new_follower']['creds'] != $this->core->zero() ) add_action( 'wp_ajax_send_follow_request', array( $this, 'following' ), 1 ); } /** * Daily Limit Check */ public function exceeds_limit( $instance = '', $user_id = 0 ) { if ( ! isset( $this->prefs[ $instance ]['limit'] ) || $this->prefs[ $instance ]['limit'] == 0 ) return false; global $wpdb; $start_today = strtotime( 'today midnight' ); $now = time(); $count = $wpdb->get_var( $wpdb->prepare( " SELECT COUNT( * ) FROM {$this->core->log_table} WHERE ref = %s AND user_id = %d AND time BETWEEN ( {$start_today} AND {$now} ) AND ctype = %s;", $instance, $user_id, $this->mycred_type ) ); if ( $count >= $this->prefs[ $instance ]['limit'] ) return true; return false; } /** * Friendships */ public function friendships() { if ( ! isset( $_POST['item_action'] ) || ! isset( $_POST['item_id'] ) || $_POST['item_id'] == '' ) return; $cui = get_current_user_id(); $friend_id = sanitize_text_field( $_POST['item_id'] ); $item_action = sanitize_text_field( $_POST['item_action'] ); // We should not be able to befriend ourselves if ( $cui == $friend_id ) return; // Action switch ( $item_action ) { case 'approve' : // Check for Exclusion and Enfoce limit (if used) and make sure we only award once if ( ! $this->core->exclude_user( $cui ) && ! $this->exceeds_limit( 'new_friend', $cui ) && ! $this->has_entry( 'new_friendship', $friend_id, $cui ) ) { // Execute $this->core->add_creds( 'new_friendship', $cui, $this->prefs['new_friend']['creds'], $this->prefs['new_friend']['log'], $friend_id, array( 'ref_type' => 'user' ), $this->mycred_type ); } // Check for Exclusion and Enfoce limit (if used) and make sure we only award once if ( ! $this->core->exclude_user( $friend_id ) && ! $this->exceeds_limit( 'new_friend', $friend_id ) && ! $this->has_entry( 'new_friendship', $cui, $friend_id ) ) { $this->core->add_creds( 'new_friendship', $friend_id, $this->prefs['new_friend']['creds'], $this->prefs['new_friend']['log'], $cui, array( 'ref_type' => 'user' ), $this->mycred_type ); } break; case 'block' : // Check for Exclusion if ( ! $this->core->exclude_user( $cui ) ) { // Execute $this->core->add_creds( 'ended_friendship', $cui, $this->prefs['ended_friendship']['creds'], $this->prefs['ended_friendship']['log'], $friend_id, array( 'ref_type' => 'user' ), $this->mycred_type ); } // Check for Exclusion if ( ! $this->core->exclude_user( $friend_id ) ) { $this->core->add_creds( 'ended_friendship', $friend_id, $this->prefs['ended_friendship']['creds'], $this->prefs['ended_friendship']['log'], $cui, array( 'ref_type' => 'user' ), $this->mycred_type ); } break; } } /** * User Is Following */ public function user_is_following( $user_id, $follow_id ) { global $wpdb; $check = $wpdb->get_var( $wpdb->prepare( " SELECT follower_id FROM {$wpdb->prefix}usersultra_followers WHERE follower_user_id = %d AND follower_following_user_id = %d;", $follow_id, $user_id ) ); if ( $check === NULL || $check == '' || $check == 0 ) return false; return true; } /** * Following * @since 1.0 * @version 1.0 */ public function following() { $cui = get_current_user_id(); $user_to_follow = $_POST['user_id']; // We can not follow our own account if ( $cui == $user_to_follow ) return; // Check if user is excluded if ( $this->core->exclude_user( $cui ) ) return; // Make sure we are not already following if ( $this->user_is_following( $cui, $user_to_follow ) ) return; // Award the act of following if ( $this->prefs['new_follow']['creds'] != $this->core->zero() ) { // Enfoce limit (if used) if ( $this->exceeds_limit( 'new_follow', $cui ) ) return; // Execute $this->core->add_creds( 'following_member', $cui, $this->prefs['new_follow']['creds'], $this->prefs['new_follow']['log'], $user_to_follow, array( 'ref_type' => 'user' ), $this->mycred_type ); } // Award the user being followed for this act if ( $this->prefs['new_follower']['creds'] != $this->core->zero() ) { // Enfoce limit (if used) if ( $this->exceeds_limit( 'new_follower', $user_to_follow ) ) return; // Execute $this->core->add_creds( 'new_follower', $user_to_follow, $this->prefs['new_follower']['creds'], $this->prefs['new_follower']['log'], $cui, array( 'ref_type' => 'user' ), $this->mycred_type ); } } public function check_ip( $ip, $profile_id ) { global $wpdb; $check = $wpdb->get_row( $wpdb->prepare( " SELECT * FROM {$wpdb->prefix}usersultra_ajaxrating_vote WHERE ajaxrating_vote_ip = %s AND ajaxrating_user_id = %s;", $ip, $profile_id ) ); if ( ! is_object( $check ) ) return true; return false; } /** * Preferences * @since 1.0 * @version 1.0 */ public function preferences() { $prefs = $this->prefs; ?> <!-- Friendship --> <label for="<?php echo $this->field_id( array( 'new_friend', 'creds' ) ); ?>" class="subheader"><?php _e( 'New Friendship', 'mycred_usersultra' ); ?></label> <ol> <li> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_friend', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_friend', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_friend']['creds'] ); ?>" size="8" /></div> </li> <li class="empty"> </li> <li> <label for="<?php echo $this->field_id( array( 'new_friend', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred_usersultra' ); ?></label> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_friend', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_friend', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['new_friend']['log'] ); ?>" class="long" /></div> <span class="description"><?php echo $this->available_template_tags( array( 'general', 'user' ) ); ?></span> </li> </ol> <!-- Ended Friendships --> <label for="<?php echo $this->field_id( array( 'ended_friendship', 'creds' ) ); ?>" class="subheader"><?php _e( 'Ended Friendship', 'mycred_usersultra' ); ?></label> <ol> <li> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'ended_friendship', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'ended_friendship', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['ended_friendship']['creds'] ); ?>" size="8" /></div> </li> <li class="empty"> </li> <li> <label for="<?php echo $this->field_id( array( 'ended_friendship', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred_usersultra' ); ?></label> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'ended_friendship', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'ended_friendship', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['ended_friendship']['log'] ); ?>" class="long" /></div> <span class="description"><?php echo $this->available_template_tags( array( 'general', 'user' ) ); ?></span> </li> </ol> <!-- Following --> <label for="<?php echo $this->field_id( array( 'new_follow', 'creds' ) ); ?>" class="subheader"><?php _e( 'Following Other Member', 'mycred_usersultra' ); ?></label> <ol> <li> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follow', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follow', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_follow']['creds'] ); ?>" size="8" /></div> </li> <li class="empty"> </li> <li> <label for="<?php echo $this->field_id( array( 'new_follow', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred_usersultra' ); ?></label> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follow', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follow', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['new_follow']['log'] ); ?>" class="long" /></div> <span class="description"><?php echo $this->available_template_tags( array( 'general', 'user' ) ); ?></span> </li> <li class="empty"> </li> <li> <label for="<?php echo $this->field_id( array( 'new_follow', 'limit' ) ); ?>"><?php _e( 'Daily Limit', 'mycred_usersultra' ); ?></label> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follow', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follow', 'limit' ) ); ?>" value="<?php echo esc_attr( $prefs['new_follow']['limit'] ); ?>" class="short" /></div> <span class="description"><?php echo $this->core->template_tags_general( __( 'Maximum number of follows per day that grants %plural%. Use zero for unlimited.', 'mycred_usersultra' ) ); ?></span> </li> </ol> <!-- New Follower --> <label for="<?php echo $this->field_id( array( 'new_photo', 'creds' ) ); ?>" class="subheader"><?php _e( 'New Follower', 'mycred_usersultra' ); ?></label> <ol> <li> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follower', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follower', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_follower']['creds'] ); ?>" size="8" /></div> </li> <li class="empty"> </li> <li> <label for="<?php echo $this->field_id( array( 'new_follower', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred_usersultra' ); ?></label> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follower', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follower', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['new_follower']['log'] ); ?>" class="long" /></div> <span class="description"><?php echo $this->available_template_tags( array( 'general', 'user' ) ); ?></span> </li> <li class="empty"> </li> <li> <label for="<?php echo $this->field_id( array( 'new_follower', 'limit' ) ); ?>"><?php _e( 'Daily Limit', 'mycred_usersultra' ); ?></label> <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follower', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follower', 'limit' ) ); ?>" value="<?php echo esc_attr( $prefs['new_follower']['limit'] ); ?>" class="short" /></div> <span class="description"><?php echo $this->core->template_tags_general( __( 'Maximum number of new followers per day that grants %plural%. Use zero for unlimited.', 'mycred_usersultra' ) ); ?></span> </li> </ol> <?php } } endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/297134-following-users/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.