/bp-xprofile/bp-xprofile-template.php

  1. <?php 
  2. /** 
  3. * BuddyPress XProfile Template Tags. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage XProfileTemplate 
  7. * @since 1.5.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. if ( ! buddypress()->do_autoload ) { 
  14. require dirname( __FILE__ ) . '/classes/class-bp-xprofile-data-template.php'; 
  15.  
  16. /** 
  17. * Query for XProfile groups and fields. 
  18. * 
  19. * @since 1.0.0 
  20. * 
  21. * @global object $profile_template 
  22. * @see BP_XProfile_Group::get() for full description of `$args` array. 
  23. * 
  24. * @param array|string $args { 
  25. * Array of arguments. See BP_XProfile_Group::get() for full description. Those arguments whose defaults differ 
  26. * from that method are described here: 
  27. * @type string|array $member_type Default: 'any'. 
  28. * @type bool $hide_empty_groups Default: true. 
  29. * @type bool $hide_empty_fields Defaults to true on the Dashboard, on a user's Edit Profile page,  
  30. * or during registration. Otherwise false. 
  31. * @type bool $fetch_visibility_level Defaults to true when an admin is viewing a profile, or when a user is 
  32. * viewing her own profile, or during registration. Otherwise false. 
  33. * @type bool $fetch_fields Default: true. 
  34. * @type bool $fetch_field_data Default: true. 
  35. * } 
  36. * 
  37. * @return bool 
  38. */ 
  39. function bp_has_profile( $args = '' ) { 
  40. global $profile_template; 
  41.  
  42. // Only show empty fields if we're on the Dashboard, or we're on a user's 
  43. // profile edit page, or this is a registration page. 
  44. $hide_empty_fields_default = ( ! is_network_admin() && ! is_admin() && ! bp_is_user_profile_edit() && ! bp_is_register_page() ); 
  45.  
  46. // We only need to fetch visibility levels when viewing your own profile. 
  47. if ( bp_is_my_profile() || bp_current_user_can( 'bp_moderate' ) || bp_is_register_page() ) { 
  48. $fetch_visibility_level_default = true; 
  49. } else { 
  50. $fetch_visibility_level_default = false; 
  51.  
  52. // Parse arguments. 
  53. $r = bp_parse_args( $args, array( 
  54. 'user_id' => bp_displayed_user_id(),  
  55. 'member_type' => 'any',  
  56. 'profile_group_id' => false,  
  57. 'hide_empty_groups' => true,  
  58. 'hide_empty_fields' => $hide_empty_fields_default,  
  59. 'fetch_fields' => true,  
  60. 'fetch_field_data' => true,  
  61. 'fetch_visibility_level' => $fetch_visibility_level_default,  
  62. 'exclude_groups' => false, // Comma-separated list of profile field group IDs to exclude. 
  63. 'exclude_fields' => false, // Comma-separated list of profile field IDs to exclude. 
  64. 'update_meta_cache' => true,  
  65. ), 'has_profile' ); 
  66.  
  67. // Populate the template loop global. 
  68. $profile_template = new BP_XProfile_Data_Template( $r ); 
  69.  
  70. /** 
  71. * Filters whether or not a group has a profile to display. 
  72. * 
  73. * @since 1.1.0 
  74. * @since 2.6.0 Added the `$r` parameter. 
  75. * 
  76. * @param bool $has_groups Whether or not there are group profiles to display. 
  77. * @param string $profile_template Current profile template being used. 
  78. * @param array $r Array of arguments passed into the BP_XProfile_Data_Template class. 
  79. */ 
  80. return apply_filters( 'bp_has_profile', $profile_template->has_groups(), $profile_template, $r ); 
  81.  
  82. /** 
  83. * Start off the profile groups. 
  84. * 
  85. * @since 1.0.0 
  86. * 
  87. * @return mixed 
  88. */ 
  89. function bp_profile_groups() { 
  90. global $profile_template; 
  91. return $profile_template->profile_groups(); 
  92.  
  93. /** 
  94. * Set up the profile groups. 
  95. * 
  96. * @since 1.0.0 
  97. * 
  98. * @return mixed 
  99. */ 
  100. function bp_the_profile_group() { 
  101. global $profile_template; 
  102. return $profile_template->the_profile_group(); 
  103.  
  104. /** 
  105. * Whether or not the group has fields to display. 
  106. * 
  107. * @since 1.0.0 
  108. * 
  109. * @return mixed 
  110. */ 
  111. function bp_profile_group_has_fields() { 
  112. global $profile_template; 
  113. return $profile_template->has_fields(); 
  114.  
  115. /** 
  116. * Output the class attribute for a field. 
  117. * 
  118. * @since 1.0.0 
  119. * 
  120. * @param mixed $class Extra classes to append to class attribute. 
  121. * Pass mutiple class names as an array or 
  122. * space-delimited string. 
  123. */ 
  124. function bp_field_css_class( $class = false ) { 
  125. echo bp_get_field_css_class( $class ); 
  126.  
  127. /** 
  128. * Return the class attribute for a field. 
  129. * 
  130. * @since 1.1.0 
  131. * 
  132. * @param string|bool $class Extra classes to append to class attribute. 
  133. * @return string 
  134. */ 
  135. function bp_get_field_css_class( $class = false ) { 
  136. global $profile_template; 
  137.  
  138. $css_classes = array(); 
  139.  
  140. if ( ! empty( $class ) ) { 
  141. if ( ! is_array( $class ) ) { 
  142. $class = preg_split( '#\s+#', $class ); 
  143. $css_classes = array_map( 'sanitize_html_class', $class ); 
  144.  
  145. // Set a class with the field ID. 
  146. $css_classes[] = 'field_' . $profile_template->field->id; 
  147.  
  148. // Set a class with the field name (sanitized). 
  149. $css_classes[] = 'field_' . sanitize_title( $profile_template->field->name ); 
  150.  
  151. // Set a class indicating whether the field is required or optional. 
  152. if ( ! empty( $profile_template->field->is_required ) ) { 
  153. $css_classes[] = 'required-field'; 
  154. } else { 
  155. $css_classes[] = 'optional-field'; 
  156.  
  157. // Add the field visibility level. 
  158. $css_classes[] = 'visibility-' . esc_attr( bp_get_the_profile_field_visibility_level() ); 
  159.  
  160. if ( $profile_template->current_field % 2 == 1 ) { 
  161. $css_classes[] = 'alt'; 
  162.  
  163. $css_classes[] = 'field_type_' . sanitize_title( $profile_template->field->type ); 
  164.  
  165. /** 
  166. * Filters the field classes to be applied to a field. 
  167. * 
  168. * @since 1.1.0 
  169. * 
  170. * @param array $css_classes Array of classes to be applied to field. Passed by reference. 
  171. */ 
  172. $css_classes = apply_filters_ref_array( 'bp_field_css_classes', array( &$css_classes ) ); 
  173.  
  174. /** 
  175. * Filters the class HTML attribute to be used on a field. 
  176. * 
  177. * @since 1.1.0 
  178. * 
  179. * @param string $value class HTML attribute with imploded classes. 
  180. */ 
  181. return apply_filters( 'bp_get_field_css_class', ' class="' . implode( ' ', $css_classes ) . '"' ); 
  182.  
  183. /** 
  184. * Whether or not the XProfile field has data to display. 
  185. * 
  186. * @since 1.0.0 
  187. * 
  188. * @global object $profile_template 
  189. * 
  190. * @return mixed 
  191. */ 
  192. function bp_field_has_data() { 
  193. global $profile_template; 
  194. return $profile_template->field_has_data; 
  195.  
  196. /** 
  197. * Whether or not the XProfile field has public data to display. 
  198. * 
  199. * @since 1.0.0 
  200. * 
  201. * @global object $profile_template 
  202. * 
  203. * @return bool 
  204. */ 
  205. function bp_field_has_public_data() { 
  206. global $profile_template; 
  207.  
  208. if ( ! empty( $profile_template->field_has_data ) ) { 
  209. return true; 
  210.  
  211. return false; 
  212.  
  213. /** 
  214. * Output the XProfile group ID. 
  215. * 
  216. * @since 1.0.0 
  217. */ 
  218. function bp_the_profile_group_id() { 
  219. echo bp_get_the_profile_group_id(); 
  220.  
  221. /** 
  222. * Return the XProfile group ID. 
  223. * 
  224. * @since 1.1.0 
  225. * 
  226. * @return mixed|void 
  227. */ 
  228. function bp_get_the_profile_group_id() { 
  229. global $group; 
  230.  
  231. /** 
  232. * Filters the XProfile group ID. 
  233. * 
  234. * @since 1.1.0 
  235. * 
  236. * @param int $id ID for the profile group. 
  237. */ 
  238. return apply_filters( 'bp_get_the_profile_group_id', $group->id ); 
  239.  
  240. /** 
  241. * Output the XProfile group name. 
  242. * 
  243. * @since 1.0.0 
  244. */ 
  245. function bp_the_profile_group_name() { 
  246. echo bp_get_the_profile_group_name(); 
  247.  
  248. /** 
  249. * Return the XProfile group name. 
  250. * 
  251. * @since 1.0.0 
  252. * 
  253. * @return mixed|void 
  254. */ 
  255. function bp_get_the_profile_group_name() { 
  256. global $group; 
  257.  
  258. /** 
  259. * Filters the XProfile group name. 
  260. * 
  261. * @since 1.0.0 
  262. * 
  263. * @param string $name Name for the profile group. 
  264. */ 
  265. return apply_filters( 'bp_get_the_profile_group_name', $group->name ); 
  266.  
  267. /** 
  268. * Output the XProfile group slug. 
  269. * 
  270. * @since 1.1.0 
  271. */ 
  272. function bp_the_profile_group_slug() { 
  273. echo bp_get_the_profile_group_slug(); 
  274.  
  275. /** 
  276. * Return the XProfile group slug. 
  277. * 
  278. * @since 1.1.0 
  279. * 
  280. * @return mixed|void 
  281. */ 
  282. function bp_get_the_profile_group_slug() { 
  283. global $group; 
  284.  
  285. /** 
  286. * Filters the XProfile group slug. 
  287. * 
  288. * @since 1.1.0 
  289. * 
  290. * @param string $value Slug for the profile group. 
  291. */ 
  292. return apply_filters( 'bp_get_the_profile_group_slug', sanitize_title( $group->name ) ); 
  293.  
  294. /** 
  295. * Output the XProfile group description. 
  296. * 
  297. * @since 1.0.0 
  298. */ 
  299. function bp_the_profile_group_description() { 
  300. echo bp_get_the_profile_group_description(); 
  301.  
  302. /** 
  303. * Return the XProfile group description. 
  304. * 
  305. * @since 1.0.0 
  306. * 
  307. * @return mixed|void 
  308. */ 
  309. function bp_get_the_profile_group_description() { 
  310. global $group; 
  311.  
  312. /** 
  313. * Filters the XProfile group description. 
  314. * 
  315. * @since 1.0.0 
  316. * 
  317. * @param string $description Description for the profile group. 
  318. */ 
  319. return apply_filters( 'bp_get_the_profile_group_description', $group->description ); 
  320.  
  321. /** 
  322. * Output the XProfile group edit form action. 
  323. * 
  324. * @since 1.1.0 
  325. */ 
  326. function bp_the_profile_group_edit_form_action() { 
  327. echo bp_get_the_profile_group_edit_form_action(); 
  328.  
  329. /** 
  330. * Return the XProfile group edit form action. 
  331. * 
  332. * @since 1.1.0 
  333. * 
  334. * @return mixed|void 
  335. */ 
  336. function bp_get_the_profile_group_edit_form_action() { 
  337. global $group; 
  338.  
  339. // Build the form action URL. 
  340. $form_action = trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . $group->id ); 
  341.  
  342. /** 
  343. * Filters the action for the XProfile group edit form. 
  344. * 
  345. * @since 1.1.0 
  346. * 
  347. * @param string $value URL for the action attribute on the 
  348. * profile group edit form. 
  349. */ 
  350. return apply_filters( 'bp_get_the_profile_group_edit_form_action', $form_action ); 
  351.  
  352. /** 
  353. * Output the XProfile group field IDs. 
  354. * 
  355. * @since 1.1.0 
  356. */ 
  357. function bp_the_profile_group_field_ids() { 
  358. echo bp_get_the_profile_group_field_ids(); 
  359.  
  360. /** 
  361. * Return the XProfile group field IDs. 
  362. * 
  363. * @since 1.1.0 
  364. * 
  365. * @return string 
  366. */ 
  367. function bp_get_the_profile_group_field_ids() { 
  368. global $group; 
  369.  
  370. $field_ids = ''; 
  371.  
  372. if ( !empty( $group->fields ) ) { 
  373. foreach ( (array) $group->fields as $field ) { 
  374. $field_ids .= $field->id . ', '; 
  375.  
  376. return substr( $field_ids, 0, -1 ); 
  377.  
  378. /** 
  379. * Output a comma-separated list of field IDs that are to be submitted on profile edit. 
  380. * 
  381. * @since 2.1.0 
  382. */ 
  383. function bp_the_profile_field_ids() { 
  384. echo bp_get_the_profile_field_ids(); 
  385. /** 
  386. * Generate a comma-separated list of field IDs that are to be submitted on profile edit. 
  387. * 
  388. * @since 2.1.0 
  389. * 
  390. * @return string 
  391. */ 
  392. function bp_get_the_profile_field_ids() { 
  393. global $profile_template; 
  394.  
  395. $field_ids = array(); 
  396. foreach ( $profile_template->groups as $group ) { 
  397. if ( ! empty( $group->fields ) ) { 
  398. $field_ids = array_merge( $field_ids, wp_list_pluck( $group->fields, 'id' ) ); 
  399.  
  400. $field_ids = implode( ', ', wp_parse_id_list( $field_ids ) ); 
  401.  
  402. /** 
  403. * Filters the comma-separated list of field IDs. 
  404. * 
  405. * @since 2.1.0 
  406. * 
  407. * @param string $field_ids Comma-separated field IDs. 
  408. */ 
  409. return apply_filters( 'bp_get_the_profile_field_ids', $field_ids ); 
  410.  
  411. /** 
  412. * Return the XProfile fields. 
  413. * 
  414. * @since 1.0.0 
  415. * 
  416. * @return mixed 
  417. */ 
  418. function bp_profile_fields() { 
  419. global $profile_template; 
  420. return $profile_template->profile_fields(); 
  421.  
  422. /** 
  423. * Sets up the XProfile field. 
  424. * 
  425. * @since 1.0.0 
  426. * 
  427. * @return mixed 
  428. */ 
  429. function bp_the_profile_field() { 
  430. global $profile_template; 
  431. return $profile_template->the_profile_field(); 
  432.  
  433. /** 
  434. * Output the XProfile field ID. 
  435. * 
  436. * @since 1.1.0 
  437. */ 
  438. function bp_the_profile_field_id() { 
  439. echo bp_get_the_profile_field_id(); 
  440.  
  441. /** 
  442. * Return the XProfile field ID. 
  443. * 
  444. * @since 1.1.0 
  445. * 
  446. * @return mixed|void 
  447. */ 
  448. function bp_get_the_profile_field_id() { 
  449. global $field; 
  450.  
  451. /** 
  452. * Filters the XProfile field ID. 
  453. * 
  454. * @since 1.1.0 
  455. * 
  456. * @param int $id ID for the profile field. 
  457. */ 
  458. return apply_filters( 'bp_get_the_profile_field_id', $field->id ); 
  459.  
  460. /** 
  461. * Outputs the XProfile field name. 
  462. * 
  463. * @since 1.0.0 
  464. */ 
  465. function bp_the_profile_field_name() { 
  466. echo bp_get_the_profile_field_name(); 
  467.  
  468. /** 
  469. * Returns the XProfile field name. 
  470. * 
  471. * @since 1.0.0 
  472. * 
  473. * @return mixed|void 
  474. */ 
  475. function bp_get_the_profile_field_name() { 
  476. global $field; 
  477.  
  478. /** 
  479. * Filters the XProfile field name. 
  480. * 
  481. * @since 1.0.0 
  482. * 
  483. * @param string $name Name for the profile field. 
  484. */ 
  485. return apply_filters( 'bp_get_the_profile_field_name', $field->name ); 
  486.  
  487. /** 
  488. * Outputs the XProfile field value. 
  489. * 
  490. * @since 1.0.0 
  491. */ 
  492. function bp_the_profile_field_value() { 
  493. echo bp_get_the_profile_field_value(); 
  494.  
  495. /** 
  496. * Returns the XProfile field value. 
  497. * 
  498. * @since 1.0.0 
  499. * 
  500. * @return mixed|void 
  501. */ 
  502. function bp_get_the_profile_field_value() { 
  503. global $field; 
  504.  
  505. $field->data->value = bp_unserialize_profile_field( $field->data->value ); 
  506.  
  507. /** 
  508. * Filters the XProfile field value. 
  509. * 
  510. * @since 1.0.0 
  511. * 
  512. * @param string $value Value for the profile field. 
  513. * @param string $type Type for the profile field. 
  514. * @param int $id ID for the profile field. 
  515. */ 
  516. return apply_filters( 'bp_get_the_profile_field_value', $field->data->value, $field->type, $field->id ); 
  517.  
  518. /** 
  519. * Outputs the XProfile field edit value. 
  520. * 
  521. * @since 1.1.0 
  522. */ 
  523. function bp_the_profile_field_edit_value() { 
  524. echo bp_get_the_profile_field_edit_value(); 
  525.  
  526. /** 
  527. * Returns the XProfile field edit value. 
  528. * 
  529. * @since 1.1.0 
  530. * 
  531. * @return mixed|void 
  532. */ 
  533. function bp_get_the_profile_field_edit_value() { 
  534. global $field; 
  535.  
  536. /** 
  537. * Check to see if the posted value is different, if it is re-display this 
  538. * value as long as it's not empty and a required field. 
  539. */ 
  540. if ( ! isset( $field->data ) ) { 
  541. $field->data = new stdClass; 
  542.  
  543. if ( ! isset( $field->data->value ) ) { 
  544. $field->data->value = ''; 
  545.  
  546. if ( isset( $_POST['field_' . $field->id] ) && $field->data->value != $_POST['field_' . $field->id] ) { 
  547. if ( ! empty( $_POST['field_' . $field->id] ) ) { 
  548. $field->data->value = $_POST['field_' . $field->id]; 
  549. } else { 
  550. $field->data->value = ''; 
  551.  
  552. $field_value = isset( $field->data->value ) ? bp_unserialize_profile_field( $field->data->value ) : ''; 
  553.  
  554. /** 
  555. * Filters the XProfile field edit value. 
  556. * 
  557. * @since 1.1.0 
  558. * 
  559. * @param string $field_value Current field edit value. 
  560. * @param string $type Type for the profile field. 
  561. * @param int $id ID for the profile field. 
  562. */ 
  563. return apply_filters( 'bp_get_the_profile_field_edit_value', $field_value, $field->type, $field->id ); 
  564.  
  565. /** 
  566. * Outputs the XProfile field type. 
  567. * 
  568. * @since 1.1.0 
  569. */ 
  570. function bp_the_profile_field_type() { 
  571. echo bp_get_the_profile_field_type(); 
  572.  
  573. /** 
  574. * Returns the XProfile field type. 
  575. * 
  576. * @since 1.1.0 
  577. * 
  578. * @return mixed|void 
  579. */ 
  580. function bp_get_the_profile_field_type() { 
  581. global $field; 
  582.  
  583. /** 
  584. * Filters the XProfile field type. 
  585. * 
  586. * @since 1.1.0 
  587. * 
  588. * @param string $type Type for the profile field. 
  589. */ 
  590. return apply_filters( 'bp_the_profile_field_type', $field->type ); 
  591.  
  592. /** 
  593. * Outputs the XProfile field description. 
  594. * 
  595. * @since 1.1.0 
  596. */ 
  597. function bp_the_profile_field_description() { 
  598. echo bp_get_the_profile_field_description(); 
  599.  
  600. /** 
  601. * Returns the XProfile field description. 
  602. * 
  603. * @since 1.1.0 
  604. * 
  605. * @return mixed|void 
  606. */ 
  607. function bp_get_the_profile_field_description() { 
  608. global $field; 
  609.  
  610. /** 
  611. * Filters the XProfile field description. 
  612. * 
  613. * @since 1.1.0 
  614. * 
  615. * @param string $description Description for the profile field. 
  616. */ 
  617. return apply_filters( 'bp_get_the_profile_field_description', $field->description ); 
  618.  
  619. /** 
  620. * Outputs the XProfile field input name. 
  621. * 
  622. * @since 1.1.0 
  623. */ 
  624. function bp_the_profile_field_input_name() { 
  625. echo bp_get_the_profile_field_input_name(); 
  626.  
  627. /** 
  628. * Retursn the XProfile field input name. 
  629. * 
  630. * @since 1.1.0 
  631. * 
  632. * @return mixed|void 
  633. */ 
  634. function bp_get_the_profile_field_input_name() { 
  635. global $field; 
  636.  
  637. /** 
  638. * Filters the profile field input name. 
  639. * 
  640. * @since 1.1.0 
  641. * 
  642. * @param string $value Value used for the name attribute on an input. 
  643. */ 
  644. return apply_filters( 'bp_get_the_profile_field_input_name', 'field_' . $field->id ); 
  645.  
  646. /** 
  647. * Returns the action name for any signup errors related to this profile field. 
  648. * 
  649. * In the registration templates, signup errors are pulled from the global 
  650. * object and rendered at actions that look like 'bp_field_12_errors'. This 
  651. * function allows the action name to be easily concatenated and called in the 
  652. * following fashion: 
  653. * do_action( bp_get_the_profile_field_errors_action() ); 
  654. * 
  655. * @since 1.8.0 
  656. * 
  657. * @return string The _errors action name corresponding to this profile field. 
  658. */ 
  659. function bp_get_the_profile_field_errors_action() { 
  660. global $field; 
  661. return 'bp_field_' . $field->id . '_errors'; 
  662.  
  663. /** 
  664. * Displays field options HTML for field types of 'selectbox', 'multiselectbox',  
  665. * 'radio', 'checkbox', and 'datebox'. 
  666. * 
  667. * @since 1.1.0 
  668. * 
  669. * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'. 
  670. */ 
  671. function bp_the_profile_field_options( $args = array() ) { 
  672. echo bp_get_the_profile_field_options( $args ); 
  673. /** 
  674. * Retrieves field options HTML for field types of 'selectbox', 'multiselectbox', 'radio', 'checkbox', and 'datebox'. 
  675. * 
  676. * @since 1.1.0 
  677. * 
  678. * 
  679. * @param array $args { 
  680. * Array of optional arguments. 
  681. * @type string|bool $type Type of datebox. False if it's not a 
  682. * datebox, otherwise 'day, 'month', or 'year'. Default: false. 
  683. * @type int $user_id ID of the user whose profile values should be 
  684. * used when rendering options. Default: displayed user. 
  685. * } 
  686. * 
  687. * @return string $vaue Field options markup. 
  688. */ 
  689. function bp_get_the_profile_field_options( $args = array() ) { 
  690. global $field; 
  691.  
  692. $args = bp_parse_args( $args, array( 
  693. 'type' => false,  
  694. 'user_id' => bp_displayed_user_id(),  
  695. ), 'get_the_profile_field_options' ); 
  696.  
  697. /** 
  698. * In some cases, the $field global is not an instantiation of the BP_XProfile_Field class. 
  699. * However, we have to make sure that all data originally in $field gets merged back in, after reinstantiation. 
  700. */ 
  701. if ( ! method_exists( $field, 'get_children' ) ) { 
  702. $field_obj = xprofile_get_field( $field->id ); 
  703.  
  704. foreach ( $field as $field_prop => $field_prop_value ) { 
  705. if ( ! isset( $field_obj->{$field_prop} ) ) { 
  706. $field_obj->{$field_prop} = $field_prop_value; 
  707.  
  708. $field = $field_obj; 
  709.  
  710. ob_start(); 
  711. $field->type_obj->edit_field_options_html( $args ); 
  712. $html = ob_get_contents(); 
  713. ob_end_clean(); 
  714.  
  715. return $html; 
  716.  
  717. /** 
  718. * Render whether or not a profile field is required. 
  719. * 
  720. * @since 1.1.0 
  721. */ 
  722. function bp_the_profile_field_is_required() { 
  723. echo bp_get_the_profile_field_is_required(); 
  724.  
  725. /** 
  726. * Return whether or not a profile field is required. 
  727. * 
  728. * @since 1.1.0 
  729. * 
  730. * @return mixed|void 
  731. */ 
  732. function bp_get_the_profile_field_is_required() { 
  733. global $field; 
  734.  
  735. $retval = false; 
  736.  
  737. if ( isset( $field->is_required ) ) { 
  738. $retval = $field->is_required; 
  739.  
  740. /** 
  741. * Filters whether or not a profile field is required. 
  742. * 
  743. * @since 1.1.0 
  744. * 
  745. * @param bool $retval Whether or not the field is required. 
  746. */ 
  747. return apply_filters( 'bp_get_the_profile_field_is_required', (bool) $retval ); 
  748.  
  749. /** 
  750. * Output the visibility level of this field. 
  751. * 
  752. * @since 1.6.0 
  753. */ 
  754. function bp_the_profile_field_visibility_level() { 
  755. echo bp_get_the_profile_field_visibility_level(); 
  756.  
  757. /** 
  758. * Return the visibility level of this field. 
  759. * 
  760. * @since 1.6.0 
  761. * 
  762. * @return mixed|void 
  763. */ 
  764. function bp_get_the_profile_field_visibility_level() { 
  765. global $field; 
  766.  
  767. // On the registration page, values stored in POST should take 
  768. // precedence over default visibility, so that submitted values 
  769. // are not lost on failure. 
  770. if ( bp_is_register_page() && ! empty( $_POST['field_' . $field->id . '_visibility'] ) ) { 
  771. $retval = esc_attr( $_POST['field_' . $field->id . '_visibility'] ); 
  772. } else { 
  773. $retval = ! empty( $field->visibility_level ) ? $field->visibility_level : 'public'; 
  774.  
  775. /** 
  776. * Filters the profile field visibility level. 
  777. * 
  778. * @since 1.6.0 
  779. * 
  780. * @param string $retval Field visibility level. 
  781. */ 
  782. return apply_filters( 'bp_get_the_profile_field_visibility_level', $retval ); 
  783.  
  784. /** 
  785. * Echo the visibility level label of this field. 
  786. * 
  787. * @since 1.6.0 
  788. */ 
  789. function bp_the_profile_field_visibility_level_label() { 
  790. echo bp_get_the_profile_field_visibility_level_label(); 
  791.  
  792. /** 
  793. * Return the visibility level label of this field. 
  794. * 
  795. * @since 1.6.0 
  796. * 
  797. * @return mixed|void 
  798. */ 
  799. function bp_get_the_profile_field_visibility_level_label() { 
  800. global $field; 
  801.  
  802. // On the registration page, values stored in POST should take 
  803. // precedence over default visibility, so that submitted values 
  804. // are not lost on failure. 
  805. if ( bp_is_register_page() && ! empty( $_POST['field_' . $field->id . '_visibility'] ) ) { 
  806. $level = esc_html( $_POST['field_' . $field->id . '_visibility'] ); 
  807. } else { 
  808. $level = ! empty( $field->visibility_level ) ? $field->visibility_level : 'public'; 
  809.  
  810. $fields = bp_xprofile_get_visibility_levels(); 
  811.  
  812. /** 
  813. * Filters the profile field visibility level label. 
  814. * 
  815. * @since 1.6.0 
  816. * @since 2.6.0 Added the `$level` parameter. 
  817. * 
  818. * @param string $retval Field visibility level label. 
  819. * @param string $level Field visibility level. 
  820. */ 
  821. return apply_filters( 'bp_get_the_profile_field_visibility_level_label', $fields[ $level ]['label'], $level ); 
  822.  
  823. /** 
  824. * Return unserialized profile field data. 
  825. * 
  826. * @since 1.0.0 
  827. * 
  828. * @param string $value Content to maybe unserialize. 
  829. * @return mixed|string 
  830. */ 
  831. function bp_unserialize_profile_field( $value ) { 
  832. if ( is_serialized($value) ) { 
  833. $field_value = maybe_unserialize($value); 
  834. $field_value = implode( ', ', $field_value ); 
  835. return $field_value; 
  836.  
  837. return $value; 
  838.  
  839. /** 
  840. * Output XProfile field data. 
  841. * 
  842. * @since 1.2.0 
  843. * 
  844. * @param string|array $args Array of arguments for field data. See {@link bp_get_profile_field_data} 
  845. */ 
  846. function bp_profile_field_data( $args = '' ) { 
  847. echo bp_get_profile_field_data( $args ); 
  848.  
  849. /** 
  850. * Return XProfile field data. 
  851. * 
  852. * @since 1.2.0 
  853. * 
  854. * @param string|array $args { 
  855. * Array of arguments for field data. 
  856. * 
  857. * @type string|int|bool $field Field identifier. 
  858. * @type int $user_id ID of the user to get field data for. 
  859. * } 
  860. * @return mixed|void 
  861. */ 
  862. function bp_get_profile_field_data( $args = '' ) { 
  863.  
  864. $r = wp_parse_args( $args, array( 
  865. 'field' => false, // Field name or ID. 
  866. 'user_id' => bp_displayed_user_id() 
  867. ) ); 
  868.  
  869. /** 
  870. * Filters the profile field data. 
  871. * 
  872. * @since 1.2.0 
  873. * @since 2.6.0 Added the `$r` parameter. 
  874. * 
  875. * @param mixed $value Profile data for a specific field for the user. 
  876. * @param array $r Array of parsed arguments. 
  877. */ 
  878. return apply_filters( 'bp_get_profile_field_data', xprofile_get_field_data( $r['field'], $r['user_id'] ), $r ); 
  879.  
  880. /** 
  881. * Get all profile field groups. 
  882. * 
  883. * @since 2.1.0 
  884. * 
  885. * @return array $groups 
  886. */ 
  887. function bp_profile_get_field_groups() { 
  888.  
  889. $groups = wp_cache_get( 'all', 'bp_xprofile_groups' ); 
  890. if ( false === $groups ) { 
  891. $groups = bp_xprofile_get_groups( array( 'fetch_fields' => true ) ); 
  892. wp_cache_set( 'all', $groups, 'bp_xprofile_groups' ); 
  893.  
  894. /** 
  895. * Filters all profile field groups. 
  896. * 
  897. * @since 2.1.0 
  898. * 
  899. * @param array $groups Array of available profile field groups. 
  900. */ 
  901. return apply_filters( 'bp_profile_get_field_groups', $groups ); 
  902.  
  903. /** 
  904. * Check if there is more than one group of fields for the profile being edited. 
  905. * 
  906. * @since 2.1.0 
  907. * 
  908. * @return bool True if there is more than one profile field group. 
  909. */ 
  910. function bp_profile_has_multiple_groups() { 
  911. $has_multiple_groups = count( (array) bp_profile_get_field_groups() ) > 1; 
  912.  
  913. /** 
  914. * Filters if there is more than one group of fields for the profile being edited. 
  915. * 
  916. * @since 2.1.0 
  917. * 
  918. * @param bool $has_multiple_groups Whether or not there are multiple groups. 
  919. */ 
  920. return (bool) apply_filters( 'bp_profile_has_multiple_groups', $has_multiple_groups ); 
  921.  
  922. /** 
  923. * Output the tabs to switch between profile field groups. 
  924. * 
  925. * @since 1.0.0 
  926. */ 
  927. function bp_profile_group_tabs() { 
  928. echo bp_get_profile_group_tabs(); 
  929.  
  930. /** 
  931. * Fires at the end of the tab output for switching between profile field 
  932. * groups. This action is in a strange place for legacy reasons. 
  933. * 
  934. * @since 1.0.0 
  935. */ 
  936. do_action( 'xprofile_profile_group_tabs' ); 
  937.  
  938. /** 
  939. * Return the XProfile group tabs. 
  940. * 
  941. * @since 2.3.0 
  942. * 
  943. * @return string 
  944. */ 
  945. function bp_get_profile_group_tabs() { 
  946.  
  947. // Get field group data. 
  948. $groups = bp_profile_get_field_groups(); 
  949. $group_name = bp_get_profile_group_name(); 
  950. $tabs = array(); 
  951.  
  952. // Loop through field groups and put a tab-lst together. 
  953. for ( $i = 0, $count = count( $groups ); $i < $count; ++$i ) { 
  954.  
  955. // Setup the selected class. 
  956. $selected = ''; 
  957. if ( $group_name === $groups[ $i ]->name ) { 
  958. $selected = ' class="current"'; 
  959.  
  960. // Skip if group has no fields. 
  961. if ( empty( $groups[ $i ]->fields ) ) { 
  962. continue; 
  963.  
  964. // Build the profile field group link. 
  965. $link = trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . $groups[ $i ]->id ); 
  966.  
  967. // Add tab to end of tabs array. 
  968. $tabs[] = sprintf( 
  969. '<li %1$s><a href="%2$s">%3$s</a></li>',  
  970. $selected,  
  971. esc_url( $link ),  
  972. esc_html( apply_filters( 'bp_get_the_profile_group_name', $groups[ $i ]->name ) ) 
  973. ); 
  974.  
  975. /** 
  976. * Filters the tabs to display for profile field groups. 
  977. * 
  978. * @since 1.5.0 
  979. * 
  980. * @param array $tabs Array of tabs to display. 
  981. * @param array $groups Array of profile groups. 
  982. * @param string $group_name Name of the current group displayed. 
  983. */ 
  984. $tabs = apply_filters( 'xprofile_filter_profile_group_tabs', $tabs, $groups, $group_name ); 
  985.  
  986. return join( '', $tabs ); 
  987.  
  988. /** 
  989. * Output the XProfile group name. 
  990. * 
  991. * @since 1.0.0 
  992. * 
  993. * @param bool $deprecated Deprecated boolean parameter. 
  994. * @return mixed|void 
  995. */ 
  996. function bp_profile_group_name( $deprecated = true ) { 
  997. if ( !$deprecated ) { 
  998. return bp_get_profile_group_name(); 
  999. } else { 
  1000. echo bp_get_profile_group_name(); 
  1001.  
  1002. /** 
  1003. * Return the XProfile group name. 
  1004. * 
  1005. * @since 1.0.0 
  1006. * 
  1007. * @return mixed|void 
  1008. */ 
  1009. function bp_get_profile_group_name() { 
  1010.  
  1011. // Check action variable. 
  1012. $group_id = bp_action_variable( 1 ); 
  1013. if ( empty( $group_id ) || ! is_numeric( $group_id ) ) { 
  1014. $group_id = 1; 
  1015.  
  1016. // Check for cached group. 
  1017. $group = new BP_XProfile_Group( $group_id ); 
  1018.  
  1019. /** 
  1020. * Filters the profile group name. 
  1021. * 
  1022. * @since 1.0.0 
  1023. * @since 2.6.0 Added the `$group_id` parameter 
  1024. * 
  1025. * @param string $name Name of the profile group. 
  1026. * @param int $group_id ID of the profile group. 
  1027. */ 
  1028. return apply_filters( 'bp_get_profile_group_name', $group->name, $group_id ); 
  1029.  
  1030. /** 
  1031. * Render a formatted string displaying when a profile was last updated. 
  1032. * 
  1033. * @since 1.0.0 
  1034. */ 
  1035. function bp_profile_last_updated() { 
  1036.  
  1037. $last_updated = bp_get_profile_last_updated(); 
  1038.  
  1039. if ( empty( $last_updated ) ) { 
  1040. _e( 'Profile not recently updated.', 'buddypress' ); 
  1041. } else { 
  1042. echo $last_updated; 
  1043.  
  1044. /** 
  1045. * Return a formatted string displaying when a profile was last updated. 
  1046. * 
  1047. * @since 1.0.0 
  1048. * 
  1049. * @return bool|mixed|void 
  1050. */ 
  1051. function bp_get_profile_last_updated() { 
  1052.  
  1053. $last_updated = bp_get_user_meta( bp_displayed_user_id(), 'profile_last_updated', true ); 
  1054.  
  1055. if ( ! empty( $last_updated ) ) { 
  1056.  
  1057. /** 
  1058. * Filters the formatted string used to display when a profile was last updated. 
  1059. * 
  1060. * @since 1.0.0 
  1061. * 
  1062. * @param string $value Formatted last updated indicator string. 
  1063. */ 
  1064. return apply_filters( 'bp_get_profile_last_updated', sprintf( __( 'Profile updated %s', 'buddypress' ), bp_core_time_since( strtotime( $last_updated ) ) ) ); 
  1065.  
  1066. return false; 
  1067.  
  1068. /** 
  1069. * Display the current profile group ID. 
  1070. * 
  1071. * @since 1.1.0 
  1072. */ 
  1073. function bp_current_profile_group_id() { 
  1074. echo bp_get_current_profile_group_id(); 
  1075.  
  1076. /** 
  1077. * Return the current profile group ID. 
  1078. * 
  1079. * @since 1.1.0 
  1080. * 
  1081. * @return mixed|void 
  1082. */ 
  1083. function bp_get_current_profile_group_id() { 
  1084. $profile_group_id = bp_action_variable( 1 ); 
  1085. if ( empty( $profile_group_id ) ) { 
  1086. $profile_group_id = 1; 
  1087.  
  1088. /** 
  1089. * Filters the current profile group ID. 
  1090. * 
  1091. * Possible values are admin/profile/edit/[group-id]. 
  1092. * 
  1093. * @since 1.1.0 
  1094. * 
  1095. * @param string $profile_group_id Current profile group ID. 
  1096. */ 
  1097. return apply_filters( 'bp_get_current_profile_group_id', $profile_group_id ); 
  1098.  
  1099. /** 
  1100. * Render an avatar delete link. 
  1101. * 
  1102. * @since 1.1.0 
  1103. */ 
  1104. function bp_avatar_delete_link() { 
  1105. echo bp_get_avatar_delete_link(); 
  1106.  
  1107. /** 
  1108. * Return an avatar delete link. 
  1109. * 
  1110. * @since 1.1.0 
  1111. * 
  1112. * @return mixed|void 
  1113. */ 
  1114. function bp_get_avatar_delete_link() { 
  1115.  
  1116. /** 
  1117. * Filters the link used for deleting an avatar. 
  1118. * 
  1119. * @since 1.1.0 
  1120. * 
  1121. * @param string $value Nonced URL used for deleting an avatar. 
  1122. */ 
  1123. return apply_filters( 'bp_get_avatar_delete_link', wp_nonce_url( bp_displayed_user_domain() . bp_get_profile_slug() . '/change-avatar/delete-avatar/', 'bp_delete_avatar_link' ) ); 
  1124.  
  1125. /** 
  1126. * Render an edit profile button. 
  1127. * 
  1128. * @since 1.0.0 
  1129. */ 
  1130. function bp_edit_profile_button() { 
  1131. bp_button( array( 
  1132. 'id' => 'edit_profile',  
  1133. 'component' => 'xprofile',  
  1134. 'must_be_logged_in' => true,  
  1135. 'block_self' => true,  
  1136. 'link_href' => trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit' ),  
  1137. 'link_class' => 'edit',  
  1138. 'link_text' => __( 'Edit Profile', 'buddypress' ),  
  1139. ) ); 
  1140.  
  1141. /** Visibility ****************************************************************/ 
  1142.  
  1143. /** 
  1144. * Echo the field visibility radio buttons. 
  1145. * 
  1146. * @since 1.6.0 
  1147. * 
  1148. * @param array|string $args Args for the radio buttons. See {@link bp_profile_get_visibility_radio_buttons} 
  1149. */ 
  1150. function bp_profile_visibility_radio_buttons( $args = '' ) { 
  1151. echo bp_profile_get_visibility_radio_buttons( $args ); 
  1152. /** 
  1153. * Return the field visibility radio buttons. 
  1154. * 
  1155. * @since 1.6.0 
  1156. * 
  1157. * @param array|string $args { 
  1158. * Args for the radio buttons. 
  1159. * 
  1160. * @type int $field_id ID of the field to render. 
  1161. * @type string $before Markup to render before the field. 
  1162. * @type string $after Markup to render after the field. 
  1163. * @type string $before_radio Markup to render before the radio button. 
  1164. * @type string $after_radio Markup to render after the radio button. 
  1165. * @type string $class Class to apply to the field markup. 
  1166. * } 
  1167. * @return string $retval 
  1168. */ 
  1169. function bp_profile_get_visibility_radio_buttons( $args = '' ) { 
  1170.  
  1171. // Parse optional arguments. 
  1172. $r = bp_parse_args( $args, array( 
  1173. 'field_id' => bp_get_the_profile_field_id(),  
  1174. 'before' => '<ul class="radio">',  
  1175. 'after' => '</ul>',  
  1176. 'before_radio' => '<li class="%s">',  
  1177. 'after_radio' => '</li>',  
  1178. 'class' => 'bp-xprofile-visibility' 
  1179. ), 'xprofile_visibility_radio_buttons' ); 
  1180.  
  1181. // Empty return value, filled in below if a valid field ID is found. 
  1182. $retval = ''; 
  1183.  
  1184. // Only do-the-do if there's a valid field ID. 
  1185. if ( ! empty( $r['field_id'] ) ) : 
  1186.  
  1187. // Start the output buffer. 
  1188. ob_start(); 
  1189.  
  1190. // Output anything before. 
  1191. echo $r['before']; ?> 
  1192.  
  1193. <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?> 
  1194.  
  1195. <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?> 
  1196.  
  1197. <?php printf( $r['before_radio'], esc_attr( $level['id'] ) ); ?> 
  1198.  
  1199. <label for="<?php echo esc_attr( 'see-field_' . $r['field_id'] . '_' . $level['id'] ); ?>"> 
  1200. <input type="radio" id="<?php echo esc_attr( 'see-field_' . $r['field_id'] . '_' . $level['id'] ); ?>" name="<?php echo esc_attr( 'field_' . $r['field_id'] . '_visibility' ); ?>" value="<?php echo esc_attr( $level['id'] ); ?>" <?php checked( $level['id'], bp_get_the_profile_field_visibility_level() ); ?> /> 
  1201. <span class="field-visibility-text"><?php echo esc_html( $level['label'] ); ?></span> 
  1202. </label> 
  1203.  
  1204. <?php echo $r['after_radio']; ?> 
  1205.  
  1206. <?php endforeach; ?> 
  1207.  
  1208. <?php endif; 
  1209.  
  1210. // Output anything after. 
  1211. echo $r['after']; 
  1212.  
  1213. // Get the output buffer and empty it. 
  1214. $retval = ob_get_clean(); 
  1215. endif; 
  1216.  
  1217. /** 
  1218. * Filters the radio buttons for setting visibility. 
  1219. * 
  1220. * @since 1.6.0 
  1221. * 
  1222. * @param string $retval HTML output for the visibility radio buttons. 
  1223. * @param array $r Parsed arguments to be used with display. 
  1224. * @param array $args Original passed in arguments to be used with display. 
  1225. */ 
  1226. return apply_filters( 'bp_profile_get_visibility_radio_buttons', $retval, $r, $args ); 
  1227.  
  1228. /** 
  1229. * Output the XProfile field visibility select list for settings. 
  1230. * 
  1231. * @since 2.0.0 
  1232. * 
  1233. * @param array|string $args Args for the select list. See {@link bp_profile_get_settings_visibility_select} 
  1234. */ 
  1235. function bp_profile_settings_visibility_select( $args = '' ) { 
  1236. echo bp_profile_get_settings_visibility_select( $args ); 
  1237. /** 
  1238. * Return the XProfile field visibility select list for settings. 
  1239. * 
  1240. * @since 2.0.0 
  1241. * 
  1242. * @param array|string $args { 
  1243. * Args for the select list. 
  1244. * 
  1245. * @type int $field_id ID of the field to render. 
  1246. * @type string $before Markup to render before the field. 
  1247. * @type string $after Markup to render after the field. 
  1248. * @type string $class Class to apply to the field markup. 
  1249. * } 
  1250. * @return string $retval 
  1251. */ 
  1252. function bp_profile_get_settings_visibility_select( $args = '' ) { 
  1253.  
  1254. // Parse optional arguments. 
  1255. $r = bp_parse_args( $args, array( 
  1256. 'field_id' => bp_get_the_profile_field_id(),  
  1257. 'before' => '',  
  1258. 'after' => '',  
  1259. 'class' => 'bp-xprofile-visibility' 
  1260. ), 'xprofile_settings_visibility_select' ); 
  1261.  
  1262. // Empty return value, filled in below if a valid field ID is found. 
  1263. $retval = ''; 
  1264.  
  1265. // Only do-the-do if there's a valid field ID. 
  1266. if ( ! empty( $r['field_id'] ) ) : 
  1267.  
  1268. // Start the output buffer. 
  1269. ob_start(); 
  1270.  
  1271. // Output anything before. 
  1272. echo $r['before']; ?> 
  1273.  
  1274. <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?> 
  1275.  
  1276. <label for="<?php echo esc_attr( 'field_' . $r['field_id'] ) ; ?>_visibility" class="bp-screen-reader-text"><?php 
  1277. /** translators: accessibility text */ 
  1278. _e( 'Select visibility', 'buddypress' ); 
  1279. ?></label> 
  1280. <select class="<?php echo esc_attr( $r['class'] ); ?>" name="<?php echo esc_attr( 'field_' . $r['field_id'] ) ; ?>_visibility" id="<?php echo esc_attr( 'field_' . $r['field_id'] ) ; ?>_visibility"> 
  1281.  
  1282. <?php foreach ( bp_xprofile_get_visibility_levels() as $level ) : ?> 
  1283.  
  1284. <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $level['id'], bp_get_the_profile_field_visibility_level() ); ?>><?php echo esc_html( $level['label'] ); ?></option> 
  1285.  
  1286. <?php endforeach; ?> 
  1287.  
  1288. </select> 
  1289.  
  1290. <?php else : ?> 
  1291.  
  1292. <span class="field-visibility-settings-notoggle"><?php bp_the_profile_field_visibility_level_label(); ?></span> 
  1293.  
  1294. <?php endif; 
  1295.  
  1296. // Output anything after. 
  1297. echo $r['after']; 
  1298.  
  1299. // Get the output buffer and empty it. 
  1300. $retval = ob_get_clean(); 
  1301. endif; 
  1302.  
  1303. /** 
  1304. * Filters the dropdown list for setting visibility. 
  1305. * 
  1306. * @since 2.0.0 
  1307. * 
  1308. * @param string $retval HTML output for the visibility dropdown list. 
  1309. * @param array $r Parsed arguments to be used with display. 
  1310. * @param array $args Original passed in arguments to be used with display. 
  1311. */ 
  1312. return apply_filters( 'bp_profile_settings_visibility_select', $retval, $r, $args ); 
  1313.  
  1314. /** 
  1315. * Output the 'required' markup in extended profile field labels. 
  1316. * 
  1317. * @since 2.4.0 
  1318. */ 
  1319. function bp_the_profile_field_required_label() { 
  1320. echo bp_get_the_profile_field_required_label(); 
  1321.  
  1322. /** 
  1323. * Return the 'required' markup in extended profile field labels. 
  1324. * 
  1325. * @since 2.4.0 
  1326. * 
  1327. * @return string HTML for the required label. 
  1328. */ 
  1329. function bp_get_the_profile_field_required_label() { 
  1330. $retval = ''; 
  1331.  
  1332. if ( bp_get_the_profile_field_is_required() ) { 
  1333. $translated_string = __( '(required)', 'buddypress' ); 
  1334.  
  1335. $retval = ' <span class="bp-required-field-label">'; 
  1336. $retval .= apply_filters( 'bp_get_the_profile_field_required_label', $translated_string, bp_get_the_profile_field_id() ); 
  1337. $retval .= '</span>'; 
  1338.  
  1339.  
  1340. return $retval; 
.