[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/ -> bp-xprofile-template.php (source)

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


Generated: Mon Apr 19 01:01:38 2021 Cross-referenced by PHPXref 0.7.1