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