[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field-type-selectbox.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileClasses
   7   * @since 2.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Selectbox xprofile field type.
  15   *
  16   * @since 2.0.0
  17   */
  18  class BP_XProfile_Field_Type_Selectbox extends BP_XProfile_Field_Type {
  19  
  20      /**
  21       * Constructor for the selectbox field type.
  22       *
  23       * @since 2.0.0
  24       */
  25  	public function __construct() {
  26          parent::__construct();
  27  
  28          $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' );
  29          $this->name     = _x( 'Drop Down Select Box', 'xprofile field type', 'buddypress' );
  30  
  31          $this->supports_options = true;
  32  
  33          $this->set_format( '/^.+$/', 'replace' );
  34  
  35          /**
  36           * Fires inside __construct() method for BP_XProfile_Field_Type_Selectbox class.
  37           *
  38           * @since 2.0.0
  39           *
  40           * @param BP_XProfile_Field_Type_Selectbox $this Current instance of
  41           *                                               the field type select box.
  42           */
  43          do_action( 'bp_xprofile_field_type_selectbox', $this );
  44      }
  45  
  46      /**
  47       * Output the edit field HTML for this field type.
  48       *
  49       * Must be used inside the {@link bp_profile_fields()} template loop.
  50       *
  51       * @since 2.0.0
  52       *
  53       * @param array $raw_properties Optional key/value array of
  54       *                              {@link http://dev.w3.org/html5/markup/select.html permitted attributes}
  55       *                              that you want to add.
  56       */
  57  	public function edit_field_html( array $raw_properties = array() ) {
  58  
  59          // User_id is a special optional parameter that we pass to
  60          // {@link bp_the_profile_field_options()}.
  61          if ( isset( $raw_properties['user_id'] ) ) {
  62              $user_id = (int) $raw_properties['user_id'];
  63              unset( $raw_properties['user_id'] );
  64          } else {
  65              $user_id = bp_displayed_user_id();
  66          } ?>
  67  
  68          <legend id="<?php bp_the_profile_field_input_name(); ?>-1">
  69              <?php bp_the_profile_field_name(); ?>
  70              <?php bp_the_profile_field_required_label(); ?>
  71          </legend>
  72  
  73          <?php
  74  
  75          /** This action is documented in bp-xprofile/bp-xprofile-classes */
  76          do_action( bp_get_the_profile_field_errors_action() ); ?>
  77  
  78          <select <?php echo $this->get_edit_field_html_elements( $raw_properties ); ?> aria-labelledby="<?php bp_the_profile_field_input_name(); ?>-1" aria-describedby="<?php bp_the_profile_field_input_name(); ?>-3">
  79              <?php bp_the_profile_field_options( array( 'user_id' => $user_id ) ); ?>
  80          </select>
  81  
  82          <?php if ( bp_get_the_profile_field_description() ) : ?>
  83              <p class="description" id="<?php bp_the_profile_field_input_name(); ?>-3"><?php bp_the_profile_field_description(); ?></p>
  84          <?php endif; ?>
  85  
  86          <?php
  87      }
  88  
  89      /**
  90       * Output the edit field options HTML for this field type.
  91       *
  92       * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
  93       * These are stored separately in the database, and their templating is handled separately.
  94       *
  95       * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
  96       * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
  97       *
  98       * Must be used inside the {@link bp_profile_fields()} template loop.
  99       *
 100       * @since 2.0.0
 101       *
 102       * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
 103       */
 104  	public function edit_field_options_html( array $args = array() ) {
 105          $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );
 106  
 107          $options = $this->field_obj->get_children();
 108          $html    = '<option value="">' . /* translators: no option picked in select box */ esc_html__( '----', 'buddypress' ) . '</option>';
 109  
 110          if ( empty( $original_option_values ) && !empty( $_POST['field_' . $this->field_obj->id] ) ) {
 111              $original_option_values = sanitize_text_field(  $_POST['field_' . $this->field_obj->id] );
 112          }
 113  
 114          $option_values = ( $original_option_values ) ? (array) $original_option_values : array();
 115          for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
 116              $selected = '';
 117  
 118              // Check for updated posted values, but errors preventing them from
 119              // being saved first time.
 120              foreach( $option_values as $i => $option_value ) {
 121                  if ( isset( $_POST['field_' . $this->field_obj->id] ) && $_POST['field_' . $this->field_obj->id] != $option_value ) {
 122                      if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
 123                          $option_values[$i] = sanitize_text_field( $_POST['field_' . $this->field_obj->id] );
 124                      }
 125                  }
 126              }
 127  
 128              // Run the allowed option name through the before_save filter, so
 129              // we'll be sure to get a match.
 130              $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
 131  
 132              // First, check to see whether the user-entered value matches.
 133              if ( in_array( $allowed_options, $option_values ) ) {
 134                  $selected = ' selected="selected"';
 135              }
 136  
 137              // Then, if the user has not provided a value, check for defaults.
 138              if ( ! is_array( $original_option_values ) && empty( $option_values ) && $options[$k]->is_default_option ) {
 139                  $selected = ' selected="selected"';
 140              }
 141  
 142              /**
 143               * Filters the HTML output for options in a select input.
 144               *
 145               * @since 1.1.0
 146               *
 147               * @param string $value    Option tag for current value being rendered.
 148               * @param object $value    Current option being rendered for.
 149               * @param int    $id       ID of the field object being rendered.
 150               * @param string $selected Current selected value.
 151               * @param string $k        Current index in the foreach loop.
 152               */
 153              $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 );
 154          }
 155  
 156          echo $html;
 157      }
 158  
 159      /**
 160       * Output HTML for this field type on the wp-admin Profile Fields screen.
 161       *
 162       * Must be used inside the {@link bp_profile_fields()} template loop.
 163       *
 164       * @since 2.0.0
 165       *
 166       * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
 167       */
 168  	public function admin_field_html( array $raw_properties = array() ) {
 169          ?>
 170  
 171          <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php
 172              /* translators: accessibility text */
 173              esc_html_e( 'Select', 'buddypress' );
 174          ?></label>
 175          <select <?php echo $this->get_edit_field_html_elements( $raw_properties ); ?>>
 176              <?php bp_the_profile_field_options(); ?>
 177          </select>
 178  
 179          <?php
 180      }
 181  
 182      /**
 183       * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
 184       *
 185       * Must be used inside the {@link bp_profile_fields()} template loop.
 186       *
 187       * @since 2.0.0
 188       *
 189       * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
 190       * @param string            $control_type  Optional. HTML input type used to render the current
 191       *                                         field's child options.
 192       */
 193  	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
 194          parent::admin_new_field_html( $current_field, 'radio' );
 195      }
 196  }


Generated: Tue Apr 23 01:01:07 2024 Cross-referenced by PHPXref 0.7.1