/**
 * Add an activity item when a user has updated his profile.
 *
 * @since BuddyPress (2.0.0)
 *
 * @param int $user_id ID of the user who has updated his profile.
 * @param array $field_ids IDs of the fields submitted.
 * @param bool $errors True if validation or saving errors occurred, otherwise
 *        false.
 * @param array $old_values Pre-save xprofile field values and visibility
 *        levels.
 * @param array $new_values Post-save xprofile field values and visibility
 *        levels.
 * @return bool True on success, false on failure.
 */
function bp_xprofile_updated_profile_activity($user_id, $field_ids, $errors, $old_values = array(), $new_values = array())
{
    // If there were errors, don't post
    if (!empty($errors)) {
        return false;
    }
    if (!bp_is_active('activity')) {
        return false;
    }
    // Don't post if there have been no changes, or if the changes are
    // related solely to non-public fields
    $public_changes = false;
    foreach ($new_values as $field_id => $new_value) {
        $old_value = isset($old_values[$field_id]) ? $old_values[$field_id] : '';
        $old_value_value = isset($old_value['value']) ? $old_value['value'] : '';
        $old_value_visibility = isset($old_value['visibility']) ? $old_value['visibility'] : '';
        // Don't register changes to private fields
        if ('public' !== $new_value['visibility']) {
            continue;
        }
        // Don't register if there have been no changes
        if ($new_value === $old_value) {
            continue;
        }
        // Looks like we have public changes - no need to keep checking
        $public_changes = true;
        break;
    }
    if (!$public_changes) {
        return false;
    }
    // Throttle to one activity of this type per 2 hours
    $existing = bp_activity_get(array('max' => 1, 'filter' => array('user_id' => $user_id, 'object' => buddypress()->profile->id, 'action' => 'updated_profile')));
    if (empty($existing['activities'])) {
        $throttle = false;
    } else {
        // Default throttle time is 2 hours. Filter to change (in seconds)
        $throttle_period = apply_filters('bp_xprofile_updated_profile_activity_throttle_time', 60 * 60 * 2);
        $then = strtotime($existing['activities'][0]->date_recorded);
        $now = strtotime(bp_core_current_time());
        $throttle = $now - $then < $throttle_period;
    }
    if ($throttle) {
        return false;
    }
    // If we've reached this point, assemble and post the activity item
    $profile_link = trailingslashit(bp_core_get_user_domain($user_id) . buddypress()->profile->slug);
    $retval = xprofile_record_activity(array('user_id' => $user_id, 'primary_link' => $profile_link, 'component' => buddypress()->profile->id, 'type' => 'updated_profile'));
    return (bool) $retval;
}
/**
 * Add an activity item when a user has updated his profile.
 *
 * @since 2.0.0
 *
 * @param int   $user_id    ID of the user who has updated his profile.
 * @param array $field_ids  IDs of the fields submitted.
 * @param bool  $errors     True if validation or saving errors occurred, otherwise false.
 * @param array $old_values Pre-save xprofile field values and visibility levels.
 * @param array $new_values Post-save xprofile field values and visibility levels.
 *
 * @return bool True on success, false on failure.
 */
