[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field-type-wordpress-textbox.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileClasses
   7   * @since 8.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * WordPress xProfile regular field type.
  15   *
  16   * @since 8.0.0
  17   */
  18  class BP_XProfile_Field_Type_WordPress_Textbox extends BP_XProfile_Field_Type_WordPress {
  19  
  20      /**
  21       * Constructor for the WordPress regular field type.
  22       *
  23       * @since 8.0.0
  24       */
  25  	public function __construct() {
  26          parent::__construct();
  27  
  28          $this->category            = _x( 'WordPress Fields', 'xprofile field type category', 'buddypress' );
  29          $this->name                = _x( 'Text field', 'xprofile field type', 'buddypress' );
  30          $this->accepts_null_value  = true;
  31          $this->do_settings_section = true;
  32          $this->wp_user_key         = '';
  33  
  34          $this->set_format( '/^.*$/', 'replace' );
  35  
  36          /**
  37           * Fires inside __construct() method for BP_XProfile_Field_Type_WordPress_Textbox class.
  38           *
  39           * @since 8.0.0
  40           *
  41           * @param BP_XProfile_Field_Type_WordPress_Textbox $this Instance of the field type object.
  42           */
  43          do_action( 'bp_xprofile_field_type_wordpress_textbox', $this );
  44  
  45          /*
  46           * As we are using an xProfile field meta to store the WordPress field meta key we need
  47           * to make sure $this->wp_user_key is set before once the field has been populated.
  48           */
  49          add_action( 'bp_xprofile_field', array( $this, 'set_wp_user_key' ), 10 );
  50      }
  51  
  52      /**
  53       * Sets the WordPress field wp_user_key property before saving the xProfile field.
  54       *
  55       * @since 8.0.0
  56       *
  57       * @param BP_XProfile_Field $field Field object.
  58       */
  59  	public function set_wp_user_key( $field ) {
  60          if ( ! $this->wp_user_key && 'wp-textbox' === $field->type ) {
  61              $this->wp_user_key = self::get_field_settings( $field->id );
  62          }
  63      }
  64  
  65      /**
  66       * Gets the WordPress field value during an xProfile fields loop.
  67       *
  68       * This function is used inside `BP_XProfile_ProfileData::get_data_for_user()`
  69       * to include the WordPress field value into the xProfile fields loop.
  70       *
  71       * @since 8.0.0
  72       *
  73       * @param integer $user_id The user ID.
  74       * @param integer $field_id The xProfile field ID.
  75       * @return array An array containing the metadata `id`, `value` and `table_name`.
  76       */
  77  	public function get_field_value( $user_id, $field_id = 0 ) {
  78          if ( ! $this->wp_user_key ) {
  79              $this->wp_user_key = self::get_field_settings( $field_id );
  80          }
  81  
  82          return parent::get_field_value( $user_id, $field_id );
  83      }
  84  
  85      /**
  86       * Sanitize the user field before saving it to db.
  87       *
  88       * @since 8.0.0
  89       *
  90       * @param string $value The user field value.
  91       * @return string The sanitized field value.
  92       */
  93  	public function sanitize_for_db( $value ) {
  94          if ( ! $value ) {
  95              return '';
  96          }
  97  
  98          if ( 'user_url' === $this->wp_user_key ) {
  99              return esc_url_raw( $value );
 100          }
 101  
 102          return sanitize_text_field( $value );
 103      }
 104  
 105      /**
 106       * Sanitize the user field before displaying it as an attribute.
 107       *
 108       * @since 8.0.0
 109       *
 110       * @param string $value The user field value.
 111       * @return string The sanitized field value.
 112       */
 113  	public function sanitize_for_output( $value, $user_id = 0 ) {
 114          if ( ! $user_id ) {
 115              $user_id = bp_displayed_user_id();
 116          }
 117  
 118          return sanitize_user_field( $this->wp_user_key, $value, $user_id, 'attribute' );
 119      }
 120  
 121      /**
 122       * Output the edit field HTML for this field type.
 123       *
 124       * Must be used inside the {@link bp_profile_fields()} template loop.
 125       *
 126       * @since 8.0.0
 127       *
 128       * @param array $raw_properties Optional key/value array of
 129       *                              {@link http://dev.w3.org/html5/markup/textarea.html permitted attributes}
 130       *                              that you want to add.
 131       */
 132  	public function edit_field_html( array $raw_properties = array() ) {
 133          /*
 134           * User_id is a special optional parameter that certain other fields
 135           * types pass to {@link bp_the_profile_field_options()}.
 136           */
 137          if ( ! is_admin() && isset( $raw_properties['user_id'] ) ) {
 138              unset( $raw_properties['user_id'] );
 139          }
 140  
 141          $user_id = bp_displayed_user_id();
 142          if ( isset( $raw_properties['user_id'] ) && $raw_properties['user_id'] ) {
 143              $user_id = (int) $raw_properties['user_id'];
 144              unset( $raw_properties['user_id'] );
 145          }
 146  
 147          if ( ! $this->wp_user_key ) {
 148              $this->wp_user_key = self::get_field_settings( bp_get_the_profile_field_id() );
 149          }
 150  
 151          $field_value = '';
 152          if ( 'user_url' === $this->wp_user_key ) {
 153              if ( bp_displayed_user_id() ) {
 154                  $field_value = bp_get_displayed_user()->userdata->{$this->wp_user_key};
 155              } elseif ( $user_id ) {
 156                  $user = get_user_by( 'id', $user_id );
 157                  $field_value = $user->{$this->wp_user_key};
 158              }
 159          } else {
 160              $field_value = bp_get_user_meta( $user_id, $this->wp_user_key, true );
 161          }
 162  
 163          $r = bp_parse_args(
 164              $raw_properties,
 165              array(
 166                  'type'  => 'text',
 167                  'value' => $this->sanitize_for_output( $field_value, $user_id ),
 168              )
 169          );
 170          ?>
 171  
 172          <legend id="<?php bp_the_profile_field_input_name(); ?>-1">
 173              <?php bp_the_profile_field_name(); ?>
 174              <?php bp_the_profile_field_required_label(); ?>
 175          </legend>
 176  
 177          <?php
 178  
 179          /** This action is documented in bp-xprofile/bp-xprofile-classes */
 180          do_action( bp_get_the_profile_field_errors_action() ); ?>
 181  
 182          <input <?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">
 183  
 184          <?php if ( bp_get_the_profile_field_description() ) : ?>
 185              <p class="description" id="<?php bp_the_profile_field_input_name(); ?>-3"><?php bp_the_profile_field_description(); ?></p>
 186          <?php endif; ?>
 187  
 188          <?php
 189      }
 190  
 191      /**
 192       * Output HTML for this field type on the wp-admin Profile Fields screen.
 193       *
 194       * Must be used inside the {@link bp_profile_fields()} template loop.
 195       *
 196       * @since 8.0.0
 197       *
 198       * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
 199       */
 200  	public function admin_field_html( array $raw_properties = array() ) {
 201          $r = bp_parse_args(
 202              $raw_properties,
 203              array(
 204                  'type' => 'text',
 205              )
 206          );
 207          ?>
 208  
 209          <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php
 210              /* translators: accessibility text */
 211              esc_html_e( 'WordPress field', 'buddypress' );
 212          ?></label>
 213          <input <?php echo $this->get_edit_field_html_elements( $r ); ?>>
 214  
 215          <?php
 216      }
 217  
 218      /**
 219       * Get settings for a given WordPress field.
 220       *
 221       * @since 8.0.0
 222       *
 223       * @param int $field_id ID of the field.
 224       * @return string The meta_key used for this field.
 225       */
 226  	public static function get_field_settings( $field_id ) {
 227          $wp_user_key = bp_xprofile_get_meta( $field_id, 'field', 'wp_user_key', true );
 228  
 229          return sanitize_key( $wp_user_key );
 230      }
 231  
 232      /**
 233       * Save settings from the field edit screen in the Dashboard.
 234       *
 235       * @since 8.0.0
 236       *
 237       * @param int   $field_id ID of the field.
 238       * @param array $settings Array of settings.
 239       * @return bool True on success.
 240       */
 241  	public function admin_save_settings( $field_id, $settings ) {
 242          $existing_setting = self::get_field_settings( $field_id );
 243          $setting = '';
 244  
 245          if ( isset( $settings['wp_user_key'] ) ) {
 246              $setting = sanitize_key( $settings['wp_user_key'] );
 247          }
 248  
 249          if ( $setting && $setting !== $existing_setting ) {
 250              bp_xprofile_update_meta( $field_id, 'field', 'wp_user_key', $setting );
 251          }
 252  
 253          return true;
 254      }
 255  
 256      /**
 257       * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
 258       * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
 259       *
 260       * @since 8.0.0
 261       *
 262       * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
 263       * @param string            $control_type  Optional. HTML input type used to render the
 264       *                                         current field's child options.
 265       */
 266  	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
 267          $type = array_search( get_class( $this ), bp_xprofile_get_field_types() );
 268  
 269          if ( false === $type ) {
 270              return;
 271          }
 272  
 273          $style = 'margin-top: 15px;';
 274          if ( $current_field->type !== $type ) {
 275              $style .= ' display: none;';
 276          };
 277  
 278          $setting = self::get_field_settings( $current_field->id );
 279  
 280          $wp_labels = array_merge(
 281              array(
 282                  'first_name' => _x( 'First Name', 'xpofile wp-textbox field type label', 'buddypress' ),
 283                  'last_name'  => _x( 'Last Name', 'xpofile wp-textbox field type label', 'buddypress' ),
 284                  'user_url'   => _x( 'Website', 'xpofile wp-textbox field type label', 'buddypress' ),
 285              ),
 286              wp_get_user_contact_methods()
 287          );
 288          ?>
 289          <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $style ); ?>">
 290              <h3><?php esc_html_e( 'Select the information you want to use for your WordPress field.', 'buddypress' ); ?></h3>
 291  
 292              <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all">
 293                  <div class="bp-option">
 294                      <ul>
 295                          <?php
 296                          foreach ( $this->supported_keys as $key ) {
 297                              if ( 'description' === $key || ! isset( $wp_labels[ $key ] ) ) {
 298                                  continue;
 299                              }
 300  
 301                              printf(
 302                                  '<li>
 303                                      <label for="wp-textbox-wp_user_key-%1$s">
 304                                          <input type="radio" id="wp-textbox-wp_user_key-%1$s" name="field-settings[wp_user_key]" value="%1$s" %2$s/>
 305                                          %3$s
 306                                      </label>
 307                                  </li>',
 308                                  esc_attr( $key ),
 309                                  checked( $key, $setting, false ),
 310                                  esc_html( $wp_labels[ $key ] )
 311                              );
 312                          }
 313                          ?>
 314                      </ul>
 315                  </div>
 316              </div>
 317          </div>
 318          <?php
 319      }
 320  
 321      /**
 322       * Format WordPress field values for display.
 323       *
 324       * @since 8.0.0
 325       *
 326       * @param string     $field_value The field value, as saved in the database.
 327       * @param string|int $field_id    Optional. ID of the field.
 328       * @return string The sanitized WordPress field.
 329       */
 330  	public static function display_filter( $field_value, $field_id = '' ) {
 331          $wp_user_key = self::get_field_settings( $field_id );
 332  
 333          if ( ! $wp_user_key ) {
 334              return '';
 335          }
 336  
 337          if ( 'user_url' === $wp_user_key ) {
 338              $sanitized_website = sanitize_user_field( $wp_user_key, $field_value, bp_displayed_user_id(), 'attribute' );
 339              return sprintf( '<a href="%1$s" rel="nofollow">%1$s</a>', $sanitized_website );
 340          }
 341  
 342          return sanitize_user_field( $wp_user_key, $field_value, bp_displayed_user_id(), 'display' );
 343      }
 344  }


Generated: Sat Apr 27 01:00:55 2024 Cross-referenced by PHPXref 0.7.1