/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled separately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. * */ public function edit_field_options_html(array $args = array()) { $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values = (array) $_POST['field_' . $this->field_obj->id]; $option_values = array_map('sanitize_text_field', $option_values); } else { $option_values = (array) $original_option_values; } //member types list as array $options = self::get_roles(); $selected = ''; //$option_values = (array) $original_option_values; if (empty($option_values) || in_array('none', $option_values)) { $selected = ' selected="selected"'; } $html = '<option value="" ' . $selected . ' >----' . '</option>'; echo $html; foreach ($options as $role => $label) { $selected = ''; // Run the allowed option name through the before_save filter, so we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($role, false, false); // First, check to see whether the user-entered value matches if (in_array($allowed_options, (array) $option_values)) { $selected = ' selected="selected"'; } echo apply_filters('bp_get_the_profile_field_options_roles', '<option' . $selected . ' value="' . esc_attr(stripslashes($role)) . '">' . $label . '</option>', $role, $this->field_obj->id, $selected); } }
/** * profile_screen_admin() * * Filters option for 'Teacher', only admins are allowed to access it. */ function profile_screen_admin($options) { for ($i = 0; $i < count($options); $i++) { if (!is_super_admin() && $options[$i]->name == __('Teacher', 'bpsp') && BP_XProfile_ProfileData::get_value_byid($options[$i]->parent_id) != __('Teacher', 'bpsp')) { unset($options[$i]); } } return array_merge($options); }
/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled separately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. * */ public function edit_field_options_html(array $args = array()) { $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values = (array) $_POST['field_' . $this->field_obj->id]; $option_values = array_map('sanitize_text_field', $option_values); } else { $option_values = (array) $original_option_values; } //member types list as array $options = self::get_member_types(); //$option_values = (array) $original_option_values; if ($this->display_as_radio()) { $this->_edit_options_html_radio($option_values, $options); } else { $this->_edit_options_html($option_values, $options); } }
/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled separately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. * @since 2.0.0 */ public function edit_field_options_html(array $args = array()) { $option_value = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']); $options = $this->field_obj->get_children(); $html = sprintf('<div id="%s">', esc_attr('field_' . $this->field_obj->id)); for ($k = 0, $count = count($options); $k < $count; ++$k) { // Check for updated posted values, but errors preventing them from // being saved first time if (isset($_POST['field_' . $this->field_obj->id]) && $option_value != $_POST['field_' . $this->field_obj->id]) { if (!empty($_POST['field_' . $this->field_obj->id])) { $option_value = sanitize_text_field($_POST['field_' . $this->field_obj->id]); } } // Run the allowed option name through the before_save filter, so // we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); $selected = ''; if ($option_value === $allowed_options || empty($option_value) && !empty($options[$k]->is_default_option)) { $selected = ' checked="checked"'; } $new_html = sprintf('<label><input %1$s type="radio" name="%2$s" id="%3$s" value="%4$s">%5$s</label>', $selected, esc_attr("field_{$this->field_obj->id}"), esc_attr("option_{$options[$k]->id}"), esc_attr(stripslashes($options[$k]->name)), esc_html(stripslashes($options[$k]->name))); /** * Filters the HTML output for an individual field options radio button. * * @since 1.1.0 * * @param string $new_html Label and radio input field. * @param object $value Current option being rendered for. * @param int $id ID of the field object being rendered. * @param string $selected Current selected value. * @param string $k Current index in the foreach loop. */ $html .= apply_filters('bp_get_the_profile_field_options_radio', $new_html, $options[$k], $this->field_obj->id, $selected, $k); } echo $html . '</div>'; }
/** * bp_get_the_profile_field_options() * * Retrieves field options HTML for field types of 'selectbox', 'multiselectbox', * 'radio', 'checkbox', and 'datebox'. * * @package BuddyPress Xprofile * @since 1.1 * * @uses BP_XProfile_Field::get_children() * @uses BP_XProfile_ProfileData::get_value_byid() * * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'. */ function bp_get_the_profile_field_options($args = '') { global $field; $defaults = array('type' => false); $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); if (!method_exists($field, 'get_children')) { $field = new BP_XProfile_Field($field->id); } $options = $field->get_children(); // Setup some defaults $html = ''; $selected = ''; switch ($field->type) { case 'selectbox': if (!$field->is_required) { $html .= '<option value="">' . __('----', 'buddypress') . '</option>'; } $original_option_values = ''; $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field->id)); if (empty($original_option_values) && !empty($_POST['field_' . $field->id])) { $original_option_values = $_POST['field_' . $field->id]; } $option_values = (array) $original_option_values; for ($k = 0, $count = count($options); $k < $count; ++$k) { // Check for updated posted values, but errors preventing them from being saved first time foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $field->id]) && $_POST['field_' . $field->id] != $option_value) { if (!empty($_POST['field_' . $field->id])) { $option_values[$i] = $_POST['field_' . $field->id]; } } } $selected = ''; // Run the allowed option name through the before_save filter, so we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); // First, check to see whether the user-entered value matches if (in_array($allowed_options, (array) $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults if (!is_array($original_option_values) && empty($option_values) && $options[$k]->is_default_option) { $selected = ' selected="selected"'; } $html .= apply_filters('bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_attr(stripslashes($options[$k]->name)) . '</option>', $options[$k], $field->id, $selected, $k); } break; case 'multiselectbox': $original_option_values = ''; $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field->id)); if (empty($original_option_values) && !empty($_POST['field_' . $field->id])) { $original_option_values = $_POST['field_' . $field->id]; } $option_values = (array) $original_option_values; for ($k = 0, $count = count($options); $k < $count; ++$k) { // Check for updated posted values, but errors preventing them from being saved first time foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $field->id]) && $_POST['field_' . $field->id][$i] != $option_value) { if (!empty($_POST['field_' . $field->id][$i])) { $option_values[] = $_POST['field_' . $field->id][$i]; } } } $selected = ''; // Run the allowed option name through the before_save filter, so we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); // First, check to see whether the user-entered value matches if (in_array($allowed_options, (array) $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults if (!is_array($original_option_values) && empty($option_values) && $options[$k]->is_default_option) { $selected = ' selected="selected"'; } $html .= apply_filters('bp_get_the_profile_field_options_multiselect', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_attr(stripslashes($options[$k]->name)) . '</option>', $options[$k], $field->id, $selected, $k); } break; case 'radio': $html .= '<div id="field_' . $field->id . '">'; $option_value = BP_XProfile_ProfileData::get_value_byid($field->id); for ($k = 0, $count = count($options); $k < $count; ++$k) { // Check for updated posted values, but errors preventing them from being saved first time if (isset($_POST['field_' . $field->id]) && $option_value != $_POST['field_' . $field->id]) { if (!empty($_POST['field_' . $field->id])) { $option_value = $_POST['field_' . $field->id]; } } // Run the allowed option name through the before_save // filter, so we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); $selected = ''; if ($option_value == $allowed_options || !empty($value) && $value == $allowed_options || empty($option_value) && $options[$k]->is_default_option) { $selected = ' checked="checked"'; } $html .= apply_filters('bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . esc_attr(stripslashes($options[$k]->name)) . '"> ' . esc_attr(stripslashes($options[$k]->name)) . '</label>', $options[$k], $field->id, $selected, $k); } $html .= '</div>'; break; case 'checkbox': $option_values = BP_XProfile_ProfileData::get_value_byid($field->id); $option_values = maybe_unserialize($option_values); // Check for updated posted values, but errors preventing them from being saved first time if (isset($_POST['field_' . $field->id]) && $option_values != maybe_serialize($_POST['field_' . $field->id])) { if (!empty($_POST['field_' . $field->id])) { $option_values = $_POST['field_' . $field->id]; } } for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // First, check to see whether the user's saved values // match the option for ($j = 0, $count_values = count($option_values); $j < $count_values; ++$j) { // Run the allowed option name through the // before_save filter, so we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); if ($option_values[$j] == $allowed_options || @in_array($allowed_options, $value)) { $selected = ' checked="checked"'; break; } } // If the user has not yet supplied a value for this field, // check to see whether there is a default value available if (!is_array($option_values) && empty($option_values) && !$selected && $options[$k]->is_default_option) { $selected = ' checked="checked"'; } $html .= apply_filters('bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . esc_attr(stripslashes($options[$k]->name)) . '"> ' . esc_attr(stripslashes($options[$k]->name)) . '</label>', $options[$k], $field->id, $selected, $k); } break; case 'datebox': $date = BP_XProfile_ProfileData::get_value_byid($field->id); // Set day, month, year defaults $day = ''; $month = ''; $year = ''; if (!empty($date)) { // If Unix timestamp if (is_numeric($date)) { $day = date('j', $date); $month = date('F', $date); $year = date('Y', $date); // If MySQL timestamp } else { $day = mysql2date('j', $date); $month = mysql2date('F', $date, false); // Not localized, so that selected() works below $year = mysql2date('Y', $date); } } // Check for updated posted values, but errors preventing them from being saved first time if (!empty($_POST['field_' . $field->id . '_day'])) { if ($day != $_POST['field_' . $field->id . '_day']) { $day = $_POST['field_' . $field->id . '_day']; } } if (!empty($_POST['field_' . $field->id . '_month'])) { if ($month != $_POST['field_' . $field->id . '_month']) { $month = $_POST['field_' . $field->id . '_month']; } } if (!empty($_POST['field_' . $field->id . '_year'])) { if ($year != date("j", $_POST['field_' . $field->id . '_year'])) { $year = $_POST['field_' . $field->id . '_year']; } } switch ($type) { case 'day': $html .= '<option value=""' . selected($day, '', false) . '>--</option>'; for ($i = 1; $i < 32; ++$i) { $html .= '<option value="' . $i . '"' . selected($day, $i, false) . '>' . $i . '</option>'; } break; case 'month': $eng_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $months = array(__('January', 'buddypress'), __('February', 'buddypress'), __('March', 'buddypress'), __('April', 'buddypress'), __('May', 'buddypress'), __('June', 'buddypress'), __('July', 'buddypress'), __('August', 'buddypress'), __('September', 'buddypress'), __('October', 'buddypress'), __('November', 'buddypress'), __('December', 'buddypress')); $html .= '<option value=""' . selected($month, '', false) . '>------</option>'; for ($i = 0; $i < 12; ++$i) { $html .= '<option value="' . $eng_months[$i] . '"' . selected($month, $eng_months[$i], false) . '>' . $months[$i] . '</option>'; } break; case 'year': $html .= '<option value=""' . selected($year, '', false) . '>----</option>'; for ($i = 2037; $i > 1901; $i--) { $html .= '<option value="' . $i . '"' . selected($year, $i, false) . '>' . $i . '</option>'; } break; } $html = apply_filters('bp_get_the_profile_field_datebox', $html, $type, $day, $month, $year, $field->id, $date); break; } return $html; }
function xprofile_filter_comments($comments, $post_id) { foreach ((array) $comments as $comment) { if ($comment->user_id) { $user_ids[] = $comment->user_id; } } if (empty($user_ids)) { return $comments; } if ($fullnames = BP_XProfile_ProfileData::get_value_byid(1, $user_ids)) { foreach ((array) $fullnames as $user) { $users[$user->user_id] = trim($user->value); } } foreach ((array) $comments as $i => $comment) { if (!empty($comment->user_id)) { if (!empty($users[$comment->user_id])) { $comments[$i]->comment_author = $users[$comment->user_id]; } } } return $comments; }
/** * xprofile_get_field_data() * * Fetches profile data for a specific field for the user. * * @package BuddyPress Core * @param $field The ID of the field, or the $name of the field. * @param $user_id The ID of the user * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals() * @uses BP_XProfile_ProfileData::get_value_byfieldname() Fetches the value based on the params passed. * @return The profile field data. */ function xprofile_get_field_data( $field, $user_id = null ) { global $bp; if ( !$user_id ) $user_id = $bp->displayed_user->id; if ( !$user_id ) return false; if ( is_numeric( $field ) ) $field_id = $field; else $field_id = xprofile_get_field_id_from_name( $field ); if ( !$field_id ) return false; return apply_filters( 'xprofile_get_field_data', BP_XProfile_ProfileData::get_value_byid( $field_id, $user_id ) ); }
/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled seperately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. * @since BuddyPress (2.0.0) */ public function edit_field_options_html(array $args = array()) { $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); $options = $this->field_obj->get_children(); $html = '<option value="">' . esc_html__('----', 'buddypress') . '</option>'; if (empty($original_option_values) && !empty($_POST['field_' . $this->field_obj->id])) { $original_option_values = sanitize_text_field($_POST['field_' . $this->field_obj->id]); } $option_values = (array) $original_option_values; for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // Check for updated posted values, but errors preventing them from being saved first time foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $this->field_obj->id]) && $_POST['field_' . $this->field_obj->id] != $option_value) { if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values[$i] = sanitize_text_field($_POST['field_' . $this->field_obj->id]); } } } // Run the allowed option name through the before_save filter, so we'll be sure to get a match $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); // First, check to see whether the user-entered value matches if (in_array($allowed_options, $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults if (!is_array($original_option_values) && empty($option_values) && $options[$k]->is_default_option) { $selected = ' selected="selected"'; } $html .= apply_filters('bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_html(stripslashes($options[$k]->name)) . '</option>', $options[$k], $this->field_obj->id, $selected, $k); } echo $html; }
/** * Fetch the display name for a group of users. * * Uses the 'Name' field in xprofile if available. Falls back on WP * display_name, and then user_nicename. * * @since 2.0.0 * * @param array $user_ids Array of user IDs to get display names for. * @return array */ function bp_core_get_user_displaynames($user_ids) { // Sanitize. $user_ids = wp_parse_id_list($user_ids); // Remove dupes and empties. $user_ids = array_unique(array_filter($user_ids)); if (empty($user_ids)) { return array(); } $uncached_ids = array(); foreach ($user_ids as $user_id) { if (false === wp_cache_get('bp_user_fullname_' . $user_id, 'bp')) { $uncached_ids[] = $user_id; } } // Prime caches. if (!empty($uncached_ids)) { if (bp_is_active('xprofile')) { $fullname_data = BP_XProfile_ProfileData::get_value_byid(1, $uncached_ids); // Key by user_id. $fullnames = array(); foreach ($fullname_data as $fd) { if (!empty($fd->value)) { $fullnames[intval($fd->user_id)] = $fd->value; } } // If xprofiledata is not found for any users, we'll look // them up separately. $no_xprofile_ids = array_diff($uncached_ids, array_keys($fullnames)); } else { $fullnames = array(); $no_xprofile_ids = $user_ids; } if (!empty($no_xprofile_ids)) { // Use WP_User_Query because we don't need BP information. $query = new WP_User_Query(array('include' => $no_xprofile_ids, 'fields' => array('ID', 'user_nicename', 'display_name'), 'count_total' => false, 'blog_id' => 0)); foreach ($query->results as $qr) { $fullnames[$qr->ID] = !empty($qr->display_name) ? $qr->display_name : $qr->user_nicename; // If xprofile is active, set this value as the // xprofile display name as well. if (bp_is_active('xprofile')) { xprofile_set_field_data(1, $qr->ID, $fullnames[$qr->ID]); } } } foreach ($fullnames as $fuser_id => $fname) { wp_cache_set('bp_user_fullname_' . $fuser_id, $fname, 'bp'); } } $retval = array(); foreach ($user_ids as $user_id) { $retval[$user_id] = wp_cache_get('bp_user_fullname_' . $user_id, 'bp'); } return $retval; }
/** * Fetches profile data for a specific field for the user. * * When the field value is serialized, this function unserializes and filters each item in the array * that results. * * @package BuddyPress Core * @param mixed $field The ID of the field, or the $name of the field. * @param int $user_id The ID of the user * @global object $bp Global BuddyPress settings object * @uses BP_XProfile_ProfileData::get_value_byid() Fetches the value based on the params passed. * @return mixed The profile field data. */ function xprofile_get_field_data($field, $user_id = 0) { global $bp; if (empty($user_id)) { $user_id = $bp->displayed_user->id; } if (empty($user_id)) { return false; } if (is_numeric($field)) { $field_id = $field; } else { $field_id = xprofile_get_field_id_from_name($field); } if (empty($field_id)) { return false; } $values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field_id, $user_id)); if (is_array($values)) { $data = array(); foreach ((array) $values as $value) { $data[] = apply_filters('xprofile_get_field_data', $value, $field_id, $user_id); } } else { $data = apply_filters('xprofile_get_field_data', $values, $field_id, $user_id); } return $data; }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $checkbox_acceptance = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); if (!empty($_POST['field_' . $this->field_obj->id])) { $new_checkbox_acceptance = $_POST['field_' . $this->field_obj->id]; $checkbox_acceptance = $checkbox_acceptance != $new_checkbox_acceptance ? $new_checkbox_acceptance : $checkbox_acceptance; } $html = '<input type="checkbox" name="check_acc_' . bp_get_the_profile_field_input_name() . '" id="check_acc_' . bp_get_the_profile_field_input_name() . '"'; if ($checkbox_acceptance == 1) { $html .= ' checked="checked"'; } if (isset($args['required']) && $args['required']) { $html .= ' required="required" aria-required="true"'; } $html .= ' value="1" /> '; $html .= '<input type="hidden" name="' . bp_get_the_profile_field_input_name() . '" id="' . bp_get_the_profile_field_input_name() . '"'; if ($checkbox_acceptance == 1) { $html .= ' value="1" /> '; } else { $html .= ' value="0" /> '; } if ($options) { foreach ($options as $option) { $html .= rawurldecode($option->name); } } // Javascript. $html .= ' <script> jQuery(document).ready(function() { jQuery("#check_acc_' . bp_get_the_profile_field_input_name() . '").click(function() { if (jQuery(this).is(":checked")) { jQuery("#' . bp_get_the_profile_field_input_name() . '").val("1"); } else { jQuery("#' . bp_get_the_profile_field_input_name() . '").val("0"); } }); }); </script> '; echo apply_filters('bp_get_the_profile_field_checkbox_acceptance', $html, $args['type'], $this->field_obj->id, $checkbox_acceptance); }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $posts_selected = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); $html = ''; if ($options) { $post_type_selected = $options[0]->name; if (!empty($_POST['field_' . $this->field_obj->id])) { $new_posts_selected = $_POST['field_' . $this->field_obj->id]; $posts_selected = $posts_selected != $new_posts_selected ? $new_posts_selected : $posts_selected; } // Get posts of custom post type selected. $posts = new WP_Query(array('posts_per_page' => -1, 'post_type' => $post_type_selected, 'orderby' => 'title', 'order' => 'ASC')); if ($posts) { foreach ($posts->posts as $post) { $html .= sprintf('<option value="%s"%s>%s</option>', $post->ID, !empty($posts_selected) && in_array($post->ID, $posts_selected) ? 'selected="selected"' : '', $post->post_title); } } } echo apply_filters('bp_get_the_profile_field_multiselect_custom_post_type', $html, $args['type'], $post_type_selected, $this->field_obj->id); }
function bp_get_the_profile_field_options( $args = '' ) { global $field; $defaults = array( 'type' => false ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( !method_exists( $field, 'get_children' ) ) $field = new BP_XProfile_Field( $field->id ); $options = $field->get_children(); switch ( $field->type ) { case 'selectbox': case 'multiselectbox': if ( 'multiselectbox' != $field->type ) $html .= '<option value="">--------</option>'; for ( $k = 0; $k < count($options); $k++ ) { $option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $options[$k]->parent_id ) ); $option_values = (array)$option_values; /* Check for updated posted values, but errors preventing them from being saved first time */ foreach( (array)$option_values as $i => $option_value ) { if ( isset( $_POST['field_' . $field->id] ) && $_POST['field_' . $field->id] != $option_value ) { if ( !empty( $_POST['field_' . $field->id] ) ) $option_values[$i] = $_POST['field_' . $field->id]; } } if ( in_array( $options[$k]->name, (array)$option_values ) || $options[$k]->is_default_option ) { $selected = ' selected="selected"'; } else { $selected = ''; } $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '">' . stripslashes( esc_attr( $options[$k]->name ) ) . '</option>', $options[$k] ); } break; case 'radio': $html = '<div id="field_' . $field->id . '">'; for ( $k = 0; $k < count($options); $k++ ) { $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); /* Check for updated posted values, but errors preventing them from being saved first time */ if ( isset( $_POST['field_' . $field->id] ) && $option_value != $_POST['field_' . $field->id] ) { if ( !empty( $_POST['field_' . $field->id] ) ) $option_value = $_POST['field_' . $field->id]; } if ( $option_value == $options[$k]->name || $value == $options[$k]->name || ( empty( $option_value ) && $options[$k]->is_default_option ) ) { $selected = ' checked="checked"'; } else { $selected = ''; } $html .= apply_filters( 'bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] ); } $html .= '</div>'; break; case 'checkbox': $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); /* Check for updated posted values, but errors preventing them from being saved first time */ if ( isset( $_POST['field_' . $field->id] ) && $option_values != maybe_serialize( $_POST['field_' . $field->id] ) ) { if ( !empty( $_POST['field_' . $field->id] ) ) $option_values = $_POST['field_' . $field->id]; } $option_values = maybe_unserialize($option_values); for ( $k = 0; $k < count($options); $k++ ) { for ( $j = 0; $j < count($option_values); $j++ ) { if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) || $options[$k]->is_default_option ) { $selected = ' checked="checked"'; break; } } $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] ); $selected = ''; } break; case 'datebox': if ( !empty( $field->data->value ) ) { $day = date("j", $field->data->value); $month = date("F", $field->data->value); $year = date("Y", $field->data->value); $default_select = ' selected="selected"'; } /* Check for updated posted values, but errors preventing them from being saved first time */ if ( !empty( $_POST['field_' . $field->id . '_day'] ) ) { if ( $day != $_POST['field_' . $field->id . '_day'] ) $day = $_POST['field_' . $field->id . '_day']; } if ( !empty( $_POST['field_' . $field->id . '_month'] ) ) { if ( $month != $_POST['field_' . $field->id . '_month'] ) $month = $_POST['field_' . $field->id . '_month']; } if ( !empty( $_POST['field_' . $field->id . '_year'] ) ) { if ( $year != date( "j", $_POST['field_' . $field->id . '_year'] ) ) $year = $_POST['field_' . $field->id . '_year']; } switch ( $type ) { case 'day': $html .= '<option value=""' . esc_attr( $default_select ) . '>--</option>'; for ( $i = 1; $i < 32; $i++ ) { if ( $day == $i ) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>'; } break; case 'month': $eng_months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); $months = array( __( 'January', 'buddypress' ), __( 'February', 'buddypress' ), __( 'March', 'buddypress' ), __( 'April', 'buddypress' ), __( 'May', 'buddypress' ), __( 'June', 'buddypress' ), __( 'July', 'buddypress' ), __( 'August', 'buddypress' ), __( 'September', 'buddypress' ), __( 'October', 'buddypress' ), __( 'November', 'buddypress' ), __( 'December', 'buddypress' ) ); $html .= '<option value=""' . esc_attr( $default_select ) . '>------</option>'; for ( $i = 0; $i < 12; $i++ ) { if ( $month == $eng_months[$i] ) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $eng_months[$i] . '"' . $selected . '>' . $months[$i] . '</option>'; } break; case 'year': $html .= '<option value=""' . esc_attr( $default_select ) . '>----</option>'; for ( $i = date( 'Y', time() ); $i > 1899; $i-- ) { if ( $year == $i ) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>'; } break; } apply_filters( 'bp_get_the_profile_field_datebox', $html, $day, $month, $year, $default_select ); break; } return $html; }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $date = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']); $day = 0; $month = 0; $year = 0; $html = ''; // Set day, month, year defaults if (!empty($date)) { // If Unix timestamp if (is_numeric($date)) { $day = date('j', $date); $month = date('F', $date); $year = date('Y', $date); // If MySQL timestamp } else { $day = mysql2date('j', $date); $month = mysql2date('F', $date, false); // Not localized, so that selected() works below $year = mysql2date('Y', $date); } } // Check for updated posted values, and errors preventing them from being saved first time. if (!empty($_POST['field_' . $this->field_obj->id . '_day'])) { $new_day = (int) $_POST['field_' . $this->field_obj->id . '_day']; $day = $day != $new_day ? $new_day : $day; } if (!empty($_POST['field_' . $this->field_obj->id . '_month'])) { $new_month = $_POST['field_' . $this->field_obj->id . '_month']; $month = $month != $new_month ? $new_month : $month; } if (!empty($_POST['field_' . $this->field_obj->id . '_year'])) { $new_year = date('j', (int) $_POST['field_' . $this->field_obj->id . '_year']); $year = $year != $new_year ? $new_year : $year; } // $type will be passed by calling function when needed switch ($args['type']) { case 'day': $html = sprintf('<option value="" %1$s>%2$s</option>', selected($day, 0, false), __('----', 'buddypress')); for ($i = 1; $i < 32; ++$i) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected($day, $i, false), (int) $i); } break; case 'month': $eng_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $months = array(0 => __('January', 'buddypress'), 1 => __('February', 'buddypress'), 2 => __('March', 'buddypress'), 3 => __('April', 'buddypress'), 4 => __('May', 'buddypress'), 5 => __('June', 'buddypress'), 6 => __('July', 'buddypress'), 7 => __('August', 'buddypress'), 8 => __('September', 'buddypress'), 9 => __('October', 'buddypress'), 10 => __('November', 'buddypress'), 11 => __('December', 'buddypress')); $html = sprintf('<option value="" %1$s>%2$s</option>', selected($month, 0, false), __('----', 'buddypress')); for ($i = 0; $i < 12; ++$i) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', esc_attr($eng_months[$i]), selected($month, $eng_months[$i], false), $months[$i]); } break; case 'year': $html = sprintf('<option value="" %1$s>%2$s</option>', selected($year, 0, false), __('----', 'buddypress')); for ($i = date('Y') - 1; $i > 1901; $i--) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected($year, $i, false), (int) $i); } break; } echo apply_filters('bp_get_the_profile_field_birthdate', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date); }
/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled separately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @since 2.0.0 * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. */ public function edit_field_options_html(array $args = array()) { $date = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']); $day = 0; $month = 0; $year = 0; $html = ''; $eng_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Set day, month, year defaults. if (!empty($date)) { // If Unix timestamp. if (is_numeric($date)) { $day = date('j', $date); $month = date('F', $date); $year = date('Y', $date); // If MySQL timestamp. } else { $day = mysql2date('j', $date); $month = mysql2date('F', $date, false); // Not localized, so that selected() works below. $year = mysql2date('Y', $date); } } // Check for updated posted values, and errors preventing them from // being saved first time. if (!empty($_POST['field_' . $this->field_obj->id . '_day'])) { $new_day = (int) $_POST['field_' . $this->field_obj->id . '_day']; $day = $day != $new_day ? $new_day : $day; } if (!empty($_POST['field_' . $this->field_obj->id . '_month'])) { if (in_array($_POST['field_' . $this->field_obj->id . '_month'], $eng_months)) { $new_month = $_POST['field_' . $this->field_obj->id . '_month']; } else { $new_month = $month; } $month = $month !== $new_month ? $new_month : $month; } if (!empty($_POST['field_' . $this->field_obj->id . '_year'])) { $new_year = (int) $_POST['field_' . $this->field_obj->id . '_year']; $year = $year != $new_year ? $new_year : $year; } // $type will be passed by calling function when needed. switch ($args['type']) { case 'day': $html = sprintf('<option value="" %1$s>%2$s</option>', selected($day, 0, false), __('----', 'buddypress')); for ($i = 1; $i < 32; ++$i) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected($day, $i, false), (int) $i); } break; case 'month': $months = array(__('January', 'buddypress'), __('February', 'buddypress'), __('March', 'buddypress'), __('April', 'buddypress'), __('May', 'buddypress'), __('June', 'buddypress'), __('July', 'buddypress'), __('August', 'buddypress'), __('September', 'buddypress'), __('October', 'buddypress'), __('November', 'buddypress'), __('December', 'buddypress')); $html = sprintf('<option value="" %1$s>%2$s</option>', selected($month, 0, false), __('----', 'buddypress')); for ($i = 0; $i < 12; ++$i) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', esc_attr($eng_months[$i]), selected($month, $eng_months[$i], false), $months[$i]); } break; case 'year': $html = sprintf('<option value="" %1$s>%2$s</option>', selected($year, 0, false), __('----', 'buddypress')); for ($i = 2037; $i > 1901; $i--) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected($year, $i, false), (int) $i); } break; } /** * Filters the output for the profile field datebox. * * @since 1.1.0 * * @param string $html HTML output for the field. * @param string $value Which date type is being rendered for. * @param string $day Date formatted for the current day. * @param string $month Date formatted for the current month. * @param string $year Date formatted for the current year. * @param int $id ID of the field object being rendered. * @param string $date Current date. */ echo apply_filters('bp_get_the_profile_field_datebox', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date); }
/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled separately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @since 2.0.0 * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. */ public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); $option_values = $option_values ? (array) $option_values : array(); $html = ''; // Check for updated posted values, but errors preventing them from // being saved first time. if (isset($_POST['field_' . $this->field_obj->id]) && $option_values != maybe_serialize($_POST['field_' . $this->field_obj->id])) { if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values = array_map('sanitize_text_field', $_POST['field_' . $this->field_obj->id]); } } for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // First, check to see whether the user's saved values match the option. for ($j = 0, $count_values = count($option_values); $j < $count_values; ++$j) { // Run the allowed option name through the before_save filter, // so we'll be sure to get a match. $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); if ($option_values[$j] === $allowed_options || in_array($allowed_options, $option_values)) { $selected = ' checked="checked"'; break; } } // If the user has not yet supplied a value for this field, check to // see whether there is a default value available. if (empty($option_values) && empty($selected) && !empty($options[$k]->is_default_option)) { $selected = ' checked="checked"'; } $new_html = sprintf('<label for="%3$s"><input %1$s type="checkbox" name="%2$s" id="%3$s" value="%4$s">%5$s</label>', $selected, esc_attr("field_{$this->field_obj->id}[]"), esc_attr("field_{$options[$k]->id}_{$k}"), esc_attr(stripslashes($options[$k]->name)), esc_html(stripslashes($options[$k]->name))); /** * Filters the HTML output for an individual field options checkbox. * * @since 1.1.0 * * @param string $new_html Label and checkbox input field. * @param object $value Current option being rendered for. * @param int $id ID of the field object being rendered. * @param string $selected Current selected value. * @param string $k Current index in the foreach loop. */ $html .= apply_filters('bp_get_the_profile_field_options_checkbox', $new_html, $options[$k], $this->field_obj->id, $selected, $k); } echo $html; }
/** * @group get_value_byid */ public function test_get_value_byid_multipleusers_cached() { $time = date('Y-m-d H:i:s', time() - 60 * 60 * 24); $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $g = $this->factory->xprofile_group->create(); $f = $this->factory->xprofile_field->create(array('field_group_id' => $g)); // Fake the cache $d1 = new stdClass(); $d1->id = 10; $d1->user_id = $u1; $d1->field_id = $f; $d1->value = 'foo'; $d1->last_updated = $time; $d2 = new stdClass(); $d1->id = 21; $d2->user_id = $u2; $d2->field_id = $f; $d2->value = 'bar'; $d2->last_updated = $time; wp_cache_set("{$u1}:{$f}", $d1, 'bp_xprofile_data'); wp_cache_set("{$u2}:{$f}", $d2, 'bp_xprofile_data'); $eu1 = new stdClass(); $eu1->id = 10; $eu1->user_id = $u1; $eu1->field_id = $f; $eu1->value = 'foo'; $eu1->last_updated = $time; $eu2 = new stdClass(); $eu1->id = 21; $eu2->user_id = $u2; $eu2->field_id = $f; $eu2->value = 'bar'; $eu2->last_updated = $time; $expected = array($eu1, $eu2); $this->assertEquals($expected, BP_XProfile_ProfileData::get_value_byid($f, array($u1, $u2))); }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $term_selected = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']); $html = ''; if ($options) { $taxonomy_selected = $options[0]->name; if (!empty($_POST['field_' . $this->field_obj->id])) { $new_term_selected = (int) $_POST['field_' . $this->field_obj->id]; $term_selected = $term_selected != $new_term_selected ? $new_term_selected : $term_selected; } // Get terms of custom taxonomy selected. $terms = get_terms($taxonomy_selected, array('hide_empty' => false)); if ($terms) { foreach ($terms as $term) { $html .= sprintf('<option value="%s"%s>%s</option>', $term->term_id, $term_selected == $term->term_id ? ' selected="selected"' : '', $term->name); } } } echo apply_filters('bp_get_the_profile_field_select_custom_taxonomy', $html, $args['type'], $term_selected, $this->field_obj->id); }
function get_edit_html($value = null) { global $bp; $asterisk = ''; if ($this->is_required) { $asterisk = '* '; } $error_class = ''; if ($this->message) { $this->message = '<p class="' . $this->message_type . '">' . $this->message . '</p>'; $message_class = ' class="' . $this->message_type . '"'; } if (!is_null($value)) { $this->data->value = $value; } $this->data->value = stripslashes(wp_filter_kses($this->data->value)); switch ($this->type) { case 'textbox': $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<input type="text" name="field_' . $this->id . '" id="field_' . $this->id . '" value="' . attribute_escape($this->data->value) . '" />'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'textarea': $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<textarea rows="5" cols="40" name="field_' . $this->id . '" id="field_' . $this->id . '">' . htmlspecialchars($this->data->value) . '</textarea>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'selectbox': $options = $this->get_children(); $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<select name="field_' . $this->id . '" id="field_' . $this->id . '">'; $html .= '<option value="">--------</option>'; for ($k = 0; $k < count($options); $k++) { $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); if ($option_value == $options[$k]->name || $value == $options[$k]->name || $options[$k]->is_default_option) { $selected = ' selected="selected"'; } else { $selected = ''; } $html .= '<option' . $selected . ' value="' . attribute_escape($options[$k]->name) . '">' . $options[$k]->name . '</option>'; } $html .= '</select>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'multiselectbox': $options = $this->get_children(); $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<select class="multi-select" multiple="multiple" name="field_' . $this->id . '[]" id="field_' . $this->id . '">'; if ($value) { $option_values = maybe_unserialize($value); } else { $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); $option_values = maybe_unserialize($option_values); } for ($k = 0; $k < count($options); $k++) { if (@in_array($options[$k]->name, $option_values)) { $selected = ' selected="selected"'; } else { $selected = ''; } $html .= '<option' . $selected . ' value="' . attribute_escape($options[$k]->name) . '">' . $options[$k]->name . '</option>'; } $html .= '</select>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'radio': $options = $this->get_children(); $html .= '<div class="radio signup-field" id="field_' . $this->id . '"><span class="signup-label">' . $asterisk . $this->name . ':</span>' . $this->message; for ($k = 0; $k < count($options); $k++) { $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); if ($option_value == $options[$k]->name || $value == $options[$k]->name || $options[$k]->is_default_option) { $selected = ' checked="checked"'; } else { $selected = ''; } $html .= '<label><input' . $selected . ' type="radio" name="field_' . $this->id . '" id="option_' . $options[$k]->id . '" value="' . attribute_escape($options[$k]->name) . '"> ' . $options[$k]->name . '</label>'; } if (!$this->is_required) { $html .= '<a class="clear-value" style="text-decoration: none;" href="javascript:clear(\'field_' . $this->id . '\');"><img src="' . $bp->profile->image_base . '/cross.gif" alt="' . __('Clear', 'buddypress') . '" /> ' . __('Clear', 'buddypress') . '</a>'; } $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '<div class="clear"></div></div>'; break; case 'checkbox': $options = $this->get_children(); $html .= '<div class="checkbox signup-field" id="field_' . $this->id . '"><span class="signup-label">' . $asterisk . $this->name . ':</span>' . $this->message; if ($value) { $option_values = maybe_unserialize($value); } else { $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); $option_values = maybe_unserialize($option_values); } for ($k = 0; $k < count($options); $k++) { for ($j = 0; $j < count($option_values); $j++) { if ($option_values[$j] == $options[$k]->name || @in_array($options[$k]->name, $value) || $options[$k]->is_default_option) { $selected = ' checked="checked"'; break; } } $html .= '<label><input' . $selected . ' type="checkbox" name="field_' . $this->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . attribute_escape($options[$k]->name) . '"> ' . $options[$k]->name . '</label>'; $selected = ''; } $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '<div class="clear"></div></div>'; break; case 'datebox': if ($this->data->value != '') { $day = date("j", $this->data->value); $month = date("F", $this->data->value); $year = date("Y", $this->data->value); $default_select = ' selected="selected"'; } $html .= '<div id="field_' . $this->id . '" class="datefield signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '_day">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . ' <select name="field_' . $this->id . '_day" id="field_' . $this->id . '_day">'; $html .= '<option value=""' . attribute_escape($default_select) . '>--</option>'; for ($i = 1; $i < 32; $i++) { if ($day == $i) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>'; } $html .= '</select>'; $months = array(__('January', 'buddypress'), __('February', 'buddypress'), __('March', 'buddypress'), __('April', 'buddypress'), __('May', 'buddypress'), __('June', 'buddypress'), __('July', 'buddypress'), __('August', 'buddypress'), __('September', 'buddypress'), __('October', 'buddypress'), __('November', 'buddypress'), __('December', 'buddypress')); $html .= ' <select name="field_' . $this->id . '_month" id="field_' . $this->id . '_month">'; $html .= '<option value=""' . attribute_escape($default_select) . '>------</option>'; for ($i = 0; $i < 12; $i++) { if ($month == $months[$i]) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $months[$i] . '"' . $selected . '>' . $months[$i] . '</option>'; } $html .= '</select>'; $html .= ' <select name="field_' . $this->id . '_year" id="field_' . $this->id . '_year">'; $html .= '<option value=""' . attribute_escape($default_select) . '>----</option>'; for ($i = date('Y', time()); $i > 1899; $i--) { if ($year == $i) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>'; } $html .= '</select>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; } return $html; }
/** * Fetches profile data for a specific field for the user. * * When the field value is serialized, this function unserializes and filters * each item in the array. * * @package BuddyPress Core * @param mixed $field The ID of the field, or the $name of the field. * @param int $user_id The ID of the user * @param string $multi_format How should array data be returned? 'comma' if you want a * comma-separated string; 'array' if you want an array * @uses BP_XProfile_ProfileData::get_value_byid() Fetches the value based on the params passed. * @return mixed The profile field data. */ function xprofile_get_field_data($field, $user_id = 0, $multi_format = 'array') { if (empty($user_id)) { $user_id = bp_displayed_user_id(); } if (empty($user_id)) { return false; } if (is_numeric($field)) { $field_id = $field; } else { $field_id = xprofile_get_field_id_from_name($field); } if (empty($field_id)) { return false; } $values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field_id, $user_id)); if (is_array($values)) { $data = array(); foreach ((array) $values as $value) { /** * Filters the field data value for a specific field for the user. * * @since BuddyPress (1.0.0) * * @param string $value Value saved for the field. * @param int $field_id ID of the field being displayed. * @param int $user_id ID of the user being displayed. */ $data[] = apply_filters('xprofile_get_field_data', $value, $field_id, $user_id); } if ('comma' == $multi_format) { $data = implode(', ', $data); } } else { /** This filter is documented in bp-xprofile/bp-xprofile-functions.php */ $data = apply_filters('xprofile_get_field_data', $values, $field_id, $user_id); } return $data; }
/** * Fetches profile data for a specific field for the user. * * When the field value is serialized, this function unserializes and filters each item in the array * that results. * * @package BuddyPress Core * @param mixed $field The ID of the field, or the $name of the field. * @param int $user_id The ID of the user * @param string $multi_format How should array data be returned? 'comma' if you want a * comma-separated string; 'array' if you want an array * @uses BP_XProfile_ProfileData::get_value_byid() Fetches the value based on the params passed. * @return mixed The profile field data. */ function xprofile_get_field_data($field, $user_id = 0, $multi_format = 'array') { if (empty($user_id)) { $user_id = bp_displayed_user_id(); } if (empty($user_id)) { return false; } if (is_numeric($field)) { $field_id = $field; } else { $field_id = xprofile_get_field_id_from_name($field); } if (empty($field_id)) { return false; } $values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field_id, $user_id)); if (is_array($values)) { $data = array(); foreach ((array) $values as $value) { $data[] = apply_filters('xprofile_get_field_data', $value, $field_id, $user_id); } if ('comma' == $multi_format) { $data = implode(', ', $data); } } else { $data = apply_filters('xprofile_get_field_data', $values, $field_id, $user_id); } return $data; }
/** * Output the edit field options HTML for this field type. * * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. * These are stored separately in the database, and their templating is handled separately. * * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @since 2.0.0 * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. */ public function edit_field_options_html(array $args = array()) { $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); $options = $this->field_obj->get_children(); $html = ''; if (empty($original_option_values) && !empty($_POST['field_' . $this->field_obj->id])) { $original_option_values = sanitize_text_field($_POST['field_' . $this->field_obj->id]); } $option_values = $original_option_values ? (array) $original_option_values : array(); for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // Check for updated posted values, but errors preventing them from // being saved first time. foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $this->field_obj->id]) && $_POST['field_' . $this->field_obj->id][$i] != $option_value) { if (!empty($_POST['field_' . $this->field_obj->id][$i])) { $option_values[] = sanitize_text_field($_POST['field_' . $this->field_obj->id][$i]); } } } // Run the allowed option name through the before_save filter, so // we'll be sure to get a match. $allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false); // First, check to see whether the user-entered value matches. if (in_array($allowed_options, $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults. if (!is_array($original_option_values) && empty($option_values) && !empty($options[$k]->is_default_option)) { $selected = ' selected="selected"'; } /** * Filters the HTML output for options in a multiselect input. * * @since 1.5.0 * * @param string $value Option tag for current value being rendered. * @param object $value Current option being rendered for. * @param int $id ID of the field object being rendered. * @param string $selected Current selected value. * @param string $k Current index in the foreach loop. */ $html .= apply_filters('bp_get_the_profile_field_options_multiselect', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_html(stripslashes($options[$k]->name)) . '</option>', $options[$k], $this->field_obj->id, $selected, $k); } echo $html; }
/** * Ensures that BP data appears in comments array * * This filter loops through the comments return by a normal WordPress request * and swaps out user data with BP xprofile data, where available * * @param array $comments * @param int $post_id * @return array $comments */ function xprofile_filter_comments($comments, $post_id) { // Locate comment authors with WP accounts foreach ((array) $comments as $comment) { if ($comment->user_id) { $user_ids[] = $comment->user_id; } } // If none are found, just return the comments array if (empty($user_ids)) { return $comments; } // Pull up the xprofile fullname of each commenter if ($fullnames = BP_XProfile_ProfileData::get_value_byid(1, $user_ids)) { foreach ((array) $fullnames as $user) { $users[$user->user_id] = trim(stripslashes($user->value)); } } // Loop through and match xprofile fullname with commenters foreach ((array) $comments as $i => $comment) { if (!empty($comment->user_id)) { if (!empty($users[$comment->user_id])) { $comments[$i]->comment_author = $users[$comment->user_id]; } } } return $comments; }