[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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


Generated: Mon May 16 01:00:52 2022 Cross-referenced by PHPXref 0.7.1