BP_XProfile_ProfileData

Class for XProfile Profile Data setup.

Defined (1)

The class is defined in the following location(s).

/bp-xprofile/classes/class-bp-xprofile-profiledata.php  
  1. class BP_XProfile_ProfileData { 
  2.  
  3. /** 
  4. * XProfile ID. 
  5. * @since 1.6.0 
  6. * @var int $id 
  7. */ 
  8. public $id; 
  9.  
  10. /** 
  11. * User ID. 
  12. * @since 1.6.0 
  13. * @var int $user_id 
  14. */ 
  15. public $user_id; 
  16.  
  17. /** 
  18. * XProfile field ID. 
  19. * @since 1.6.0 
  20. * @var int $field_id 
  21. */ 
  22. public $field_id; 
  23.  
  24. /** 
  25. * XProfile field value. 
  26. * @since 1.6.0 
  27. * @var string $value 
  28. */ 
  29. public $value; 
  30.  
  31. /** 
  32. * XProfile field last updated time. 
  33. * @since 1.6.0 
  34. * @var string $last_updated 
  35. */ 
  36. public $last_updated; 
  37.  
  38. /** 
  39. * BP_XProfile_ProfileData constructor. 
  40. * @since 1.5.0 
  41. * @param int|null $field_id Field ID to instantiate. 
  42. * @param int|null $user_id User ID to instantiate for. 
  43. */ 
  44. public function __construct( $field_id = null, $user_id = null ) { 
  45. if ( !empty( $field_id ) ) { 
  46. $this->populate( $field_id, $user_id ); 
  47.  
  48. /** 
  49. * Populates the XProfile profile data. 
  50. * @since 1.0.0 
  51. * @param int $field_id Field ID to populate. 
  52. * @param int $user_id User ID to populate for. 
  53. */ 
  54. public function populate( $field_id, $user_id ) { 
  55. global $wpdb; 
  56.  
  57. $cache_key = "{$user_id}:{$field_id}"; 
  58. $profiledata = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 
  59.  
  60. if ( false === $profiledata ) { 
  61. $bp = buddypress(); 
  62.  
  63. $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ); 
  64. $profiledata = $wpdb->get_row( $sql ); 
  65.  
  66. if ( $profiledata ) { 
  67. wp_cache_set( $cache_key, $profiledata, 'bp_xprofile_data' ); 
  68.  
  69. if ( $profiledata ) { 
  70. $this->id = (int) $profiledata->id; 
  71. $this->user_id = (int) $profiledata->user_id; 
  72. $this->field_id = (int) $profiledata->field_id; 
  73. $this->value = stripslashes( $profiledata->value ); 
  74. $this->last_updated = $profiledata->last_updated; 
  75.  
  76. } else { 
  77. // When no row is found, we'll need to set these properties manually. 
  78. $this->field_id = (int) $field_id; 
  79. $this->user_id = (int) $user_id; 
  80.  
  81. /** 
  82. * Check if there is data already for the user. 
  83. * @since 1.0.0 
  84. * @global object $wpdb 
  85. * @global array $bp 
  86. * @return bool 
  87. */ 
  88. public function exists() { 
  89. global $wpdb; 
  90.  
  91. // Check cache first. 
  92. $cache_key = "{$this->user_id}:{$this->field_id}"; 
  93. $cached = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 
  94.  
  95. if ( $cached && ! empty( $cached->id ) ) { 
  96. $retval = true; 
  97. } else { 
  98. $bp = buddypress(); 
  99. $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 ) ); 
  100.  
  101. /** 
  102. * Filters whether or not data already exists for the user. 
  103. * @since 1.2.7 
  104. * @param bool $retval Whether or not data already exists. 
  105. * @param BP_XProfile_ProfileData $this Instance of the current BP_XProfile_ProfileData class. 
  106. */ 
  107. return apply_filters_ref_array( 'xprofile_data_exists', array( (bool)$retval, $this ) ); 
  108.  
  109. /** 
  110. * Check if this data is for a valid field. 
  111. * @since 1.0.0 
  112. * @global object $wpdb 
  113. * @return bool 
  114. */ 
  115. public function is_valid_field() { 
  116. global $wpdb; 
  117.  
  118. $bp = buddypress(); 
  119.  
  120. $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id = %d", $this->field_id ) ); 
  121.  
  122. /** 
  123. * Filters whether or not data is for a valid field. 
  124. * @since 1.2.7 
  125. * @param bool $retval Whether or not data is valid. 
  126. * @param BP_XProfile_ProfileData $this Instance of the current BP_XProfile_ProfileData class. 
  127. */ 
  128. return apply_filters_ref_array( 'xprofile_data_is_valid_field', array( (bool)$retval, $this ) ); 
  129.  
  130. /** 
  131. * Save the data for the XProfile field. 
  132. * @since 1.0.0 
  133. * @return bool 
  134. */ 
  135. public function save() { 
  136. global $wpdb; 
  137.  
  138. $bp = buddypress(); 
  139.  
  140. $this->user_id = apply_filters( 'xprofile_data_user_id_before_save', $this->user_id, $this->id ); 
  141. $this->field_id = apply_filters( 'xprofile_data_field_id_before_save', $this->field_id, $this->id ); 
  142. $this->value = apply_filters( 'xprofile_data_value_before_save', $this->value, $this->id, true, $this ); 
  143. $this->last_updated = apply_filters( 'xprofile_data_last_updated_before_save', bp_core_current_time(), $this->id ); 
  144.  
  145. /** 
  146. * Fires before the current profile data instance gets saved. 
  147. * Please use this hook to filter the properties above. Each part will be passed in. 
  148. * @since 1.0.0 
  149. * @param BP_XProfile_ProfileData $this Current instance of the profile data being saved. 
  150. */ 
  151. do_action_ref_array( 'xprofile_data_before_save', array( $this ) ); 
  152.  
  153. if ( $this->is_valid_field() ) { 
  154. if ( $this->exists() && strlen( trim( $this->value ) ) ) { 
  155. $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 ) ); 
  156.  
  157. } elseif ( $this->exists() && empty( $this->value ) ) { 
  158. // Data removed, delete the entry. 
  159. $result = $this->delete(); 
  160.  
  161. } else { 
  162. $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 ) ); 
  163. $this->id = $wpdb->insert_id; 
  164.  
  165. if ( false === $result ) { 
  166. return false; 
  167.  
  168. /** 
  169. * Fires after the current profile data instance gets saved. 
  170. * @since 1.0.0 
  171. * @param BP_XProfile_ProfileData $this Current instance of the profile data being saved. 
  172. */ 
  173. do_action_ref_array( 'xprofile_data_after_save', array( $this ) ); 
  174.  
  175. return true; 
  176.  
  177. return false; 
  178.  
  179. /** 
  180. * Delete specific XProfile field data. 
  181. * @since 1.0.0 
  182. * @global object $wpdb 
  183. * @return boolean 
  184. */ 
  185. public function delete() { 
  186. global $wpdb; 
  187.  
  188. $bp = buddypress(); 
  189.  
  190. /** 
  191. * Fires before the current profile data instance gets deleted. 
  192. * @since 1.9.0 
  193. * @param BP_XProfile_ProfileData $this Current instance of the profile data being deleted. 
  194. */ 
  195. do_action_ref_array( 'xprofile_data_before_delete', array( $this ) ); 
  196.  
  197. $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 ) ); 
  198. if ( empty( $deleted ) ) { 
  199. return false; 
  200.  
  201. /** 
  202. * Fires after the current profile data instance gets deleted. 
  203. * @since 1.9.0 
  204. * @param BP_XProfile_ProfileData $this Current instance of the profile data being deleted. 
  205. */ 
  206. do_action_ref_array( 'xprofile_data_after_delete', array( $this ) ); 
  207.  
  208. return true; 
  209.  
  210. /** Static Methods ********************************************************/ 
  211.  
  212. /** 
  213. * Get a user's profile data for a set of fields. 
  214. * @since 2.0.0 
  215. * @param int $user_id ID of user whose data is being queried. 
  216. * @param array $field_ids Array of field IDs to query for. 
  217. * @return array 
  218. */ 
  219. public static function get_data_for_user( $user_id, $field_ids ) { 
  220. global $wpdb; 
  221.  
  222. $data = array(); 
  223.  
  224. $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids ); 
  225.  
  226. // Prime the cache. 
  227. if ( ! empty( $uncached_field_ids ) ) { 
  228. $bp = buddypress(); 
  229. $uncached_field_ids_sql = implode( ', ', wp_parse_id_list( $uncached_field_ids ) ); 
  230. $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 ) ); 
  231.  
  232. // Rekey. 
  233. $queried_data = array(); 
  234. foreach ( $uncached_data as $ud ) { 
  235. $d = new stdClass; 
  236. $d->id = $ud->id; 
  237. $d->user_id = $ud->user_id; 
  238. $d->field_id = $ud->field_id; 
  239. $d->value = $ud->value; 
  240. $d->last_updated = $ud->last_updated; 
  241.  
  242. $queried_data[ $ud->field_id ] = $d; 
  243.  
  244. // Set caches. 
  245. foreach ( $uncached_field_ids as $field_id ) { 
  246.  
  247. $cache_key = "{$user_id}:{$field_id}"; 
  248.  
  249. // If a value was found, cache it. 
  250. if ( isset( $queried_data[ $field_id ] ) ) { 
  251. wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' ); 
  252.  
  253. // If no value was found, cache an empty item 
  254. // to avoid future cache misses. 
  255. } else { 
  256. $d = new stdClass; 
  257. $d->id = ''; 
  258. $d->user_id = $user_id; 
  259. $d->field_id = $field_id; 
  260. $d->value = ''; 
  261. $d->last_updated = ''; 
  262.  
  263. wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); 
  264.  
  265. // Now that all items are cached, fetch them. 
  266. foreach ( $field_ids as $field_id ) { 
  267. $cache_key = "{$user_id}:{$field_id}"; 
  268. $data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 
  269.  
  270. // Integer casting. 
  271. foreach ( (array) $data as $key => $d ) { 
  272. if ( isset( $data[ $key ]->id ) ) { 
  273. $data[ $key ]->id = (int) $data[ $key ]->id; 
  274. if ( isset( $data[ $key ]->user_id ) ) { 
  275. $data[ $key ]->user_id = (int) $data[ $key ]->user_id; 
  276.  
  277. $data[ $key ]->field_id = (int) $data[ $key ]->field_id; 
  278.  
  279. return $data; 
  280.  
  281. /** 
  282. * Get all of the profile information for a specific user. 
  283. * @since 1.2.0 
  284. * @param int $user_id ID of the user. 
  285. * @return array 
  286. */ 
  287. public static function get_all_for_user( $user_id ) { 
  288.  
  289. $groups = bp_xprofile_get_groups( array( 
  290. 'user_id' => $user_id,  
  291. 'hide_empty_groups' => true,  
  292. 'hide_empty_fields' => true,  
  293. 'fetch_fields' => true,  
  294. 'fetch_field_data' => true,  
  295. ) ); 
  296.  
  297. $profile_data = array(); 
  298.  
  299. if ( ! empty( $groups ) ) { 
  300. $user = new WP_User( $user_id ); 
  301.  
  302. $profile_data['user_login'] = $user->user_login; 
  303. $profile_data['user_nicename'] = $user->user_nicename; 
  304. $profile_data['user_email'] = $user->user_email; 
  305.  
  306. foreach ( (array) $groups as $group ) { 
  307. if ( empty( $group->fields ) ) { 
  308. continue; 
  309.  
  310. foreach ( (array) $group->fields as $field ) { 
  311. $profile_data[ $field->name ] = array( 
  312. 'field_group_id' => $group->id,  
  313. 'field_group_name' => $group->name,  
  314. 'field_id' => $field->id,  
  315. 'field_type' => $field->type,  
  316. 'field_data' => $field->data->value,  
  317. ); 
  318.  
  319. return $profile_data; 
  320.  
  321. /** 
  322. * Get the user's field data id by the id of the xprofile field. 
  323. * @since 1.6.0 
  324. * @param int $field_id Field ID being queried for. 
  325. * @param int $user_id User ID associated with field. 
  326. * @return int $fielddata_id 
  327. */ 
  328. public static function get_fielddataid_byid( $field_id, $user_id ) { 
  329. global $wpdb; 
  330.  
  331. if ( empty( $field_id ) || empty( $user_id ) ) { 
  332. $fielddata_id = 0; 
  333. } else { 
  334. $bp = buddypress(); 
  335.  
  336. // Check cache first. 
  337. $cache_key = "{$user_id}:{$field_id}"; 
  338. $fielddata = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 
  339. if ( false === $fielddata || empty( $fielddata->id ) ) { 
  340. $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 ) ); 
  341. } else { 
  342. $fielddata_id = $fielddata->id; 
  343.  
  344. return (int) $fielddata_id; 
  345.  
  346. /** 
  347. * Get profile field values by field ID and user IDs. 
  348. * Supports multiple user IDs. 
  349. * @since 1.0.0 
  350. * @param int $field_id ID of the field. 
  351. * @param int|array|null $user_ids ID or IDs of user(s). 
  352. * @return string|array Single value if a single user is queried,  
  353. * otherwise an array of results. 
  354. */ 
  355. public static function get_value_byid( $field_id, $user_ids = null ) { 
  356. global $wpdb; 
  357.  
  358. if ( empty( $user_ids ) ) { 
  359. $user_ids = bp_displayed_user_id(); 
  360.  
  361. $return_single_result = false; 
  362. if ( ! is_array( $user_ids ) ) { 
  363. $return_single_result = true; 
  364.  
  365. $user_ids = wp_parse_id_list( $user_ids ); 
  366.  
  367. // Assemble uncached IDs. 
  368. $uncached_ids = array(); 
  369. foreach ( $user_ids as $user_id ) { 
  370. $cache_key = "{$user_id}:{$field_id}"; 
  371. if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) { 
  372. $uncached_ids[] = $user_id; 
  373.  
  374. // Prime caches. 
  375. if ( ! empty( $uncached_ids ) ) { 
  376. $bp = buddypress(); 
  377. $uncached_ids_sql = implode( ', ', $uncached_ids ); 
  378. $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 ) ); 
  379.  
  380. // Rekey. 
  381. $qd = array(); 
  382. foreach ( $queried_data as $data ) { 
  383. $qd[ $data->user_id ] = $data; 
  384.  
  385. foreach ( $uncached_ids as $id ) { 
  386. // The value was successfully fetched. 
  387. if ( isset( $qd[ $id ] ) ) { 
  388. $d = $qd[ $id ]; 
  389.  
  390. // No data found for the user, so we fake it to 
  391. // avoid cache misses and PHP notices. 
  392. } else { 
  393. $d = new stdClass; 
  394. $d->id = ''; 
  395. $d->user_id = $id; 
  396. $d->field_id = $field_id; 
  397. $d->value = ''; 
  398. $d->last_updated = ''; 
  399.  
  400. $cache_key = "{$d->user_id}:{$field_id}"; 
  401. wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); 
  402.  
  403. // Now that the cache is primed with all data, fetch it. 
  404. $data = array(); 
  405. foreach ( $user_ids as $user_id ) { 
  406. $cache_key = "{$user_id}:{$field_id}"; 
  407. $data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 
  408.  
  409. // Integer casting. 
  410. foreach ( (array) $data as $key => $d ) { 
  411. if ( isset( $data[ $key ]->id ) ) { 
  412. $data[ $key ]->id = (int) $data[ $key ]->id; 
  413. if ( isset( $data[ $key ]->user_id ) ) { 
  414. $data[ $key ]->user_id = (int) $data[ $key ]->user_id; 
  415.  
  416. $data[ $key ]->field_id = (int) $data[ $key ]->field_id; 
  417.  
  418. // If a single ID was passed, just return the value. 
  419. if ( $return_single_result ) { 
  420. return $data[0]->value; 
  421.  
  422. // Otherwise return the whole array. 
  423. } else { 
  424. return $data; 
  425.  
  426. /** 
  427. * Get profile field values by field name and user ID. 
  428. * @since 1.0.0 
  429. * @param array|string $fields Field(s) to get. 
  430. * @param int|null $user_id User ID to get field data for. 
  431. * @return array|bool 
  432. */ 
  433. public static function get_value_byfieldname( $fields, $user_id = null ) { 
  434. global $wpdb; 
  435.  
  436. if ( empty( $fields ) ) { 
  437. return false; 
  438.  
  439. $bp = buddypress(); 
  440.  
  441. if ( empty( $user_id ) ) { 
  442. $user_id = bp_displayed_user_id(); 
  443.  
  444. $field_sql = ''; 
  445.  
  446. if ( is_array( $fields ) ) { 
  447. for ( $i = 0, $count = count( $fields ); $i < $count; ++$i ) { 
  448. if ( $i == 0 ) { 
  449. $field_sql .= $wpdb->prepare( "AND ( f.name = %s ", $fields[$i] ); 
  450. } else { 
  451. $field_sql .= $wpdb->prepare( "OR f.name = %s ", $fields[$i] ); 
  452.  
  453. $field_sql .= ')'; 
  454. } else { 
  455. $field_sql .= $wpdb->prepare( "AND f.name = %s", $fields ); 
  456.  
  457. $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 ); 
  458. $values = $wpdb->get_results( $sql ); 
  459.  
  460. if ( empty( $values ) || is_wp_error( $values ) ) { 
  461. return false; 
  462.  
  463. $new_values = array(); 
  464.  
  465. if ( is_array( $fields ) ) { 
  466. for ( $i = 0, $count = count( $values ); $i < $count; ++$i ) { 
  467. for ( $j = 0; $j < count( $fields ); $j++ ) { 
  468. if ( $values[$i]->name == $fields[$j] ) { 
  469. $new_values[$fields[$j]] = $values[$i]->value; 
  470. } elseif ( !array_key_exists( $fields[$j], $new_values ) ) { 
  471. $new_values[$fields[$j]] = NULL; 
  472. } else { 
  473. $new_values = $values[0]->value; 
  474.  
  475. return $new_values; 
  476.  
  477. /** 
  478. * Delete field. 
  479. * @since 1.0.0 
  480. * @param int $field_id ID of the field to delete. 
  481. * @return bool 
  482. */ 
  483. public static function delete_for_field( $field_id ) { 
  484. global $wpdb; 
  485.  
  486. $bp = buddypress(); 
  487. $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d", $field_id ) ); 
  488. if ( empty( $deleted ) || is_wp_error( $deleted ) ) { 
  489. return false; 
  490.  
  491. return true; 
  492.  
  493. /** 
  494. * Get time for last XProfile field data update by user. 
  495. * @since 1.0.0 
  496. * @param int $user_id User ID to get time for. 
  497. * @return null|string 
  498. */ 
  499. public static function get_last_updated( $user_id ) { 
  500. global $wpdb; 
  501.  
  502. $bp = buddypress(); 
  503.  
  504. $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 ) ); 
  505.  
  506. return $last_updated; 
  507.  
  508. /** 
  509. * Delete all data for provided user ID. 
  510. * @since 1.0.0 
  511. * @param int $user_id User ID to remove data for. 
  512. * @return false|int 
  513. */ 
  514. public static function delete_data_for_user( $user_id ) { 
  515. global $wpdb; 
  516.  
  517. $bp = buddypress(); 
  518.  
  519. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) ); 
  520.  
  521. /** 
  522. * Get random field type by user ID. 
  523. * @since 1.0.0 
  524. * @param int $user_id User ID to query for. 
  525. * @param string $exclude_fullname SQL portion used to exclude by field ID. 
  526. * @return array|null|object 
  527. */ 
  528. public static function get_random( $user_id, $exclude_fullname ) { 
  529. global $wpdb; 
  530.  
  531. $exclude_sql = ! empty( $exclude_fullname ) ? ' AND pf.id != 1' : ''; 
  532.  
  533. $bp = buddypress(); 
  534.  
  535. 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 ) ); 
  536.  
  537. /** 
  538. * Get fullname for provided user ID. 
  539. * @since 1.0.0 
  540. * @param int $user_id ID of the user to query. 
  541. * @return mixed 
  542. */ 
  543. public static function get_fullname( $user_id = 0 ) { 
  544.  
  545. if ( empty( $user_id ) ) { 
  546. $user_id = bp_displayed_user_id(); 
  547.  
  548. return xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id );