RS.js 77628cf8bf first comm il y a 3 ans RU.js 77628cf8bf first comm il y a 3 ans RW.js 77628cf8bf first comm il y a 3 ans SA.js 77628cf8bf first comm il y a 3 ans SB.js 77628cf8bf first comm il y a 3 ans SC.js 77628cf8bf first comm il y a 3 ans SD.js 77628cf8bf first comm il y a 3 ans SE.js 77628cf8bf first comm il y a 3 ans SG.js 77628cf8bf first comm il y a 3 ans SH.js 77628cf8bf first comm il y a 3 ans SI.js 77628cf8bf first comm il y a 3 ans SK.js 77628cf8bf first comm il y a 3 ans SL.js 77628cf8bf first comm il y a 3 ans SM.js 77628cf8bf first comm il y a 3 ans SN.js 77628cf8bf first comm il y a 3 ans SO.js 77628cf8bf first comm il y a 3 ans SR.js 77628cf8bf first comm il y a 3 ans ST.js 77628cf8bf first comm il y a 3 ans SV.js 77628cf8bf first comm il y a 3 ans SY.js 77628cf8bf first comm il y a 3 ans SZ.js 77628cf8bf first comm il y a 3 ans TC.js 77628cf8bf first comm il y a 3 ans TD.js 77628cf8bf first comm il y a 3 ans TG.js 77628cf8bf first comm il y a 3 ans TH.js 77628cf8bf first comm il y a 3 ans TJ.js 77628cf8bf first comm il y a 3 ans TK.js 77628cf8bf first comm il y a 3 ans TL.js 77628cf8bf first comm il y a 3 ans TM.js 77628cf8bf first comm il y a 3 ans TN.js 77628cf8bf first comm il y a 3 ans TO.js 77628cf8bf first comm il y a 3 ans TR.js 77628cf8bf first comm il y a 3 ans TT.js 77628cf8bf first comm il y a 3 ans TV.js 77628cf8bf first comm il y a 3 ans TW.js 77628cf8bf first comm il y a 3 ans TZ.js 77628cf8bf first comm il y a 3 ans UA.js 77628cf8bf first comm il y a 3 ans UG.js 77628cf8bf first comm il y a 3 ans US.js 77628cf8bf first comm il y a 3 ans UY.js 77628cf8bf first comm il y a 3 ans UZ.js 77628cf8bf first comm il y a 3 ans VA.js 77628cf8bf first comm il y a 3 ans VC.js 77628cf8bf first comm il y a 3 ans VE.js 77628cf8bf first comm il y a 3 ans VG.js 77628cf8bf first comm il y a 3 ans VI.js 77628cf8bf first comm il y a 3 ans VN.js 77628cf8bf first comm il y a 3 ans VU.js 77628cf8bf first comm il y a 3 ans WF.js 77628cf8bf first comm il y a 3 ans WS.js 77628cf8bf first comm il y a 3 ans YE.js 77628cf8bf first comm il y a 3 ans YT.js 77628cf8bf first comm il y a 3 ans ZA.js 77628cf8bf first comm il y a 3 ans ZM.js 77628cf8bf first comm il y a 3 ans ZW.js 77628cf8bf first comm il y a 3 ans alt-af.js 77628cf8bf first comm il y a 3 ans alt-an.js 77628cf8bf first comm il y a 3 ans alt-as.js 77628cf8bf first comm il y a 3 ans alt-eu.js 77628cf8bf first comm il y a 3 ans alt-na.js 77628cf8bf first comm il y a 3 ans alt-oc.js 77628cf8bf first comm il y a 3 ans alt-sa.js 77628cf8bf first comm il y a 3 ans alt-ww.js 77628cf8bf first comm il y a 3 ans tum/whitesports - Gogs: Simplico Git Service

No Description

class-custom-background.php 20KB

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