[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-profiledata.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileClasses
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Class for XProfile Profile Data setup.
  15   *
  16   * @since 1.6.0
  17   */
  18  class BP_XProfile_ProfileData {
  19  
  20      /**
  21       * XProfile ID.
  22       *
  23       * @since 1.6.0
  24       * @var int $id
  25       */
  26      public $id;
  27  
  28      /**
  29       * User ID.
  30       *
  31       * @since 1.6.0
  32       * @var int $user_id
  33       */
  34      public $user_id;
  35  
  36      /**
  37       * XProfile field ID.
  38       *
  39       * @since 1.6.0
  40       * @var int $field_id
  41       */
  42      public $field_id;
  43  
  44      /**
  45       * XProfile field value.
  46       *
  47       * @since 1.6.0
  48       * @var string $value
  49       */
  50      public $value;
  51  
  52      /**
  53       * XProfile field last updated time.
  54       *
  55       * @since 1.6.0
  56       * @var string $last_updated
  57       */
  58      public $last_updated;
  59  
  60      /**
  61       * BP_XProfile_ProfileData constructor.
  62       *
  63       * @since 1.5.0
  64       *
  65       * @param int|null $field_id Field ID to instantiate.
  66       * @param int|null $user_id  User ID to instantiate for.
  67       */
  68  	public function __construct( $field_id = null, $user_id = null ) {
  69          if ( !empty( $field_id ) ) {
  70              $this->populate( $field_id, $user_id );
  71          }
  72      }
  73  
  74      /**
  75       * Populates the XProfile profile data.
  76       *
  77       * @since 1.0.0
  78       *
  79       * @param int $field_id Field ID to populate.
  80       * @param int $user_id  User ID to populate for.
  81       */
  82  	public function populate( $field_id, $user_id ) {
  83          global $wpdb;
  84  
  85          $cache_key   = "{$user_id}:{$field_id}";
  86          $profiledata = wp_cache_get( $cache_key, 'bp_xprofile_data' );
  87  
  88          if ( false === $profiledata ) {
  89              $bp = buddypress();
  90  
  91              $sql         = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id );
  92              $profiledata = $wpdb->get_row( $sql );
  93  
  94              if ( $profiledata ) {
  95                  wp_cache_set( $cache_key, $profiledata, 'bp_xprofile_data' );
  96              }
  97          }
  98  
  99          if ( $profiledata ) {
 100              $this->id           = (int) $profiledata->id;
 101              $this->user_id      = (int) $profiledata->user_id;
 102              $this->field_id     = (int) $profiledata->field_id;
 103              $this->value        = stripslashes( $profiledata->value );
 104              $this->last_updated = $profiledata->last_updated;
 105  
 106          } else {
 107              // When no row is found, we'll need to set these properties manually.
 108              $this->field_id        = (int) $field_id;
 109              $this->user_id        = (int) $user_id;
 110          }
 111      }
 112  
 113      /**
 114       * Check if there is data already for the user.
 115       *
 116       * @since 1.0.0
 117       *
 118       * @global object $wpdb
 119       * @global array $bp
 120       *
 121       * @return bool
 122       */
 123  	public function exists() {
 124          global $wpdb;
 125  
 126          // Check cache first.
 127          $cache_key = "{$this->user_id}:{$this->field_id}";
 128          $cached    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
 129  
 130          if ( $cached && ! empty( $cached->id ) ) {
 131              $retval = true;
 132          } else {
 133              $bp = buddypress();
 134              $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = %d", $this->user_id, $this->field_id ) );
 135          }
 136  
 137          /**
 138           * Filters whether or not data already exists for the user.
 139           *
 140           * @since 1.2.7
 141           *
 142           * @param bool                    $retval Whether or not data already exists.
 143           * @param BP_XProfile_ProfileData $this   Instance of the current BP_XProfile_ProfileData class.
 144           */
 145          return apply_filters_ref_array( 'xprofile_data_exists', array( (bool)$retval, $this ) );
 146      }
 147  
 148      /**
 149       * Check if this data is for a valid field.
 150       *
 151       * @since 1.0.0
 152       *
 153       * @global object $wpdb
 154       *
 155       * @return bool
 156       */
 157  	public function is_valid_field() {
 158          global $wpdb;
 159  
 160          $bp = buddypress();
 161  
 162          $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id = %d", $this->field_id ) );
 163  
 164          /**
 165           * Filters whether or not data is for a valid field.
 166           *
 167           * @since 1.2.7
 168           *
 169           * @param bool                    $retval Whether or not data is valid.
 170           * @param BP_XProfile_ProfileData $this   Instance of the current BP_XProfile_ProfileData class.
 171           */
 172          return apply_filters_ref_array( 'xprofile_data_is_valid_field', array( (bool)$retval, $this ) );
 173      }
 174  
 175      /**
 176       * Save the data for the XProfile field.
 177       *
 178       * @since 1.0.0
 179       *
 180       * @return bool
 181       */
 182  	public function save() {
 183          global $wpdb;
 184  
 185          $bp = buddypress();
 186  
 187          /**
 188           * Filters the data's user ID before saving to the database.
 189           *
 190           * @since 1.0.0
 191           *
 192           * @param int $user_id The user ID.
 193           * @param int $data_id The field data ID.
 194           */
 195          $this->user_id = apply_filters( 'xprofile_data_user_id_before_save', $this->user_id, $this->id );
 196  
 197          /**
 198           * Filters the data's field ID before saving to the database.
 199           *
 200           * @since 1.0.0
 201           *
 202           * @param int $field_id The field ID.
 203           * @param int $data_id  The field data ID.
 204           */
 205          $this->field_id = apply_filters( 'xprofile_data_field_id_before_save', $this->field_id, $this->id );
 206  
 207          /**
 208           * Filters the data's value before saving to the database.
 209           *
 210           * @since 1.0.0
 211           * @since 2.1.0 Added `$reserialize` and `$this` parameters.
 212           *
 213           * @param string                  $field_value The field value.
 214           * @param int                     $data_id     The field data ID.
 215           * @param bool                    $reserialize Whether to reserialize arrays before returning. Defaults to true.
 216           * @param BP_XProfile_ProfileData $this        Current instance of the profile data being saved.
 217           */
 218          $this->value = apply_filters( 'xprofile_data_value_before_save', $this->value, $this->id, true, $this );
 219  
 220          /**
 221           * Filters the data's last updated timestamp before saving to the database.
 222           *
 223           * @since 1.0.0
 224           *
 225           * @param int $last_updated The last updated timestamp.
 226           * @param int $data_id      The field data ID.
 227           */
 228          $this->last_updated = apply_filters( 'xprofile_data_last_updated_before_save', bp_core_current_time(), $this->id );
 229  
 230          /**
 231           * Fires before the current profile data instance gets saved.
 232           *
 233           * Please use this hook to filter the properties above. Each part will be passed in.
 234           *
 235           * @since 1.0.0
 236           *
 237           * @param BP_XProfile_ProfileData $this Current instance of the profile data being saved.
 238           */
 239          do_action_ref_array( 'xprofile_data_before_save', array( $this ) );
 240  
 241          if ( $this->is_valid_field() ) {
 242              if ( $this->exists() && strlen( trim( $this->value ) ) ) {
 243                  $result   = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_data} SET value = %s, last_updated = %s WHERE user_id = %d AND field_id = %d", $this->value, $this->last_updated, $this->user_id, $this->field_id ) );
 244  
 245              } elseif ( $this->exists() && empty( $this->value ) ) {
 246                  // Data removed, delete the entry.
 247                  $result   = $this->delete();
 248  
 249              } else {
 250                  $result   = $wpdb->query( $wpdb->prepare("INSERT INTO {$bp->profile->table_name_data} (user_id, field_id, value, last_updated) VALUES (%d, %d, %s, %s)", $this->user_id, $this->field_id, $this->value, $this->last_updated ) );
 251                  $this->id = $wpdb->insert_id;
 252              }
 253  
 254              if ( false === $result ) {
 255                  return false;
 256              }
 257  
 258              /**
 259               * Fires after the current profile data instance gets saved.
 260               *
 261               * @since 1.0.0
 262               *
 263               * @param BP_XProfile_ProfileData $this Current instance of the profile data being saved.
 264               */
 265              do_action_ref_array( 'xprofile_data_after_save', array( $this ) );
 266  
 267              return true;
 268          }
 269  
 270          return false;
 271      }
 272  
 273      /**
 274       * Delete specific XProfile field data.
 275       *
 276       * @since 1.0.0
 277       *
 278       * @global object $wpdb
 279       *
 280       * @return boolean
 281       */
 282  	public function delete() {
 283          global $wpdb;
 284  
 285          $bp = buddypress();
 286  
 287          /**
 288           * Fires before the current profile data instance gets deleted.
 289           *
 290           * @since 1.9.0
 291           *
 292           * @param BP_XProfile_ProfileData $this Current instance of the profile data being deleted.
 293           */
 294          do_action_ref_array( 'xprofile_data_before_delete', array( $this ) );
 295  
 296          $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $this->field_id, $this->user_id ) );
 297          if ( empty( $deleted ) ) {
 298              return false;
 299          }
 300  
 301          /**
 302           * Fires after the current profile data instance gets deleted.
 303           *
 304           * @since 1.9.0
 305           *
 306           * @param BP_XProfile_ProfileData $this Current instance of the profile data being deleted.
 307           */
 308          do_action_ref_array( 'xprofile_data_after_delete', array( $this ) );
 309  
 310          return true;
 311      }
 312  
 313      /** Static Methods ********************************************************/
 314  
 315      /**
 316       * Get a user's profile data for a set of fields.
 317       *
 318       * @since 2.0.0
 319       * @since 8.0.0 Checks if a null field data is an xProfile WP Field.
 320       *              Adds a new parameter `$field_type_objects` to pass the list of field type objects.
 321       *
 322       * @param int   $user_id            ID of user whose data is being queried.
 323       * @param array $field_ids          Array of field IDs to query for.
 324       * @param array $field_type_objects Array of field type objects keyed by the queried filed IDs.
 325       * @return array
 326       */
 327  	public static function get_data_for_user( $user_id, $field_ids, $field_type_objects = array() ) {
 328          global $wpdb;
 329  
 330          $data = array();
 331  
 332          $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids );
 333  
 334          // Prime the cache.
 335          if ( ! empty( $uncached_field_ids ) ) {
 336              $bp = buddypress();
 337              $uncached_field_ids_sql = implode( ',', wp_parse_id_list( $uncached_field_ids ) );
 338              $uncached_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id IN ({$uncached_field_ids_sql}) AND user_id = %d", $user_id ) );
 339  
 340              // Rekey.
 341              $queried_data = array();
 342              foreach ( $uncached_data as $ud ) {
 343                  $d               = new stdClass;
 344                  $d->id           = $ud->id;
 345                  $d->table_name   = $bp->profile->table_name_data;
 346                  $d->user_id      = $ud->user_id;
 347                  $d->field_id     = $ud->field_id;
 348                  $d->value        = $ud->value;
 349                  $d->last_updated = $ud->last_updated;
 350  
 351                  $queried_data[ $ud->field_id ] = $d;
 352              }
 353  
 354              // Set caches.
 355              foreach ( $uncached_field_ids as $field_id ) {
 356  
 357                  $cache_key = "{$user_id}:{$field_id}";
 358  
 359                  // If a value was found, cache it.
 360                  if ( isset( $queried_data[ $field_id ] ) && ! isset( $field_type_objects[ $field_id ]->wp_user_key ) ) {
 361                      wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' );
 362  
 363                  // If no value was found, cache an empty item
 364                  // to avoid future cache misses.
 365                  } else {
 366                      $d = new stdClass;
 367  
 368                      // Check if it's a WordPress field.
 369                      if ( isset( $field_type_objects[ $field_id ]->wp_user_key ) ) {
 370                          $meta          = $field_type_objects[ $field_id ]->get_field_value( $user_id, $field_id );
 371                          $d->id         = $meta['id'];
 372                          $d->value      = $meta['value'];
 373                          $d->table_name = $meta['table_name'];
 374  
 375                      } else {
 376                          $d->id         = '';
 377                          $d->value      = '';
 378                          $d->table_name = '';
 379                      }
 380  
 381                      $d->user_id      = $user_id;
 382                      $d->field_id     = $field_id;
 383                      $d->last_updated = '';
 384  
 385                      wp_cache_set( $cache_key, $d, 'bp_xprofile_data' );
 386                  }
 387              }
 388          }
 389  
 390          // Now that all items are cached, fetch them.
 391          foreach ( $field_ids as $field_id ) {
 392              $cache_key = "{$user_id}:{$field_id}";
 393              $data[]    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
 394          }
 395  
 396          // Integer casting.
 397          foreach ( (array) $data as $key => $d ) {
 398              if ( isset( $data[ $key ]->id ) ) {
 399                  $data[ $key ]->id = (int) $data[ $key ]->id;
 400              }
 401              if ( isset( $data[ $key ]->user_id ) ) {
 402                  $data[ $key ]->user_id  = (int) $data[ $key ]->user_id;
 403              }
 404  
 405              $data[ $key ]->field_id = (int) $data[ $key ]->field_id;
 406          }
 407  
 408          return $data;
 409      }
 410  
 411      /**
 412       * Get all of the profile information for a specific user.
 413       *
 414       * @since 1.2.0
 415       * @since 8.0.0 Checks if a null field data is an xProfile WP Field.
 416       *
 417       * @param int $user_id ID of the user.
 418       * @return array
 419       */
 420  	public static function get_all_for_user( $user_id ) {
 421  
 422          $groups = bp_xprofile_get_groups( array(
 423              'user_id'                => $user_id,
 424              'hide_empty_groups'      => true,
 425              'hide_empty_fields'      => true,
 426              'fetch_fields'           => true,
 427              'fetch_field_data'       => true,
 428          ) );
 429  
 430          $profile_data = array();
 431  
 432          if ( ! empty( $groups ) ) {
 433              $user = new WP_User( $user_id );
 434  
 435              $profile_data['user_login']    = $user->user_login;
 436              $profile_data['user_nicename'] = $user->user_nicename;
 437              $profile_data['user_email']    = $user->user_email;
 438  
 439              foreach ( (array) $groups as $group ) {
 440                  if ( empty( $group->fields ) ) {
 441                      continue;
 442                  }
 443  
 444                  foreach ( (array) $group->fields as $field ) {
 445                      $profile_data[ $field->name ] = array(
 446                          'field_group_id'   => $group->id,
 447                          'field_group_name' => $group->name,
 448                          'field_id'         => $field->id,
 449                          'field_type'       => $field->type,
 450                      );
 451  
 452                      if ( is_null( $field->data ) ) {
 453                          if ( 1 === $field->id ) {
 454                              $profile_data[ $field->name ]['field_data'] = $user->display_name;
 455                          } elseif ( isset( $field->type_obj ) && $field->type_obj instanceof BP_XProfile_Field_Type && isset( $field->type_obj->wp_user_key ) ) {
 456                              $meta = $field->type_obj->get_field_value( $user->ID, $field->id );
 457  
 458                              if ( isset( $meta['value'] ) ) {
 459                                  $profile_data[ $field->name ]['field_data'] = $meta['value'];
 460                              }
 461                          } else {
 462                              $profile_data[ $field->name ]['field_data'] = false;
 463                          }
 464                      } else {
 465                          $profile_data[ $field->name ]['field_data'] = $field->data->value;
 466                      }
 467                  }
 468              }
 469          }
 470  
 471          return $profile_data;
 472      }
 473  
 474      /**
 475       * Get the user's field data id by the id of the xprofile field.
 476       *
 477       * @since 1.6.0
 478       *
 479       * @param int $field_id Field ID being queried for.
 480       * @param int $user_id  User ID associated with field.
 481       * @return int $fielddata_id
 482       */
 483  	public static function get_fielddataid_byid( $field_id, $user_id ) {
 484          global $wpdb;
 485  
 486          if ( empty( $field_id ) || empty( $user_id ) ) {
 487              $fielddata_id = 0;
 488          } else {
 489              $bp = buddypress();
 490  
 491              // Check cache first.
 492              $cache_key = "{$user_id}:{$field_id}";
 493              $fielddata = wp_cache_get( $cache_key, 'bp_xprofile_data' );
 494              if ( false === $fielddata || empty( $fielddata->id ) ) {
 495                  $fielddata_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ) );
 496              } else {
 497                  $fielddata_id = $fielddata->id;
 498              }
 499          }
 500  
 501          return (int) $fielddata_id;
 502      }
 503  
 504      /**
 505       * Get profile field values by field ID and user IDs.
 506       *
 507       * Supports multiple user IDs.
 508       *
 509       * @since 1.0.0
 510       * @since 8.0.0 Checks if a null field data is an xProfile WP Field.
 511       *
 512       * @param int            $field_id ID of the field.
 513       * @param int|array|null $user_ids ID or IDs of user(s).
 514       * @return string|array Single value if a single user is queried,
 515       *                      otherwise an array of results.
 516       */
 517  	public static function get_value_byid( $field_id, $user_ids = null ) {
 518          global $wpdb;
 519  
 520          if ( empty( $user_ids ) ) {
 521              $user_ids = bp_displayed_user_id();
 522          }
 523  
 524          $return_single_result = false;
 525          if ( ! is_array( $user_ids ) ) {
 526              $return_single_result = true;
 527          }
 528  
 529          $user_ids = wp_parse_id_list( $user_ids );
 530  
 531          // Assemble uncached IDs.
 532          $uncached_ids = array();
 533          foreach ( $user_ids as $user_id ) {
 534              $cache_key = "{$user_id}:{$field_id}";
 535              if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) {
 536                  $uncached_ids[] = $user_id;
 537              }
 538          }
 539  
 540          // Prime caches.
 541          if ( ! empty( $uncached_ids ) ) {
 542              $bp = buddypress();
 543              $uncached_ids_sql = implode( ',', $uncached_ids );
 544              $queried_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$uncached_ids_sql})", $field_id ) );
 545  
 546              // Rekey.
 547              $qd = array();
 548              foreach ( $queried_data as $data ) {
 549                  $qd[ $data->user_id ] = $data;
 550              }
 551  
 552              foreach ( $uncached_ids as $id ) {
 553                  // The value was successfully fetched.
 554                  if ( isset( $qd[ $id ] ) ) {
 555                      $d = $qd[ $id ];
 556  
 557                  // No data found for the user, so we fake it to
 558                  // avoid cache misses and PHP notices.
 559                  } else {
 560                      $d          = new stdClass;
 561                      $field_type = bp_xprofile_get_field_type( $field_id );
 562  
 563                      // Check WordPress if it's a WordPress field.
 564                      if ( isset( $field_type->wp_user_key ) ) {
 565                          $meta          = $field_type->get_field_value( $user_id, $field_id );
 566                          $d->id         = $meta['id'];
 567                          $d->value      = $meta['value'];
 568                          $d->table_name = $meta['table_name'];
 569  
 570                      } else {
 571                          $d->id    = '';
 572                          $d->value = '';
 573                      }
 574  
 575                      $d->table_name   = '';
 576                      $d->user_id      = $id;
 577                      $d->field_id     = $field_id;
 578                      $d->last_updated = '';
 579                  }
 580  
 581                  $cache_key = "{$d->user_id}:{$field_id}";
 582                  wp_cache_set( $cache_key, $d, 'bp_xprofile_data' );
 583              }
 584          }
 585  
 586          // Now that the cache is primed with all data, fetch it.
 587          $data = array();
 588          foreach ( $user_ids as $user_id ) {
 589              $cache_key = "{$user_id}:{$field_id}";
 590              $data[]    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
 591          }
 592  
 593          // Integer casting.
 594          foreach ( (array) $data as $key => $d ) {
 595              if ( isset( $data[ $key ]->id ) ) {
 596                  $data[ $key ]->id = (int) $data[ $key ]->id;
 597              }
 598              if ( isset( $data[ $key ]->user_id ) ) {
 599                  $data[ $key ]->user_id  = (int) $data[ $key ]->user_id;
 600              }
 601  
 602              $data[ $key ]->field_id = (int) $data[ $key ]->field_id;
 603          }
 604  
 605          // If a single ID was passed, just return the value.
 606          if ( $return_single_result ) {
 607              return $data[0]->value;
 608  
 609          // Otherwise return the whole array.
 610          } else {
 611              return $data;
 612          }
 613      }
 614  
 615      /**
 616       * Get profile field values by field name and user ID.
 617       *
 618       * @since 1.0.0
 619       * @deprecated 8.0.0 This function is not used anymore.
 620       *
 621       * @param array|string $fields  Field(s) to get.
 622       * @param int|null     $user_id User ID to get field data for.
 623       * @return array|bool
 624       */
 625  	public static function get_value_byfieldname( $fields, $user_id = null ) {
 626          _deprecated_function( __FUNCTION__, '8.0.0' );
 627          global $wpdb;
 628  
 629          if ( empty( $fields ) ) {
 630              return false;
 631          }
 632  
 633          $bp = buddypress();
 634  
 635          if ( empty( $user_id ) ) {
 636              $user_id = bp_displayed_user_id();
 637          }
 638  
 639          $field_sql = '';
 640  
 641          if ( is_array( $fields ) ) {
 642              for ( $i = 0, $count = count( $fields ); $i < $count; ++$i ) {
 643                  if ( $i == 0 ) {
 644                      $field_sql .= $wpdb->prepare( "AND ( f.name = %s ", $fields[$i] );
 645                  } else {
 646                      $field_sql .= $wpdb->prepare( "OR f.name = %s ", $fields[$i] );
 647                  }
 648              }
 649  
 650              $field_sql .= ')';
 651          } else {
 652              $field_sql .= $wpdb->prepare( "AND f.name = %s", $fields );
 653          }
 654  
 655          $sql    = $wpdb->prepare( "SELECT d.value, f.name FROM {$bp->profile->table_name_data} d, {$bp->profile->table_name_fields} f WHERE d.field_id = f.id AND d.user_id = %d AND f.parent_id = 0 $field_sql", $user_id );
 656          $values = $wpdb->get_results( $sql );
 657  
 658          if ( empty( $values ) || is_wp_error( $values ) ) {
 659              return false;
 660          }
 661  
 662          $new_values = array();
 663  
 664          if ( is_array( $fields ) ) {
 665              for ( $i = 0, $count = count( $values ); $i < $count; ++$i ) {
 666                  for ( $j = 0; $j < count( $fields ); $j++ ) {
 667                      if ( $values[$i]->name == $fields[$j] ) {
 668                          $new_values[$fields[$j]] = $values[$i]->value;
 669                      } elseif ( !array_key_exists( $fields[$j], $new_values ) ) {
 670                          $new_values[$fields[$j]] = NULL;
 671                      }
 672                  }
 673              }
 674          } else {
 675              $new_values = $values[0]->value;
 676          }
 677  
 678          return $new_values;
 679      }
 680  
 681      /**
 682       * Delete field.
 683       *
 684       * @since 1.0.0
 685       *
 686       * @param int $field_id ID of the field to delete.
 687       * @return bool
 688       */
 689  	public static function delete_for_field( $field_id ) {
 690          global $wpdb;
 691  
 692          $bp      = buddypress();
 693          $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d", $field_id ) );
 694          if ( empty( $deleted ) || is_wp_error( $deleted ) ) {
 695              return false;
 696          }
 697  
 698          return true;
 699      }
 700  
 701      /**
 702       * Get time for last XProfile field data update by user.
 703       *
 704       * @since 1.0.0
 705       *
 706       * @param int $user_id User ID to get time for.
 707       * @return null|string
 708       */
 709  	public static function get_last_updated( $user_id ) {
 710          global $wpdb;
 711  
 712          $bp = buddypress();
 713  
 714          $last_updated = $wpdb->get_var( $wpdb->prepare( "SELECT last_updated FROM {$bp->profile->table_name_data} WHERE user_id = %d ORDER BY last_updated LIMIT 1", $user_id ) );
 715  
 716          return $last_updated;
 717      }
 718  
 719      /**
 720       * Delete all data for provided user ID.
 721       *
 722       * @since 1.0.0
 723       *
 724       * @param int $user_id User ID to remove data for.
 725       * @return false|int
 726       */
 727  	public static function delete_data_for_user( $user_id ) {
 728          global $wpdb;
 729  
 730          $bp = buddypress();
 731  
 732          $field_ids = $wpdb->get_col( $wpdb->prepare( "SELECT field_id FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) );
 733  
 734          if ( ! $field_ids ) {
 735              return false;
 736          }
 737  
 738          foreach ( $field_ids as $field_id ) {
 739              xprofile_delete_field_data( $field_id, $user_id );
 740          }
 741  
 742          return count( $field_ids );
 743      }
 744  
 745      /**
 746       * Get random field type by user ID.
 747       *
 748       * @since 1.0.0
 749       *
 750       * @param int    $user_id          User ID to query for.
 751       * @param string $exclude_fullname SQL portion used to exclude by field ID.
 752       * @return array|null|object
 753       */
 754  	public static function get_random( $user_id, $exclude_fullname ) {
 755          global $wpdb;
 756  
 757          $exclude_sql = ! empty( $exclude_fullname ) ? ' AND pf.id != 1' : '';
 758  
 759          $bp = buddypress();
 760  
 761          return $wpdb->get_results( $wpdb->prepare( "SELECT pf.type, pf.name, pd.value FROM {$bp->profile->table_name_data} pd INNER JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id AND pd.user_id = %d {$exclude_sql} ORDER BY RAND() LIMIT 1", $user_id ) );
 762      }
 763  
 764      /**
 765       * Get fullname for provided user ID.
 766       *
 767       * @since 1.0.0
 768       *
 769       * @param int $user_id ID of the user to query.
 770       * @return mixed
 771       */
 772  	public static function get_fullname( $user_id = 0 ) {
 773  
 774          if ( empty( $user_id ) ) {
 775              $user_id = bp_displayed_user_id();
 776          }
 777  
 778          return xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id );
 779      }
 780  }


Generated: Sun Apr 18 01:01:43 2021 Cross-referenced by PHPXref 0.7.1