[ 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              $clear = 'clear';
 103              if ( is_admin() && ! wp_doing_ajax() ) {
 104                  $clear = 'bp.clear';
 105              }
 106  
 107              $js_clear = sprintf( 'javascript:%1$s( \'%2$s[]\' );', $clear, esc_js( bp_get_the_profile_field_input_name() ) );
 108          ?>
 109  
 110              <a class="clear-value" href="<?php echo $js_clear; ?>">
 111                  <?php esc_html_e( 'Clear', 'buddypress' ); ?>
 112              </a>
 113  
 114          <?php endif; ?>
 115      <?php
 116      }
 117  
 118      /**
 119       * Output the edit field options HTML for this field type.
 120       *
 121       * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
 122       * These are stored separately in the database, and their templating is handled separately.
 123       *
 124       * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
 125       * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
 126       *
 127       * Must be used inside the {@link bp_profile_fields()} template loop.
 128       *
 129       * @since 2.0.0
 130       *
 131       * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
 132       */
 133  	public function edit_field_options_html( array $args = array() ) {
 134          $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );
 135  
 136          $options = $this->field_obj->get_children();
 137          $html    = '';
 138  
 139          if ( empty( $original_option_values ) && ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
 140              $original_option_values = sanitize_text_field( $_POST['field_' . $this->field_obj->id] );
 141          }
 142  
 143          $option_values = ( $original_option_values ) ? (array) $original_option_values : array();
 144          for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
 145              $selected = '';
 146  
 147              // Check for updated posted values, but errors preventing them from
 148              // being saved first time.
 149              foreach( $option_values as $i => $option_value ) {
 150                  if ( isset( $_POST['field_' . $this->field_obj->id] ) && $_POST['field_' . $this->field_obj->id][$i] != $option_value ) {
 151                      if ( ! empty( $_POST['field_' . $this->field_obj->id][$i] ) ) {
 152                          $option_values[] = sanitize_text_field( $_POST['field_' . $this->field_obj->id][$i] );
 153                      }
 154                  }
 155              }
 156  
 157              // Run the allowed option name through the before_save filter, so
 158              // we'll be sure to get a match.
 159              $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
 160  
 161              // First, check to see whether the user-entered value matches.
 162              if ( in_array( $allowed_options, $option_values ) ) {
 163                  $selected = ' selected="selected"';
 164              }
 165  
 166              // Then, if the user has not provided a value, check for defaults.
 167              if ( ! is_array( $original_option_values ) && empty( $option_values ) && ! empty( $options[$k]->is_default_option ) ) {
 168                  $selected = ' selected="selected"';
 169              }
 170  
 171              /**
 172               * Filters the HTML output for options in a multiselect input.
 173               *
 174               * @since 1.5.0
 175               *
 176               * @param string $value    Option tag for current value being rendered.
 177               * @param object $value    Current option being rendered for.
 178               * @param int    $id       ID of the field object being rendered.
 179               * @param string $selected Current selected value.
 180               * @param string $k        Current index in the foreach loop.
 181               */
 182              $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 );
 183          }
 184  
 185          echo $html;
 186      }
 187  
 188      /**
 189       * Output HTML for this field type on the wp-admin Profile Fields screen.
 190       *
 191       * Must be used inside the {@link bp_profile_fields()} template loop.
 192       *
 193       * @since 2.0.0
 194       *
 195       * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
 196       */
 197  	public function admin_field_html( array $raw_properties = array() ) {
 198          $r = bp_parse_args(
 199              $raw_properties,
 200              array(
 201                  'multiple' => 'multiple',
 202              )
 203          );
 204          ?>
 205  
 206          <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php
 207              /* translators: accessibility text */
 208              esc_html_e( 'Select', 'buddypress' );
 209          ?></label>
 210          <select <?php echo $this->get_edit_field_html_elements( $r ); ?>>
 211              <?php bp_the_profile_field_options(); ?>
 212          </select>
 213  
 214          <?php
 215      }
 216  
 217      /**
 218       * Output HTML for this field type's children options on the wp-admin Profile Fields,
 219       * "Add Field" and "Edit Field" screens.
 220       *
 221       * Must be used inside the {@link bp_profile_fields()} template loop.
 222       *
 223       * @since 2.0.0
 224       *
 225       * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
 226       * @param string            $control_type  Optional. HTML input type used to render the current
 227       *                                         field's child options.
 228       */
 229  	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
 230          parent::admin_new_field_html( $current_field, 'checkbox' );
 231      }
 232  }


Generated: Thu Apr 25 01:01:12 2024 Cross-referenced by PHPXref 0.7.1