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


Generated: Sat Dec 14 01:01:40 2019 Cross-referenced by PHPXref 0.7.1