]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/custom-background.php
WordPress 3.4.1
[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 ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
77                         add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
78                         add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
79                         add_filter( 'media_upload_mime_type_links', '__return_empty_array' );
80                 }
81
82                 if ( $this->admin_header_callback )
83                         add_action("admin_head-$page", $this->admin_header_callback, 51);
84         }
85
86         /**
87          * Set up the enqueue for the CSS & JavaScript files.
88          *
89          * @since 3.0.0
90          */
91         function admin_load() {
92                 get_current_screen()->add_help_tab( array(
93                         'id'      => 'overview',
94                         'title'   => __('Overview'),
95                         'content' =>
96                                 '<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>' .
97                                 '<p>' . __( 'To use a background image, simply upload it, then choose your display options below. 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>' .
98                                 '<p>' . __( 'You can also choose a background color. If you know the hexadecimal code for the color you want, enter it in the Background Color field. If not, click on the Select a Color link, and a color picker will allow you to choose the exact shade you want.' ) . '</p>' .
99                                 '<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>'
100                 ) );
101
102                 get_current_screen()->set_help_sidebar(
103                         '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
104                         '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
105                         '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
106                 );
107
108                 add_thickbox();
109                 wp_enqueue_script('media-upload');
110                 wp_enqueue_script('custom-background');
111                 wp_enqueue_style('farbtastic');
112         }
113
114         /**
115          * Execute custom background modification.
116          *
117          * @since 3.0.0
118          */
119         function take_action() {
120
121                 if ( empty($_POST) )
122                         return;
123
124                 if ( isset($_POST['reset-background']) ) {
125                         check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
126                         remove_theme_mod('background_image');
127                         remove_theme_mod('background_image_thumb');
128                         $this->updated = true;
129                         return;
130                 }
131
132                 if ( isset($_POST['remove-background']) ) {
133                         // @TODO: Uploaded files are not removed here.
134                         check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
135                         set_theme_mod('background_image', '');
136                         set_theme_mod('background_image_thumb', '');
137                         $this->updated = true;
138                         wp_safe_redirect( $_POST['_wp_http_referer'] );
139                         return;
140                 }
141
142                 if ( isset($_POST['background-repeat']) ) {
143                         check_admin_referer('custom-background');
144                         if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
145                                 $repeat = $_POST['background-repeat'];
146                         else
147                                 $repeat = 'repeat';
148                         set_theme_mod('background_repeat', $repeat);
149                 }
150
151                 if ( isset($_POST['background-position-x']) ) {
152                         check_admin_referer('custom-background');
153                         if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
154                                 $position = $_POST['background-position-x'];
155                         else
156                                 $position = 'left';
157                         set_theme_mod('background_position_x', $position);
158                 }
159
160                 if ( isset($_POST['background-attachment']) ) {
161                         check_admin_referer('custom-background');
162                         if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
163                                 $attachment = $_POST['background-attachment'];
164                         else
165                                 $attachment = 'fixed';
166                         set_theme_mod('background_attachment', $attachment);
167                 }
168
169                 if ( isset($_POST['background-color']) ) {
170                         check_admin_referer('custom-background');
171                         $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
172                         if ( strlen($color) == 6 || strlen($color) == 3 )
173                                 set_theme_mod('background_color', $color);
174                         else
175                                 set_theme_mod('background_color', '');
176                 }
177
178                 $this->updated = true;
179         }
180
181         /**
182          * Display the custom background page.
183          *
184          * @since 3.0.0
185          */
186         function admin_page() {
187 ?>
188 <div class="wrap" id="custom-background">
189 <?php screen_icon(); ?>
190 <h2><?php _e('Custom Background'); ?></h2>
191 <?php if ( !empty($this->updated) ) { ?>
192 <div id="message" class="updated">
193 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
194 </div>
195 <?php }
196
197         if ( $this->admin_image_div_callback ) {
198                 call_user_func($this->admin_image_div_callback);
199         } else {
200 ?>
201 <h3><?php _e('Background Image'); ?></h3>
202 <table class="form-table">
203 <tbody>
204 <tr valign="top">
205 <th scope="row"><?php _e('Preview'); ?></th>
206 <td>
207 <?php
208 $background_styles = '';
209 if ( $bgcolor = get_background_color() )
210         $background_styles .= 'background-color: #' . $bgcolor . ';';
211
212 if ( get_background_image() ) {
213         // background-image URL must be single quote, see below
214         $background_styles .= ' background-image: url(\'' . set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ) . '\');'
215                 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
216                 . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
217 }
218 ?>
219 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
220 <?php if ( get_background_image() ) { ?>
221 <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" /><br />
222 <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" />
223 <?php } ?>
224 </div>
225 <?php } ?>
226 </td>
227 </tr>
228 <?php if ( get_background_image() ) : ?>
229 <tr valign="top">
230 <th scope="row"><?php _e('Remove Image'); ?></th>
231 <td>
232 <form method="post" action="">
233 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
234 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
235 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
236 </form>
237 </td>
238 </tr>
239 <?php endif; ?>
240
241 <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
242 <?php if ( $default_image && get_background_image() != $default_image ) : ?>
243 <tr valign="top">
244 <th scope="row"><?php _e('Restore Original Image'); ?></th>
245 <td>
246 <form method="post" action="">
247 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
248 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
249 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
250 </form>
251 </td>
252 </tr>
253
254 <?php endif; ?>
255 <tr valign="top">
256 <th scope="row"><?php _e('Select Image'); ?></th>
257 <td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
258         <p>
259                 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
260                 <input type="file" id="upload" name="import" />
261                 <input type="hidden" name="action" value="save" />
262                 <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
263                 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
264         </p>
265         <?php
266                 $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
267                 $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
268                 $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
269         ?>
270         <p>
271                 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
272                 <a id="choose-from-library-link" class="button thickbox" href="<?php echo esc_url( $image_library_url ); ?>"><?php _e( 'Choose Image' ); ?></a>
273         </p>
274         </form>
275 </td>
276 </tr>
277 </tbody>
278 </table>
279
280 <h3><?php _e('Display Options') ?></h3>
281 <form method="post" action="">
282 <table class="form-table">
283 <tbody>
284 <?php if ( get_background_image() ) : ?>
285 <tr valign="top">
286 <th scope="row"><?php _e( 'Position' ); ?></th>
287 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
288 <label>
289 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
290 <?php _e('Left') ?>
291 </label>
292 <label>
293 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
294 <?php _e('Center') ?>
295 </label>
296 <label>
297 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
298 <?php _e('Right') ?>
299 </label>
300 </fieldset></td>
301 </tr>
302
303 <tr valign="top">
304 <th scope="row"><?php _e( 'Repeat' ); ?></th>
305 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
306 <label><input type="radio" name="background-repeat" value="no-repeat"<?php checked('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('No Repeat'); ?></label>
307         <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
308         <label><input type="radio" name="background-repeat" value="repeat-x"<?php checked('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Horizontally'); ?></label>
309         <label><input type="radio" name="background-repeat" value="repeat-y"<?php checked('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Vertically'); ?></label>
310 </fieldset></td>
311 </tr>
312
313 <tr valign="top">
314 <th scope="row"><?php _e( 'Attachment' ); ?></th>
315 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
316 <label>
317 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
318 <?php _e('Scroll') ?>
319 </label>
320 <label>
321 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
322 <?php _e('Fixed') ?>
323 </label>
324 </fieldset></td>
325 </tr>
326 <?php endif; // get_background_image() ?>
327 <tr valign="top">
328 <th scope="row"><?php _e( 'Background Color' ); ?></th>
329 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
330 <?php $show_clear = get_theme_mod('background_color') ? '' : ' style="display:none"'; ?>
331 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
332 <a class="hide-if-no-js" href="#" id="pickcolor"><?php _e('Select a Color'); ?></a> <span<?php echo $show_clear; ?> class="hide-if-no-js" id="clearcolor"> (<a href="#"><?php current_theme_supports( 'custom-background', 'default-color' ) ? _e( 'Default' ) : _e( 'Clear' ); ?></a>)</span>
333 <input type="hidden" id="defaultcolor" value="<?php if ( current_theme_supports( 'custom-background', 'default-color' ) ) echo '#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ); ?>" />
334 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
335 </fieldset></td>
336 </tr>
337 </tbody>
338 </table>
339
340 <?php wp_nonce_field('custom-background'); ?>
341 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
342 </form>
343
344 </div>
345 <?php
346         }
347
348         /**
349          * Handle an Image upload for the background image.
350          *
351          * @since 3.0.0
352          */
353         function handle_upload() {
354
355                 if ( empty($_FILES) )
356                         return;
357
358                 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
359                 $overrides = array('test_form' => false);
360                 $file = wp_handle_upload($_FILES['import'], $overrides);
361
362                 if ( isset($file['error']) )
363                         wp_die( $file['error'] );
364
365                 $url = $file['url'];
366                 $type = $file['type'];
367                 $file = $file['file'];
368                 $filename = basename($file);
369
370                 // Construct the object array
371                 $object = array(
372                         'post_title' => $filename,
373                         'post_content' => $url,
374                         'post_mime_type' => $type,
375                         'guid' => $url,
376                         'context' => 'custom-background'
377                 );
378
379                 // Save the data
380                 $id = wp_insert_attachment($object, $file);
381
382                 // Add the meta-data
383                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
384                 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
385
386                 set_theme_mod('background_image', esc_url_raw($url));
387
388                 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
389                 set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
390
391                 do_action('wp_create_file_in_uploads', $file, $id); // For replication
392                 $this->updated = true;
393         }
394
395         /**
396          * Replace default attachment actions with "Set as background" link.
397          *
398          * @since 3.4.0
399          */
400         function attachment_fields_to_edit( $form_fields, $post ) {
401                 $form_fields = array( 'image-size' => $form_fields['image-size'] );
402                 $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-attachment-id="' . $post->ID . '" class="wp-set-background">' . __( 'Set as background' ) . '</a></td></tr>' );
403                 $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
404
405                 return $form_fields;
406         }
407
408         /**
409          * Leave only "Media Library" tab in the uploader window.
410          *
411          * @since 3.4.0
412          */
413         function filter_upload_tabs() {
414                 return array( 'library' => __('Media Library') );
415         }
416
417         public function wp_set_background_image() {
418                 if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
419                 $attachment_id = absint($_POST['attachment_id']);
420                 $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
421                 $size = 'thumbnail';
422                 if ( in_array( $_POST['size'], $sizes ) )
423                         $size = esc_attr( $_POST['size'] );
424
425                 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
426                 $url = wp_get_attachment_image_src( $attachment_id, $size );
427                 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
428                 set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
429                 set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
430                 exit;
431         }
432 }