[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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


Generated: Sun Sep 19 01:01:40 2021 Cross-referenced by PHPXref 0.7.1