Skip to:
Content

BuddyPress.org

Ticket #5715: 5715.diff

File 5715.diff, 8.8 KB (added by boonebgorges, 7 years ago)
  • src/bp-core/admin/bp-core-admin-settings.php

    diff --git src/bp-core/admin/bp-core-admin-settings.php src/bp-core/admin/bp-core-admin-settings.php
    index 4bfcc69..41463d9 100644
    function bp_core_admin_settings_save() { 
    330330                        'bp-disable-profile-sync',
    331331                        'bp_restrict_group_creation',
    332332                        'hide-loggedout-adminbar',
     333                        //Added for default profile text avatar
     334                        'bp-disable-choice-avatar',
    333335                );
    334336
    335337                foreach( $legacy_options as $legacy_option ) {
    336338                        // Note: Each of these options is represented by its opposite in the UI
    337339                        // Ie, the Profile Syncing option reads "Enable Sync", so when it's checked,
    338340                        // the corresponding option should be unset.
    339                         $value = isset( $_POST[$legacy_option] ) ? '' : 1;
     341                        $value = (isset( $_POST[$legacy_option] ) OR !empty( $_POST[$legacy_option] ))? '' : 1;
     342
     343                        //Validation POST data from the drop-down list avatar customization plug-in
     344            if($legacy_option == 'bp-disable-choice-avatar'){
     345
     346              $value = ($_POST['bp-disable-choice-avatar'] == 1)? 1: 0;
     347           }
     348
    340349                        bp_update_option( $legacy_option, $value );
    341350                }
    342351
    function bp_core_admin_settings_save() { 
    346355add_action( 'bp_admin_init', 'bp_core_admin_settings_save', 100 );
    347356
    348357/**
     358 * To use Text Avatars
     359 *
     360 * @since BuddyPress (2.4)
     361 *
     362 * @uses selected() To display the selected attribute
     363 */
     364function bp_admin_setting_choice_avatar_plugin()
     365{
     366    $disable_choice_avatar = (int)bp_disable_choice_avatar(false);
     367    ?>
     368
     369    <select id="bp-disable-choice-avatar" name="bp-disable-choice-avatar">
     370        <option value="0" <?php selected($disable_choice_avatar, 0); ?>><?php _e('Mystery Man', 'buddypress'); ?></option>
     371        <option value="1" <?php selected($disable_choice_avatar, 1); ?>><?php _e('Textavatar', 'buddypress'); ?></option>
     372    </select>
     373    <p class="description"><?php _e('If a users profile photo is not available what should be shown as the users profile photo?', 'buddypress'); ?></p>
     374<?php
     375}
     376
     377/**
    349378 * Output settings API option.
    350379 *
    351380 * @since 1.6.0
  • src/bp-core/bp-core-avatars.php

    diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php
    index 786c09f..d2dfe30 100644
    function bp_core_fetch_avatar( $args = '' ) { 
    564564                }
    565565        }
    566566
     567
     568           /** Filters should skip the First letter Avatar.
     569     *
     570     * @since BuddyPress (2.4)
     571     *
     572     * @param bool  $value  Whether or not to skip First letter Avatar.
     573     * @param array $params Array of parameters for the avatar request.
     574     */
     575    $disable_choice_avatar = bp_get_option('bp-disable-choice-avatar');
     576    if ($disable_choice_avatar == 1) {
     577        $user_id   = '';
     578        $item_name = '';
     579
     580        if ($params['object'] == 'user') {
     581            $item_name = bp_core_get_user_displayname($params['item_id']);
     582            $user_id   = $params['item_id'];
     583        }
     584
     585        $background = bp_get_background_letter($item_name);
     586
     587        $letter = bp_get_first_letter($item_name);
     588
     589        if ($letter != '') {
     590
     591            return bp_get_text_avatar_html($letter, $background, $html_class, $html_css_id, $html_width, $html_height);
     592        }
     593    }
     594
    567595        /**
    568596         * Filters whether or not to skip Gravatar check.
    569597         *
    function bp_core_fetch_avatar( $args = '' ) { 
    686714}
    687715
    688716/**
     717 * Get html content for text avatar
     718 * @param $letter
     719 * @param $background_color
     720 * @param $html_class
     721 * @param $html_css_id
     722 * @param $html_width
     723 * @param $html_height
     724 * @return String
     725 */
     726function bp_get_text_avatar_html($letter, $background_color, $html_class, $html_css_id, $html_width, $html_height)
     727{
     728
     729    $width     = preg_replace("/[^0-9]/", '', $html_width);
     730    $height    = preg_replace("/[^0-9]/", '', $html_height);
     731    $font_size = 16;
     732
     733    $styles = array();
     734
     735    if ($width > 0) {
     736        $styles[] = "width: {$width}px;";
     737    }
     738
     739    if ($height > 0) {
     740        $styles[] = "height: {$height}px;";
     741
     742        $font_size = round($height / 1.5);
     743    }
     744
     745    if (!empty($background_color)) {
     746        $styles[] = "background-color: $background_color;";
     747    }
     748
     749    if ($font_size > 0) {
     750        $styles[] = "font-size: {$font_size}px";
     751    }
     752
     753    $styles = implode("", $styles);
     754
     755    $html_class = substr(trim($html_class), 0, -1);
     756    $html_class .= ' text-avatar"';
     757
     758    return sprintf('<div style="%s;" %s %s %s id="%s">%s</div>', $styles, $html_width, $html_height, $html_class, $html_css_id, $letter);
     759}
     760
     761/**
    689762 * Delete an existing avatar.
    690763 *
    691764 * @since 1.1.0
  • src/bp-core/bp-core-options.php

    diff --git src/bp-core/bp-core-options.php src/bp-core/bp-core-options.php
    index d04568e..d586336 100644
    function bp_get_default_options() { 
    118118        return apply_filters( 'bp_get_default_options', $options );
    119119}
    120120
     121 /**
     122 * Show avatar plugin?
     123 *
     124 * @since BuddyPress (2.4)
     125 *
     126 * @uses bp_get_option() To get the avatar show option.
     127 *
     128 * @param bool $default Optional. Fallback value if not found in the database.
     129 *        Default: true.
     130 * @return bool True if avatar show are disabled, otherwise false.
     131 */
     132function bp_disable_choice_avatar( $default = false ) {
     133    return (bool) apply_filters( 'bp_disable_choice_avatar', (bool) bp_get_option( 'bp-disable-choice-avatar', $default ) );
     134}
     135
    121136/**
    122137 * Add default options when BuddyPress is first activated.
    123138 *
  • src/bp-core/classes/class-bp-admin.php

    diff --git src/bp-core/classes/class-bp-admin.php src/bp-core/classes/class-bp-admin.php
    index 73f6982..3a3b1de 100644
    class BP_Admin { 
    413413                        add_settings_field( 'bp-disable-avatar-uploads', __( 'Profile Photo Uploads', 'buddypress' ), 'bp_admin_setting_callback_avatar_uploads', 'buddypress', 'bp_xprofile' );
    414414                        register_setting( 'buddypress', 'bp-disable-avatar-uploads', 'intval' );
    415415
     416                        add_settings_field( 'bp-disable-choice-avatar', __( 'Default Profile Photo',  'buddypress' ), 'bp_admin_setting_choice_avatar_plugin', 'buddypress', 'bp_xprofile' );
     417            register_setting( 'buddypress', 'bp-disable-choice-avatar', 'intval');
     418
    416419                        // Cover images.
    417420                        if ( bp_is_active( 'xprofile', 'cover_image' ) ) {
    418421                                add_settings_field( 'bp-disable-cover-image-uploads', __( 'Cover Image Uploads', 'buddypress' ), 'bp_admin_setting_callback_cover_image_uploads', 'buddypress', 'bp_xprofile' );
  • src/bp-members/bp-members-functions.php

    diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php
    index 23435a3..5782500 100644
    function bp_core_delete_avatar_on_user_delete( $user_id ) { 
    13741374add_action( 'wpmu_delete_user', 'bp_core_delete_avatar_on_user_delete' );
    13751375add_action( 'delete_user', 'bp_core_delete_avatar_on_user_delete' );
    13761376
     1377
     1378/**
     1379 *To get the first letter of the name of the current items
     1380 *
     1381 * @param  string $item_name name current items.
     1382 * @since BuddyPress (2.2.0)
     1383 * @return string.
     1384 */
     1385function bp_get_first_letter($item_name)
     1386{
     1387    $letter = '';
     1388    $valid_letter = '';
     1389
     1390    if(!empty($item_name)){
     1391        $letter = mb_substr($item_name, 0, 1);
     1392    }
     1393
     1394    if (preg_match("/^[a-zA-Z]+$/", $letter)) {
     1395        $valid_letter = $letter;
     1396   }
     1397
     1398    return apply_filters( 'bp_get_first_letter', $valid_letter, $item_name);
     1399}
     1400add_filter('bp_get_first_letter','mb_strtoupper');
     1401
     1402/**
     1403 * Get the background color of the letters the user
     1404 *
     1405 * @param  int $user_id ID of the user.
     1406 * @since BuddyPress (2.2.0)
     1407 * @return string.
     1408 */
     1409function bp_get_background_letter($name)
     1410{
     1411
     1412    return bp_get_background($name);
     1413}
     1414
     1415/**
     1416 * Getting the color code.
     1417 *
     1418 * @since BuddyPress (2.2.0)
     1419 * @return string.
     1420 */
     1421function bp_get_background($name){
     1422
     1423        $default_color = '333333';
     1424        $hash = 0;
     1425    $color = '#';
     1426    $value = 0;
     1427    $name_length = strlen($name);
     1428
     1429         if(!$name || empty($name)) {
     1430        return $color.$default_color;
     1431    }
     1432
     1433    for($i = 0; $i < $name_length; $i++) {
     1434        $hash = ord($name{$i})+(($hash << 5)-$hash);
     1435    }
     1436
     1437
     1438    for ($i=0; $i < 3; $i++) {
     1439        $value = ($hash >> ($i*8)) & 0xFF;
     1440        $color .= substr('00'.dechex($value),-2);
     1441    }
     1442
     1443    return $color;
     1444}
     1445
     1446function bp_register_text_avatar_styles() {
     1447
     1448
     1449        $min = bp_core_get_minified_asset_suffix();
     1450        $url = buddypress()->plugin_url . 'bp-core/css/';
     1451
     1452        $style_file = "$urlavatar$min.css";
     1453
     1454        wp_register_style('bp-text-avatar',  "{$url}text-avatar{$min}.css", Array(), bp_get_version() );
     1455
     1456        $disable_choice_avatar = bp_get_option('bp-disable-choice-avatar');
     1457    if ($disable_choice_avatar == 1) {
     1458        wp_enqueue_style('bp-text-avatar');
     1459    }
     1460}
     1461add_action( 'bp_enqueue_scripts','bp_register_text_avatar_styles', 1 );
     1462add_action( 'bp_admin_enqueue_scripts', 'bp_register_text_avatar_styles',1);
     1463
    13771464/**
    13781465 * Multibyte-safe ucfirst() support.
    13791466 *