Make WordPress Core

Ticket #16020: 16020.3.diff

File 16020.3.diff, 20.2 KB (added by cdog, 13 years ago)
  • wp-includes/user.php

     
    13091309        }
    13101310        $display_name = apply_filters( 'pre_user_display_name', $display_name );
    13111311
     1312        if ( empty( $avatar_type ) || $avatar_type != 'custom' )
     1313                $avatar_type = 'gravatar';
     1314
     1315        if ( empty( $custom_avatar ) )
     1316                $custom_avatar = '';
     1317
    13121318        if ( empty($description) )
    13131319                $description = '';
    13141320        $description = apply_filters('pre_user_description', $description);
     
    14751481 * @return array
    14761482 */
    14771483function _get_additional_user_keys( $user ) {
    1478         $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
     1484        $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'avatar_type', 'custom_avatar' );
    14791485        return array_merge( $keys, array_keys( _wp_get_user_contactmethods( $user ) ) );
    14801486}
    14811487
  • wp-includes/formatting.php

     
    27872787                                $value = absint( $value );
    27882788                        break;
    27892789
     2790                case 'avatar_size':
     2791                        $value = absint( $value );
     2792                        if ( $value < 1 )
     2793                                $value = 1;
     2794                        elseif ( $value > 512 )
     2795                                $value = 512;
     2796                        break;
     2797
    27902798                case 'posts_per_page':
    27912799                case 'posts_per_rss':
    27922800                        $value = (int) $value;
  • wp-includes/pluggable.php

     
    15791579 * @param int $size Size of the avatar image
    15801580 * @param string $default URL to a default image to use if no avatar is available
    15811581 * @param string $alt Alternative text to use in image tag. Defaults to blank
     1582 * @param string $avatar_type Override user preference (Used in user-edit.php)
    15821583 * @return string <img> tag for the user's avatar
    15831584*/
    1584 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
     1585function get_avatar( $id_or_email, $size = '', $default = '', $alt = false, $avatar_type = '' ) {
    15851586        if ( ! get_option('show_avatars') )
    15861587                return false;
    15871588
     
    15901591        else
    15911592                $safe_alt = esc_attr( $alt );
    15921593
    1593         if ( !is_numeric($size) )
    1594                 $size = '96';
     1594        if ( empty( $size ) || ! is_numeric( $size ) ) {
     1595                $size = get_option( 'avatar_size' );
     1596        } else {
     1597                $size = absint( $size );
    15951598
     1599                if ( $size < 1 )
     1600                        $size = 1;
     1601                elseif ( $size > 512 )
     1602                        $size = 512;
     1603        }
     1604 
     1605        if ( ! empty( $avatar_type ) )
     1606                if ( $avatar_type != 'custom' )
     1607                        $avatar_type = 'gravatar';
     1608
    15961609        $email = '';
    15971610        if ( is_numeric($id_or_email) ) {
    15981611                $id = (int) $id_or_email;
     
    16171630                $email = $id_or_email;
    16181631        }
    16191632
     1633        if ( isset( $user ) ) {
     1634                if ( empty( $avatar_type ) )
     1635                        $avatar_type = $user->avatar_type;
     1636        } else {
     1637                $avatar_type = 'gravatar';
     1638        }
     1639
    16201640        if ( empty($default) ) {
    16211641                $avatar_default = get_option('avatar_default');
    16221642                if ( empty($avatar_default) )
     
    16501670        elseif ( strpos($default, 'http://') === 0 )
    16511671                $default = add_query_arg( 's', $size, $default );
    16521672
    1653         if ( !empty($email) ) {
    1654                 $out = "$host/avatar/";
    1655                 $out .= $email_hash;
    1656                 $out .= '?s='.$size;
    1657                 $out .= '&amp;d=' . urlencode( $default );
     1673        $img = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
     1674 
     1675        if ( $avatar_type == 'custom' ) {
     1676                $avatar_rating = get_option( 'avatar_rating' );
     1677                $custom_avatar_rating = get_post_meta( $user->custom_avatar, '_wp_attachment_custom_avatar_rating', true );
    16581678
     1679                $ratings['G']  = 1;
     1680                $ratings['PG'] = 2;
     1681                $ratings['R']  = 3;
     1682                $ratings['X']  = 4;
     1683
     1684                if ( $ratings[ $custom_avatar_rating ] <= $ratings[ $avatar_rating ] ) {
     1685                        $custom_avatar = get_post_meta( $user->custom_avatar, '_wp_attachment_custom_avatar', true );
     1686
     1687                        if ( empty( $custom_avatar[ $size ] ) ) {
     1688                                $url = wp_get_attachment_image_src( $user->custom_avatar, 'full' );
     1689
     1690                                // Resize the avatar image
     1691                                $custom_avatar[ $size ] = custom_avatar_resize( $url[0], $size );
     1692
     1693                                // Update the user meta-data
     1694                                update_post_meta( $user->custom_avatar, '_wp_attachment_custom_avatar', $custom_avatar );
     1695                        }
     1696
     1697                        $src = $custom_avatar[ $size ]['url'];
     1698                        $img = "<img alt='{$safe_alt}' src='{$src}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
     1699                }
     1700        } else if ( !empty($email) ) {
     1701                $src = "$host/avatar/";
     1702                $src .= $email_hash;
     1703                $src .= '?s='.$size;
     1704                $src .= '&amp;d=' . urlencode( $default );
     1705
    16591706                $rating = get_option('avatar_rating');
    16601707                if ( !empty( $rating ) )
    1661                         $out .= "&amp;r={$rating}";
     1708                        $src .= "&amp;r={$rating}";
    16621709
    1663                 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    1664         } else {
    1665                 $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
     1710                $img = "<img alt='{$safe_alt}' src='{$src}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    16661711        }
    16671712
    1668         return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
     1713        return apply_filters('get_avatar', $img, $id_or_email, $size, $default, $alt, $avatar_type);
    16691714}
    16701715endif;
    16711716
  • wp-admin/includes/misc.php

     
    582582<?php
    583583}
    584584
     585/**
     586 * Resize a custom avatar
     587 *
     588 * @since 3.5.0
     589 * @param string URL to an avatar image to resize
     590 * @param int Size of the new avatar image
     591 * @return array Array with the URL of the new avatar image
     592 */
     593function custom_avatar_resize( $url, $size ) {
     594        $upload_dir = wp_upload_dir();
     595        $file = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $url );
     596        $info = pathinfo($file);
     597        $dir = $info['dirname'];
     598        $ext = $info['extension'];
     599        $name = wp_basename($file, ".$ext");
     600        $suffix = "{$size}x{$size}";
     601        $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
     602        $custom_avatar = array();
     603
     604        if ( file_exists( $destfilename ) ) {
     605                $custom_avatar['url'] = str_replace( $upload_dir['basedir'], $upload_dir['baseurl'], $destfilename );
     606                $custom_avatar['skip'] = true;
     607        } else {
     608                $file = image_resize( $file, $size, $size, true );
     609
     610                if ( ! is_wp_error( $file ) ) {
     611                        $custom_avatar['url'] = str_replace( $upload_dir['basedir'], $upload_dir['baseurl'], $file );
     612                        $custom_avatar['skip'] = false;
     613                }
     614        }
     615
     616        return $custom_avatar;
     617}
     618
     619/**
     620 * Cleanup a custom avatar
     621 *
     622 * @since 3.5.0
     623 * @param int $attachment_id An attachment ID
     624 */
     625function custom_avatar_cleanup( $attachment_id ) {
     626        $upload_dir = wp_upload_dir();
     627        $custom_avatar = get_post_meta( $attachment_id, '_wp_attachment_custom_avatar', true );
     628
     629        if ( is_array( $custom_avatar ) ) {
     630                foreach ( $custom_avatar as $file ) {
     631                        if ( ! $file['skip'] ) {
     632                                $file = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $file['url'] );
     633                                @unlink( $file );
     634                        }
     635                }
     636        }
     637
     638        delete_post_meta( $attachment_id, '_wp_attachment_custom_avatar' );
     639        delete_post_meta( $attachment_id, '_wp_attachment_custom_avatar_rating' );
     640        delete_post_meta( $attachment_id, '_wp_attachment_is_custom_avatar' );
     641}
     642
     643/**
     644 * Delete a custom avatar
     645 *
     646 * @since 3.5.0
     647 * @param int $attachment_id An attachment ID
     648 */
     649function delete_custom_avatar( $attachment_id ) {
     650        custom_avatar_cleanup( $attachment_id );
     651
     652        $args = array(
     653                'meta_key' => 'custom_avatar',
     654                'meta_value' => $attachment_id
     655        );
     656
     657        $users = get_users( $args );
     658
     659        foreach ($users as $user) {
     660                // Update the user meta-data
     661                update_user_meta( $user->ID, 'avatar_type', 'gravatar' );
     662                delete_user_meta( $user->ID, 'custom_avatar' );
     663        }
     664}
     665
     666add_action( 'delete_attachment', 'delete_custom_avatar' );
     667
    585668function _ipad_meta() {
    586669        if ( wp_is_mobile() ) {
    587670                ?>
  • wp-admin/includes/schema.php

     
    482482
    483483        // 3.5
    484484        'link_manager_enabled' => 0,
     485        'avatar_size' => 96
    485486        );
    486487
    487488        // 3.3
  • wp-admin/includes/template.php

     
    15181518        $media_states = array();
    15191519        $stylesheet = get_option('stylesheet');
    15201520
     1521        $meta_avatar = get_post_meta( $post->ID, '_wp_attachment_is_custom_avatar', true );
     1522        if ( ! empty( $meta_avatar ) )
     1523                $media_states[] = __( 'Avatar Image' );
     1524
    15211525        if ( current_theme_supports( 'custom-header') ) {
    15221526                $meta_header = get_post_meta($post->ID, '_wp_attachment_is_custom_header', true );
    15231527                if ( ! empty( $meta_header ) && $meta_header == $stylesheet )
     
    15361540                $state_count = count( $media_states );
    15371541                $i = 0;
    15381542                echo ' - ';
     1543                echo '<span class="post-state">';
    15391544                foreach ( $media_states as $state ) {
    15401545                        ++$i;
    15411546                        ( $i == $state_count ) ? $sep = '' : $sep = ', ';
    1542                         echo "<span class='post-state'>$state$sep</span>";
     1547                        echo $state . $sep;
    15431548                }
     1549                echo '</span>';
    15441550        }
    15451551}
    15461552
  • wp-admin/includes/user.php

     
    9494                $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true';
    9595                $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
    9696                $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
     97                $user->avatar_type = isset( $_POST['avatar_type'] ) ? sanitize_text_field( $_POST['avatar_type'] ) : 'gravatar';
     98
     99                if ( isset( $userdata->custom_avatar ) ) {
     100                        $user->custom_avatar = $userdata->custom_avatar;
     101                        $custom_avatar_rating = isset( $_POST['custom_avatar_rating'] ) ? sanitize_text_field( $_POST['custom_avatar_rating'] ) : 'G';
     102
     103                        // Update the user meta-data
     104                        update_post_meta( $user->custom_avatar, '_wp_attachment_custom_avatar_rating', $custom_avatar_rating );
     105                }
     106
     107                // Add a new avatar
     108                if ( isset( $_POST['upload-avatar'] ) && $_POST['upload-avatar'] ) {
     109                        if ( isset( $user->custom_avatar ) ) {
     110                                // Reset the current avatar
     111                                custom_avatar_cleanup( $user->custom_avatar );
     112                        }
     113
     114                        $mimes = array(
     115                                'bmp'   =>  'image/bmp',
     116                                'gif'   =>  'image/gif',
     117                                'jpeg'  =>  'image/jpeg',
     118                                'jpg'   =>  'image/jpeg',
     119                                'jpe'   =>  'image/jpeg',
     120                                'png'   =>  'image/png',
     121                                'tiff'  =>  'image/tiff',
     122                                'tif'   =>  'image/tiff'
     123                        );
     124
     125                        $overrides = array( 'mimes' => $mimes, 'test_form' => false );
     126                        $file = wp_handle_upload( $_FILES['import'], $overrides );
     127
     128                        if ( isset( $file['error'] ) )
     129                                wp_die( $file['error'],  __( 'Image Upload Error' ) );
     130
     131                        $url = $file['url'];
     132                        $type = $file['type'];
     133                        $file = $file['file'];
     134                        $filename = basename($file);
     135
     136                        // Construct the object array
     137                        $object = array(
     138                                'guid'           => $url,
     139                                'post_content'   => $url,
     140                                'post_mime_type' => $type,
     141                                'post_title'     => $filename
     142                        );
     143
     144                        // Save the data
     145                        $attachment_id = wp_insert_attachment( $object, $file );
     146
     147                        // Add the attachment meta-data
     148                        wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
     149
     150                        $size = get_option( 'avatar_size' );
     151                        $custom_avatar = array();
     152
     153                        // Resize the avatar image to default size
     154                        $custom_avatar[ $size ] = custom_avatar_resize( $url, $size );
     155
     156                        update_post_meta( $attachment_id, '_wp_attachment_custom_avatar', $custom_avatar );
     157                        update_post_meta( $attachment_id, '_wp_attachment_custom_avatar_rating', 'G' );
     158                        update_post_meta( $attachment_id, '_wp_attachment_is_custom_avatar', true );
     159
     160                        // Add the user meta-data
     161                        $user->avatar_type = 'custom';
     162                        $user->custom_avatar = $attachment_id;
     163                }
     164
     165                // Remove the current avatar
     166                if ( isset( $_POST['remove-avatar'] ) && $_POST['remove-avatar'] ) {
     167                        if ( isset( $user->custom_avatar ) ) {
     168                                custom_avatar_cleanup( $user->custom_avatar );
     169                        }
     170
     171                        // Update the user meta-data
     172                        $user->avatar_type = 'gravatar';
     173                        unset( $user->custom_avatar );
     174                }
    97175        }
    98176
    99177        $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
  • wp-admin/options-discussion.php

     
    243243
    244244</fieldset></td>
    245245</tr>
     246<tr valign="top">
     247        <th scope="row"><?php _e( 'Avatar Size' ) ?></th>
     248        <td>
     249                <fieldset>
     250                        <legend class="screen-reader-text"><span><?php _e( 'Avatar Size' ); ?></span></legend>
     251                        <label>
     252                                <?php _e( 'Size of the avatar image' ); ?>
     253                                <input name="avatar_size" type="number" step="1" min="0" value="<?php form_option( 'avatar_size' ); ?>" class="small-text" />
     254                        </label>
     255                </fieldset>
     256        </td>
     257</tr>
    246258<?php do_settings_fields('discussion', 'avatars'); ?>
    247259</table>
    248260
  • wp-admin/options.php

     
    6060
    6161$whitelist_options = array(
    6262        'general' => array( 'blogname', 'blogdescription', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'timezone_string' ),
    63         'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
     63        'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'avatar_size', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
    6464        'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type', 'embed_autourls', 'embed_size_w', 'embed_size_h' ),
    6565        'reading' => array( 'posts_per_page', 'posts_per_rss', 'rss_use_excerpt', 'show_on_front', 'page_on_front', 'page_for_posts', 'blog_public' ),
    6666        'writing' => array( 'default_post_edit_rows', 'use_smilies', 'default_category', 'default_email_category', 'use_balanceTags', 'default_link_category', 'default_post_format' )
  • wp-admin/user-edit.php

     
    189189} ?>
    190190</h2>
    191191
    192 <form id="your-profile" action="<?php echo esc_url( self_admin_url( IS_PROFILE_PAGE ? 'profile.php' : 'user-edit.php' ) ); ?>" method="post"<?php do_action('user_edit_form_tag'); ?>>
     192<form enctype="multipart/form-data" id="your-profile" action="<?php echo esc_url( self_admin_url( IS_PROFILE_PAGE ? 'profile.php' : 'user-edit.php' ) ); ?>" method="post"<?php do_action('user_edit_form_tag'); ?>>
    193193<?php wp_nonce_field('update-user_' . $user_id) ?>
    194194<?php if ( $wp_http_referer ) : ?>
    195195        <input type="hidden" name="wp_http_referer" value="<?php echo esc_url($wp_http_referer); ?>" />
     
    362362?>
    363363</table>
    364364
     365<h3><?php _e( 'Avatar' ); ?></h3>
     366
     367<?php
     368$avatar_type = isset( $profileuser->avatar_type ) ? $profileuser->avatar_type : 'gravatar';
     369
     370if ( isset( $profileuser->custom_avatar ) ) {
     371        $custom_avatar_rating = get_post_meta( $profileuser->custom_avatar, '_wp_attachment_custom_avatar_rating', true );
     372        $has_custom_avatar = get_post_meta( $profileuser->custom_avatar, '_wp_attachment_is_custom_avatar', true );
     373}
     374
     375if ( ! isset( $custom_avatar_rating ) )
     376        $custom_avatar_rating = 'G';
     377
     378if ( ! isset( $has_custom_avatar ) )
     379        $has_custom_avatar = false;
     380?>
     381
     382<table class="form-table">
     383<tr>
     384        <th><?php _e( 'Display this avatar' ); ?></th>
     385        <td class="avatar-picker">
     386                <fieldset>
     387                        <legend class="screen-reader-text"><span><?php _e( 'Display this avatar' ); ?></span></legend>
     388                        <label>
     389                                <input <?php checked( $avatar_type, 'gravatar' ); ?> name="avatar_type" type="radio" value="gravatar" />
     390                                <?php echo get_avatar( $profileuser->ID, 32, '', false, 'gravatar' ); ?>
     391                                <?php _e( 'Gravatar' ); ?>
     392                                <span class="description"><?php _e( '<a href="http://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress" target="_blank">More information</a>' ); ?></span>
     393                        </label>
     394                        <?php if ( $has_custom_avatar ) : ?>
     395                        <br />
     396                        <label>
     397                                <input <?php checked( $avatar_type, 'custom' ); ?> name="avatar_type" type="radio" value="custom" />
     398                                <?php echo get_avatar( $profileuser->ID, 32, '', false, 'custom' ); ?>
     399                                <?php _e( 'Custom' ); ?>
     400                        </label>
     401                        <?php endif; ?>
     402                </fieldset>
     403        </td>
     404</tr>
     405<?php if ( current_user_can( 'upload_files' ) ) : ?>
     406<tr>
     407        <th><?php _e( 'Select Image' ); ?></th>
     408        <td>
     409                <fieldset>
     410                        <legend class="screen-reader-text"><span><?php _e( 'Select Image' ); ?></span></legend>
     411                        <label class="description" for="upload-avatar"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     412                        <input name="import" type="file" />
     413                        <?php submit_button( __( 'Upload' ), 'button', 'upload-avatar', false ); ?>
     414                </fieldset>
     415        </td>
     416</tr>
     417<?php endif; ?>
     418<?php if ( $has_custom_avatar ) : ?>
     419<tr>
     420        <th><?php _e( 'Avatar Rating' ); ?></th>
     421        <td>
     422                <fieldset>
     423                        <legend class="screen-reader-text"><span><?php _e( 'Avatar Rating' ); ?></span></legend>
     424                        <?php
     425                        $ratings = array(
     426                                /* translators: Content suitability rating: http://bit.ly/89QxZA */
     427                                'G' => __( 'G &#8212; Suitable for all audiences' ),
     428                                /* translators: Content suitability rating: http://bit.ly/89QxZA */
     429                                'PG' => __( 'PG &#8212; Possibly offensive, usually for audiences 13 and above' ),
     430                                /* translators: Content suitability rating: http://bit.ly/89QxZA */
     431                                'R' => __( 'R &#8212; Intended for adult audiences above 17' ),
     432                                /* translators: Content suitability rating: http://bit.ly/89QxZA */
     433                                'X' => __( 'X &#8212; Even more mature than above' )
     434                        );
     435
     436                        foreach ( $ratings as $key => $rating ) {
     437                                $selected = ( $custom_avatar_rating == $key ) ? 'checked="checked"' : '';
     438                                echo '<label><input ' . $selected . ' name="custom_avatar_rating" type="radio" value="' . esc_attr( $key ) . '" /> ' . $rating . '</label><br />';
     439                        };
     440                        ?>
     441                        <span class="description"><?php _e( 'Choose a rating for your custom avatar.' ); ?></span>
     442                </fieldset>
     443        </td>
     444</tr>
     445<?php endif; ?>
     446<?php if ( $has_custom_avatar && current_user_can( 'upload_files' ) ) : ?>
     447<tr>
     448        <th><?php _e( 'Remove Image' ); ?></th>
     449        <td>
     450                <legend class="screen-reader-text"><span><?php _e( 'Remove Image' ); ?></span></legend>
     451                <?php submit_button( __( 'Remove Avatar Image' ), 'button', 'remove-avatar', false ); ?>
     452                <span class="description"><?php _e( 'This will remove the avatar image. You will not be able to restore any customizations.' ); ?></span>
     453        </td>
     454</tr>
     455<?php endif; ?>
     456</table>
     457
    365458<h3><?php IS_PROFILE_PAGE ? _e('About Yourself') : _e('About the user'); ?></h3>
    366459
    367460<table class="form-table">
  • wp-admin/css/wp-admin.css

     
    47884788        width: 15em;
    47894789}
    47904790
     4791#profile-page .avatar-picker img {
     4792        margin: 2px 0;
     4793        vertical-align: middle;
     4794}
     4795
    47914796#createuser .form-field input {
    47924797        width: 25em;
    47934798}