[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
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 * 320 * @param int $user_id ID of user whose data is being queried. 321 * @param array $field_ids Array of field IDs to query for. 322 * @return array 323 */ 324 public static function get_data_for_user( $user_id, $field_ids ) { 325 global $wpdb; 326 327 $data = array(); 328 329 $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids ); 330 331 // Prime the cache. 332 if ( ! empty( $uncached_field_ids ) ) { 333 $bp = buddypress(); 334 $uncached_field_ids_sql = implode( ',', wp_parse_id_list( $uncached_field_ids ) ); 335 $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 ) ); 336 337 // Rekey. 338 $queried_data = array(); 339 foreach ( $uncached_data as $ud ) { 340 $d = new stdClass; 341 $d->id = $ud->id; 342 $d->user_id = $ud->user_id; 343 $d->field_id = $ud->field_id; 344 $d->value = $ud->value; 345 $d->last_updated = $ud->last_updated; 346 347 $queried_data[ $ud->field_id ] = $d; 348 } 349 350 // Set caches. 351 foreach ( $uncached_field_ids as $field_id ) { 352 353 $cache_key = "{$user_id}:{$field_id}"; 354 355 // If a value was found, cache it. 356 if ( isset( $queried_data[ $field_id ] ) ) { 357 wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' ); 358 359 // If no value was found, cache an empty item 360 // to avoid future cache misses. 361 } else { 362 $d = new stdClass; 363 $d->id = ''; 364 $d->user_id = $user_id; 365 $d->field_id = $field_id; 366 $d->value = ''; 367 $d->last_updated = ''; 368 369 wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); 370 } 371 } 372 } 373 374 // Now that all items are cached, fetch them. 375 foreach ( $field_ids as $field_id ) { 376 $cache_key = "{$user_id}:{$field_id}"; 377 $data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 378 } 379 380 // Integer casting. 381 foreach ( (array) $data as $key => $d ) { 382 if ( isset( $data[ $key ]->id ) ) { 383 $data[ $key ]->id = (int) $data[ $key ]->id; 384 } 385 if ( isset( $data[ $key ]->user_id ) ) { 386 $data[ $key ]->user_id = (int) $data[ $key ]->user_id; 387 } 388 389 $data[ $key ]->field_id = (int) $data[ $key ]->field_id; 390 } 391 392 return $data; 393 } 394 395 /** 396 * Get all of the profile information for a specific user. 397 * 398 * @since 1.2.0 399 * 400 * @param int $user_id ID of the user. 401 * @return array 402 */ 403 public static function get_all_for_user( $user_id ) { 404 405 $groups = bp_xprofile_get_groups( array( 406 'user_id' => $user_id, 407 'hide_empty_groups' => true, 408 'hide_empty_fields' => true, 409 'fetch_fields' => true, 410 'fetch_field_data' => true, 411 ) ); 412 413 $profile_data = array(); 414 415 if ( ! empty( $groups ) ) { 416 $user = new WP_User( $user_id ); 417 418 $profile_data['user_login'] = $user->user_login; 419 $profile_data['user_nicename'] = $user->user_nicename; 420 $profile_data['user_email'] = $user->user_email; 421 422 foreach ( (array) $groups as $group ) { 423 if ( empty( $group->fields ) ) { 424 continue; 425 } 426 427 foreach ( (array) $group->fields as $field ) { 428 $profile_data[ $field->name ] = array( 429 'field_group_id' => $group->id, 430 'field_group_name' => $group->name, 431 'field_id' => $field->id, 432 'field_type' => $field->type, 433 'field_data' => $field->data->value, 434 ); 435 } 436 } 437 } 438 439 return $profile_data; 440 } 441 442 /** 443 * Get the user's field data id by the id of the xprofile field. 444 * 445 * @since 1.6.0 446 * 447 * @param int $field_id Field ID being queried for. 448 * @param int $user_id User ID associated with field. 449 * @return int $fielddata_id 450 */ 451 public static function get_fielddataid_byid( $field_id, $user_id ) { 452 global $wpdb; 453 454 if ( empty( $field_id ) || empty( $user_id ) ) { 455 $fielddata_id = 0; 456 } else { 457 $bp = buddypress(); 458 459 // Check cache first. 460 $cache_key = "{$user_id}:{$field_id}"; 461 $fielddata = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 462 if ( false === $fielddata || empty( $fielddata->id ) ) { 463 $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 ) ); 464 } else { 465 $fielddata_id = $fielddata->id; 466 } 467 } 468 469 return (int) $fielddata_id; 470 } 471 472 /** 473 * Get profile field values by field ID and user IDs. 474 * 475 * Supports multiple user IDs. 476 * 477 * @since 1.0.0 478 * 479 * @param int $field_id ID of the field. 480 * @param int|array|null $user_ids ID or IDs of user(s). 481 * @return string|array Single value if a single user is queried, 482 * otherwise an array of results. 483 */ 484 public static function get_value_byid( $field_id, $user_ids = null ) { 485 global $wpdb; 486 487 if ( empty( $user_ids ) ) { 488 $user_ids = bp_displayed_user_id(); 489 } 490 491 $return_single_result = false; 492 if ( ! is_array( $user_ids ) ) { 493 $return_single_result = true; 494 } 495 496 $user_ids = wp_parse_id_list( $user_ids ); 497 498 // Assemble uncached IDs. 499 $uncached_ids = array(); 500 foreach ( $user_ids as $user_id ) { 501 $cache_key = "{$user_id}:{$field_id}"; 502 if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) { 503 $uncached_ids[] = $user_id; 504 } 505 } 506 507 // Prime caches. 508 if ( ! empty( $uncached_ids ) ) { 509 $bp = buddypress(); 510 $uncached_ids_sql = implode( ',', $uncached_ids ); 511 $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 ) ); 512 513 // Rekey. 514 $qd = array(); 515 foreach ( $queried_data as $data ) { 516 $qd[ $data->user_id ] = $data; 517 } 518 519 foreach ( $uncached_ids as $id ) { 520 // The value was successfully fetched. 521 if ( isset( $qd[ $id ] ) ) { 522 $d = $qd[ $id ]; 523 524 // No data found for the user, so we fake it to 525 // avoid cache misses and PHP notices. 526 } else { 527 $d = new stdClass; 528 $d->id = ''; 529 $d->user_id = $id; 530 $d->field_id = $field_id; 531 $d->value = ''; 532 $d->last_updated = ''; 533 } 534 535 $cache_key = "{$d->user_id}:{$field_id}"; 536 wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); 537 } 538 } 539 540 // Now that the cache is primed with all data, fetch it. 541 $data = array(); 542 foreach ( $user_ids as $user_id ) { 543 $cache_key = "{$user_id}:{$field_id}"; 544 $data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 545 } 546 547 // Integer casting. 548 foreach ( (array) $data as $key => $d ) { 549 if ( isset( $data[ $key ]->id ) ) { 550 $data[ $key ]->id = (int) $data[ $key ]->id; 551 } 552 if ( isset( $data[ $key ]->user_id ) ) { 553 $data[ $key ]->user_id = (int) $data[ $key ]->user_id; 554 } 555 556 $data[ $key ]->field_id = (int) $data[ $key ]->field_id; 557 } 558 559 // If a single ID was passed, just return the value. 560 if ( $return_single_result ) { 561 return $data[0]->value; 562 563 // Otherwise return the whole array. 564 } else { 565 return $data; 566 } 567 } 568 569 /** 570 * Get profile field values by field name and user ID. 571 * 572 * @since 1.0.0 573 * 574 * @param array|string $fields Field(s) to get. 575 * @param int|null $user_id User ID to get field data for. 576 * @return array|bool 577 */ 578 public static function get_value_byfieldname( $fields, $user_id = null ) { 579 global $wpdb; 580 581 if ( empty( $fields ) ) { 582 return false; 583 } 584 585 $bp = buddypress(); 586 587 if ( empty( $user_id ) ) { 588 $user_id = bp_displayed_user_id(); 589 } 590 591 $field_sql = ''; 592 593 if ( is_array( $fields ) ) { 594 for ( $i = 0, $count = count( $fields ); $i < $count; ++$i ) { 595 if ( $i == 0 ) { 596 $field_sql .= $wpdb->prepare( "AND ( f.name = %s ", $fields[$i] ); 597 } else { 598 $field_sql .= $wpdb->prepare( "OR f.name = %s ", $fields[$i] ); 599 } 600 } 601 602 $field_sql .= ')'; 603 } else { 604 $field_sql .= $wpdb->prepare( "AND f.name = %s", $fields ); 605 } 606 607 $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 ); 608 $values = $wpdb->get_results( $sql ); 609 610 if ( empty( $values ) || is_wp_error( $values ) ) { 611 return false; 612 } 613 614 $new_values = array(); 615 616 if ( is_array( $fields ) ) { 617 for ( $i = 0, $count = count( $values ); $i < $count; ++$i ) { 618 for ( $j = 0; $j < count( $fields ); $j++ ) { 619 if ( $values[$i]->name == $fields[$j] ) { 620 $new_values[$fields[$j]] = $values[$i]->value; 621 } elseif ( !array_key_exists( $fields[$j], $new_values ) ) { 622 $new_values[$fields[$j]] = NULL; 623 } 624 } 625 } 626 } else { 627 $new_values = $values[0]->value; 628 } 629 630 return $new_values; 631 } 632 633 /** 634 * Delete field. 635 * 636 * @since 1.0.0 637 * 638 * @param int $field_id ID of the field to delete. 639 * @return bool 640 */ 641 public static function delete_for_field( $field_id ) { 642 global $wpdb; 643 644 $bp = buddypress(); 645 $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d", $field_id ) ); 646 if ( empty( $deleted ) || is_wp_error( $deleted ) ) { 647 return false; 648 } 649 650 return true; 651 } 652 653 /** 654 * Get time for last XProfile field data update by user. 655 * 656 * @since 1.0.0 657 * 658 * @param int $user_id User ID to get time for. 659 * @return null|string 660 */ 661 public static function get_last_updated( $user_id ) { 662 global $wpdb; 663 664 $bp = buddypress(); 665 666 $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 ) ); 667 668 return $last_updated; 669 } 670 671 /** 672 * Delete all data for provided user ID. 673 * 674 * @since 1.0.0 675 * 676 * @param int $user_id User ID to remove data for. 677 * @return false|int 678 */ 679 public static function delete_data_for_user( $user_id ) { 680 global $wpdb; 681 682 $bp = buddypress(); 683 684 $field_ids = $wpdb->get_col( $wpdb->prepare( "SELECT field_id FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) ); 685 686 if ( ! $field_ids ) { 687 return false; 688 } 689 690 foreach ( $field_ids as $field_id ) { 691 xprofile_delete_field_data( $field_id, $user_id ); 692 } 693 694 return count( $field_ids ); 695 } 696 697 /** 698 * Get random field type by user ID. 699 * 700 * @since 1.0.0 701 * 702 * @param int $user_id User ID to query for. 703 * @param string $exclude_fullname SQL portion used to exclude by field ID. 704 * @return array|null|object 705 */ 706 public static function get_random( $user_id, $exclude_fullname ) { 707 global $wpdb; 708 709 $exclude_sql = ! empty( $exclude_fullname ) ? ' AND pf.id != 1' : ''; 710 711 $bp = buddypress(); 712 713 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 ) ); 714 } 715 716 /** 717 * Get fullname for provided user ID. 718 * 719 * @since 1.0.0 720 * 721 * @param int $user_id ID of the user to query. 722 * @return mixed 723 */ 724 public static function get_fullname( $user_id = 0 ) { 725 726 if ( empty( $user_id ) ) { 727 $user_id = bp_displayed_user_id(); 728 } 729 730 return xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id ); 731 } 732 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Jan 23 01:01:37 2021 | Cross-referenced by PHPXref 0.7.1 |