Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 05:37:16 AM (5 years ago)
Author:
boonebgorges
Message:

Move bp-xprofile classes to their own files.

See #6870.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php

    r10515 r10525  
    11<?php
    22/**
    3  * BuddyPress XProfile Template Tags.
     3 * BuddyPress XProfile Template Loop Class.
    44 *
    55 * @package BuddyPress
    6  * @subpackage XProfileTemplate
    7  * @since 1.5.0
     6 * @since 1.0.0
    87 */
    98
     
    344343    }
    345344}
    346 
    347 /**
    348  * Query for XProfile groups and fields.
    349  *
    350  * @since 1.0.0
    351  *
    352  * @global object $profile_template
    353  * @see BP_XProfile_Group::get() for full description of `$args` array.
    354  *
    355  * @param array|string $args {
    356  *     Array of arguments. See BP_XProfile_Group::get() for full description. Those arguments whose defaults differ
    357  *     from that method are described here:
    358  *     @type string|array $member_type            Default: 'any'.
    359  *     @type bool         $hide_empty_groups      Default: true.
    360  *     @type bool         $hide_empty_fields      Defaults to true on the Dashboard, on a user's Edit Profile page,
    361  *                                                or during registration. Otherwise false.
    362  *     @type bool         $fetch_visibility_level Defaults to true when an admin is viewing a profile, or when a user is
    363  *                                                viewing her own profile, or during registration. Otherwise false.
    364  *     @type bool         $fetch_fields           Default: true.
    365  *     @type bool         $fetch_field_data       Default: true.
    366  * }
    367  *
    368  * @return bool
    369  */
    370 function bp_has_profile( $args = '' ) {
    371     global $profile_template;
    372 
    373     // Only show empty fields if we're on the Dashboard, or we're on a user's
    374     // profile edit page, or this is a registration page.
    375     $hide_empty_fields_default = ( ! is_network_admin() && ! is_admin() && ! bp_is_user_profile_edit() && ! bp_is_register_page() );
    376 
    377     // We only need to fetch visibility levels when viewing your own profile.
    378     if ( bp_is_my_profile() || bp_current_user_can( 'bp_moderate' ) || bp_is_register_page() ) {
    379         $fetch_visibility_level_default = true;
    380     } else {
    381         $fetch_visibility_level_default = false;
    382     }
    383 
    384     // Parse arguments.
    385     $r = bp_parse_args( $args, array(
    386         'user_id'                => bp_displayed_user_id(),
    387         'member_type'            => 'any',
    388         'profile_group_id'       => false,
    389         'hide_empty_groups'      => true,
    390         'hide_empty_fields'      => $hide_empty_fields_default,
    391         'fetch_fields'           => true,
    392         'fetch_field_data'       => true,
    393         'fetch_visibility_level' => $fetch_visibility_level_default,
    394         'exclude_groups'         => false, // Comma-separated list of profile field group IDs to exclude.
    395         'exclude_fields'         => false, // Comma-separated list of profile field IDs to exclude.
    396         'update_meta_cache'      => true,
    397     ), 'has_profile' );
    398 
    399     // Populate the template loop global.
    400     $profile_template = new BP_XProfile_Data_Template( $r );
    401 
    402     /**
    403      * Filters whether or not a group has a profile to display.
    404      *
    405      * @since 1.1.0
    406      *
    407      * @param bool   $has_groups       Whether or not there are group profiles to display.
    408      * @param string $profile_template Current profile template being used.
    409      */
    410     return apply_filters( 'bp_has_profile', $profile_template->has_groups(), $profile_template );
    411 }
    412 
    413 /**
    414  * Start off the profile groups.
    415  *
    416  * @return mixed
    417  */
    418 function bp_profile_groups() {
    419     global $profile_template;
    420     return $profile_template->profile_groups();
    421 }
    422 
    423 /**
    424  * Set up the profile groups.
    425  *
    426  * @return mixed
    427  */
    428 function bp_the_profile_group() {
    429     global $profile_template;
    430     return $profile_template->the_profile_group();
    431 }
    432 
    433 /**
    434  * Whether or not the group has fields to display.
    435  *
    436  * @return mixed
    437  */
    438 function bp_profile_group_has_fields() {
    439     global $profile_template;
    440     return $profile_template->has_fields();
    441 }
    442 
    443 /**
    444  * Output the class attribute for a field.
    445  *
    446  * @since 1.0.0
    447  *
    448  * @param array|string $class Extra classes to append to class attribute.
    449  *                            Pass mutiple class names as an array or
    450  *                            space-delimited string.
    451  *
    452  * @return string
    453  */
    454 function bp_field_css_class( $class = false ) {
    455     echo bp_get_field_css_class( $class );
    456 }
    457 
    458     /**
    459      * Return the class attribute for a field.
    460      *
    461      * @param string|bool $class Extra classes to append to class attribute.
    462      * @return string
    463      */
    464     function bp_get_field_css_class( $class = false ) {
    465         global $profile_template;
    466 
    467         $css_classes = array();
    468 
    469         if ( ! empty( $class ) ) {
    470             if ( ! is_array( $class ) ) {
    471                 $class = preg_split( '#\s+#', $class );
    472             }
    473             $css_classes = array_map( 'sanitize_html_class', $class );
    474         }
    475 
    476         // Set a class with the field ID.
    477         $css_classes[] = 'field_' . $profile_template->field->id;
    478 
    479         // Set a class with the field name (sanitized).
    480         $css_classes[] = 'field_' . sanitize_title( $profile_template->field->name );
    481 
    482         // Set a class indicating whether the field is required or optional.
    483         if ( ! empty( $profile_template->field->is_required ) ) {
    484             $css_classes[] = 'required-field';
    485         } else {
    486             $css_classes[] = 'optional-field';
    487         }
    488 
    489         // Add the field visibility level.
    490         $css_classes[] = 'visibility-' . esc_attr( bp_get_the_profile_field_visibility_level() );
    491 
    492         if ( $profile_template->current_field % 2 == 1 ) {
    493             $css_classes[] = 'alt';
    494         }
    495 
    496         $css_classes[] = 'field_type_' . sanitize_title( $profile_template->field->type );
    497 
    498         /**
    499          * Filters the field classes to be applied to a field.
    500          *
    501          * @since 1.1.0
    502          *
    503          * @param array $css_classes Array of classes to be applied to field. Passed by reference.
    504          */
    505         $css_classes = apply_filters_ref_array( 'bp_field_css_classes', array( &$css_classes ) );
    506 
    507         /**
    508          * Filters the class HTML attribute to be used on a field.
    509          *
    510          * @since 1.1.0
    511          *
    512          * @param string $value class HTML attribute with imploded classes.
    513          */
    514         return apply_filters( 'bp_get_field_css_class', ' class="' . implode( ' ', $css_classes ) . '"' );
    515     }
    516 
    517 /**
    518  * Whether or not the XProfile field has data to display.
    519  *
    520  * @return mixed
    521  */
    522 function bp_field_has_data() {
    523     global $profile_template;
    524     return $profile_template->field_has_data;
    525 }
    526 
    527 /**
    528  * Whether or not the XProfile field has public data to display.
    529  *
    530  * @return bool
    531  */
    532 function bp_field_has_public_data() {
    533     global $profile_template;
    534 
    535     if ( ! empty( $profile_template->field_has_data ) ) {
    536         return true;
    537     }
    538 
    539     return false;
    540 }
    541 
    542 /**
    543  * Output the XProfile group ID.
    544  */
    545 function bp_the_profile_group_id() {
    546     echo bp_get_the_profile_group_id();
    547 }
    548 
    549     /**
    550      * Return the XProfile group ID.
    551      *
    552      * @return mixed|void
    553      */
    554     function bp_get_the_profile_group_id() {
    555         global $group;
    556 
    557         /**
    558          * Filters the XProfile group ID.
    559          *
    560          * @since 1.1.0
    561          *
    562          * @param int $id ID for the profile group.
    563          */
    564         return apply_filters( 'bp_get_the_profile_group_id', $group->id );
    565     }
    566 
    567 /**
    568  * Output the XProfile group name.
    569  */
    570 function bp_the_profile_group_name() {
    571     echo bp_get_the_profile_group_name();
    572 }
    573 
    574     /**
    575      * Return the XProfile group name.
    576      *
    577      * @return mixed|void
    578      */
    579     function bp_get_the_profile_group_name() {
    580         global $group;
    581 
    582         /**
    583          * Filters the XProfile group name.
    584          *
    585          * @since 1.0.0
    586          *
    587          * @param string $name Name for the profile group.
    588          */
    589         return apply_filters( 'bp_get_the_profile_group_name', $group->name );
    590     }
    591 
    592 /**
    593  * Output the XProfile group slug.
    594  */
    595 function bp_the_profile_group_slug() {
    596     echo bp_get_the_profile_group_slug();
    597 }
    598 
    599     /**
    600      * Return the XProfile group slug.
    601      *
    602      * @return mixed|void
    603      */
    604     function bp_get_the_profile_group_slug() {
    605         global $group;
    606 
    607         /**
    608          * Filters the XProfile group slug.
    609          *
    610          * @since 1.1.0
    611          *
    612          * @param string $value Slug for the profile group.
    613          */
    614         return apply_filters( 'bp_get_the_profile_group_slug', sanitize_title( $group->name ) );
    615     }
    616 
    617 /**
    618  * Output the XProfile group description.
    619  */
    620 function bp_the_profile_group_description() {
    621     echo bp_get_the_profile_group_description();
    622 }
    623 
    624     /**
    625      * Return the XProfile group description.
    626      *
    627      * @return mixed|void
    628      */
    629     function bp_get_the_profile_group_description() {
    630         global $group;
    631 
    632         /**
    633          * Filters the XProfile group description.
    634          *
    635          * @since 1.0.0
    636          *
    637          * @param string $description Description for the profile group.
    638          */
    639         return apply_filters( 'bp_get_the_profile_group_description', $group->description );
    640     }
    641 
    642 /**
    643  * Output the XProfile group edit form action.
    644  */
    645 function bp_the_profile_group_edit_form_action() {
    646     echo bp_get_the_profile_group_edit_form_action();
    647 }
    648 
    649     /**
    650      * Return the XProfile group edit form action.
    651      *
    652      * @return mixed|void
    653      */
    654     function bp_get_the_profile_group_edit_form_action() {
    655         global $group;
    656 
    657         // Build the form action URL.
    658         $form_action = trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . $group->id );
    659 
    660         /**
    661          * Filters the action for the XProfile group edit form.
    662          *
    663          * @since 1.1.0
    664          *
    665          * @param string $value URL for the action attribute on the
    666          *                      profile group edit form.
    667          */
    668         return apply_filters( 'bp_get_the_profile_group_edit_form_action', $form_action );
    669     }
    670 
    671 /**
    672  * Output the XProfile group field IDs.
    673  */
    674 function bp_the_profile_group_field_ids() {
    675     echo bp_get_the_profile_group_field_ids();
    676 }
    677 
    678     /**
    679      * Return the XProfile group field IDs.
    680      *
    681      * @return string
    682      */
    683     function bp_get_the_profile_group_field_ids() {
    684         global $group;
    685 
    686         $field_ids = '';
    687 
    688         if ( !empty( $group->fields ) ) {
    689             foreach ( (array) $group->fields as $field ) {
    690                 $field_ids .= $field->id . ',';
    691             }
    692         }
    693 
    694         return substr( $field_ids, 0, -1 );
    695     }
    696 
    697 /**
    698  * Output a comma-separated list of field IDs that are to be submitted on profile edit.
    699  *
    700  * @since 2.1.0
    701  */
    702 function bp_the_profile_field_ids() {
    703     echo bp_get_the_profile_field_ids();
    704 }
    705     /**
    706      * Generate a comma-separated list of field IDs that are to be submitted on profile edit.
    707      *
    708      * @since 2.1.0
    709      *
    710      * @return string
    711      */
    712     function bp_get_the_profile_field_ids() {
    713         global $profile_template;
    714 
    715         $field_ids = array();
    716         foreach ( $profile_template->groups as $group ) {
    717             if ( ! empty( $group->fields ) ) {
    718                 $field_ids = array_merge( $field_ids, wp_list_pluck( $group->fields, 'id' ) );
    719             }
    720         }
    721 
    722         $field_ids = implode( ',', wp_parse_id_list( $field_ids ) );
    723 
    724         /**
    725          * Filters the comma-separated list of field IDs.
    726          *
    727          * @since 2.1.0
    728          *
    729          * @param string $field_ids Comma-separated field IDs.
    730          */
    731         return apply_filters( 'bp_get_the_profile_field_ids', $field_ids );
    732     }
    733 
    734 /**
    735  * Return the XProfile fields.
    736  *
    737  * @return mixed
    738  */
    739 function bp_profile_fields() {
    740     global $profile_template;
    741     return $profile_template->profile_fields();
    742 }
    743 
    744 /**
    745  * Sets up the XProfile field.
    746  *
    747  * @return mixed
    748  */
    749 function bp_the_profile_field() {
    750     global $profile_template;
    751     return $profile_template->the_profile_field();
    752 }
    753 
    754 /**
    755  * Output the XProfile field ID.
    756  */
    757 function bp_the_profile_field_id() {
    758     echo bp_get_the_profile_field_id();
    759 }
    760 
    761     /**
    762      * Return the XProfile field ID.
    763      * @return mixed|void
    764      */
    765     function bp_get_the_profile_field_id() {
    766         global $field;
    767 
    768         /**
    769          * Filters the XProfile field ID.
    770          *
    771          * @since 1.1.0
    772          *
    773          * @param int $id ID for the profile field.
    774          */
    775         return apply_filters( 'bp_get_the_profile_field_id', $field->id );
    776     }
    777 
    778 /**
    779  * Outputs the XProfile field name.
    780  */
    781 function bp_the_profile_field_name() {
    782     echo bp_get_the_profile_field_name();
    783 }
    784 
    785     /**
    786      * Returns the XProfile field name.
    787      *
    788      * @return mixed|void
    789      */
    790     function bp_get_the_profile_field_name() {
    791         global $field;
    792 
    793         /**
    794          * Filters the XProfile field name.
    795          *
    796          * @since 1.0.0
    797          *
    798          * @param string $name Name for the profile field.
    799          */
    800         return apply_filters( 'bp_get_the_profile_field_name', $field->name );
    801     }
    802 
    803 /**
    804  * Outputs the XProfile field value.
    805  */
    806 function bp_the_profile_field_value() {
    807     echo bp_get_the_profile_field_value();
    808 }
    809 
    810     /**
    811      * Returns the XProfile field value.
    812      *
    813      * @return mixed|void
    814      */
    815     function bp_get_the_profile_field_value() {
    816         global $field;
    817 
    818         $field->data->value = bp_unserialize_profile_field( $field->data->value );
    819 
    820         /**
    821          * Filters the XProfile field value.
    822          *
    823          * @since 1.0.0
    824          *
    825          * @param string $value Value for the profile field.
    826          * @param string $type  Type for the profile field.
    827          * @param int    $id    ID for the profile field.
    828          */
    829         return apply_filters( 'bp_get_the_profile_field_value', $field->data->value, $field->type, $field->id );
    830     }
    831 
    832 /**
    833  * Outputs the XProfile field edit value.
    834  */
    835 function bp_the_profile_field_edit_value() {
    836     echo bp_get_the_profile_field_edit_value();
    837 }
    838 
    839     /**
    840      * Returns the XProfile field edit value.
    841      *
    842      * @return mixed|void
    843      */
    844     function bp_get_the_profile_field_edit_value() {
    845         global $field;
    846 
    847         /**
    848          * Check to see if the posted value is different, if it is re-display this
    849          * value as long as it's not empty and a required field.
    850          */
    851         if ( ! isset( $field->data ) ) {
    852             $field->data = new stdClass;
    853         }
    854 
    855         if ( ! isset( $field->data->value ) ) {
    856             $field->data->value = '';
    857         }
    858 
    859         if ( isset( $_POST['field_' . $field->id] ) && $field->data->value != $_POST['field_' . $field->id] ) {
    860             if ( ! empty( $_POST['field_' . $field->id] ) ) {
    861                 $field->data->value = $_POST['field_' . $field->id];
    862             } else {
    863                 $field->data->value = '';
    864             }
    865         }
    866 
    867         $field_value = isset( $field->data->value ) ? bp_unserialize_profile_field( $field->data->value ) : '';
    868 
    869         /**
    870          * Filters the XProfile field edit value.
    871          *
    872          * @since 1.1.0
    873          *
    874          * @param string $field_value Current field edit value.
    875          * @param string $type        Type for the profile field.
    876          * @param int    $id          ID for the profile field.
    877          */
    878         return apply_filters( 'bp_get_the_profile_field_edit_value', $field_value, $field->type, $field->id );
    879     }
    880 
    881 /**
    882  * Outputs the XProfile field type.
    883  */
    884 function bp_the_profile_field_type() {
    885     echo bp_get_the_profile_field_type();
    886 }
    887 
    888     /**
    889      * Returns the XProfile field type.
    890      *
    891      * @return mixed|void
    892      */
    893     function bp_get_the_profile_field_type() {
    894         global $field;
    895 
    896         /**
    897          * Filters the XProfile field type.
    898          *
    899          * @since 1.1.0
    900          *
    901          * @param string $type Type for the profile field.
    902          */
    903         return apply_filters( 'bp_the_profile_field_type', $field->type );
    904     }
    905 
    906 /**
    907  * Outputs the XProfile field description.
    908  */
    909 function bp_the_profile_field_description() {
    910     echo bp_get_the_profile_field_description();
    911 }
    912 
    913     /**
    914      * Returns the XProfile field description.
    915      *
    916      * @return mixed|void
    917      */
    918     function bp_get_the_profile_field_description() {
    919         global $field;
    920 
    921         /**
    922          * Filters the XProfile field description.
    923          *
    924          * @since 1.1.0
    925          *
    926          * @param string $description Description for the profile field.
    927          */
    928         return apply_filters( 'bp_get_the_profile_field_description', $field->description );
    929     }
    930 
    931 /**
    932  * Outputs the XProfile field input name.
    933  */
    934 function bp_the_profile_field_input_name() {
    935     echo bp_get_the_profile_field_input_name();
    936 }
    937 
    938     /**
    939      * Retursn the XProfile field input name.
    940      *
    941      * @return mixed|void
    942      */
    943     function bp_get_the_profile_field_input_name() {
    944         global $field;
    945 
    946         /**
    947          * Filters the profile field input name.
    948          *
    949          * @since 1.1.0
    950          *
    951          * @param string $value Value used for the name attribute on an input.
    952          */
    953         return apply_filters( 'bp_get_the_profile_field_input_name', 'field_' . $field->id );
    954     }
    955 
    956 /**
    957  * Returns the action name for any signup errors related to this profile field.
    958  *
    959  * In the registration templates, signup errors are pulled from the global
    960  * object and rendered at actions that look like 'bp_field_12_errors'. This
    961  * function allows the action name to be easily concatenated and called in the
    962  * following fashion:
    963  *   do_action( bp_get_the_profile_field_errors_action() );
    964  *
    965  * @since 1.8.0
    966  *
    967  * @return string The _errors action name corresponding to this profile field.
    968  */
    969 function bp_get_the_profile_field_errors_action() {
    970     global $field;
    971     return 'bp_field_' . $field->id . '_errors';
    972 }
    973 
    974 /**
    975  * Displays field options HTML for field types of 'selectbox', 'multiselectbox',
    976  * 'radio', 'checkbox', and 'datebox'.
    977  *
    978  * @since 1.1.0
    979  *
    980  * @uses bp_get_the_profile_field_options()
    981  *
    982  * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'.
    983  */
    984 function bp_the_profile_field_options( $args = array() ) {
    985     echo bp_get_the_profile_field_options( $args );
    986 }
    987     /**
    988      * Retrieves field options HTML for field types of 'selectbox', 'multiselectbox', 'radio', 'checkbox', and 'datebox'.
    989      *
    990      * @since 1.1.0
    991      *
    992      * @uses BP_XProfile_Field::get_children()
    993      * @uses BP_XProfile_ProfileData::get_value_byid()
    994      *
    995      * @param array $args {
    996      *     Array of optional arguments.
    997      *     @type string|bool $type    Type of datebox. False if it's not a
    998      *                                datebox, otherwise 'day, 'month', or 'year'. Default: false.
    999      *     @type int         $user_id ID of the user whose profile values should be
    1000      *                                used when rendering options. Default: displayed user.
    1001      * }
    1002      *
    1003      * @return string $vaue Field options markup.
    1004      */
    1005     function bp_get_the_profile_field_options( $args = array() ) {
    1006         global $field;
    1007 
    1008         $args = bp_parse_args( $args, array(
    1009             'type'    => false,
    1010             'user_id' => bp_displayed_user_id(),
    1011         ), 'get_the_profile_field_options' );
    1012 
    1013         /**
    1014          * In some cases, the $field global is not an instantiation of the BP_XProfile_Field class.
    1015          * However, we have to make sure that all data originally in $field gets merged back in, after reinstantiation.
    1016          */
    1017         if ( ! method_exists( $field, 'get_children' ) ) {
    1018             $field_obj = xprofile_get_field( $field->id );
    1019 
    1020             foreach ( $field as $field_prop => $field_prop_value ) {
    1021                 if ( ! isset( $field_obj->{$field_prop} ) ) {
    1022                     $field_obj->{$field_prop} = $field_prop_value;
    1023                 }
    1024             }
    1025 
    1026             $field = $field_obj;
    1027         }
    1028 
    1029         ob_start();
    1030         $field->type_obj->edit_field_options_html( $args );
    1031         $html = ob_get_contents();
    1032         ob_end_clean();
    1033 
    1034         return $html;
    1035     }
    1036 
    1037 /**
    1038  * Render whether or not a profile field is required.
    1039  */
    1040 function bp_the_profile_field_is_required() {
    1041     echo bp_get_the_profile_field_is_required();
    1042 }
    1043 
    1044     /**
    1045      * Return whether or not a profile field is required.
    1046      * @return mixed|void
    1047      */
    1048     function bp_get_the_profile_field_is_required() {
    1049         global $field;
    1050 
    1051         $retval = false;
    1052 
    1053         if ( isset( $field->is_required ) ) {
    1054             $retval = $field->is_required;
    1055         }
    1056 
    1057         /**
    1058          * Filters whether or not a profile field is required.
    1059          *
    1060          * @since 1.1.0
    1061          *
    1062          * @param bool $retval Whether or not the field is required.
    1063          */
    1064         return apply_filters( 'bp_get_the_profile_field_is_required', (bool) $retval );
    1065     }
    1066 
    1067 /**
    1068  * Output the visibility level of this field.
    1069  */
    1070 function bp_the_profile_field_visibility_level() {
    1071     echo bp_get_the_profile_field_visibility_level();
    1072 }
    1073 
    1074     /**
    1075      * Return the visibility level of this field.
    1076      *
    1077      * @return mixed|void
    1078      */
    1079     function bp_get_the_profile_field_visibility_level() {
    1080         global $field;
    1081 
    1082         // On the registration page, values stored in POST should take
    1083         // precedence over default visibility, so that submitted values
    1084         // are not lost on failure.
    1085         if ( bp_is_register_page() && ! empty( $_POST['field_' . $field->id . '_visibility'] ) ) {
    1086             $retval = esc_attr( $_POST['field_' . $field->id . '_visibility'] );
    1087         } else {
    1088             $retval = ! empty( $field->visibility_level ) ? $field->visibility_level : 'public';
    1089         }
    1090 
    1091         /**
    1092          * Filters the profile field visibility level.
    1093          *
    1094          * @since 1.6.0
    1095          *
    1096          * @param string $retval Field visibility level.
    1097          */
    1098         return apply_filters( 'bp_get_the_profile_field_visibility_level', $retval );
    1099     }
    1100 
    1101 /**
    1102  * Echo the visibility level label of this field.
    1103  */
    1104 function bp_the_profile_field_visibility_level_label() {
    1105     echo bp_get_the_profile_field_visibility_level_label();
    1106 }
    1107 
    1108     /**
    1109      * Return the visibility level label of this field.
    1110      *
    1111      * @return mixed|void
    1112      */
    1113     function bp_get_the_profile_field_visibility_level_label() {
    1114         global $field;
    1115 
    1116         // On the registration page, values stored in POST should take
    1117         // precedence over default visibility, so that submitted values
    1118         // are not lost on failure.
    1119         if ( bp_is_register_page() && ! empty( $_POST['field_' . $field->id . '_visibility'] ) ) {
    1120             $level = esc_html( $_POST['field_' . $field->id . '_visibility'] );
    1121         } else {
    1122             $level = ! empty( $field->visibility_level ) ? $field->visibility_level : 'public';
    1123         }
    1124 
    1125         $fields = bp_xprofile_get_visibility_levels();
    1126 
    1127         /**
    1128          * Filters the profile field visibility level label.
    1129          *
    1130          * @since 1.6.0
    1131          *
    1132          * @param string $retval Field visibility level label.
    1133          */
    1134         return apply_filters( 'bp_get_the_profile_field_visibility_level_label', $fields[ $level ]['label'] );
    1135     }
    1136 
    1137 /**
    1138  * Return unserialized profile field data.
    1139  *
    1140  * @param string $value Content to maybe unserialize.
    1141  * @return mixed|string
    1142  */
    1143 function bp_unserialize_profile_field( $value ) {
    1144     if ( is_serialized($value) ) {
    1145         $field_value = maybe_unserialize($value);
    1146         $field_value = implode( ', ', $field_value );
    1147         return $field_value;
    1148     }
    1149 
    1150     return $value;
    1151 }
    1152 
    1153 /**
    1154  * Output XProfile field data.
    1155  *
    1156  * @param string|array $args Array of arguments for field data.
    1157  */
    1158 function bp_profile_field_data( $args = '' ) {
    1159     echo bp_get_profile_field_data( $args );
    1160 }
    1161 
    1162     /**
    1163      * Return XProfile field data.
    1164      *
    1165      * @param string|array $args Array of arguments for field data.
    1166      * @return mixed|void
    1167      */
    1168     function bp_get_profile_field_data( $args = '' ) {
    1169 
    1170         $r = wp_parse_args( $args, array(
    1171             'field'   => false, // Field name or ID.
    1172             'user_id' => bp_displayed_user_id()
    1173         ) );
    1174 
    1175         /**
    1176          * Filters the profile field data.
    1177          *
    1178          * @since 1.2.0
    1179          *
    1180          * @param mixed $value Profile data for a specific field for the user.
    1181          */
    1182         return apply_filters( 'bp_get_profile_field_data', xprofile_get_field_data( $r['field'], $r['user_id'] ) );
    1183     }
    1184 
    1185 /**
    1186  * Get all profile field groups.
    1187  *
    1188  * @since 2.1.0
    1189  *
    1190  * @return array $groups
    1191  */
    1192 function bp_profile_get_field_groups() {
    1193 
    1194     $groups = wp_cache_get( 'all', 'bp_xprofile_groups' );
    1195     if ( false === $groups ) {
    1196         $groups = bp_xprofile_get_groups( array( 'fetch_fields' => true ) );
    1197         wp_cache_set( 'all', $groups, 'bp_xprofile' );
    1198     }
    1199 
    1200     /**
    1201      * Filters all profile field groups.
    1202      *
    1203      * @since 2.1.0
    1204      *
    1205      * @param array $groups Array of available profile field groups.
    1206      */
    1207     return apply_filters( 'bp_profile_get_field_groups', $groups );
    1208 }
    1209 
    1210 /**
    1211  * Check if there is more than one group of fields for the profile being edited.
    1212  *
    1213  * @since 2.1.0
    1214  *
    1215  * @return bool True if there is more than one profile field group.
    1216  */
    1217 function bp_profile_has_multiple_groups() {
    1218     $has_multiple_groups = count( (array) bp_profile_get_field_groups() ) > 1;
    1219 
    1220     /**
    1221      * Filters if there is more than one group of fields for the profile being edited.
    1222      *
    1223      * @since 2.1.0
    1224      *
    1225      * @param bool $has_multiple_groups Whether or not there are multiple groups.
    1226      */
    1227     return (bool) apply_filters( 'bp_profile_has_multiple_groups', $has_multiple_groups );
    1228 }
    1229 
    1230 /**
    1231  * Output the tabs to switch between profile field groups.
    1232  *
    1233  * @since 1.0.0
    1234  */
    1235 function bp_profile_group_tabs() {
    1236     echo bp_get_profile_group_tabs();
    1237 
    1238     /**
    1239      * Fires at the end of the tab output for switching between profile field
    1240      * groups. This action is in a strange place for legacy reasons.
    1241      *
    1242      * @since 1.0.0
    1243      */
    1244     do_action( 'xprofile_profile_group_tabs' );
    1245 }
    1246 
    1247 /**
    1248  * Return the XProfile group tabs.
    1249  *
    1250  * @since 2.3.0
    1251  *
    1252  * @return string
    1253  */
    1254 function bp_get_profile_group_tabs() {
    1255 
    1256     // Get field group data.
    1257     $groups     = bp_profile_get_field_groups();
    1258     $group_name = bp_get_profile_group_name();
    1259     $tabs       = array();
    1260 
    1261     // Loop through field groups and put a tab-lst together.
    1262     for ( $i = 0, $count = count( $groups ); $i < $count; ++$i ) {
    1263 
    1264         // Setup the selected class.
    1265         $selected = '';
    1266         if ( $group_name === $groups[ $i ]->name ) {
    1267             $selected = ' class="current"';
    1268         }
    1269 
    1270         // Skip if group has no fields.
    1271         if ( empty( $groups[ $i ]->fields ) ) {
    1272             continue;
    1273         }
    1274 
    1275         // Build the profile field group link.
    1276         $link   = trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . $groups[ $i ]->id );
    1277 
    1278         // Add tab to end of tabs array.
    1279         $tabs[] = sprintf(
    1280             '<li %1$s><a href="%2$s">%3$s</a></li>',
    1281             $selected,
    1282             esc_url( $link ),
    1283             esc_html( apply_filters( 'bp_get_the_profile_group_name', $groups[ $i ]->name ) )
    1284         );
    1285     }
    1286 
    1287     /**
    1288      * Filters the tabs to display for profile field groups.
    1289      *
    1290      * @since 1.5.0
    1291      *
    1292      * @param array  $tabs       Array of tabs to display.
    1293      * @param array  $groups     Array of profile groups.
    1294      * @param string $group_name Name of the current group displayed.
    1295      */
    1296     $tabs = apply_filters( 'xprofile_filter_profile_group_tabs', $tabs, $groups, $group_name );
    1297 
    1298     return join( '', $tabs );
    1299 }
    1300 
    1301 /**
    1302  * Output the XProfile group name.
    1303  *
    1304  * @param bool $deprecated Deprecated boolean parameter.
    1305  * @return mixed|void
    1306  */
    1307 function bp_profile_group_name( $deprecated = true ) {
    1308     if ( !$deprecated ) {
    1309         return bp_get_profile_group_name();
    1310     } else {
    1311         echo bp_get_profile_group_name();
    1312     }
    1313 }
    1314 
    1315     /**
    1316      * Return the XProfile group name.
    1317      *
    1318      * @return mixed|void
    1319      */
    1320     function bp_get_profile_group_name() {
    1321 
    1322         // Check action variable.
    1323         $group_id = bp_action_variable( 1 );
    1324         if ( empty( $group_id ) || ! is_numeric( $group_id ) ) {
    1325             $group_id = 1;
    1326         }
    1327 
    1328         // Check for cached group.
    1329         $group = new BP_XProfile_Group( $group_id );
    1330 
    1331         /**
    1332          * Filters the profile group name.
    1333          *
    1334          * @since 1.0.0
    1335          *
    1336          * @param string $name Name of the profile group.
    1337          */
    1338         return apply_filters( 'bp_get_profile_group_name', $group->name );
    1339     }
    1340 
    1341 /**
    1342  * Render a formatted string displaying when a profile was last updated.
    1343  */
    1344 function bp_profile_last_updated() {
    1345 
    1346     $last_updated = bp_get_profile_last_updated();
    1347 
    1348     if ( empty( $last_updated ) ) {
    1349         _e( 'Profile not recently updated.', 'buddypress' );
    1350     } else {
    1351         echo $last_updated;
    1352     }
    1353 }
    1354 
    1355     /**
    1356      * Return a formatted string displaying when a profile was last updated.
    1357      *
    1358      * @return bool|mixed|void
    1359      */
    1360     function bp_get_profile_last_updated() {
    1361 
    1362         $last_updated = bp_get_user_meta( bp_displayed_user_id(), 'profile_last_updated', true );
    1363 
    1364         if ( ! empty( $last_updated ) ) {
    1365 
    1366             /**
    1367              * Filters the formatted string used to display when a profile was last updated.
    1368              *
    1369              * @since 1.0.0
    1370              *
    1371              * @param string $value Formatted last updated indicator string.
    1372              */
    1373             return apply_filters( 'bp_get_profile_last_updated', sprintf( __( 'Profile updated %s', 'buddypress' ), bp_core_time_since( strtotime( $last_updated ) ) ) );
    1374         }
    1375 
    1376         return false;
    1377     }
    1378 
    1379 /**
    1380  * Display the current profile group ID.
    1381  */
    1382 function bp_current_profile_group_id() {
    1383     echo bp_get_current_profile_group_id();
    1384 }
    1385 
    1386     /**
    1387      * Return the current profile group ID.
    1388      *
    1389      * @since 1.1.0
    1390      *
    1391      * @return mixed|void
    1392      */
    1393     function bp_get_current_profile_group_id() {
    1394         $profile_group_id = bp_action_variable( 1 );
    1395         if ( empty( $profile_group_id ) ) {
    1396             $profile_group_id = 1;
    1397         }
    1398 
    1399         /**
    1400          * Filters the current profile group ID.
    1401          *
    1402          * Possible values are admin/profile/edit/[group-id].
    1403          *
    1404          * @since 1.1.0
    1405          *
    1406          * @param string $profile_group_id Current profile group ID.
    1407          */
    1408         return apply_filters( 'bp_get_current_profile_group_id', $profile_group_id );
    1409     }
    1410 
    1411 /**
    1412  * Render an avatar delete link.
    1413  */
    1414 function bp_avatar_delete_link() {
    1415     echo bp_get_avatar_delete_link();
    1416 }
    1417 
    1418     /**
    1419      * Return an avatar delete link.
    1420      *
    1421      * @since 1.1.0
    1422      * @return mixed|void
    1423      */
    1424     function bp_get_avatar_delete_link() {
    1425 
    1426         /**
    1427          * Filters the link used for deleting an avatar.
    1428          *
    1429          * @since 1.1.0
    1430          *
    1431          * @param string $value Nonced URL used for deleting an avatar.
    1432          */
    1433         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' ) );
    1434     }
    1435 
    1436 /**
    1437  * Render an edit profile button.
    1438  */
    1439 function bp_edit_profile_button() {
    1440     bp_button( array(
    1441         'id'                => 'edit_profile',
    1442         'component'         => 'xprofile',
    1443         'must_be_logged_in' => true,
    1444         'block_self'        => true,
    1445         'link_href'         => trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit' ),
    1446         'link_class'        => 'edit',
    1447         'link_text'         => __( 'Edit Profile', 'buddypress' ),
    1448         'link_title'        => __( 'Edit Profile', 'buddypress' ),
    1449     ) );
    1450 }
    1451 
    1452 /** Visibility ****************************************************************/
    1453 
    1454 /**
    1455  * Echo the field visibility radio buttons.
    1456  *
    1457  * @param array|string $args Args for the radio buttons.
    1458  */
    1459 function bp_profile_visibility_radio_buttons( $args = '' ) {
    1460     echo bp_profile_get_visibility_radio_buttons( $args );
    1461 }
    1462     /**
    1463      * Return the field visibility radio buttons.
    1464      *
    1465      * @param array|string $args Args for the radio buttons.
    1466      * @return string $retval
    1467      */
    1468     function bp_profile_get_visibility_radio_buttons( $args = '' ) {
    1469 
    1470         // Parse optional arguments.
    1471         $r = bp_parse_args( $args, array(
    1472             'field_id'     => bp_get_the_profile_field_id(),
    1473             'before'       => '<ul class="radio">',
    1474             'after'        => '</ul>',
    1475             'before_radio' => '<li class="%s">',
    1476             'after_radio'  => '</li>',
    1477             'class'        => 'bp-xprofile-visibility'
    1478         ), 'xprofile_visibility_radio_buttons' );
    1479 
    1480         // Empty return value, filled in below if a valid field ID is found.
    1481         $retval = '';
    1482 
    1483         // Only do-the-do if there's a valid field ID.
    1484         if ( ! empty( $r['field_id'] ) ) :
    1485 
    1486             // Start the output buffer.
    1487             ob_start();
    1488 
    1489             // Output anything before.
    1490             echo $r['before']; ?>
    1491 
    1492             <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
    1493 
    1494                 <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
    1495 
    1496                     <?php printf( $r['before_radio'], esc_attr( $level['id'] ) ); ?>
    1497 
    1498                     <label for="<?php echo esc_attr( 'see-field_' . $r['field_id'] . '_' . $level['id'] ); ?>">
    1499                         <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() ); ?> />
    1500                         <span class="field-visibility-text"><?php echo esc_html( $level['label'] ); ?></span>
    1501                     </label>
    1502 
    1503                     <?php echo $r['after_radio']; ?>
    1504 
    1505                 <?php endforeach; ?>
    1506 
    1507             <?php endif;
    1508 
    1509             // Output anything after.
    1510             echo $r['after'];
    1511 
    1512             // Get the output buffer and empty it.
    1513             $retval = ob_get_clean();
    1514         endif;
    1515 
    1516         /**
    1517          * Filters the radio buttons for setting visibility.
    1518          *
    1519          * @since 1.6.0
    1520          *
    1521          * @param string $retval HTML output for the visibility radio buttons.
    1522          * @param array  $r      Parsed arguments to be used with display.
    1523          * @param array  $args   Original passed in arguments to be used with display.
    1524          */
    1525         return apply_filters( 'bp_profile_get_visibility_radio_buttons', $retval, $r, $args );
    1526     }
    1527 
    1528 /**
    1529  * Output the XProfile field visibility select list for settings.
    1530  *
    1531  * @since 2.0.0
    1532  *
    1533  * @param array|string $args Args for the select list.
    1534  */
    1535 function bp_profile_settings_visibility_select( $args = '' ) {
    1536     echo bp_profile_get_settings_visibility_select( $args );
    1537 }
    1538     /**
    1539      * Return the XProfile field visibility select list for settings.
    1540      *
    1541      * @since 2.0.0
    1542      *
    1543      * @param array|string $args Args for the select list.
    1544      * @return string $retval
    1545      */
    1546     function bp_profile_get_settings_visibility_select( $args = '' ) {
    1547 
    1548         // Parse optional arguments.
    1549         $r = bp_parse_args( $args, array(
    1550             'field_id' => bp_get_the_profile_field_id(),
    1551             'before'   => '',
    1552             'after'    => '',
    1553             'class'    => 'bp-xprofile-visibility'
    1554         ), 'xprofile_settings_visibility_select' );
    1555 
    1556         // Empty return value, filled in below if a valid field ID is found.
    1557         $retval = '';
    1558 
    1559         // Only do-the-do if there's a valid field ID.
    1560         if ( ! empty( $r['field_id'] ) ) :
    1561 
    1562             // Start the output buffer.
    1563             ob_start();
    1564 
    1565             // Output anything before.
    1566             echo $r['before']; ?>
    1567 
    1568             <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
    1569 
    1570                 <label for="<?php echo esc_attr( 'field_' . $r['field_id'] ) ; ?>_visibility" class="bp-screen-reader-text"><?php _e( 'Select visibility', 'buddypress' ); ?></label>
    1571                 <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">
    1572 
    1573                     <?php foreach ( bp_xprofile_get_visibility_levels() as $level ) : ?>
    1574 
    1575                         <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>
    1576 
    1577                     <?php endforeach; ?>
    1578 
    1579                 </select>
    1580 
    1581             <?php else : ?>
    1582 
    1583                 <span class="field-visibility-settings-notoggle" title="<?php esc_attr_e( "This field's visibility cannot be changed.", 'buddypress' ); ?>"><?php bp_the_profile_field_visibility_level_label(); ?></span>
    1584 
    1585             <?php endif;
    1586 
    1587             // Output anything after.
    1588             echo $r['after'];
    1589 
    1590             // Get the output buffer and empty it.
    1591             $retval = ob_get_clean();
    1592         endif;
    1593 
    1594         /**
    1595          * Filters the dropdown list for setting visibility.
    1596          *
    1597          * @since 2.0.0
    1598          *
    1599          * @param string $retval HTML output for the visibility dropdown list.
    1600          * @param array  $r      Parsed arguments to be used with display.
    1601          * @param array  $args   Original passed in arguments to be used with display.
    1602          */
    1603         return apply_filters( 'bp_profile_settings_visibility_select', $retval, $r, $args );
    1604     }
    1605 
    1606 /**
    1607  * Output the 'required' markup in extended profile field labels.
    1608  *
    1609  * @since 2.4.0
    1610  */
    1611 function bp_the_profile_field_required_label() {
    1612     echo bp_get_the_profile_field_required_label();
    1613 }
    1614 
    1615     /**
    1616      * Return the 'required' markup in extended profile field labels.
    1617      *
    1618      * @since 2.4.0
    1619      *
    1620      * @return string HTML for the required label.
    1621      */
    1622     function bp_get_the_profile_field_required_label() {
    1623         $retval = '';
    1624 
    1625         if ( bp_get_the_profile_field_is_required() ) {
    1626             $translated_string = __( '(required)', 'buddypress' );
    1627 
    1628             $retval = ' <span class="bp-required-field-label">';
    1629             $retval .= apply_filters( 'bp_get_the_profile_field_required_label', $translated_string, bp_get_the_profile_field_id() );
    1630             $retval .= '</span>';
    1631 
    1632         }
    1633 
    1634         return $retval;
    1635     }
Note: See TracChangeset for help on using the changeset viewer.