/** * @ticket BP7237 * @ticket BP6643 * @ticket BP7245 */ public function test_last_activity_should_bust_activity_with_last_activity_cache() { global $wpdb; $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $time_1 = date('Y-m-d H:i:s', time() - HOUR_IN_SECONDS); $time_2 = date('Y-m-d H:i:s', time() - HOUR_IN_SECONDS * 2); bp_update_user_last_activity($u1, $time_1); bp_update_user_last_activity($u2, $time_2); $activity_args_a = array('filter' => array('object' => buddypress()->members->id, 'action' => 'last_activity'), 'max' => 1); $activity_args_b = array('filter' => array('action' => 'new_member'), 'fields' => 'ids'); // Prime bp_activity and bp_activity_with_last_activity caches. $a1 = bp_activity_get($activity_args_a); $expected = array($u1, $u2); $found = array_map('intval', wp_list_pluck($a1['activities'], 'user_id')); $this->assertSame($expected, $found); $b1 = bp_activity_get($activity_args_b); // Bump u2 activity so it should appear first. $new_time = date('Y-m-d H:i:s', time() - HOUR_IN_SECONDS); bp_update_user_last_activity($u2, $new_time); $a2 = bp_activity_get($activity_args_a); $expected = array($u2, $u1); $found = array_map('intval', wp_list_pluck($a2['activities'], 'user_id')); $this->assertSame($expected, $found); $num_queries = $wpdb->num_queries; // bp_activity cache should not have been touched. $b2 = bp_activity_get($activity_args_b); $this->assertEqualSets($b1, $b2); $this->assertSame($num_queries, $wpdb->num_queries); }
/** * Listener function for the logged-in user's 'last_activity' metadata. * * Many functions use a "last active" feature to show the length of time since * the user was last active. This function will update that time as a usermeta * setting for the user every 5 minutes while the user is actively browsing the * site. * * @uses bp_update_user_meta() BP function to update user metadata in the * usermeta table. * * @return bool|null Returns false if there is nothing to do. */ function bp_core_record_activity() { // Bail if user is not logged in if (!is_user_logged_in()) { return false; } // Get the user ID $user_id = bp_loggedin_user_id(); // Bail if user is not active if (bp_is_user_inactive($user_id)) { return false; } // Get the user's last activity $activity = bp_get_user_last_activity($user_id); // Make sure it's numeric if (!is_numeric($activity)) { $activity = strtotime($activity); } // Get current time $current_time = bp_core_current_time(); // Use this action to detect the very first activity for a given member if (empty($activity)) { do_action('bp_first_activity_for_member', $user_id); } // If it's been more than 5 minutes, record a newer last-activity time if (empty($activity) || strtotime($current_time) >= strtotime('+5 minutes', $activity)) { bp_update_user_last_activity($user_id, $current_time); } }
public static function prepare_buddypress_data($user_id, $config, $entry) { // required for user to display in the directory if (function_exists('bp_update_user_last_activity')) { bp_update_user_last_activity($user_id); } else { bp_update_user_meta($user_id, 'last_activity', true); } $buddypress_meta = rgars($config, 'meta/buddypress_meta'); if (empty($buddypress_meta)) { return; } self::log_debug(__METHOD__ . '(): starting.'); $form = RGFormsModel::get_form_meta($entry['form_id']); $buddypress_row = array(); $i = 0; foreach ($buddypress_meta as $meta_item) { if (empty($meta_item['meta_name']) || empty($meta_item['meta_value'])) { continue; } $buddypress_row[$i]['field_id'] = $meta_item['meta_name']; $buddypress_row[$i]['user_id'] = $user_id; // get GF and BP fields $gform_field = RGFormsModel::get_field($form, $meta_item['meta_value']); if (version_compare(BP_VERSION, '1.6', '<')) { $bp_field = new BP_XProfile_Field(); $bp_field->bp_xprofile_field($meta_item['meta_name']); } else { require_once WP_PLUGIN_DIR . '/buddypress/bp-xprofile/bp-xprofile-classes.php'; $bp_field = new BP_XProfile_Field($meta_item['meta_name']); } // if bp field is a checkbox AND gf field is a checkbox, get array of input values $input_type = RGFormsModel::get_input_type($gform_field); if (in_array($bp_field->type, array('checkbox', 'multiselectbox')) && in_array($input_type, array('checkbox', 'multiselect'))) { $meta_value = RGFormsModel::get_lead_field_value($entry, $gform_field); if (!is_array($meta_value)) { $meta_value = explode(',', $meta_value); } $meta_value = self::maybe_get_category_name($gform_field, $meta_value); $meta_value = array_filter($meta_value, 'GFUser::not_empty'); } else { if ($bp_field->type == 'datebox' && $gform_field['type'] == 'date') { if (version_compare(BP_VERSION, '2.1.1', '<')) { $meta_value = strtotime(self::get_prepared_value($gform_field, $meta_item['meta_value'], $entry)); } else { $meta_value = self::get_prepared_value($gform_field, $meta_item['meta_value'], $entry) . ' 00:00:00'; } } else { $meta_value = self::get_prepared_value($gform_field, $meta_item['meta_value'], $entry); } } self::log_debug(__METHOD__ . "(): Meta item: {$meta_item['meta_name']}. Value: {$meta_value}"); $buddypress_row[$i]['value'] = $meta_value; $buddypress_row[$i]['last_update'] = date('Y-m-d H:i:s'); $buddypress_row[$i]['field'] = $bp_field; $i++; } GFUserData::insert_buddypress_data($buddypress_row); self::log_debug(__METHOD__ . '(): finished.'); }
/** * Backward compatibility for 'last_activity' usermeta setting. * * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For * backward compatibility, we continue to mirror the data there. This function * serves two purposes: it warns plugin authors of the change, and it updates * the data in the proper location. * * @since 2.0.0 * * @access private For internal use only. * * @param int $meta_id ID of the just-set usermeta row. * @param int $object_id ID of the user. * @param string $meta_key Meta key being fetched. * @param string $meta_value Active time. */ function _bp_update_user_meta_last_activity_warning($meta_id, $object_id, $meta_key, $meta_value) { if ('last_activity' === $meta_key) { _doing_it_wrong('update_user_meta( $user_id, \'last_activity\' )', __('User last_activity data is no longer stored in usermeta. Use bp_update_user_last_activity() instead.', 'buddypress'), '2.0.0'); bp_update_user_last_activity($object_id, $meta_value); } }
/** * Listener function for the logged-in user's 'last_activity' metadata. * * Many functions use a "last active" feature to show the length of time since * the user was last active. This function will update that time as a usermeta * setting for the user every 5 minutes while the user is actively browsing the * site. * * @uses bp_update_user_meta() BP function to update user metadata in the * usermeta table. * * @return bool|null Returns false if there is nothing to do. */ function bp_core_record_activity() { if (!is_user_logged_in()) { return false; } $user_id = bp_loggedin_user_id(); if (bp_is_user_inactive($user_id)) { return false; } $activity = bp_get_user_last_activity($user_id); if (!is_numeric($activity)) { $activity = strtotime($activity); } // Get current time $current_time = bp_core_current_time(); // Use this action to detect the very first activity for a given member if (empty($activity)) { do_action('bp_first_activity_for_member', $user_id); } if (empty($activity) || strtotime($current_time) >= strtotime('+5 minutes', $activity)) { bp_update_user_last_activity($user_id, $current_time); } }
/** * Saves a notice. * * @since 1.0.0 * * @return bool */ public function save() { global $wpdb; $bp = buddypress(); $this->subject = apply_filters('messages_notice_subject_before_save', $this->subject, $this->id); $this->message = apply_filters('messages_notice_message_before_save', $this->message, $this->id); /** * Fires before the current message notice item gets saved. * * Please use this hook to filter the properties above. Each part will be passed in. * * @since 1.0.0 * * @param BP_Messages_Notice $this Current instance of the message notice item being saved. Passed by reference. */ do_action_ref_array('messages_notice_before_save', array(&$this)); if (empty($this->id)) { $sql = $wpdb->prepare("INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, %s, %d)", $this->subject, $this->message, $this->date_sent, $this->is_active); } else { $sql = $wpdb->prepare("UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id); } if (!$wpdb->query($sql)) { return false; } if (!($id = $this->id)) { $id = $wpdb->insert_id; } // Now deactivate all notices apart from the new one. $wpdb->query($wpdb->prepare("UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id)); bp_update_user_last_activity(bp_loggedin_user_id(), bp_core_current_time()); /** * Fires after the current message notice item has been saved. * * @since 1.0.0 * * @param BP_Messages_Notice $this Current instance of the message item being saved. Passed by reference. */ do_action_ref_array('messages_notice_after_save', array(&$this)); return true; }
function create_object($args) { if (!isset($args['creator_id'])) { if (is_user_logged_in()) { $args['creator_id'] = get_current_user_id(); // Create a user. This is based on from BP_UnitTestCase->create_user(). } else { $last_activity = date('Y-m-d H:i:s', strtotime(bp_core_current_time()) - 60 * 60 * 24 * 365); $user_factory = new WP_UnitTest_Factory_For_User(); $args['creator_id'] = $this->factory->user->create(array('role' => 'subscriber')); bp_update_user_last_activity($args['creator_id'], $last_activity); if (bp_is_active('xprofile')) { $user = new WP_User($args['creator_id']); xprofile_set_field_data(1, $args['creator_id'], $user->display_name); } } } $group_id = groups_create_group($args); if (!$group_id) { return false; } groups_update_groupmeta($group_id, 'total_member_count', 1); $last_activity = isset($args['last_activity']) ? $args['last_activity'] : bp_core_current_time(); groups_update_groupmeta($group_id, 'last_activity', $last_activity); return $group_id; }
function active_user_for_wps($user_id, $user_login, $user_password, $user_email, $usermeta) { global $bp, $wpdb; $user = null; if (defined('DOING_AJAX')) { return $user_id; } if (is_multisite()) { return $user_id; } //do not proceed for mu $signups = BP_Signup::get(array('user_login' => $user_login)); $signups = $signups['signups']; if (!$signups) { return false; } //if we are here, just popout the array $signup = array_pop($signups); // password is hashed again in wp_insert_user $password = wp_generate_password(12, false); $user_id = username_exists($signup->user_login); $key = $signup->activation_key; if (!$key) { $key = bp_get_user_meta($user_id, 'activation_key', true); } // Create the user if (!$user_id) { //this should almost never happen $user_id = wp_create_user($signup->user_login, $password, $signup->user_email); // If a user ID is found, this may be a legacy signup, or one // created locally for backward compatibility. Process it. } elseif ($key == wp_hash($user_id)) { // Change the user's status so they become active if (!$wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id))) { return new WP_Error('invalid_key', __('Invalid activation key', 'buddypress')); } bp_delete_user_meta($user_id, 'activation_key'); $member = get_userdata($user_id); $member->set_role(get_option('default_role')); $user_already_created = true; } else { $user_already_exists = true; } if (!$user_id) { return new WP_Error('create_user', __('Could not create user', 'buddypress'), $signup); } // Fetch the signup so we have the data later on $signups = BP_Signup::get(array('activation_key' => $key)); $signup = isset($signups['signups']) && !empty($signups['signups'][0]) ? $signups['signups'][0] : false; // Activate the signup BP_Signup::validate($key); if (isset($user_already_exists)) { return new WP_Error('user_already_exists', __('That username is already activated.', 'buddypress'), $signup); } // Set up data to pass to the legacy filter $user = array('user_id' => $user_id, 'password' => $signup->meta['password'], 'meta' => $signup->meta); // Notify the site admin of a new user registration wp_new_user_notification($user_id); wp_cache_delete('bp_total_member_count', 'bp'); /* Add a last active entry */ bp_update_user_last_activity($user_id); do_action('bp_core_activated_user', $user_id, $key, $user); bp_core_add_message(__('Your account is now active!')); $bp->activation_complete = true; xprofile_sync_wp_profile(); //$ud = get_userdata($signup['user_id']); self::login_redirect($user_login, $user_password); //will never reach here anyway return $user_id; }
/** * @group bp_update_user_last_activity */ public function test_bp_last_activity_multi_network() { // Filter the usermeta key add_filter('bp_get_user_meta_key', array($this, 'filter_usermeta_key')); // We explicitly do not want last_activity created, so use the // WP factory methods $user = $this->factory->user->create(); $time = date('Y-m-d H:i:s', time() - 50); // Update last user activity bp_update_user_last_activity($user, $time); // Setup parameters to assert to be the same $expected = $time; $found = bp_get_user_meta($user, 'last_activity', true); $this->assertSame($expected, $found); }
/** * Listener function for the logged-in user's 'last_activity' metadata. * * Many functions use a "last active" feature to show the length of time since * the user was last active. This function will update that time as a usermeta * setting for the user every 5 minutes while the user is actively browsing the * site. * * @uses bp_update_user_meta() BP function to update user metadata in the * usermeta table. * * @return bool|null Returns false if there is nothing to do. */ function bp_core_record_activity() { // Bail if user is not logged in if (!is_user_logged_in()) { return false; } // Get the user ID $user_id = bp_loggedin_user_id(); // Bail if user is not active if (bp_is_user_inactive($user_id)) { return false; } // Get the user's last activity $activity = bp_get_user_last_activity($user_id); // Make sure it's numeric if (!is_numeric($activity)) { $activity = strtotime($activity); } // Get current time $current_time = bp_core_current_time(); // Use this action to detect the very first activity for a given member if (empty($activity)) { /** * Fires inside the recording of an activity item. * * Use this action to detect the very first activity for a given member. * * @since BuddyPress (1.6.0) * * @param int $user_id ID of the user whose activity is recorded. */ do_action('bp_first_activity_for_member', $user_id); } // If it's been more than 5 minutes, record a newer last-activity time if (empty($activity) || strtotime($current_time) >= strtotime('+5 minutes', $activity)) { bp_update_user_last_activity($user_id, $current_time); } }
/** * When posting via email, we also update the last activity entries in BuddyPress. * * This is so your BuddyPress site doesn't look dormant when your members * are emailing each other back and forth! :) * * @param array $args Depending on the filter that this function is hooked into, contents will vary * @since 1.0-RC1 */ function bp_rbe_log_last_activity($args) { // get user id from activity entry if (!empty($args['user_id'])) { $user_id = $args['user_id']; } elseif (!empty($args['sender_id'])) { $user_id = $args['sender_id']; } else { $user_id = false; } // if no user ID, return now if (empty($user_id)) { return; } // update user's last activity bp_update_user_last_activity($user_id); // now update 'last_activity' group meta entry if applicable if (!empty($args['type'])) { switch ($args['type']) { case 'new_forum_topic': case 'new_forum_post': case 'bbp_topic_create': case 'bbp_reply_create': // sanity check! if (!bp_is_active('groups')) { return; } groups_update_last_activity($args['item_id']); break; // for group activity comments, we have to look up the parent activity to see // if the activity comment came from a group // for group activity comments, we have to look up the parent activity to see // if the activity comment came from a group case 'activity_comment': // we don't need to proceed if the groups component was disabled if (!bp_is_active('groups')) { return; } // sanity check! if (!bp_is_active('activity')) { return; } // grab the parent activity $activity = bp_activity_get_specific('activity_ids=' . $args['item_id']); if (!empty($activity['activities'][0])) { $parent_activity = $activity['activities'][0]; // if parent activity update is from the groups component, // that means the activity comment was in a group! // so update group 'last_activity' meta entry if ($parent_activity->component == 'groups') { groups_update_last_activity($parent_activity->item_id); } } break; } } }
/** * Importer engine - USERS */ function bpdd_import_users() { $users = array(); $users_data = (require_once dirname(__FILE__) . '/data/users.php'); foreach ($users_data as $user) { $user_id = wp_insert_user(array('user_login' => $user['login'], 'user_pass' => $user['pass'], 'display_name' => $user['display_name'], 'user_email' => $user['email'], 'user_registered' => bpdd_get_random_date(45, 1))); xprofile_set_field_data(1, $user_id, $user['display_name']); $name = explode(' ', $user['display_name']); update_user_meta($user_id, 'first_name', $name[0]); update_user_meta($user_id, 'last_name', isset($name[1]) ? $name[1] : ''); // BuddyPress 1.9+ if (function_exists('bp_update_user_last_activity')) { bp_update_user_last_activity($user_id, bpdd_get_random_date(5)); } else { // BuddyPress 1.8.x and below bp_update_user_meta($user_id, 'last_activity', bpdd_get_random_date(5)); } bp_update_user_meta($user_id, 'notification_messages_new_message', 'no'); $users[] = $user_id; } return $users; }
/** * Saves a notice. * * @since BuddyPress (1.0.0) * * @return bool */ public function save() { global $wpdb, $bp; $this->subject = apply_filters('messages_notice_subject_before_save', $this->subject, $this->id); $this->message = apply_filters('messages_notice_message_before_save', $this->message, $this->id); do_action_ref_array('messages_notice_before_save', array(&$this)); if (empty($this->id)) { $sql = $wpdb->prepare("INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, %s, %d)", $this->subject, $this->message, $this->date_sent, $this->is_active); } else { $sql = $wpdb->prepare("UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id); } if (!$wpdb->query($sql)) { return false; } if (!($id = $this->id)) { $id = $wpdb->insert_id; } // Now deactivate all notices apart from the new one. $wpdb->query($wpdb->prepare("UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id)); bp_update_user_last_activity(bp_loggedin_user_id(), bp_core_current_time()); do_action_ref_array('messages_notice_after_save', array(&$this)); return true; }
/** * Create a new deposit activity record. */ function humcore_new_deposit_activity($deposit_id, $deposit_content = '', $deposit_link = '') { if (!bp_is_active('activity')) { return false; } $bp = buddypress(); $user_id = bp_loggedin_user_id(); $userlink = bp_core_get_userlink($user_id); $activity_ID = bp_activity_add(array('user_id' => $user_id, 'secondary_item_id' => $deposit_id, 'action' => '', 'component' => $bp->humcore_deposits->id, 'content' => $deposit_content, 'primary_link' => $deposit_link, 'type' => 'new_deposit')); // Update the last activity date of the members or committee. $post_metadata = json_decode(get_post_meta($deposit_id, '_deposit_metadata', true), true); if (!empty($post_metadata['committee_id'])) { groups_update_last_activity($post_metadata['committee_id']); } else { bp_update_user_last_activity($user_id); } return $activity_ID; }