]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/custom-background.php
WordPress 4.7.1-scripts
[autoinstalls/wordpress.git] / wp-admin / custom-background.php
1 <?php
2 /**
3  * The custom background script.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * The custom background class.
11  *
12  * @since 3.0.0
13  * @package WordPress
14  * @subpackage Administration
15  */
16 class Custom_Background {
17
18         /**
19          * Callback for administration header.
20          *
21          * @var callable
22          * @since 3.0.0
23          */
24         public $admin_header_callback;
25
26         /**
27          * Callback for header div.
28          *
29          * @var callable
30          * @since 3.0.0
31          */
32         public $admin_image_div_callback;
33
34         /**
35          * Used to trigger a success message when settings updated and set to true.
36          *
37          * @since 3.0.0
38          * @access private
39          * @var bool
40          */
41         private $updated;
42
43         /**
44          * Constructor - Register administration header callback.
45          *
46          * @since 3.0.0
47          * @param callable $admin_header_callback
48          * @param callable $admin_image_div_callback Optional custom image div output callback.
49          */
50         public function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
51                 $this->admin_header_callback = $admin_header_callback;
52                 $this->admin_image_div_callback = $admin_image_div_callback;
53
54                 add_action( 'admin_menu', array( $this, 'init' ) );
55
56                 add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
57
58                 // Unused since 3.5.0.
59                 add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
60         }
61
62         /**
63          * Set up the hooks for the Custom Background admin page.
64          *
65          * @since 3.0.0
66          */
67         public function init() {
68                 $page = add_theme_page( __( 'Background' ), __( 'Background' ), 'edit_theme_options', 'custom-background', array( $this, 'admin_page' ) );
69                 if ( ! $page ) {
70                         return;
71                 }
72
73                 add_action( "load-$page", array( $this, 'admin_load' ) );
74                 add_action( "load-$page", array( $this, 'take_action' ), 49 );
75                 add_action( "load-$page", array( $this, 'handle_upload' ), 49 );
76
77                 if ( $this->admin_header_callback ) {
78                         add_action( "admin_head-$page", $this->admin_header_callback, 51 );
79                 }
80         }
81
82         /**
83          * Set up the enqueue for the CSS & JavaScript files.
84          *
85          * @since 3.0.0
86          */
87         public function admin_load() {
88                 get_current_screen()->add_help_tab( array(
89                         'id'      => 'overview',
90                         'title'   => __('Overview'),
91                         'content' =>
92                                 '<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>' .
93                                 '<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>' .
94                                 '<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>' .
95                                 '<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>'
96                 ) );
97
98                 get_current_screen()->set_help_sidebar(
99                         '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
100                         '<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' .
101                         '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
102                 );
103
104                 wp_enqueue_media();
105                 wp_enqueue_script('custom-background');
106                 wp_enqueue_style('wp-color-picker');
107         }
108
109         /**
110          * Execute custom background modification.
111          *
112          * @since 3.0.0
113          */
114         public function take_action() {
115                 if ( empty($_POST) )
116                         return;
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');
122                         $this->updated = true;
123                         return;
124                 }
125
126                 if ( isset($_POST['remove-background']) ) {
127                         // @TODO: Uploaded files are not removed here.
128                         check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
129                         set_theme_mod('background_image', '');
130                         set_theme_mod('background_image_thumb', '');
131                         $this->updated = true;
132                         wp_safe_redirect( $_POST['_wp_http_referer'] );
133                         return;
134                 }
135
136                 if ( isset( $_POST['background-preset'] ) ) {
137                         check_admin_referer( 'custom-background' );
138
139                         if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) {
140                                 $preset = $_POST['background-preset'];
141                         } else {
142                                 $preset = 'default';
143                         }
144
145                         set_theme_mod( 'background_preset', $preset );
146                 }
147
148                 if ( isset( $_POST['background-position'] ) ) {
149                         check_admin_referer( 'custom-background' );
150
151                         $position = explode( ' ', $_POST['background-position'] );
152
153                         if ( in_array( $position[0], array( 'left', 'center', 'right' ), true ) ) {
154                                 $position_x = $position[0];
155                         } else {
156                                 $position_x = 'left';
157                         }
158
159                         if ( in_array( $position[1], array( 'top', 'center', 'bottom' ), true ) ) {
160                                 $position_y = $position[1];
161                         } else {
162                                 $position_y = 'top';
163                         }
164
165                         set_theme_mod( 'background_position_x', $position_x );
166                         set_theme_mod( 'background_position_y', $position_y );
167                 }
168
169                 if ( isset( $_POST['background-size'] ) ) {
170                         check_admin_referer( 'custom-background' );
171
172                         if ( in_array( $_POST['background-size'], array( 'auto', 'contain', 'cover' ), true ) ) {
173                                 $size = $_POST['background-size'];
174                         } else {
175                                 $size = 'auto';
176                         }
177
178                         set_theme_mod( 'background_size', $size );
179                 }
180
181                 if ( isset( $_POST['background-repeat'] ) ) {
182                         check_admin_referer( 'custom-background' );
183
184                         $repeat = $_POST['background-repeat'];
185
186                         if ( 'no-repeat' !== $repeat ) {
187                                 $repeat = 'repeat';
188                         }
189
190                         set_theme_mod( 'background_repeat', $repeat );
191                 }
192
193                 if ( isset( $_POST['background-attachment'] ) ) {
194                         check_admin_referer( 'custom-background' );
195
196                         $attachment = $_POST['background-attachment'];
197
198                         if ( 'fixed' !== $attachment ) {
199                                 $attachment = 'scroll';
200                         }
201
202                         set_theme_mod( 'background_attachment', $attachment );
203                 }
204
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                 }
213
214                 $this->updated = true;
215         }
216
217         /**
218          * Display the custom background page.
219          *
220          * @since 3.0.0
221          */
222         public function admin_page() {
223 ?>
224 <div class="wrap" id="custom-background">
225 <h1><?php _e( 'Custom Background' ); ?></h1>
226
227 <?php if ( current_user_can( 'customize' ) ) { ?>
228 <div class="notice notice-info hide-if-no-customize">
229         <p>
230                 <?php
231                 printf(
232                         __( 'You can now manage and live-preview Custom Backgrounds in the <a href="%1$s">Customizer</a>.' ),
233                         admin_url( 'customize.php?autofocus[control]=background_image' )
234                 );
235                 ?>
236         </p>
237 </div>
238 <?php } ?>
239
240 <?php if ( ! empty( $this->updated ) ) { ?>
241 <div id="message" class="updated">
242 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
243 </div>
244 <?php } ?>
245
246 <h3><?php _e( 'Background Image' ); ?></h3>
247
248 <table class="form-table">
249 <tbody>
250 <tr>
251 <th scope="row"><?php _e( 'Preview' ); ?></th>
252 <td>
253         <?php
254         if ( $this->admin_image_div_callback ) {
255                 call_user_func( $this->admin_image_div_callback );
256         } else {
257                 $background_styles = '';
258                 if ( $bgcolor = get_background_color() )
259                         $background_styles .= 'background-color: #' . $bgcolor . ';';
260
261                 $background_image_thumb = get_background_image();
262                 if ( $background_image_thumb ) {
263                         $background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
264                         $background_position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
265                         $background_position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
266                         $background_size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
267                         $background_repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
268                         $background_attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
269
270                         // Background-image URL must be single quote, see below.
271                         $background_styles .= " background-image: url('$background_image_thumb');"
272                                 . " background-size: $background_size;"
273                                 . " background-position: $background_position_x $background_position_y;"
274                                 . " background-repeat: $background_repeat;"
275                                 . " background-attachment: $background_attachment;";
276                 }
277         ?>
278         <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
279                 <?php if ( $background_image_thumb ) { ?>
280                 <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
281                 <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
282                 <?php } ?>
283         </div>
284         <?php } ?>
285 </td>
286 </tr>
287
288 <?php if ( get_background_image() ) : ?>
289 <tr>
290 <th scope="row"><?php _e('Remove Image'); ?></th>
291 <td>
292 <form method="post">
293 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
294 <?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
295 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
296 </form>
297 </td>
298 </tr>
299 <?php endif; ?>
300
301 <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
302 <?php if ( $default_image && get_background_image() != $default_image ) : ?>
303 <tr>
304 <th scope="row"><?php _e('Restore Original Image'); ?></th>
305 <td>
306 <form method="post">
307 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
308 <?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
309 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
310 </form>
311 </td>
312 </tr>
313 <?php endif; ?>
314
315 <?php if ( current_user_can( 'upload_files' ) ): ?>
316 <tr>
317 <th scope="row"><?php _e('Select Image'); ?></th>
318 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
319         <p>
320                 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
321                 <input type="file" id="upload" name="import" />
322                 <input type="hidden" name="action" value="save" />
323                 <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
324                 <?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
325         </p>
326         <p>
327                 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
328                 <button id="choose-from-library-link" class="button"
329                         data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
330                         data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
331         </p>
332         </form>
333 </td>
334 </tr>
335 <?php endif; ?>
336 </tbody>
337 </table>
338
339 <h3><?php _e( 'Display Options' ); ?></h3>
340 <form method="post">
341 <table class="form-table">
342 <tbody>
343 <?php if ( get_background_image() ) : ?>
344 <input name="background-preset" type="hidden" value="custom">
345
346 <?php
347 $background_position = sprintf(
348         '%s %s',
349         get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
350         get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
351 );
352
353 $background_position_options = array(
354         array(
355                 'left top'   => array( 'label' => __( 'Top Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
356                 'center top' => array( 'label' => __( 'Top' ), 'icon' => 'dashicons dashicons-arrow-up-alt' ),
357                 'right top'  => array( 'label' => __( 'Top Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
358         ),
359         array(
360                 'left center'   => array( 'label' => __( 'Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
361                 'center center' => array( 'label' => __( 'Center' ), 'icon' => 'background-position-center-icon' ),
362                 'right center'  => array( 'label' => __( 'Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
363         ),
364         array(
365                 'left bottom'   => array( 'label' => __( 'Bottom Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
366                 'center bottom' => array( 'label' => __( 'Bottom' ), 'icon' => 'dashicons dashicons-arrow-down-alt' ),
367                 'right bottom'  => array( 'label' => __( 'Bottom Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
368         ),
369 );
370 ?>
371 <tr>
372 <th scope="row"><?php _e( 'Image Position' ); ?></th>
373 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
374 <div class="background-position-control">
375 <?php foreach ( $background_position_options as $group ) : ?>
376         <div class="button-group">
377         <?php foreach ( $group as $value => $input ) : ?>
378                 <label>
379                         <input class="screen-reader-text" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
380                         <span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
381                         <span class="screen-reader-text"><?php echo $input['label']; ?></span>
382                 </label>
383         <?php endforeach; ?>
384         </div>
385 <?php endforeach; ?>
386 </div>
387 </fieldset></td>
388 </tr>
389
390 <tr>
391 <th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th>
392 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Size' ); ?></span></legend>
393 <select id="background-size" name="background-size">
394 <option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option>
395 <option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option>
396 <option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option>
397 </select>
398 </fieldset></td>
399 </tr>
400
401 <tr>
402 <th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th>
403 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Repeat', 'Background Repeat' ); ?></span></legend>
404 <input name="background-repeat" type="hidden" value="no-repeat">
405 <label><input type="checkbox" name="background-repeat" value="repeat"<?php checked( 'repeat', get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) ) ); ?>> <?php _e( 'Repeat Background Image' ); ?></label>
406 </fieldset></td>
407 </tr>
408
409 <tr>
410 <th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th>
411 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Scroll', 'Background Scroll' ); ?></span></legend>
412 <input name="background-attachment" type="hidden" value="fixed">
413 <label><input name="background-attachment" type="checkbox" value="scroll" <?php checked( 'scroll', get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) ) ); ?>> <?php _e( 'Scroll with Page' ); ?></label>
414 </fieldset></td>
415 </tr>
416 <?php endif; // get_background_image() ?>
417 <tr>
418 <th scope="row"><?php _e( 'Background Color' ); ?></th>
419 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
420 <?php
421 $default_color = '';
422 if ( current_theme_supports( 'custom-background', 'default-color' ) )
423         $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
424 ?>
425 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?>>
426 </fieldset></td>
427 </tr>
428 </tbody>
429 </table>
430
431 <?php wp_nonce_field('custom-background'); ?>
432 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
433 </form>
434
435 </div>
436 <?php
437         }
438
439         /**
440          * Handle an Image upload for the background image.
441          *
442          * @since 3.0.0
443          */
444         public function handle_upload() {
445                 if ( empty($_FILES) )
446                         return;
447
448                 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
449                 $overrides = array('test_form' => false);
450
451                 $uploaded_file = $_FILES['import'];
452                 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
453                 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
454                         wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
455
456                 $file = wp_handle_upload($uploaded_file, $overrides);
457
458                 if ( isset($file['error']) )
459                         wp_die( $file['error'] );
460
461                 $url = $file['url'];
462                 $type = $file['type'];
463                 $file = $file['file'];
464                 $filename = basename($file);
465
466                 // Construct the object array
467                 $object = array(
468                         'post_title' => $filename,
469                         'post_content' => $url,
470                         'post_mime_type' => $type,
471                         'guid' => $url,
472                         'context' => 'custom-background'
473                 );
474
475                 // Save the data
476                 $id = wp_insert_attachment($object, $file);
477
478                 // Add the meta-data
479                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
480                 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
481
482                 set_theme_mod('background_image', esc_url_raw($url));
483
484                 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
485                 set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
486
487                 /** This action is documented in wp-admin/custom-header.php */
488                 do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
489                 $this->updated = true;
490         }
491
492         /**
493          * Ajax handler for adding custom background context to an attachment.
494          *
495          * Triggered when the user adds a new background image from the
496          * Media Manager.
497          *
498          * @since 4.1.0
499          */
500         public function ajax_background_add() {
501                 check_ajax_referer( 'background-add', 'nonce' );
502
503                 if ( ! current_user_can( 'edit_theme_options' ) ) {
504                         wp_send_json_error();
505                 }
506
507                 $attachment_id = absint( $_POST['attachment_id'] );
508                 if ( $attachment_id < 1 ) {
509                         wp_send_json_error();
510                 }
511
512                 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
513
514                 wp_send_json_success();
515         }
516
517         /**
518          *
519          * @since 3.4.0
520          * @deprecated 3.5.0
521          *
522          * @param array $form_fields
523          * @return array $form_fields
524          */
525         public function attachment_fields_to_edit( $form_fields ) {
526                 return $form_fields;
527         }
528
529         /**
530          *
531          * @since 3.4.0
532          * @deprecated 3.5.0
533          *
534          * @param array $tabs
535          * @return array $tabs
536          */
537         public function filter_upload_tabs( $tabs ) {
538                 return $tabs;
539         }
540
541         /**
542          *
543          * @since 3.4.0
544          * @deprecated 3.5.0
545          */
546         public function wp_set_background_image() {
547                 if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
548                 $attachment_id = absint($_POST['attachment_id']);
549                 /** This filter is documented in wp-admin/includes/media.php */
550                 $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
551                 $size = 'thumbnail';
552                 if ( in_array( $_POST['size'], $sizes ) )
553                         $size = esc_attr( $_POST['size'] );
554
555                 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
556                 $url = wp_get_attachment_image_src( $attachment_id, $size );
557                 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
558                 set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
559                 set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
560                 exit;
561         }
562 }