Make WordPress Core

Ticket #22058: custom-background.php

File custom-background.php, 17.1 KB (added by grapplerulrich, 11 years ago)

custom-background

Line 
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 */
16class 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('wp-color-picker');
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                                $hposition = $_POST['background-position-x'];
155                        else
156                                $hposition = 'left';
157                        set_theme_mod('background_position_x', $hposition);
158                }
159
160                if ( isset($_POST['background-position-y']) ) {
161                        check_admin_referer('custom-background');
162                        if ( in_array($_POST['background-position-y'], array('center', 'top', 'bottom')) )
163                                $vposition = $_POST['background-position-y'];
164                        else
165                                $vposition = 'top';
166                        set_theme_mod('background_position_y', $vposition);
167                }
168               
169                if ( isset($_POST['background-attachment']) ) {
170                        check_admin_referer('custom-background');
171                        if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
172                                $attachment = $_POST['background-attachment'];
173                        else
174                                $attachment = 'fixed';
175                        set_theme_mod('background_attachment', $attachment);
176                }
177
178                if ( isset($_POST['background-color']) ) {
179                        check_admin_referer('custom-background');
180                        $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
181                        if ( strlen($color) == 6 || strlen($color) == 3 )
182                                set_theme_mod('background_color', $color);
183                        else
184                                set_theme_mod('background_color', '');
185                }
186
187                $this->updated = true;
188        }
189
190        /**
191         * Display the custom background page.
192         *
193         * @since 3.0.0
194         */
195        function admin_page() {
196?>
197<div class="wrap" id="custom-background">
198<?php screen_icon(); ?>
199<h2><?php _e('Custom Background'); ?></h2>
200<?php if ( !empty($this->updated) ) { ?>
201<div id="message" class="updated">
202<p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
203</div>
204<?php }
205
206        if ( $this->admin_image_div_callback ) {
207                call_user_func($this->admin_image_div_callback);
208        } else {
209?>
210<h3><?php _e('Background Image'); ?></h3>
211<table class="form-table">
212<tbody>
213<tr valign="top">
214<th scope="row"><?php _e('Preview'); ?></th>
215<td>
216<?php
217$background_styles = '';
218if ( $bgcolor = get_background_color() )
219        $background_styles .= 'background-color: #' . $bgcolor . ';';
220
221if ( get_background_image() ) {
222        // background-image URL must be single quote, see below
223        $background_styles .= ' background-image: url(\'' . set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ) . '\');'
224                . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
225                . ' background-position: ' . get_theme_mod('background_position_y', 'top') . get_theme_mod('background_position_x', 'left');
226}
227?>
228<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
229<?php if ( get_background_image() ) { ?>
230<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 />
231<img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" />
232<?php } ?>
233</div>
234<?php } ?>
235</td>
236</tr>
237<?php if ( get_background_image() ) : ?>
238<tr valign="top">
239<th scope="row"><?php _e('Remove Image'); ?></th>
240<td>
241<form method="post" action="">
242<?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
243<?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
244<?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
245</form>
246</td>
247</tr>
248<?php endif; ?>
249
250<?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
251<?php if ( $default_image && get_background_image() != $default_image ) : ?>
252<tr valign="top">
253<th scope="row"><?php _e('Restore Original Image'); ?></th>
254<td>
255<form method="post" action="">
256<?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
257<?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
258<?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
259</form>
260</td>
261</tr>
262
263<?php endif; ?>
264<tr valign="top">
265<th scope="row"><?php _e('Select Image'); ?></th>
266<td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
267        <p>
268                <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
269                <input type="file" id="upload" name="import" />
270                <input type="hidden" name="action" value="save" />
271                <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
272                <?php submit_button( __( 'Upload' ), 'small', 'submit', false ); ?>
273        </p>
274        <?php
275                $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
276                $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
277                $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
278        ?>
279        <p>
280                <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
281                <a id="choose-from-library-link" class="button thickbox" href="<?php echo esc_url( $image_library_url ); ?>"><?php _e( 'Choose Image' ); ?></a>
282        </p>
283        </form>
284</td>
285</tr>
286</tbody>
287</table>
288
289<h3><?php _e('Display Options') ?></h3>
290<form method="post" action="">
291<table class="form-table">
292<tbody>
293<?php if ( get_background_image() ) : ?>
294<tr valign="top">
295<th scope="row"><?php _e( 'Horizontal Position' ); ?></th>
296<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
297<label>
298<input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
299<?php _e('Left') ?>
300</label>
301<label>
302<input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
303<?php _e('Center') ?>
304</label>
305<label>
306<input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
307<?php _e('Right') ?>
308</label>
309</fieldset></td>
310</tr>
311
312<tr valign="top">
313<th scope="row"><?php _e( 'Vertical Position' ); ?></th>
314<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
315<label>
316<input name="background-position-y" type="radio" value="top"<?php checked('top', get_theme_mod('background_position_y', 'top')); ?> />
317<?php _e('Top') ?>
318</label>
319<label>
320<input name="background-position-y" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_y', 'top')); ?> />
321<?php _e('Center') ?>
322</label>
323<label>
324<input name="background-position-y" type="radio" value="bottom"<?php checked('bottom', get_theme_mod('background_position_y', 'top')); ?> />
325<?php _e('Bottom') ?>
326</label>
327</fieldset></td>
328</tr>
329
330<tr valign="top">
331<th scope="row"><?php _e( 'Repeat' ); ?></th>
332<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
333<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>
334        <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
335        <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>
336        <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>
337</fieldset></td>
338</tr>
339
340<tr valign="top">
341<th scope="row"><?php _e( 'Attachment' ); ?></th>
342<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
343<label>
344<input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
345<?php _e('Scroll') ?>
346</label>
347<label>
348<input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
349<?php _e('Fixed') ?>
350</label>
351</fieldset></td>
352</tr>
353<?php endif; // get_background_image() ?>
354<tr valign="top">
355<th scope="row"><?php _e( 'Background Color' ); ?></th>
356<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
357<?php
358$default_color = '';
359if ( current_theme_supports( 'custom-background', 'default-color' ) )
360        $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
361?>
362<input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?> />
363</fieldset></td>
364</tr>
365</tbody>
366</table>
367
368<?php wp_nonce_field('custom-background'); ?>
369<?php submit_button( null, 'primary', 'save-background-options' ); ?>
370</form>
371
372</div>
373<?php
374        }
375
376        /**
377         * Handle an Image upload for the background image.
378         *
379         * @since 3.0.0
380         */
381        function handle_upload() {
382
383                if ( empty($_FILES) )
384                        return;
385
386                check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
387                $overrides = array('test_form' => false);
388                $file = wp_handle_upload($_FILES['import'], $overrides);
389
390                if ( isset($file['error']) )
391                        wp_die( $file['error'] );
392
393                $url = $file['url'];
394                $type = $file['type'];
395                $file = $file['file'];
396                $filename = basename($file);
397
398                // Construct the object array
399                $object = array(
400                        'post_title' => $filename,
401                        'post_content' => $url,
402                        'post_mime_type' => $type,
403                        'guid' => $url,
404                        'context' => 'custom-background'
405                );
406
407                // Save the data
408                $id = wp_insert_attachment($object, $file);
409
410                // Add the meta-data
411                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
412                update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
413
414                set_theme_mod('background_image', esc_url_raw($url));
415
416                $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
417                set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
418
419                do_action('wp_create_file_in_uploads', $file, $id); // For replication
420                $this->updated = true;
421        }
422
423        /**
424         * Replace default attachment actions with "Set as background" link.
425         *
426         * @since 3.4.0
427         */
428        function attachment_fields_to_edit( $form_fields, $post ) {
429                $form_fields = array( 'image-size' => $form_fields['image-size'] );
430                $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>' );
431                $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
432
433                return $form_fields;
434        }
435
436        /**
437         * Leave only "Media Library" tab in the uploader window.
438         *
439         * @since 3.4.0
440         */
441        function filter_upload_tabs() {
442                return array( 'library' => __('Media Library') );
443        }
444
445        public function wp_set_background_image() {
446                if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
447                $attachment_id = absint($_POST['attachment_id']);
448                $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
449                $size = 'thumbnail';
450                if ( in_array( $_POST['size'], $sizes ) )
451                        $size = esc_attr( $_POST['size'] );
452
453                update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
454                $url = wp_get_attachment_image_src( $attachment_id, $size );
455                $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
456                set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
457                set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
458                exit;
459        }
460}