function bp_xprofile_updated_profile_activity($user_id, $field_ids = array(), $errors = false, $old_values = array(), $new_values = array())
{
    // If there were errors, don't post.
    if (!empty($errors)) {
        return false;
    }
    // Bail if activity component is not active.
    if (!bp_is_active('activity')) {
        return false;
    }
    // Don't post if there have been no changes, or if the changes are
    // related solely to non-public fields.
    $public_changes = false;
    foreach ($new_values as $field_id => $new_value) {
        $old_value = isset($old_values[$field_id]) ? $old_values[$field_id] : '';
        // Don't register changes to private fields.
        if (empty($new_value['visibility']) || 'public' !== $new_value['visibility']) {
            continue;
        }
        // Don't register if there have been no changes.
        if ($new_value === $old_value) {
            continue;
        }
        // Looks like we have public changes - no need to keep checking.
        $public_changes = true;
        break;
    }
    // Bail if no public changes.
    if (empty($public_changes)) {
        return false;
    }
    // Throttle to one activity of this type per 2 hours.
    $existing = bp_activity_get(array('max' => 1, 'filter' => array('user_id' => $user_id, 'object' => buddypress()->profile->id, 'action' => 'updated_profile')));
    // Default throttle time is 2 hours. Filter to change (in seconds).
    if (!empty($existing['activities'])) {
        /**
         * Filters the throttle time, in seconds, used to prevent excessive activity posting.
         *
         * @since 2.0.0
         *
         * @param int $value Throttle time, in seconds.
         */
        $throttle_period = apply_filters('bp_xprofile_updated_profile_activity_throttle_time', HOUR_IN_SECONDS * 2);
        $then = strtotime($existing['activities'][0]->date_recorded);
        $now = strtotime(bp_core_current_time());
        // Bail if throttled.
        if ($now - $then < $throttle_period) {
            return false;
        }
    }
    // If we've reached this point, assemble and post the activity item.
    $profile_link = trailingslashit(bp_core_get_user_domain($user_id) . bp_get_profile_slug());
    return (bool) xprofile_record_activity(array('user_id' => $user_id, 'primary_link' => $profile_link, 'component' => buddypress()->profile->id, 'type' => 'updated_profile'));
}
Example #3
0
/**
 * xprofile_edit()
 *
 * Renders the edit form for the profile fields within a group as well as
 * handling the save action.
 *
 * [NOTE] This is old code that was written when editing was not done in the theme.
 * It is big and clunky and will be broken up in future versions.
 * 
 * @package BuddyPress XProfile
 * @param $group_id The ID of the group of fields to edit
 * @param $action The HTML form action
 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
 * @global $wpdb WordPress DB access object.
 * @global $userdata WordPress global object containing current logged in user userdata
 */
