Пример #1
0
/**
 * Get the ids of fields that are hidden for this displayed/loggedin user pair
 *
 * This is the function primarily responsible for profile field visibility. It works by determining
 * the relationship between the displayed_user (ie the profile owner) and the current_user (ie the
 * profile viewer). Then, based on that relationship, we query for the set of fields that should
 * be excluded from the profile loop.
 *
 * @since BuddyPress (1.6.0)
 * @see BP_XProfile_Group::get()
 * @uses apply_filters() Filter bp_xprofile_get_hidden_fields_for_user to modify visibility levels,
 *   or if you have added your own custom levels
 *
 * @param int $displayed_user_id The id of the user the profile fields belong to
 * @param int $current_user_id The id of the user viewing the profile
 * @return array An array of field ids that should be excluded from the profile query
 */
function bp_xprofile_get_hidden_fields_for_user($displayed_user_id = 0, $current_user_id = 0)
{
    if (!$displayed_user_id) {
        $displayed_user_id = bp_displayed_user_id();
    }
    if (!$displayed_user_id) {
        return array();
    }
    if (!$current_user_id) {
        $current_user_id = bp_loggedin_user_id();
    }
    // @todo - This is where you'd swap out for current_user_can() checks
    $hidden_levels = bp_xprofile_get_hidden_field_types_for_user($displayed_user_id, $current_user_id);
    $hidden_fields = bp_xprofile_get_fields_by_visibility_levels($displayed_user_id, $hidden_levels);
    /**
     * Filters the ids of fields that are hidden for this displayed/loggedin user pair.
     *
     * @since BuddyPress (1.6.0)
     *
     * @param array $hidden_fields     Array of hidden fields for the displayed/logged in user.
     * @param int   $displayed_user_id ID of the displayed user.
     * @param int   $current_user_id   ID of the current user.
     */
    return apply_filters('bp_xprofile_get_hidden_fields_for_user', $hidden_fields, $displayed_user_id, $current_user_id);
}
Пример #2
0
function amt_buddypress_jsonld_schemaorg($metadata_arr, $post, $options, $attachments, $embedded_media)
{
    // User Profiles
    // Determines if a BuddyPress user profile has been requested
    if (bp_is_user_profile()) {
        // https://codex.buddypress.org/developer/the-bp-global/
        global $bp;
        // $user_id = $bp->displayed_user->id;
        $user_id = bp_displayed_user_id();
        // $user_domain = $bp->displayed_user->domain;
        // bp_core_get_user_domain( bp_displayed_user_id() )
        $user_domain = bp_displayed_user_domain();
        $user_profile_url = trailingslashit(bp_displayed_user_domain() . amt_bp_get_profile_slug());
        $user_fullname = $bp->displayed_user->fullname;
        // $user_fullname = bp_displayed_user_fullname();
        // $user_username = $bp->displayed_user->user_login;
        $user_username = bp_get_displayed_user_username();
        //$wp_user_obj = get_user_by( 'id', $user_id );
        $wp_user_obj = get_userdata($user_id);
        //var_dump($wp_user_obj);
        // Context
        $metadata_arr['@context'] = 'http://schema.org';
        // Schema.org type
        $metadata_arr['@type'] = 'Person';
        // name
        $metadata_arr['name'] = esc_attr($user_fullname);
        // URL
        $metadata_arr['url'] = esc_url($user_profile_url, array('http', 'https'));
        // mainEntityOfPage
        $metadata_arr['mainEntityOfPage'] = esc_url($user_profile_url, array('http', 'https'));
        // Related resources as sameAs
        $metadata_arr['sameAs'] = array();
        // Facebook Profile
        //$fb_author_url = get_the_author_meta('amt_facebook_author_profile_url', $user_id);
        $fb_author_url = get_user_meta($user_id, 'amt_facebook_author_profile_url', true);
        if (!empty($fb_author_url)) {
            $metadata_arr['sameAs'][] = esc_url($fb_author_url, array('http', 'https'));
        }
        // Twitter
        //$twitter_author_username = get_the_author_meta('amt_twitter_author_username', $user_id);
        $twitter_author_username = get_user_meta($user_id, 'amt_twitter_author_username', true);
        if (!empty($twitter_author_username)) {
            $metadata_arr['sameAs'][] = 'https://twitter.com/' . esc_attr($twitter_author_username);
        }
        // Google+
        //$googleplus_author_url = get_the_author_meta('amt_googleplus_author_profile_url', $wp_user_obj);
        $googleplus_author_url = get_user_meta($user_id, 'amt_googleplus_author_profile_url', true);
        if (!empty($googleplus_author_url)) {
            $metadata_arr['sameAs'][] = esc_url($googleplus_author_url, array('http', 'https'));
        }
        // Determines if Extended Profiles component is active.
        if (!bp_is_active('xprofile')) {
            // Website
            //$website_url = get_user_meta($user_id, 'amt_googleplus_author_profile_url', true);
            $website_url = get_the_author_meta('user_url', $user_id);
            if (!empty($website_url)) {
                $metadata_arr['sameAs'][] = esc_url($website_url, array('http', 'https'));
            }
            // Description
            $author_description = sanitize_text_field(amt_sanitize_description($wp_user_obj->description));
            if (empty($author_description)) {
                $metadata_arr['description'] = esc_attr(__('Profile of', 'add-meta-tags') . ' ' . $wp_user_obj->display_name);
            } else {
                $metadata_arr['description'] = esc_attr($author_description);
            }
            // Profile Image
            $author_email = sanitize_email($wp_user_obj->user_email);
            $avatar_size = apply_filters('amt_bp_avatar_size', array('width' => 50, 'height' => 50));
            $avatar_url = '';
            // First try to get the avatar link by using get_avatar().
            // Important: for this to work the "Show Avatars" option should be enabled in Settings > Discussion.
            $avatar_img = get_avatar(get_the_author_meta('ID', $wp_user_obj->ID), $avatar_size, '', get_the_author_meta('display_name', $wp_user_obj->ID));
            if (!empty($avatar_img)) {
                if (preg_match("#src=['\"]([^'\"]+)['\"]#", $avatar_img, $matches)) {
                    $avatar_url = $matches[1];
                }
            } elseif (!empty($author_email)) {
                // If the user has provided an email, we use it to construct a gravatar link.
                $avatar_url = "http://www.gravatar.com/avatar/" . md5($author_email) . "?s=" . $avatar_size;
            }
            if (!empty($avatar_url)) {
                //$avatar_url = html_entity_decode($avatar_url, ENT_NOQUOTES, 'UTF-8');
                $metadata_arr['image'] = esc_url($avatar_url);
            }
            // familyName
            $last_name = $wp_user_obj->last_name;
            if (!empty($last_name)) {
                $metadata_arr['familyName'] = esc_attr($last_name);
            }
            // givenName
            $first_name = $wp_user_obj->first_name;
            if (!empty($first_name)) {
                $metadata_arr['givenName'] = esc_attr($first_name);
            }
            // Extended Profiles
        } else {
            // https://codex.buddypress.org/themes/guides/displaying-extended-profile-fields-on-member-profiles/
            $xprofile_field_map = amt_buddypress_get_xprofile_field_map();
            // Get list of IDs of public fields
            $xprofile_public_fields = bp_xprofile_get_fields_by_visibility_levels($user_id, array('public'));
            // Website
            $field_value = amt_bp_get_profile_field_data('website', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['sameAs'][] = esc_url($field_value, array('http', 'https'));
            }
            // Description
            $field_value = amt_bp_get_profile_field_data('description', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field(amt_sanitize_description($field_value));
            if (!empty($field_value)) {
                $metadata_arr['description'] = esc_attr($field_value);
            } else {
                $metadata_arr['description'] = esc_attr(__('Profile of', 'add-meta-tags') . ' ' . $user_fullname);
            }
            // Profile Image
            // Important: for this to work the "Show Avatars" option should be enabled in Settings > Discussion.
            $avatar_size = apply_filters('amt_bp_avatar_size', array('width' => 50, 'height' => 50));
            $avatar_url = '';
            $avatar_args = array('item_id' => $user_id, 'width' => $avatar_size['width'], 'height' => $avatar_size['height']);
            $avatar_img = bp_core_fetch_avatar($avatar_args);
            if (!empty($avatar_img)) {
                if (preg_match("#src=['\"]([^'\"]+)['\"]#", $avatar_img, $matches)) {
                    $avatar_url = $matches[1];
                }
            }
            if (!empty($avatar_url)) {
                //$avatar_url = html_entity_decode($avatar_url, ENT_NOQUOTES, 'UTF-8');
                $metadata_arr['image'] = esc_url($avatar_url);
            }
            // familyName
            $has_last_name = false;
            $field_value = amt_bp_get_profile_field_data('last_name', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['familyName'] = esc_attr($field_value);
                $has_last_name = true;
            }
            // givenName
            $has_first_name = false;
            $field_value = amt_bp_get_profile_field_data('first_name', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['givenName'] = esc_attr($field_value);
                $has_first_name = true;
            }
            // Generate first and last name from full name if needed.
            if (!$has_last_name && !$has_first_name && !empty($user_fullname)) {
                $parts = explode(' ', $user_fullname);
                $last_name = sanitize_text_field(array_pop($parts));
                // Removes and returns the element off the end of array
                if (!empty($last_name)) {
                    $metadata_arr['familyName'] = esc_attr($last_name);
                }
                $first_name = sanitize_text_field(implode(' ', $parts));
                if (!empty($first_name)) {
                    $metadata_arr['givenName'] = esc_attr($first_name);
                }
            }
            // alternateName
            $field_value = amt_bp_get_profile_field_data('nickname', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['alternateName'] = esc_attr($field_value);
            }
            // additionalName
            $field_value = amt_bp_get_profile_field_data('additional_name', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['additionalName'] = esc_attr($field_value);
            }
            // honorificPrefix
            $field_value = amt_bp_get_profile_field_data('honorific_prefix', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['honorificPrefix'] = esc_attr($field_value);
            }
            // honorificSuffix
            $field_value = amt_bp_get_profile_field_data('honorific_suffix', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['honorificSuffix'] = esc_attr($field_value);
            }
            // gender
            $field_value = amt_bp_get_profile_field_data('gender', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['gender'] = esc_attr($field_value);
            }
            // nationality
            $field_value = amt_bp_get_profile_field_data('nationality', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['nationality'] = array();
                $metadata_arr['nationality']['@type'] = 'Country';
                $metadata_arr['nationality']['name'] = esc_attr($field_value);
            }
            // telephone
            $field_value = amt_bp_get_profile_field_data('telephone', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['telephone'] = esc_attr($field_value);
            }
            // faxNumber
            $field_value = amt_bp_get_profile_field_data('fax', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['faxNumber'] = esc_attr($field_value);
            }
            // email
            $field_value = amt_bp_get_profile_field_data('email', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['email'] = 'mailto:' . esc_attr($field_value);
            }
            // jobTitle
            $field_value = amt_bp_get_profile_field_data('job_title', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['jobTitle'] = esc_attr($field_value);
            }
            // worksFor
            $field_value = amt_bp_get_profile_field_data('works_for', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $metadata_arr['worksFor'] = array();
                $metadata_arr['worksFor']['@type'] = 'Organization';
                $metadata_arr['worksFor']['name'] = esc_attr($field_value);
            }
            // worksFor URL
            $field_value = amt_bp_get_profile_field_data('works_for_url', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                if (!array_key_exists('worksFor', $metadata_arr) || !is_array($metadata_arr['worksFor'])) {
                    $metadata_arr['worksFor'] = array();
                    $metadata_arr['worksFor']['@type'] = 'Organization';
                }
                $metadata_arr['worksFor']['url'] = esc_attr($field_value);
            }
            // Home Location Geo Coordinates
            // home latitude
            $latitude = '';
            $field_value = amt_bp_get_profile_field_data('home_latitude', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $latitude = esc_attr($field_value);
            }
            // home longitude
            $longitude = '';
            $field_value = amt_bp_get_profile_field_data('home_longitude', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $longitude = esc_attr($field_value);
            }
            if (!empty($latitude) && !empty($longitude)) {
                $metadata_arr['homeLocation'] = array();
                $metadata_arr['homeLocation']['@type'] = 'Place';
                $metadata_arr['homeLocation']['latitude'] = esc_attr($latitude);
                $metadata_arr['homeLocation']['longitude'] = esc_attr($longitude);
            }
            // Work Location Geo Coordinates
            // work latitude
            $latitude = '';
            $field_value = amt_bp_get_profile_field_data('work_latitude', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $latitude = esc_attr($field_value);
            }
            // work longitude
            $longitude = '';
            $field_value = amt_bp_get_profile_field_data('work_longitude', $user_id, $xprofile_field_map, $xprofile_public_fields);
            $field_value = sanitize_text_field($field_value);
            if (!empty($field_value)) {
                $longitude = esc_attr($field_value);
            }
            if (!empty($latitude) && !empty($longitude)) {
                $metadata_arr['workLocation'] = array();
                $metadata_arr['workLocation']['@type'] = 'Place';
                $metadata_arr['workLocation']['latitude'] = esc_attr($latitude);
                $metadata_arr['workLocation']['longitude'] = esc_attr($longitude);
            }
        }
    }
    // Allow filtering of the generated metadata
    // Customize with: add_filter('amt_buddypress_jsonld_schemaorg_extra', 'my_function', 10, 5);
    $metadata_arr = apply_filters('amt_buddypress_jsonld_schemaorg_extra', $metadata_arr, $post, $options, $attachments, $embedded_media);
    return $metadata_arr;
}
/**
 * Get the ids of fields that are hidden for this displayed/loggedin user pair
 *
 * This is the function primarily responsible for profile field visibility. It works by determining
 * the relationship between the displayed_user (ie the profile owner) and the current_user (ie the
 * profile viewer). Then, based on that relationship, we query for the set of fields that should
 * be excluded from the profile loop.
 *
 * @since 1.6
 * @see BP_XProfile_Group::get()
 * @uses apply_filters() Filter bp_xprofile_get_hidden_fields_for_user to modify visibility levels,
 *   or if you have added your own custom levels
 *
 * @param int $displayed_user_id The id of the user the profile fields belong to
 * @param int $current_user_id The id of the user viewing the profile
 * @return array An array of field ids that should be excluded from the profile query
 */
function bp_xprofile_get_hidden_fields_for_user($displayed_user_id = 0, $current_user_id = 0)
{
    if (!$displayed_user_id) {
        $displayed_user_id = bp_displayed_user_id();
    }
    if (!$displayed_user_id) {
        return array();
    }
    if (!$current_user_id) {
        $current_user_id = bp_loggedin_user_id();
    }
    // @todo - This is where you'd swap out for current_user_can() checks
    if ($current_user_id) {
        // Current user is logged in
        if ($displayed_user_id == $current_user_id) {
            // If you're viewing your own profile, nothing's private
            $hidden_fields = array();
        } else {
            if (bp_is_active('friends') && friends_check_friendship($displayed_user_id, $current_user_id)) {
                // If the current user and displayed user are friends, show all
                $hidden_fields = array();
            } else {
                // current user is logged-in but not friends, so exclude friends-only
                $hidden_levels = array('friends');
                $hidden_fields = bp_xprofile_get_fields_by_visibility_levels($displayed_user_id, $hidden_levels);
            }
        }
    } else {
        // Current user is not logged in, so exclude friends-only and loggedin
        $hidden_levels = array('friends', 'loggedin');
        $hidden_fields = bp_xprofile_get_fields_by_visibility_levels($displayed_user_id, $hidden_levels);
    }
    return apply_filters('bp_xprofile_get_hidden_fields_for_user', $hidden_fields, $displayed_user_id, $current_user_id);
}
/**
 * Get the ids of fields that are hidden for this displayed/loggedin user pair
 *
 * This is the function primarily responsible for profile field visibility. It works by determining
 * the relationship between the displayed_user (ie the profile owner) and the current_user (ie the
 * profile viewer). Then, based on that relationship, we query for the set of fields that should
 * be excluded from the profile loop.
 *
 * @since BuddyPress (1.6)
 * @see BP_XProfile_Group::get()
 * @uses apply_filters() Filter bp_xprofile_get_hidden_fields_for_user to modify visibility levels,
 *   or if you have added your own custom levels
 *
 * @param int $displayed_user_id The id of the user the profile fields belong to
 * @param int $current_user_id The id of the user viewing the profile
 * @return array An array of field ids that should be excluded from the profile query
 */
function bp_xprofile_get_hidden_fields_for_user($displayed_user_id = 0, $current_user_id = 0)
{
    if (!$displayed_user_id) {
        $displayed_user_id = bp_displayed_user_id();
    }
    if (!$displayed_user_id) {
        return array();
    }
    if (!$current_user_id) {
        $current_user_id = bp_loggedin_user_id();
    }
    // @todo - This is where you'd swap out for current_user_can() checks
    $hidden_levels = bp_xprofile_get_hidden_field_types_for_user($displayed_user_id, $current_user_id);
    $hidden_fields = bp_xprofile_get_fields_by_visibility_levels($displayed_user_id, $hidden_levels);
    return apply_filters('bp_xprofile_get_hidden_fields_for_user', $hidden_fields, $displayed_user_id, $current_user_id);
}