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