Custom_Background

The custom background class.

Defined (1)

The class is defined in the following location(s).

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