[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field-type-checkbox.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   * Checkbox xprofile field type.
  15   *
  16   * @since 2.0.0
  17   */
  18  class BP_XProfile_Field_Type_Checkbox extends BP_XProfile_Field_Type {
  19  
  20      /**
  21       * Constructor for the checkbox 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( 'Checkboxes', '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_Checkbox class.
  39           *
  40           * @since 2.0.0
  41           *
  42           * @param BP_XProfile_Field_Type_Checkbox $this Current instance of
  43           *                                              the field type checkbox.
  44           */
  45          do_action( 'bp_xprofile_field_type_checkbox', $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/input.checkbox.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              <legend>
  71                  <?php bp_the_profile_field_name(); ?>
  72                  <?php bp_the_profile_field_required_label(); ?>
  73              </legend>
  74  
  75              <?php if ( bp_get_the_profile_field_description() ) : ?>
  76                  <p class="description" tabindex="0"><?php bp_the_profile_field_description(); ?></p>
  77              <?php endif; ?>
  78  
  79              <?php
  80  
  81              /** This action is documented in bp-xprofile/bp-xprofile-classes */
  82              do_action( bp_get_the_profile_field_errors_action() ); ?>
  83  
  84              <?php bp_the_profile_field_options( array(
  85                  'user_id' => $user_id
  86              ) ); ?>
  87  
  88          <?php
  89      }
  90  
  91      /**
  92       * Output the edit field options HTML for this field type.
  93       *
  94       * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
  95       * These are stored separately in the database, and their templating is handled separately.
  96       *
  97       * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
  98       * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
  99       *
 100       * Must be used inside the {@link bp_profile_fields()} template loop.
 101       *
 102       * @since 2.0.0
 103       *
 104       * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
 105       */
 106  	public function edit_field_options_html( array $args = array() ) {
 107          $options       = $this->field_obj->get_children();
 108          $option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );
 109  
 110          /*
 111           * Determine whether to pre-select the default option.
 112           *
 113           * If there's no saved value, take the following into account:
 114           * If the user has never saved a value for this field,
 115           * $option_values will be an empty string, and we should pre-select the default option.
 116           * If the user has specifically chosen none of the options,
 117           * $option_values will be an empty array, and we should respect that value.
 118           */
 119          $select_default_option = false;
 120          if ( empty( $option_values ) && ! is_array( $option_values ) ) {
 121              $select_default_option = true;
 122          }
 123  
 124          $option_values = ( $option_values ) ? (array) $option_values : array();
 125  
 126          $html = '';
 127  
 128          // Check for updated posted values, but errors preventing them from
 129          // being saved first time.
 130          if ( isset( $_POST['field_' . $this->field_obj->id] ) && $option_values != maybe_serialize( $_POST['field_' . $this->field_obj->id] ) ) {
 131              if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
 132                  $option_values = array_map( 'sanitize_text_field', $_POST['field_' . $this->field_obj->id] );
 133              }
 134          }
 135  
 136          for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
 137              $selected = '';
 138  
 139              // First, check to see whether the user's saved values match the option.
 140              for ( $j = 0, $count_values = count( $option_values ); $j < $count_values; ++$j ) {
 141  
 142                  // Run the allowed option name through the before_save filter,
 143                  // so we'll be sure to get a match.
 144                  $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
 145  
 146                  if ( $option_values[$j] === $allowed_options || in_array( $allowed_options, $option_values ) ) {
 147                      $selected = ' checked="checked"';
 148                      break;
 149                  }
 150              }
 151  
 152              // If the user has not yet supplied a value for this field, check to
 153              // see whether there is a default value available.
 154              if ( empty( $selected ) && $select_default_option && ! empty( $options[$k]->is_default_option ) ) {
 155                  $selected = ' checked="checked"';
 156              }
 157  
 158              $new_html = sprintf( '<label for="%3$s" class="option-label"><input %1$s type="checkbox" name="%2$s" id="%3$s" value="%4$s">%5$s</label>',
 159                  $selected,
 160                  esc_attr( bp_get_the_profile_field_input_name() . '[]' ),
 161                  esc_attr( "field_{$options[$k]->id}_{$k}" ),
 162                  esc_attr( stripslashes( $options[$k]->name ) ),
 163                  esc_html( stripslashes( $options[$k]->name ) )
 164              );
 165  
 166              /**
 167               * Filters the HTML output for an individual field options checkbox.
 168               *
 169               * @since 1.1.0
 170               *
 171               * @param string $new_html Label and checkbox input field.
 172               * @param object $value    Current option being rendered for.
 173               * @param int    $id       ID of the field object being rendered.
 174               * @param string $selected Current selected value.
 175               * @param string $k        Current index in the foreach loop.
 176               */
 177              $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', $new_html, $options[$k], $this->field_obj->id, $selected, $k );
 178          }
 179  
 180          printf( '<div id="%1$s" class="input-options checkbox-options">%2$s</div>',
 181              esc_attr( 'field_' . $this->field_obj->id ),
 182              $html
 183          );
 184      }
 185  
 186      /**
 187       * Output HTML for this field type on the wp-admin Profile Fields screen.
 188       *
 189       * Must be used inside the {@link bp_profile_fields()} template loop.
 190       *
 191       * @since 2.0.0
 192       *
 193       * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
 194       */
 195  	public function admin_field_html( array $raw_properties = array() ) {
 196          bp_the_profile_field_options();
 197      }
 198  
 199      /**
 200       * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
 201       *
 202       * Must be used inside the {@link bp_profile_fields()} template loop.
 203       *
 204       * @since 2.0.0
 205       *
 206       * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
 207       * @param string            $control_type  Optional. HTML input type used to render the current
 208       *                                         field's child options.
 209       */
 210  	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
 211          parent::admin_new_field_html( $current_field, 'checkbox' );
 212      }
 213  }


Generated: Fri Mar 29 01:01:02 2024 Cross-referenced by PHPXref 0.7.1