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