Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/custom-background.php

    r41161 r42343  
    4545     * @param callable $admin_image_div_callback Optional custom image div output callback.
    4646     */
    47     public function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
    48         $this->admin_header_callback = $admin_header_callback;
     47    public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) {
     48        $this->admin_header_callback    = $admin_header_callback;
    4949        $this->admin_image_div_callback = $admin_image_div_callback;
    5050
     
    8383     */
    8484    public function admin_load() {
    85         get_current_screen()->add_help_tab( array(
    86             'id'      => 'overview',
    87             'title'   => __('Overview'),
    88             'content' =>
    89                 '<p>' . __( 'You can customize the look of your site without touching any of your theme&#8217;s code by using a custom background. Your background can be an image or a color.' ) . '</p>' .
    90                 '<p>' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '</p>' .
    91                 '<p>' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
    92                 '<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>'
    93         ) );
     85        get_current_screen()->add_help_tab(
     86            array(
     87                'id'      => 'overview',
     88                'title'   => __( 'Overview' ),
     89                'content' =>
     90                    '<p>' . __( 'You can customize the look of your site without touching any of your theme&#8217;s code by using a custom background. Your background can be an image or a color.' ) . '</p>' .
     91                    '<p>' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '</p>' .
     92                    '<p>' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
     93                    '<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>',
     94            )
     95        );
    9496
    9597        get_current_screen()->set_help_sidebar(
     
    100102
    101103        wp_enqueue_media();
    102         wp_enqueue_script('custom-background');
    103         wp_enqueue_style('wp-color-picker');
     104        wp_enqueue_script( 'custom-background' );
     105        wp_enqueue_style( 'wp-color-picker' );
    104106    }
    105107
     
    110112     */
    111113    public function take_action() {
    112         if ( empty($_POST) )
     114        if ( empty( $_POST ) ) {
    113115            return;
    114 
    115         if ( isset($_POST['reset-background']) ) {
    116             check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
    117             remove_theme_mod('background_image');
    118             remove_theme_mod('background_image_thumb');
     116        }
     117
     118        if ( isset( $_POST['reset-background'] ) ) {
     119            check_admin_referer( 'custom-background-reset', '_wpnonce-custom-background-reset' );
     120            remove_theme_mod( 'background_image' );
     121            remove_theme_mod( 'background_image_thumb' );
    119122            $this->updated = true;
    120123            return;
    121124        }
    122125
    123         if ( isset($_POST['remove-background']) ) {
     126        if ( isset( $_POST['remove-background'] ) ) {
    124127            // @TODO: Uploaded files are not removed here.
    125             check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
    126             set_theme_mod('background_image', '');
    127             set_theme_mod('background_image_thumb', '');
     128            check_admin_referer( 'custom-background-remove', '_wpnonce-custom-background-remove' );
     129            set_theme_mod( 'background_image', '' );
     130            set_theme_mod( 'background_image_thumb', '' );
    128131            $this->updated = true;
    129132            wp_safe_redirect( $_POST['_wp_http_referer'] );
     
    200203        }
    201204
    202         if ( isset($_POST['background-color']) ) {
    203             check_admin_referer('custom-background');
    204             $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
    205             if ( strlen($color) == 6 || strlen($color) == 3 )
    206                 set_theme_mod('background_color', $color);
    207             else
    208                 set_theme_mod('background_color', '');
     205        if ( isset( $_POST['background-color'] ) ) {
     206            check_admin_referer( 'custom-background' );
     207            $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] );
     208            if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
     209                set_theme_mod( 'background_color', $color );
     210            } else {
     211                set_theme_mod( 'background_color', '' );
     212            }
    209213        }
    210214
     
    253257    } else {
    254258        $background_styles = '';
    255         if ( $bgcolor = get_background_color() )
     259        if ( $bgcolor = get_background_color() ) {
    256260            $background_styles .= 'background-color: #' . $bgcolor . ';';
     261        }
    257262
    258263        $background_image_thumb = get_background_image();
    259264        if ( $background_image_thumb ) {
    260265            $background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
    261             $background_position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
    262             $background_position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
    263             $background_size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
    264             $background_repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
    265             $background_attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
     266            $background_position_x  = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
     267            $background_position_y  = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
     268            $background_size        = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
     269            $background_repeat      = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
     270            $background_attachment  = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
    266271
    267272            // Background-image URL must be single quote, see below.
     
    285290<?php if ( get_background_image() ) : ?>
    286291<tr>
    287 <th scope="row"><?php _e('Remove Image'); ?></th>
     292<th scope="row"><?php _e( 'Remove Image' ); ?></th>
    288293<td>
    289294<form method="post">
    290 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
     295<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
    291296<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
    292 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
     297<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?>
    293298</form>
    294299</td>
     
    299304<?php if ( $default_image && get_background_image() != $default_image ) : ?>
    300305<tr>
    301 <th scope="row"><?php _e('Restore Original Image'); ?></th>
     306<th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
    302307<td>
    303308<form method="post">
    304 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
     309<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
    305310<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
    306 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
     311<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?>
    307312</form>
    308313</td>
     
    310315<?php endif; ?>
    311316
    312 <?php if ( current_user_can( 'upload_files' ) ): ?>
    313 <tr>
    314 <th scope="row"><?php _e('Select Image'); ?></th>
     317<?php if ( current_user_can( 'upload_files' ) ) : ?>
     318<tr>
     319<th scope="row"><?php _e( 'Select Image' ); ?></th>
    315320<td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
    316321    <p>
     
    350355$background_position_options = array(
    351356    array(
    352         'left top'   => array( 'label' => __( 'Top Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
    353         'center top' => array( 'label' => __( 'Top' ), 'icon' => 'dashicons dashicons-arrow-up-alt' ),
    354         'right top'  => array( 'label' => __( 'Top Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
     357        'left top'   => array(
     358            'label' => __( 'Top Left' ),
     359            'icon'  => 'dashicons dashicons-arrow-left-alt',
     360        ),
     361        'center top' => array(
     362            'label' => __( 'Top' ),
     363            'icon'  => 'dashicons dashicons-arrow-up-alt',
     364        ),
     365        'right top'  => array(
     366            'label' => __( 'Top Right' ),
     367            'icon'  => 'dashicons dashicons-arrow-right-alt',
     368        ),
    355369    ),
    356370    array(
    357         'left center'   => array( 'label' => __( 'Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
    358         'center center' => array( 'label' => __( 'Center' ), 'icon' => 'background-position-center-icon' ),
    359         'right center'  => array( 'label' => __( 'Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
     371        'left center'   => array(
     372            'label' => __( 'Left' ),
     373            'icon'  => 'dashicons dashicons-arrow-left-alt',
     374        ),
     375        'center center' => array(
     376            'label' => __( 'Center' ),
     377            'icon'  => 'background-position-center-icon',
     378        ),
     379        'right center'  => array(
     380            'label' => __( 'Right' ),
     381            'icon'  => 'dashicons dashicons-arrow-right-alt',
     382        ),
    360383    ),
    361384    array(
    362         'left bottom'   => array( 'label' => __( 'Bottom Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
    363         'center bottom' => array( 'label' => __( 'Bottom' ), 'icon' => 'dashicons dashicons-arrow-down-alt' ),
    364         'right bottom'  => array( 'label' => __( 'Bottom Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
     385        'left bottom'   => array(
     386            'label' => __( 'Bottom Left' ),
     387            'icon'  => 'dashicons dashicons-arrow-left-alt',
     388        ),
     389        'center bottom' => array(
     390            'label' => __( 'Bottom' ),
     391            'icon'  => 'dashicons dashicons-arrow-down-alt',
     392        ),
     393        'right bottom'  => array(
     394            'label' => __( 'Bottom Right' ),
     395            'icon'  => 'dashicons dashicons-arrow-right-alt',
     396        ),
    365397    ),
    366398);
     
    417449<?php
    418450$default_color = '';
    419 if ( current_theme_supports( 'custom-background', 'default-color' ) )
     451if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
    420452    $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
     453}
    421454?>
    422 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?>>
     455<input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>>
    423456</fieldset></td>
    424457</tr>
     
    426459</table>
    427460
    428 <?php wp_nonce_field('custom-background'); ?>
     461<?php wp_nonce_field( 'custom-background' ); ?>
    429462<?php submit_button( null, 'primary', 'save-background-options' ); ?>
    430463</form>
     
    440473     */
    441474    public function handle_upload() {
    442         if ( empty($_FILES) )
     475        if ( empty( $_FILES ) ) {
    443476            return;
    444 
    445         check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
    446         $overrides = array('test_form' => false);
     477        }
     478
     479        check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );
     480        $overrides = array( 'test_form' => false );
    447481
    448482        $uploaded_file = $_FILES['import'];
    449         $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
    450         if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
     483        $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
     484        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
    451485            wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
    452 
    453         $file = wp_handle_upload($uploaded_file, $overrides);
    454 
    455         if ( isset($file['error']) )
     486        }
     487
     488        $file = wp_handle_upload( $uploaded_file, $overrides );
     489
     490        if ( isset( $file['error'] ) ) {
    456491            wp_die( $file['error'] );
    457 
    458         $url = $file['url'];
    459         $type = $file['type'];
    460         $file = $file['file'];
    461         $filename = basename($file);
     492        }
     493
     494        $url      = $file['url'];
     495        $type     = $file['type'];
     496        $file     = $file['file'];
     497        $filename = basename( $file );
    462498
    463499        // Construct the object array
    464500        $object = array(
    465             'post_title' => $filename,
    466             'post_content' => $url,
     501            'post_title'     => $filename,
     502            'post_content'   => $url,
    467503            'post_mime_type' => $type,
    468             'guid' => $url,
    469             'context' => 'custom-background'
     504            'guid'           => $url,
     505            'context'        => 'custom-background',
    470506        );
    471507
    472508        // Save the data
    473         $id = wp_insert_attachment($object, $file);
     509        $id = wp_insert_attachment( $object, $file );
    474510
    475511        // Add the meta-data
    476512        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    477         update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
    478 
    479         set_theme_mod('background_image', esc_url_raw($url));
     513        update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
     514
     515        set_theme_mod( 'background_image', esc_url_raw( $url ) );
    480516
    481517        $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
    482         set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
     518        set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
    483519
    484520        /** This action is documented in wp-admin/custom-header.php */
     
    513549
    514550    /**
    515      *
    516551     * @since 3.4.0
    517552     * @deprecated 3.5.0
     
    525560
    526561    /**
    527      *
    528562     * @since 3.4.0
    529563     * @deprecated 3.5.0
     
    537571
    538572    /**
    539      *
    540573     * @since 3.4.0
    541574     * @deprecated 3.5.0
    542575     */
    543576    public function wp_set_background_image() {
    544         if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
    545         $attachment_id = absint($_POST['attachment_id']);
     577        if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
     578            exit;
     579        }
     580        $attachment_id = absint( $_POST['attachment_id'] );
    546581        /** This filter is documented in wp-admin/includes/media.php */
    547         $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
    548         $size = 'thumbnail';
    549         if ( in_array( $_POST['size'], $sizes ) )
     582        $sizes = array_keys(
     583            apply_filters(
     584                'image_size_names_choose', array(
     585                    'thumbnail' => __( 'Thumbnail' ),
     586                    'medium'    => __( 'Medium' ),
     587                    'large'     => __( 'Large' ),
     588                    'full'      => __( 'Full Size' ),
     589                )
     590            )
     591        );
     592        $size  = 'thumbnail';
     593        if ( in_array( $_POST['size'], $sizes ) ) {
    550594            $size = esc_attr( $_POST['size'] );
    551 
    552         update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
    553         $url = wp_get_attachment_image_src( $attachment_id, $size );
     595        }
     596
     597        update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
     598        $url       = wp_get_attachment_image_src( $attachment_id, $size );
    554599        $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
    555600        set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
Note: See TracChangeset for help on using the changeset viewer.