WordPress.org

Make WordPress Core

Ticket #40885: 40885.diff

File 40885.diff, 8.3 KB (added by rmccue, 3 years ago)

Use /wp/v2/users/me for changing color scheme

  • src/wp-admin/admin-ajax.php

    diff --git a/src/wp-admin/admin-ajax.php b/src/wp-admin/admin-ajax.php
    index 3213d55028..2966c21252 100644
    a b $core_actions_post = array( 
    6060        'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    6161        'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
    6262        'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
    63         'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
     63        'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
    6464        'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
    6565        'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
    6666        'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
    $core_actions_post = array( 
    6969
    7070// Deprecated
    7171$core_actions_post[] = 'wp-fullscreen-save-post';
     72$core_actions_post[] = 'save-user-color-scheme';
    7273
    7374// Register core Ajax calls.
    7475if ( ! empty( $_GET['action'] ) && in_array( $_GET['action'], $core_actions_get ) )
  • src/wp-admin/includes/ajax-actions.php

    diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php
    index e132ac8101..ad3c7d0a67 100644
    a b function wp_ajax_get_revision_diffs() { 
    28742874}
    28752875
    28762876/**
    2877  * Ajax handler for auto-saving the selected color scheme for
    2878  * a user's own profile.
    2879  *
    2880  * @since 3.8.0
    2881  *
    2882  * @global array $_wp_admin_css_colors
    2883  */
    2884 function wp_ajax_save_user_color_scheme() {
    2885         global $_wp_admin_css_colors;
    2886 
    2887         check_ajax_referer( 'save-color-scheme', 'nonce' );
    2888 
    2889         $color_scheme = sanitize_key( $_POST['color_scheme'] );
    2890 
    2891         if ( ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
    2892                 wp_send_json_error();
    2893         }
    2894 
    2895         $previous_color_scheme = get_user_meta( get_current_user_id(), 'admin_color', true );
    2896         update_user_meta( get_current_user_id(), 'admin_color', $color_scheme );
    2897 
    2898         wp_send_json_success( array(
    2899                 'previousScheme' => 'admin-color-' . $previous_color_scheme,
    2900                 'currentScheme'  => 'admin-color-' . $color_scheme
    2901         ) );
    2902 }
    2903 
    2904 /**
    29052877 * Ajax handler for getting themes from themes_api().
    29062878 *
    29072879 * @since 3.9.0
  • src/wp-admin/includes/deprecated.php

    diff --git a/src/wp-admin/includes/deprecated.php b/src/wp-admin/includes/deprecated.php
    index a9e0e6f9d1..06672d6068 100644
    a b function post_form_autocomplete_off() { 
    14781478                echo ' autocomplete="off"';
    14791479        }
    14801480}
     1481
     1482/**
     1483 * Ajax handler for auto-saving the selected color scheme for
     1484 * a user's own profile.
     1485 *
     1486 * @since 3.8.0
     1487 * @deprecated 4.8.0 Use /wp/v2/users REST API endpoint instead.
     1488 *
     1489 * @global array $_wp_admin_css_colors
     1490 */
     1491function wp_ajax_save_user_color_scheme() {
     1492        global $_wp_admin_css_colors;
     1493
     1494        _deprecated_function( __FUNCTION__, '4.8.0', '/wp/v2/users' );
     1495
     1496        check_ajax_referer( 'save-color-scheme', 'nonce' );
     1497
     1498        $color_scheme = sanitize_key( $_POST['color_scheme'] );
     1499
     1500        if ( ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
     1501                wp_send_json_error();
     1502        }
     1503
     1504        $previous_color_scheme = get_user_meta( get_current_user_id(), 'admin_color', true );
     1505        update_user_meta( get_current_user_id(), 'admin_color', $color_scheme );
     1506
     1507        wp_send_json_success( array(
     1508                'previousScheme' => 'admin-color-' . $previous_color_scheme,
     1509                'currentScheme'  => 'admin-color-' . $color_scheme
     1510        ) );
     1511}
  • src/wp-admin/includes/misc.php

    diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php
    index 679b9ac5d6..277d6456dd 100644
    a b function admin_color_scheme_picker( $user_id ) { 
    658658        <fieldset id="color-picker" class="scheme-list">
    659659                <legend class="screen-reader-text"><span><?php _e( 'Admin Color Scheme' ); ?></span></legend>
    660660                <?php
    661                 wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
    662661                foreach ( $_wp_admin_css_colors as $color => $color_info ) :
    663662
    664663                        ?>
  • src/wp-admin/js/user-profile.js

    diff --git a/src/wp-admin/js/user-profile.js b/src/wp-admin/js/user-profile.js
    index ca99584215..1c919eba85 100644
    a b  
    327327        }
    328328
    329329        $(document).ready( function() {
    330                 var $colorpicker, $stylesheet, user_id, current_user_id,
     330                var $colorpicker, $stylesheet, user_id, current_user_id, current_scheme,
    331331                        select       = $( '#display_name' ),
    332332                        current_name = select.val(),
    333333                        greeting     = $( '#wp-admin-bar-my-account' ).find( '.display-name' );
     
    391391                $stylesheet = $( '#colors-css' );
    392392                user_id = $( 'input#user_id' ).val();
    393393                current_user_id = $( 'input[name="checkuser_id"]' ).val();
     394                current_scheme = $( 'input[name="admin_color"]:checked' ).val();
    394395
    395396                $colorpicker.on( 'click.colorpicker', '.color-option', function() {
    396                         var colors,
     397                        var colors, user, previous_scheme
    397398                                $this = $(this);
    398399
    399400                        if ( $this.hasClass( 'selected' ) ) {
     
    425426                                }
    426427
    427428                                // update user option
    428                                 $.post( ajaxurl, {
    429                                         action:       'save-user-color-scheme',
    430                                         color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
    431                                         nonce:        $('#color-nonce').val()
    432                                 }).done( function( response ) {
    433                                         if ( response.success ) {
    434                                                 $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
    435                                         }
     429                                previous_scheme = current_scheme;
     430                                current_scheme = $this.children( 'input[name="admin_color"]' ).val();
     431
     432                                user = new wp.api.models.UsersMe();
     433                                user.save( { meta: { admin_color: current_scheme } } ).done( function ( response ) {
     434                                        $( 'body' ).removeClass( 'admin-color-' + previous_scheme ).addClass( 'admin-color-' + current_scheme );
    436435                                });
    437436                        }
    438437                });
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index 8f0237028d..b72efaedc5 100644
    a b add_filter( 'get_post_format', '_post_format_get_term' ); 
    426426add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
    427427add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
    428428
     429// User
     430add_action( 'init', 'create_initial_user_meta', 0 );
     431
    429432// KSES
    430433add_action( 'init', 'kses_init' );
    431434add_action( 'set_current_user', 'kses_init' );
  • src/wp-includes/script-loader.php

    diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php
    index ff7d8bb855..25213de631 100644
    a b function wp_default_scripts( &$scripts ) { 
    406406                'mismatch' => _x( 'Mismatch', 'password mismatch' ),
    407407        ) );
    408408
    409         $scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'jquery', 'password-strength-meter', 'wp-util' ), false, 1 );
     409        $scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'jquery', 'password-strength-meter', 'wp-util', 'wp-api' ), false, 1 );
    410410        did_action( 'init' ) && $scripts->localize( 'user-profile', 'userProfileL10n', array(
    411411                'warn'     => __( 'Your new password has not been saved.' ),
    412412                'warnWeak' => __( 'Confirm use of weak password' ),
  • src/wp-includes/user.php

    diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
    index 2bb4e0543e..ef556c10b1 100644
    a b  
    77 */
    88
    99/**
     10 * Registers the initial user meta.
     11 *
     12 * @since 4.8.0
     13 */
     14function create_initial_user_meta() {
     15        register_meta( 'user', 'admin_color', array(
     16                'type'              => 'string',
     17                'description'       => __( 'Admin color scheme' ),
     18                'single'            => true,
     19                'sanitize_callback' => 'wp_sanitize_admin_color',
     20                'show_in_rest'      => true,
     21        ));
     22}
     23
     24/**
     25 * Sanitizes admin_color meta value.
     26 *
     27 * @since 4.8.0
     28 *
     29 * @param string $value Supplied admin color scheme value.
     30 * @return string Sanitized admin color scheme value.
     31 */
     32function wp_sanitize_admin_color( $value ) {
     33        $value = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $value );
     34        $value = sanitize_key( $value );
     35        if ( empty( $value ) ) {
     36                return 'fresh';
     37        }
     38
     39        return $value;
     40}
     41
     42/**
    1043 * Authenticates and logs a user in with 'remember' capability.
    1144 *
    1245 * The credentials is an array that has 'user_login', 'user_password', and