function xprofile_edit($group_id, $action)
{
    global $wpdb, $userdata, $bp;
    // Create a new group object based on the group ID.
    $group = new BP_XProfile_Group($group_id);
    ?>
	<div class="wrap">
		
		<h2><?php 
    echo attribute_escape($group->name);
    ?>
 <?php 
    _e("Information", 'buddypress');
    ?>
</h2>
		
		<?php 
    // If this group has fields then continue
    if ($group->fields) {
        $errors = null;
        $list_html = '<ul class="forTab" id="' . strtolower($group_name) . '">';
        // Loop through each field in the group
        for ($j = 0; $j < count($group->fields); $j++) {
            // Create a new field object for this field based on the field ID.
            $field = new BP_XProfile_Field($group->fields[$j]->id);
            // Add the ID for this field to the field_ids array
            $field_ids[] = $group->fields[$j]->id;
            // If the user has submitted the form - validate and save the new value for this field
            if (isset($_GET['mode']) && 'save' == $_GET['mode']) {
                /* Check the nonce */
                if (!check_admin_referer('bp_xprofile_edit')) {
                    return false;
                }
                // If the current field is a datebox, we need to append '_day' to the end of the field name
                // otherwise the field name will not exist
                $post_field_string = 'datebox' == $group->fields[$j]->type ? '_day' : null;
                // Explode the posted field IDs into an array so we know which fields have been submitted
                $posted_fields = explode(',', $_POST['field_ids']);
                // Fetch the current field from the _POST array based on field ID.
                $current_field = $_POST['field_' . $posted_fields[$j] . $post_field_string];
                // If the field is required and has been left blank then we need to add a callback error.
                if ($field->is_required && !isset($current_field) || $field->is_required && empty($current_field)) {
                    // Add the error message to the errors array
                    $field->message = sprintf(__('%s cannot be left blank.', 'buddypress'), $field->name);
                    $errors[] = $field->message . "<br />";
                    // If the field is not required and the field has been left blank, delete any values for the
                    // field from the database.
                } else {
                    if (!$field->is_required && (empty($current_field) || is_null($current_field))) {
                        // Create a new profile data object for the logged in user based on field ID.
                        $profile_data = new BP_Xprofile_ProfileData($group->fields[$j]->id, $bp->loggedin_user->id);
                        if ($profile_data) {
                            // Delete any data
                            $profile_data->delete();
                            // Also remove any selected profile field data from the $field object.
                            $field->data->value = null;
                        }
                        // If we get to this point then the field validates ok and we have new data.
                    } else {
                        // Create an empty profile data object and populate it with new data
                        $profile_data = new BP_Xprofile_ProfileData();
                        $profile_data->field_id = $group->fields[$j]->id;
                        $profile_data->user_id = $userdata->ID;
                        $profile_data->last_updated = time();
                        // If the $post_field_string we set up earlier is not null, then this is a datebox
                        // we need to concatenate each of the three select boxes for day, month and year into
                        // one value.
                        if ($post_field_string != null) {
                            // Concatenate the values.
                            $date_value = $_POST['field_' . $group->fields[$j]->id . '_day'] . $_POST['field_' . $group->fields[$j]->id . '_month'] . $_POST['field_' . $group->fields[$j]->id . '_year'];
                            // Turn the concatenated value into a timestamp
                            $profile_data->value = strtotime($date_value);
                        } else {
                            // Checkbox and multi select box fields will submit an array as their value
                            // so we need to serialize them before saving to the DB.
                            if (is_array($current_field)) {
                                $current_field = serialize($current_field);
                            }
                            $profile_data->value = $current_field;
                        }
                        // Finally save the value to the database.
                        if (!$profile_data->save()) {
                            $field->message = __('There was a problem saving changes to this field, please try again.', 'buddypress');
                        } else {
                            $field->data->value = $profile_data->value;
                        }
                    }
                }
            }
            // Each field object comes with HTML that can be rendered to edit that field.
            // We just need to render that to the page by adding it to the $list_html variable
            // that will be rendered when the field loop has finished.
            $list_html .= '<li>' . $field->get_edit_html() . '</li>';
        }
        // Now that the loop has finished put the final touches on the HTML including the submit button.
        $list_html .= '</ul>';
        $list_html .= '<p class="submit">
								<input type="submit" name="save" id="save" value="' . __('Save Changes &raquo;', 'buddypress') . '" />
							   </p>';
        $list_html .= wp_nonce_field('bp_xprofile_edit');
        // If the user submitted the form to save new values, and there were errors, make sure we display them.
        if ($errors && isset($_POST['save'])) {
            $type = 'error';
            $message = __('There were problems saving your information. Please fix the following:<br />', 'buddypress');
            for ($i = 0; $i < count($errors); $i++) {
                $message .= $errors[$i];
            }
            // If there were no errors then we can display a nice "Changes saved." message.
        } else {
            if (!$errors && isset($_POST['save'])) {
                $type = 'success';
                $message = __('Changes saved.', 'buddypress');
                // Record in activity stream
                xprofile_record_activity(array('item_id' => $group->id, 'component_name' => 'profile', 'component_action' => 'updated_profile', 'is_private' => 0));
                do_action('xprofile_updated_profile', $group->id);
            }
        }
    } else {
        ?>
				<div id="message" class="error fade">
					<p><?php 
        _e('That group does not exist.', 'buddypress');
        ?>
</p>
				</div>
			<?php 
    }
    ?>
		
		<?php 
    // Finally, we can now render everything to the screen.
    ?>
		
		<?php 
    if ($message != '') {
        $type = 'error' == $type ? 'error' : 'updated';
        ?>
			<div id="message" class="<?php 
        echo $type;
        ?>
 fade">
				<p><?php 
        echo $message;
        ?>
</p>
			</div>
		<?php 
    }
    ?>

		<p><form action="<?php 
    echo $action;
    ?>
" method="post" id="profile-edit-form" class="generic-form">
		<?php 
    if ($field_ids) {
        $field_ids = implode(",", $field_ids);
    }
    ?>
		<input type="hidden" name="field_ids" id="field_ids" value="<?php 
    echo attribute_escape($field_ids);
    ?>
" />
		
		<?php 
    echo $list_html;
    ?>

		</form>
		</p>
		
	</div> 
<?php 
}