author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
16 | 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 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
14 |
#[AllowDynamicProperties] |
16 | 15 |
class Custom_Background { |
16 |
||
17 |
/** |
|
18 |
* Callback for administration header. |
|
19 |
* |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
20 |
* @since 3.0.0 |
16 | 21 |
* @var callable |
22 |
*/ |
|
23 |
public $admin_header_callback; |
|
24 |
||
25 |
/** |
|
26 |
* Callback for header div. |
|
27 |
* |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
28 |
* @since 3.0.0 |
16 | 29 |
* @var callable |
30 |
*/ |
|
31 |
public $admin_image_div_callback; |
|
32 |
||
33 |
/** |
|
34 |
* Used to trigger a success message when settings updated and set to true. |
|
35 |
* |
|
36 |
* @since 3.0.0 |
|
37 |
* @var bool |
|
38 |
*/ |
|
39 |
private $updated; |
|
40 |
||
41 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
* Constructor - Registers administration header callback. |
16 | 43 |
* |
44 |
* @since 3.0.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
46 |
* @param callable $admin_header_callback Optional. Administration header callback. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
47 |
* Default empty string. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
48 |
* @param callable $admin_image_div_callback Optional. Custom image div output callback. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
* Default empty string. |
16 | 50 |
*/ |
51 |
public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) { |
|
52 |
$this->admin_header_callback = $admin_header_callback; |
|
53 |
$this->admin_image_div_callback = $admin_image_div_callback; |
|
54 |
||
55 |
add_action( 'admin_menu', array( $this, 'init' ) ); |
|
56 |
||
57 |
add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) ); |
|
58 |
||
59 |
// Unused since 3.5.0. |
|
60 |
add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) ); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
64 |
* Sets up the hooks for the Custom Background admin page. |
16 | 65 |
* |
66 |
* @since 3.0.0 |
|
67 |
*/ |
|
68 |
public function init() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
$page = add_theme_page( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
_x( 'Background', 'custom background' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
_x( 'Background', 'custom background' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
'edit_theme_options', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
'custom-background', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
array( $this, 'admin_page' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
|
16 | 77 |
if ( ! $page ) { |
78 |
return; |
|
79 |
} |
|
80 |
||
81 |
add_action( "load-{$page}", array( $this, 'admin_load' ) ); |
|
82 |
add_action( "load-{$page}", array( $this, 'take_action' ), 49 ); |
|
83 |
add_action( "load-{$page}", array( $this, 'handle_upload' ), 49 ); |
|
84 |
||
85 |
if ( $this->admin_header_callback ) { |
|
86 |
add_action( "admin_head-{$page}", $this->admin_header_callback, 51 ); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
* Sets up the enqueue for the CSS & JavaScript files. |
16 | 92 |
* |
93 |
* @since 3.0.0 |
|
94 |
*/ |
|
95 |
public function admin_load() { |
|
96 |
get_current_screen()->add_help_tab( |
|
97 |
array( |
|
98 |
'id' => 'overview', |
|
99 |
'title' => __( 'Overview' ), |
|
100 |
'content' => |
|
101 |
'<p>' . __( 'You can customize the look of your site without touching any of your theme’s code by using a custom background. Your background can be an image or a color.' ) . '</p>' . |
|
102 |
'<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 “Choose Image” 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>' . |
|
103 |
'<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. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' . |
|
19 | 104 |
'<p>' . __( 'Do not forget to click on the Save Changes button when you are finished.' ) . '</p>', |
16 | 105 |
) |
106 |
); |
|
107 |
||
108 |
get_current_screen()->set_help_sidebar( |
|
109 |
'<p><strong>' . __( 'For more information:' ) . '</strong></p>' . |
|
110 |
'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' . |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
'<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>' |
16 | 112 |
); |
113 |
||
114 |
wp_enqueue_media(); |
|
115 |
wp_enqueue_script( 'custom-background' ); |
|
116 |
wp_enqueue_style( 'wp-color-picker' ); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
* Executes custom background modification. |
16 | 121 |
* |
122 |
* @since 3.0.0 |
|
123 |
*/ |
|
124 |
public function take_action() { |
|
125 |
if ( empty( $_POST ) ) { |
|
126 |
return; |
|
127 |
} |
|
128 |
||
129 |
if ( isset( $_POST['reset-background'] ) ) { |
|
130 |
check_admin_referer( 'custom-background-reset', '_wpnonce-custom-background-reset' ); |
|
18 | 131 |
|
16 | 132 |
remove_theme_mod( 'background_image' ); |
133 |
remove_theme_mod( 'background_image_thumb' ); |
|
18 | 134 |
|
16 | 135 |
$this->updated = true; |
136 |
return; |
|
137 |
} |
|
138 |
||
139 |
if ( isset( $_POST['remove-background'] ) ) { |
|
140 |
// @todo Uploaded files are not removed here. |
|
141 |
check_admin_referer( 'custom-background-remove', '_wpnonce-custom-background-remove' ); |
|
18 | 142 |
|
16 | 143 |
set_theme_mod( 'background_image', '' ); |
144 |
set_theme_mod( 'background_image_thumb', '' ); |
|
18 | 145 |
|
16 | 146 |
$this->updated = true; |
147 |
wp_safe_redirect( $_POST['_wp_http_referer'] ); |
|
148 |
return; |
|
149 |
} |
|
150 |
||
151 |
if ( isset( $_POST['background-preset'] ) ) { |
|
152 |
check_admin_referer( 'custom-background' ); |
|
153 |
||
154 |
if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) { |
|
155 |
$preset = $_POST['background-preset']; |
|
156 |
} else { |
|
157 |
$preset = 'default'; |
|
158 |
} |
|
159 |
||
160 |
set_theme_mod( 'background_preset', $preset ); |
|
161 |
} |
|
162 |
||
163 |
if ( isset( $_POST['background-position'] ) ) { |
|
164 |
check_admin_referer( 'custom-background' ); |
|
165 |
||
166 |
$position = explode( ' ', $_POST['background-position'] ); |
|
167 |
||
168 |
if ( in_array( $position[0], array( 'left', 'center', 'right' ), true ) ) { |
|
169 |
$position_x = $position[0]; |
|
170 |
} else { |
|
171 |
$position_x = 'left'; |
|
172 |
} |
|
173 |
||
174 |
if ( in_array( $position[1], array( 'top', 'center', 'bottom' ), true ) ) { |
|
175 |
$position_y = $position[1]; |
|
176 |
} else { |
|
177 |
$position_y = 'top'; |
|
178 |
} |
|
179 |
||
180 |
set_theme_mod( 'background_position_x', $position_x ); |
|
181 |
set_theme_mod( 'background_position_y', $position_y ); |
|
182 |
} |
|
183 |
||
184 |
if ( isset( $_POST['background-size'] ) ) { |
|
185 |
check_admin_referer( 'custom-background' ); |
|
186 |
||
187 |
if ( in_array( $_POST['background-size'], array( 'auto', 'contain', 'cover' ), true ) ) { |
|
188 |
$size = $_POST['background-size']; |
|
189 |
} else { |
|
190 |
$size = 'auto'; |
|
191 |
} |
|
192 |
||
193 |
set_theme_mod( 'background_size', $size ); |
|
194 |
} |
|
195 |
||
196 |
if ( isset( $_POST['background-repeat'] ) ) { |
|
197 |
check_admin_referer( 'custom-background' ); |
|
198 |
||
199 |
$repeat = $_POST['background-repeat']; |
|
200 |
||
201 |
if ( 'no-repeat' !== $repeat ) { |
|
202 |
$repeat = 'repeat'; |
|
203 |
} |
|
204 |
||
205 |
set_theme_mod( 'background_repeat', $repeat ); |
|
206 |
} |
|
207 |
||
208 |
if ( isset( $_POST['background-attachment'] ) ) { |
|
209 |
check_admin_referer( 'custom-background' ); |
|
210 |
||
211 |
$attachment = $_POST['background-attachment']; |
|
212 |
||
213 |
if ( 'fixed' !== $attachment ) { |
|
214 |
$attachment = 'scroll'; |
|
215 |
} |
|
216 |
||
217 |
set_theme_mod( 'background_attachment', $attachment ); |
|
218 |
} |
|
219 |
||
220 |
if ( isset( $_POST['background-color'] ) ) { |
|
221 |
check_admin_referer( 'custom-background' ); |
|
18 | 222 |
|
16 | 223 |
$color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] ); |
18 | 224 |
|
225 |
if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) { |
|
16 | 226 |
set_theme_mod( 'background_color', $color ); |
227 |
} else { |
|
228 |
set_theme_mod( 'background_color', '' ); |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
$this->updated = true; |
|
233 |
} |
|
234 |
||
235 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
236 |
* Displays the custom background page. |
16 | 237 |
* |
238 |
* @since 3.0.0 |
|
239 |
*/ |
|
240 |
public function admin_page() { |
|
241 |
?> |
|
242 |
<div class="wrap" id="custom-background"> |
|
243 |
<h1><?php _e( 'Custom Background' ); ?></h1> |
|
244 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
245 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
246 |
if ( current_user_can( 'customize' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
247 |
$message = sprintf( |
16 | 248 |
/* translators: %s: URL to background image configuration in Customizer. */ |
249 |
__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%s">Customizer</a>.' ), |
|
250 |
admin_url( 'customize.php?autofocus[control]=background_image' ) |
|
251 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
252 |
wp_admin_notice( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
253 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
254 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
255 |
'type' => 'info', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
256 |
'additional_classes' => array( 'hide-if-no-customize' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
257 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
258 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
259 |
} |
16 | 260 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
261 |
if ( ! empty( $this->updated ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
262 |
$updated_message = sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
263 |
/* translators: %s: Home URL. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
264 |
__( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
265 |
esc_url( home_url( '/' ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
266 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
267 |
wp_admin_notice( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
268 |
$updated_message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
269 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
270 |
'id' => 'message', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
271 |
'additional_classes' => array( 'updated' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
272 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
273 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
274 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
275 |
?> |
16 | 276 |
|
277 |
<h2><?php _e( 'Background Image' ); ?></h2> |
|
278 |
||
279 |
<table class="form-table" role="presentation"> |
|
280 |
<tbody> |
|
281 |
<tr> |
|
282 |
<th scope="row"><?php _e( 'Preview' ); ?></th> |
|
283 |
<td> |
|
284 |
<?php |
|
285 |
if ( $this->admin_image_div_callback ) { |
|
286 |
call_user_func( $this->admin_image_div_callback ); |
|
287 |
} else { |
|
288 |
$background_styles = ''; |
|
289 |
$bgcolor = get_background_color(); |
|
290 |
if ( $bgcolor ) { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
291 |
$background_styles .= 'background-color: ' . maybe_hash_hex_color( $bgcolor ) . ';'; |
16 | 292 |
} |
293 |
||
294 |
$background_image_thumb = get_background_image(); |
|
295 |
if ( $background_image_thumb ) { |
|
296 |
$background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) ); |
|
297 |
$background_position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ); |
|
298 |
$background_position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) ); |
|
299 |
$background_size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ); |
|
300 |
$background_repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) ); |
|
301 |
$background_attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) ); |
|
302 |
||
303 |
// Background-image URL must be single quote, see below. |
|
304 |
$background_styles .= " background-image: url('$background_image_thumb');" |
|
305 |
. " background-size: $background_size;" |
|
306 |
. " background-position: $background_position_x $background_position_y;" |
|
307 |
. " background-repeat: $background_repeat;" |
|
308 |
. " background-attachment: $background_attachment;"; |
|
309 |
} |
|
310 |
?> |
|
311 |
<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // Must be double quote, see above. ?> |
|
312 |
<?php if ( $background_image_thumb ) { ?> |
|
313 |
<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br /> |
|
314 |
<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /> |
|
315 |
<?php } ?> |
|
316 |
</div> |
|
317 |
<?php } ?> |
|
318 |
</td> |
|
319 |
</tr> |
|
320 |
||
321 |
<?php if ( get_background_image() ) : ?> |
|
322 |
<tr> |
|
323 |
<th scope="row"><?php _e( 'Remove Image' ); ?></th> |
|
324 |
<td> |
|
325 |
<form method="post"> |
|
326 |
<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
327 |
<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br /> |
16 | 328 |
<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?> |
329 |
</form> |
|
330 |
</td> |
|
331 |
</tr> |
|
332 |
<?php endif; ?> |
|
333 |
||
334 |
<?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?> |
|
18 | 335 |
<?php if ( $default_image && get_background_image() !== $default_image ) : ?> |
16 | 336 |
<tr> |
337 |
<th scope="row"><?php _e( 'Restore Original Image' ); ?></th> |
|
338 |
<td> |
|
339 |
<form method="post"> |
|
340 |
<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
341 |
<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br /> |
16 | 342 |
<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?> |
343 |
</form> |
|
344 |
</td> |
|
345 |
</tr> |
|
346 |
<?php endif; ?> |
|
347 |
||
348 |
<?php if ( current_user_can( 'upload_files' ) ) : ?> |
|
349 |
<tr> |
|
350 |
<th scope="row"><?php _e( 'Select Image' ); ?></th> |
|
351 |
<td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post"> |
|
352 |
<p> |
|
353 |
<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br /> |
|
354 |
<input type="file" id="upload" name="import" /> |
|
355 |
<input type="hidden" name="action" value="save" /> |
|
356 |
<?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?> |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
357 |
<?php submit_button( _x( 'Upload', 'verb' ), '', 'submit', false ); ?> |
16 | 358 |
</p> |
359 |
<p> |
|
360 |
<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br /> |
|
361 |
<button id="choose-from-library-link" class="button" |
|
362 |
data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>" |
|
363 |
data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button> |
|
364 |
</p> |
|
365 |
</form> |
|
366 |
</td> |
|
367 |
</tr> |
|
368 |
<?php endif; ?> |
|
369 |
</tbody> |
|
370 |
</table> |
|
371 |
||
372 |
<h2><?php _e( 'Display Options' ); ?></h2> |
|
373 |
<form method="post"> |
|
374 |
<table class="form-table" role="presentation"> |
|
375 |
<tbody> |
|
376 |
<?php if ( get_background_image() ) : ?> |
|
377 |
<input name="background-preset" type="hidden" value="custom"> |
|
378 |
||
379 |
<?php |
|
380 |
$background_position = sprintf( |
|
381 |
'%s %s', |
|
382 |
get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ), |
|
383 |
get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) ) |
|
384 |
); |
|
385 |
||
386 |
$background_position_options = array( |
|
387 |
array( |
|
388 |
'left top' => array( |
|
389 |
'label' => __( 'Top Left' ), |
|
390 |
'icon' => 'dashicons dashicons-arrow-left-alt', |
|
391 |
), |
|
392 |
'center top' => array( |
|
393 |
'label' => __( 'Top' ), |
|
394 |
'icon' => 'dashicons dashicons-arrow-up-alt', |
|
395 |
), |
|
396 |
'right top' => array( |
|
397 |
'label' => __( 'Top Right' ), |
|
398 |
'icon' => 'dashicons dashicons-arrow-right-alt', |
|
399 |
), |
|
400 |
), |
|
401 |
array( |
|
402 |
'left center' => array( |
|
403 |
'label' => __( 'Left' ), |
|
404 |
'icon' => 'dashicons dashicons-arrow-left-alt', |
|
405 |
), |
|
406 |
'center center' => array( |
|
407 |
'label' => __( 'Center' ), |
|
408 |
'icon' => 'background-position-center-icon', |
|
409 |
), |
|
410 |
'right center' => array( |
|
411 |
'label' => __( 'Right' ), |
|
412 |
'icon' => 'dashicons dashicons-arrow-right-alt', |
|
413 |
), |
|
414 |
), |
|
415 |
array( |
|
416 |
'left bottom' => array( |
|
417 |
'label' => __( 'Bottom Left' ), |
|
418 |
'icon' => 'dashicons dashicons-arrow-left-alt', |
|
419 |
), |
|
420 |
'center bottom' => array( |
|
421 |
'label' => __( 'Bottom' ), |
|
422 |
'icon' => 'dashicons dashicons-arrow-down-alt', |
|
423 |
), |
|
424 |
'right bottom' => array( |
|
425 |
'label' => __( 'Bottom Right' ), |
|
426 |
'icon' => 'dashicons dashicons-arrow-right-alt', |
|
427 |
), |
|
428 |
), |
|
429 |
); |
|
430 |
?> |
|
431 |
<tr> |
|
432 |
<th scope="row"><?php _e( 'Image Position' ); ?></th> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
433 |
<td><fieldset><legend class="screen-reader-text"><span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
434 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
435 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
436 |
_e( 'Image Position' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
437 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
438 |
</span></legend> |
16 | 439 |
<div class="background-position-control"> |
440 |
<?php foreach ( $background_position_options as $group ) : ?> |
|
441 |
<div class="button-group"> |
|
442 |
<?php foreach ( $group as $value => $input ) : ?> |
|
443 |
<label> |
|
18 | 444 |
<input class="ui-helper-hidden-accessible" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>> |
16 | 445 |
<span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span> |
446 |
<span class="screen-reader-text"><?php echo $input['label']; ?></span> |
|
447 |
</label> |
|
448 |
<?php endforeach; ?> |
|
449 |
</div> |
|
450 |
<?php endforeach; ?> |
|
451 |
</div> |
|
452 |
</fieldset></td> |
|
453 |
</tr> |
|
454 |
||
455 |
<tr> |
|
456 |
<th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
457 |
<td><fieldset><legend class="screen-reader-text"><span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
458 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
459 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
460 |
_e( 'Image Size' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
461 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
462 |
</span></legend> |
16 | 463 |
<select id="background-size" name="background-size"> |
464 |
<option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option> |
|
465 |
<option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option> |
|
466 |
<option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option> |
|
467 |
</select> |
|
468 |
</fieldset></td> |
|
469 |
</tr> |
|
470 |
||
471 |
<tr> |
|
472 |
<th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
473 |
<td><fieldset><legend class="screen-reader-text"><span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
474 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
475 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
476 |
_ex( 'Repeat', 'Background Repeat' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
477 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
478 |
</span></legend> |
16 | 479 |
<input name="background-repeat" type="hidden" value="no-repeat"> |
480 |
<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> |
|
481 |
</fieldset></td> |
|
482 |
</tr> |
|
483 |
||
484 |
<tr> |
|
485 |
<th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
486 |
<td><fieldset><legend class="screen-reader-text"><span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
487 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
488 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
489 |
_ex( 'Scroll', 'Background Scroll' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
490 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
491 |
</span></legend> |
16 | 492 |
<input name="background-attachment" type="hidden" value="fixed"> |
493 |
<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> |
|
494 |
</fieldset></td> |
|
495 |
</tr> |
|
496 |
<?php endif; // get_background_image() ?> |
|
497 |
<tr> |
|
498 |
<th scope="row"><?php _e( 'Background Color' ); ?></th> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
499 |
<td><fieldset><legend class="screen-reader-text"><span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
500 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
501 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
502 |
_e( 'Background Color' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
503 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
504 |
</span></legend> |
16 | 505 |
<?php |
506 |
$default_color = ''; |
|
507 |
if ( current_theme_supports( 'custom-background', 'default-color' ) ) { |
|
508 |
$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"'; |
|
509 |
} |
|
510 |
?> |
|
511 |
<input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>> |
|
512 |
</fieldset></td> |
|
513 |
</tr> |
|
514 |
</tbody> |
|
515 |
</table> |
|
516 |
||
517 |
<?php wp_nonce_field( 'custom-background' ); ?> |
|
518 |
<?php submit_button( null, 'primary', 'save-background-options' ); ?> |
|
519 |
</form> |
|
520 |
||
521 |
</div> |
|
522 |
<?php |
|
523 |
} |
|
524 |
||
525 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
* Handles an Image upload for the background image. |
16 | 527 |
* |
528 |
* @since 3.0.0 |
|
529 |
*/ |
|
530 |
public function handle_upload() { |
|
531 |
if ( empty( $_FILES ) ) { |
|
532 |
return; |
|
533 |
} |
|
534 |
||
535 |
check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' ); |
|
18 | 536 |
|
16 | 537 |
$overrides = array( 'test_form' => false ); |
538 |
||
539 |
$uploaded_file = $_FILES['import']; |
|
540 |
$wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] ); |
|
541 |
if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { |
|
542 |
wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); |
|
543 |
} |
|
544 |
||
545 |
$file = wp_handle_upload( $uploaded_file, $overrides ); |
|
546 |
||
547 |
if ( isset( $file['error'] ) ) { |
|
548 |
wp_die( $file['error'] ); |
|
549 |
} |
|
550 |
||
551 |
$url = $file['url']; |
|
552 |
$type = $file['type']; |
|
553 |
$file = $file['file']; |
|
554 |
$filename = wp_basename( $file ); |
|
555 |
||
19 | 556 |
// Construct the attachment array. |
557 |
$attachment = array( |
|
16 | 558 |
'post_title' => $filename, |
559 |
'post_content' => $url, |
|
560 |
'post_mime_type' => $type, |
|
561 |
'guid' => $url, |
|
562 |
'context' => 'custom-background', |
|
563 |
); |
|
564 |
||
565 |
// Save the data. |
|
19 | 566 |
$id = wp_insert_attachment( $attachment, $file ); |
16 | 567 |
|
568 |
// Add the metadata. |
|
569 |
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
|
570 |
update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) ); |
|
571 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
572 |
set_theme_mod( 'background_image', sanitize_url( $url ) ); |
16 | 573 |
|
574 |
$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
575 |
set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) ); |
16 | 576 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
/** This filter is documented in wp-admin/includes/class-custom-image-header.php */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
578 |
$file = apply_filters( 'wp_create_file_in_uploads', $file, $id ); // For replication. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
579 |
|
16 | 580 |
$this->updated = true; |
581 |
} |
|
582 |
||
583 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
584 |
* Handles Ajax request for adding custom background context to an attachment. |
16 | 585 |
* |
586 |
* Triggers when the user adds a new background image from the |
|
587 |
* Media Manager. |
|
588 |
* |
|
589 |
* @since 4.1.0 |
|
590 |
*/ |
|
591 |
public function ajax_background_add() { |
|
592 |
check_ajax_referer( 'background-add', 'nonce' ); |
|
593 |
||
594 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
595 |
wp_send_json_error(); |
|
596 |
} |
|
597 |
||
598 |
$attachment_id = absint( $_POST['attachment_id'] ); |
|
599 |
if ( $attachment_id < 1 ) { |
|
600 |
wp_send_json_error(); |
|
601 |
} |
|
602 |
||
603 |
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() ); |
|
604 |
||
605 |
wp_send_json_success(); |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* @since 3.4.0 |
|
610 |
* @deprecated 3.5.0 |
|
611 |
* |
|
612 |
* @param array $form_fields |
|
613 |
* @return array $form_fields |
|
614 |
*/ |
|
615 |
public function attachment_fields_to_edit( $form_fields ) { |
|
616 |
return $form_fields; |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* @since 3.4.0 |
|
621 |
* @deprecated 3.5.0 |
|
622 |
* |
|
623 |
* @param array $tabs |
|
624 |
* @return array $tabs |
|
625 |
*/ |
|
626 |
public function filter_upload_tabs( $tabs ) { |
|
627 |
return $tabs; |
|
628 |
} |
|
629 |
||
630 |
/** |
|
631 |
* @since 3.4.0 |
|
632 |
* @deprecated 3.5.0 |
|
633 |
*/ |
|
634 |
public function wp_set_background_image() { |
|
18 | 635 |
check_ajax_referer( 'custom-background' ); |
636 |
||
16 | 637 |
if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) { |
638 |
exit; |
|
639 |
} |
|
640 |
||
641 |
$attachment_id = absint( $_POST['attachment_id'] ); |
|
642 |
||
643 |
$sizes = array_keys( |
|
644 |
/** This filter is documented in wp-admin/includes/media.php */ |
|
645 |
apply_filters( |
|
646 |
'image_size_names_choose', |
|
647 |
array( |
|
648 |
'thumbnail' => __( 'Thumbnail' ), |
|
649 |
'medium' => __( 'Medium' ), |
|
650 |
'large' => __( 'Large' ), |
|
651 |
'full' => __( 'Full Size' ), |
|
652 |
) |
|
653 |
) |
|
654 |
); |
|
655 |
||
656 |
$size = 'thumbnail'; |
|
657 |
if ( in_array( $_POST['size'], $sizes, true ) ) { |
|
658 |
$size = esc_attr( $_POST['size'] ); |
|
659 |
} |
|
660 |
||
661 |
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) ); |
|
662 |
||
663 |
$url = wp_get_attachment_image_src( $attachment_id, $size ); |
|
664 |
$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
665 |
set_theme_mod( 'background_image', sanitize_url( $url[0] ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
666 |
set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) ); |
16 | 667 |
exit; |
668 |
} |
|
669 |
} |