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


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1