[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field-type-wordpress.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   * Base class for xprofile field types that set/get WordPress profile data from usermeta.
  15   *
  16   * @since 8.0.0
  17   */
  18  abstract class BP_XProfile_Field_Type_WordPress extends BP_XProfile_Field_Type {
  19  
  20      /**
  21       * The usermeta key for the WordPress field.
  22       *
  23       * @since 8.0.0
  24       * @var string The meta key name of this WordPress field.
  25       */
  26      public $wp_user_key = '';
  27  
  28      /**
  29       * The WordPress supported user keys.
  30       *
  31       * @since 8.0.0
  32       * @var string[] The WordPress supported user keys.
  33       */
  34      public $supported_keys = array();
  35  
  36      /**
  37       * WordPress field's visibility setting.
  38       *
  39       * Defaults to 'public'. This property enforces Field's default visibility.
  40       *
  41       * @since 8.0.0
  42       *
  43       * @return string The WordPress field's visibility setting.
  44       */
  45      public $visibility = 'public';
  46  
  47      /**
  48       * Supported features for the WordPress field type.
  49       *
  50       * @since 8.0.0
  51       * @var bool[] The WordPress field supported features.
  52       */
  53      public static $supported_features = array(
  54          'switch_fieldtype'        => false,
  55          'required'                => false,
  56          'do_autolink'             => false,
  57          'allow_custom_visibility' => false,
  58          'member_types'            => false,
  59          'signup_position'         => true,
  60      );
  61  
  62      /**
  63       * Constructor for the WordPress field type.
  64       *
  65       * @since 8.0.0
  66       */
  67  	public function __construct() {
  68          parent::__construct();
  69  
  70          /**
  71           * Fires inside __construct() method for BP_XProfile_Field_Type_WordPress class.
  72           *
  73           * @since 8.0.0
  74           *
  75           * @param BP_XProfile_Field_Type_WordPress $this Instance of the field type object.
  76           */
  77          do_action( 'bp_xprofile_field_type_wordpress', $this );
  78  
  79          // Use the `$wpdb->usermeta` table instead of the $bp->profile->table_name_data one.
  80          add_filter( 'bp_xprofile_set_field_data_pre_save', array( $this, 'set_field_value' ), 10, 2 );
  81  
  82          // Set the supported keys.
  83          $this->supported_keys = bp_xprofile_get_wp_user_keys();
  84      }
  85  
  86      /**
  87       * Sanitize the user field before inserting it into db.
  88       *
  89       * @since 8.0.0
  90       *
  91       * @param string $value The user field value.
  92       */
  93      abstract public function sanitize_for_db( $value );
  94  
  95      /**
  96       * Sanitize the user field before displaying it as an attribute.
  97       *
  98       * @since 8.0.0
  99       *
 100       * @param string $value The user field value.
 101       * @param integer $user_id The user ID.
 102       */
 103      abstract public function sanitize_for_output( $value, $user_id = 0 );
 104  
 105      /**
 106       * Sets the WordPress field value.
 107       *
 108       * @since 8.0.0
 109       *
 110       * @param boolean $retval Whether to shortcircuit the $bp->profile->table_name_data table.
 111       *                        Default `false`.
 112       * @param array $field_args {
 113       *     An array of arguments.
 114       *
 115       *     @type object            $field_type_obj Field type object.
 116       *     @type BP_XProfile_Field $field          Field object.
 117       *     @type integer           $user_id        The user ID.
 118       *     @type mixed             $value          Value passed to xprofile_set_field_data().
 119       *     @type boolean           $is_required    Whether or not the field is required.
 120       * }
 121       * @return boolean Whether to shortcircuit the $bp->profile->table_name_data table.
 122       */
 123  	public function set_field_value( $retval = false, $field_args = array() ) {
 124          // Check the wp_user_key is valid and supported.
 125          if ( ! isset( $field_args['field']->type_obj->wp_user_key ) || $this->wp_user_key !== $field_args['field']->type_obj->wp_user_key || ! in_array( $field_args['field']->type_obj->wp_user_key, $this->supported_keys, true ) ) {
 126              return false;
 127          }
 128  
 129          $wp_user_field_value = $this->sanitize_for_db( $field_args['value'] );
 130          $bp_displayed_user   = bp_get_displayed_user();
 131  
 132          if ( isset( $bp_displayed_user->updated_keys ) ) {
 133              $bp_displayed_user->updated_keys[ $this->wp_user_key ] = $wp_user_field_value;
 134              $retval = true;
 135          } else {
 136              $retval = wp_update_user(
 137                  array(
 138                      'ID'               => (int) $field_args['user_id'],
 139                      $this->wp_user_key => $wp_user_field_value,
 140                  )
 141              );
 142          }
 143  
 144          if ( ! is_wp_error( $retval ) ) {
 145              $retval = true;
 146          }
 147  
 148          return $retval;
 149      }
 150  
 151      /**
 152       * Gets the WordPress field value during an xProfile fields loop.
 153       *
 154       * This function is used inside `BP_XProfile_ProfileData::get_data_for_user()`
 155       * to include the WordPress field value into the xProfile fields loop.
 156       *
 157       * @since 8.0.0
 158       *
 159       * @param integer $user_id The user ID.
 160       * @param integer $field_id The xProfile field ID.
 161       * @return array An array containing the metadata `id`, `value` and `table_name`.
 162       */
 163  	public function get_field_value( $user_id, $field_id = 0 ) {
 164          global $wpdb;
 165          $wp_field = array(
 166              'id'         => 0,
 167              'value'      => '',
 168              'table_name' => $wpdb->usermeta,
 169          );
 170  
 171          if ( 'user_url' === $this->wp_user_key ) {
 172              if ( bp_displayed_user_id() ) {
 173                  $wp_field['value'] = bp_get_displayed_user()->userdata->{$this->wp_user_key};
 174              } elseif ( $user_id ) {
 175                  $user = get_user_by( 'id', $user_id );
 176                  $wp_field['value'] = $user->{$this->wp_user_key};
 177              }
 178  
 179              $wp_field['id']         = $user_id;
 180              $wp_field['table_name'] = $wpdb->users;
 181          } else {
 182              $umeta_key = $this->wp_user_key;
 183              $user_mid  = wp_cache_get( $user_id, 'bp_user_mid' );
 184              if ( ! $user_mid ) {
 185                  $user_mid = array();
 186              }
 187  
 188              if ( ! $user_mid ) {
 189                  $list_values = bp_get_user_meta( $user_id, $umeta_key );
 190  
 191                  if ( is_array( $list_values ) ) {
 192                      $wp_field['value'] = reset( $list_values );
 193                      $wp_field['id']    = key( $list_values );
 194  
 195                      if ( 0 === $wp_field['id'] ) {
 196                          /*
 197                          * We can't just update the WP User Meta cache to key again meta values with meta_ids because of
 198                          * `return maybe_unserialize( $meta_cache[ $meta_key ][0] );` in `get_metadata_raw()`.
 199                          */
 200                          $user_meta_cache = wp_cache_get( $user_id, 'user_meta' );
 201  
 202                          if ( $user_meta_cache ) {
 203                              $metas = $wpdb->get_results( $wpdb->prepare( "SELECT umeta_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE user_id = %d ORDER BY umeta_id ASC", $user_id ) );
 204  
 205                              if ( $metas ) {
 206                                  foreach ( $user_meta_cache as $meta_key => $meta_values ) {
 207                                      if ( ! in_array( $meta_key, $this->supported_keys, true ) ) {
 208                                          continue;
 209                                      }
 210  
 211                                      foreach ( $meta_values as $meta_value ) {
 212                                          $meta_object = wp_list_filter( $metas, array( 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );
 213  
 214                                          if ( 1 === count( $meta_object ) ) {
 215                                              $meta_object = reset( $meta_object );
 216                                              $user_mid[ $meta_key ][ $meta_object->umeta_id ] = $meta_value;
 217  
 218                                              // Set the meta_id for the requested field.
 219                                              if ( $umeta_key === $meta_key ) {
 220                                                  $wp_field['id'] = $meta_object->umeta_id;
 221                                              }
 222                                          }
 223                                      }
 224                                  }
 225                              }
 226  
 227                              // Set the User mid cache.
 228                              wp_cache_set( $user_id, $user_mid, 'bp_user_mid' );
 229                          }
 230                      }
 231                  }
 232              }
 233  
 234              if ( isset( $user_mid[ $umeta_key ] ) ) {
 235                  $wp_field['value'] = reset( $user_mid[ $umeta_key ] );
 236                  $wp_field['id']    = key( $user_mid[ $umeta_key ] );
 237              }
 238          }
 239  
 240          return $wp_field;
 241      }
 242  }


Generated: Wed Apr 24 01:01:03 2024 Cross-referenced by PHPXref 0.7.1