Changeset 42343 for trunk/src/wp-admin/custom-background.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-admin/custom-background.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/custom-background.php
r41161 r42343 45 45 * @param callable $admin_image_div_callback Optional custom image div output callback. 46 46 */ 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; 49 49 $this->admin_image_div_callback = $admin_image_div_callback; 50 50 … … 83 83 */ 84 84 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’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 “Choose Image” 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. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' . 92 '<p>' . __( 'Don’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’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 “Choose Image” 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. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' . 93 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>', 94 ) 95 ); 94 96 95 97 get_current_screen()->set_help_sidebar( … … 100 102 101 103 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' ); 104 106 } 105 107 … … 110 112 */ 111 113 public function take_action() { 112 if ( empty( $_POST) )114 if ( empty( $_POST ) ) { 113 115 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' ); 119 122 $this->updated = true; 120 123 return; 121 124 } 122 125 123 if ( isset( $_POST['remove-background']) ) {126 if ( isset( $_POST['remove-background'] ) ) { 124 127 // @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', '' ); 128 131 $this->updated = true; 129 132 wp_safe_redirect( $_POST['_wp_http_referer'] ); … … 200 203 } 201 204 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 } 209 213 } 210 214 … … 253 257 } else { 254 258 $background_styles = ''; 255 if ( $bgcolor = get_background_color() ) 259 if ( $bgcolor = get_background_color() ) { 256 260 $background_styles .= 'background-color: #' . $bgcolor . ';'; 261 } 257 262 258 263 $background_image_thumb = get_background_image(); 259 264 if ( $background_image_thumb ) { 260 265 $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' ) ); 266 271 267 272 // Background-image URL must be single quote, see below. … … 285 290 <?php if ( get_background_image() ) : ?> 286 291 <tr> 287 <th scope="row"><?php _e( 'Remove Image'); ?></th>292 <th scope="row"><?php _e( 'Remove Image' ); ?></th> 288 293 <td> 289 294 <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' ); ?> 291 296 <?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.' ); ?> 293 298 </form> 294 299 </td> … … 299 304 <?php if ( $default_image && get_background_image() != $default_image ) : ?> 300 305 <tr> 301 <th scope="row"><?php _e( 'Restore Original Image'); ?></th>306 <th scope="row"><?php _e( 'Restore Original Image' ); ?></th> 302 307 <td> 303 308 <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' ); ?> 305 310 <?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.' ); ?> 307 312 </form> 308 313 </td> … … 310 315 <?php endif; ?> 311 316 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> 315 320 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post"> 316 321 <p> … … 350 355 $background_position_options = array( 351 356 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 ), 355 369 ), 356 370 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 ), 360 383 ), 361 384 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 ), 365 397 ), 366 398 ); … … 417 449 <?php 418 450 $default_color = ''; 419 if ( current_theme_supports( 'custom-background', 'default-color' ) ) 451 if ( current_theme_supports( 'custom-background', 'default-color' ) ) { 420 452 $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"'; 453 } 421 454 ?> 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; ?>> 423 456 </fieldset></td> 424 457 </tr> … … 426 459 </table> 427 460 428 <?php wp_nonce_field( 'custom-background'); ?>461 <?php wp_nonce_field( 'custom-background' ); ?> 429 462 <?php submit_button( null, 'primary', 'save-background-options' ); ?> 430 463 </form> … … 440 473 */ 441 474 public function handle_upload() { 442 if ( empty( $_FILES) )475 if ( empty( $_FILES ) ) { 443 476 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 ); 447 481 448 482 $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'] ) ) { 451 485 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'] ) ) { 456 491 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 ); 462 498 463 499 // Construct the object array 464 500 $object = array( 465 'post_title' => $filename,466 'post_content' => $url,501 'post_title' => $filename, 502 'post_content' => $url, 467 503 'post_mime_type' => $type, 468 'guid' => $url,469 'context' => 'custom-background'504 'guid' => $url, 505 'context' => 'custom-background', 470 506 ); 471 507 472 508 // Save the data 473 $id = wp_insert_attachment( $object, $file);509 $id = wp_insert_attachment( $object, $file ); 474 510 475 511 // Add the meta-data 476 512 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 ) ); 480 516 481 517 $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] ) ); 483 519 484 520 /** This action is documented in wp-admin/custom-header.php */ … … 513 549 514 550 /** 515 *516 551 * @since 3.4.0 517 552 * @deprecated 3.5.0 … … 525 560 526 561 /** 527 *528 562 * @since 3.4.0 529 563 * @deprecated 3.5.0 … … 537 571 538 572 /** 539 *540 573 * @since 3.4.0 541 574 * @deprecated 3.5.0 542 575 */ 543 576 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'] ); 546 581 /** 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 ) ) { 550 594 $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 ); 554 599 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); 555 600 set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
Note: See TracChangeset
for help on using the changeset viewer.