PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/custom-header.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 1381 lines | 894 code | 181 blank | 306 comment | 171 complexity | 883cc839bfb38083f72d41214bed2473 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * The custom header image script.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * The custom header image class.
  10. *
  11. * @since 2.1.0
  12. * @package WordPress
  13. * @subpackage Administration
  14. */
  15. class Custom_Image_Header {
  16. /**
  17. * Callback for administration header.
  18. *
  19. * @var callback
  20. * @since 2.1.0
  21. * @access private
  22. */
  23. private $admin_header_callback;
  24. /**
  25. * Callback for header div.
  26. *
  27. * @var callback
  28. * @since 3.0.0
  29. * @access private
  30. */
  31. private $admin_image_div_callback;
  32. /**
  33. * Holds default headers.
  34. *
  35. * @var array
  36. * @since 3.0.0
  37. * @access private
  38. */
  39. private $default_headers = array();
  40. /**
  41. * Holds custom headers uploaded by the user.
  42. *
  43. * @var array
  44. * @since 3.2.0
  45. * @access private
  46. */
  47. private $uploaded_headers = array();
  48. /**
  49. * Holds the page menu hook.
  50. *
  51. * @var string
  52. * @since 3.0.0
  53. * @access private
  54. */
  55. private $page = '';
  56. /**
  57. * @var bool
  58. */
  59. private $updated;
  60. /**
  61. * Constructor - Register administration header callback.
  62. *
  63. * @since 2.1.0
  64. * @param callback $admin_header_callback
  65. * @param callback $admin_image_div_callback Optional custom image div output callback.
  66. * @return Custom_Image_Header
  67. */
  68. public function __construct($admin_header_callback, $admin_image_div_callback = '') {
  69. $this->admin_header_callback = $admin_header_callback;
  70. $this->admin_image_div_callback = $admin_image_div_callback;
  71. add_action( 'admin_menu', array( $this, 'init' ) );
  72. add_action( 'customize_save_after', array( $this, 'customize_set_last_used' ) );
  73. add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) );
  74. add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) );
  75. add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
  76. }
  77. /**
  78. * Make private properties readable for backwards compatibility.
  79. *
  80. * @since 4.0.0
  81. * @access public
  82. *
  83. * @param string $name Property to get.
  84. * @return mixed Property.
  85. */
  86. public function __get( $name ) {
  87. return $this->$name;
  88. }
  89. /**
  90. * Make private properties settable for backwards compatibility.
  91. *
  92. * @since 4.0.0
  93. * @access public
  94. *
  95. * @param string $name Property to set.
  96. * @param mixed $value Property value.
  97. * @return mixed Newly-set property.
  98. */
  99. public function __set( $name, $value ) {
  100. return $this->$name = $value;
  101. }
  102. /**
  103. * Make private properties checkable for backwards compatibility.
  104. *
  105. * @since 4.0.0
  106. * @access public
  107. *
  108. * @param string $name Property to check if set.
  109. * @return bool Whether the property is set.
  110. */
  111. public function __isset( $name ) {
  112. return isset( $this->$name );
  113. }
  114. /**
  115. * Make private properties un-settable for backwards compatibility.
  116. *
  117. * @since 4.0.0
  118. * @access public
  119. *
  120. * @param string $name Property to unset.
  121. */
  122. public function __unset( $name ) {
  123. unset( $this->$name );
  124. }
  125. /**
  126. * Set up the hooks for the Custom Header admin page.
  127. *
  128. * @since 2.1.0
  129. */
  130. public function init() {
  131. if ( ! current_user_can('edit_theme_options') )
  132. return;
  133. $this->page = $page = add_theme_page(__('Header'), __('Header'), 'edit_theme_options', 'custom-header', array($this, 'admin_page'));
  134. add_action("admin_print_scripts-$page", array($this, 'js_includes'));
  135. add_action("admin_print_styles-$page", array($this, 'css_includes'));
  136. add_action("admin_head-$page", array($this, 'help') );
  137. add_action("admin_head-$page", array($this, 'take_action'), 50);
  138. add_action("admin_head-$page", array($this, 'js'), 50);
  139. if ( $this->admin_header_callback )
  140. add_action("admin_head-$page", $this->admin_header_callback, 51);
  141. }
  142. /**
  143. * Adds contextual help.
  144. *
  145. * @since 3.0.0
  146. */
  147. public function help() {
  148. get_current_screen()->add_help_tab( array(
  149. 'id' => 'overview',
  150. 'title' => __('Overview'),
  151. 'content' =>
  152. '<p>' . __( 'This screen is used to customize the header section of your theme.') . '</p>' .
  153. '<p>' . __( 'You can choose from the theme&#8217;s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.') . '<p>'
  154. ) );
  155. get_current_screen()->add_help_tab( array(
  156. 'id' => 'set-header-image',
  157. 'title' => __('Header Image'),
  158. 'content' =>
  159. '<p>' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button.' ) . '</p>' .
  160. '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you&#8217;d like and click the &#8220;Save Changes&#8221; button.' ) . '</p>' .
  161. '<p>' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the &#8220;Random&#8221; radio button next to the Uploaded Images or Default Images section to enable this feature.') . '</p>' .
  162. '<p>' . __( 'If you don&#8217;t want a header image to be displayed on your site at all, click the &#8220;Remove Header Image&#8221; button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click &#8220;Save Changes&#8221;.') . '</p>'
  163. ) );
  164. get_current_screen()->add_help_tab( array(
  165. 'id' => 'set-header-text',
  166. 'title' => __('Header Text'),
  167. 'content' =>
  168. '<p>' . sprintf( __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%1$s">General Settings</a> section.' ), admin_url( 'options-general.php' ) ) . '<p>' .
  169. '<p>' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text 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>' .
  170. '<p>' . __( 'Don&#8217;t forget to click &#8220;Save Changes&#8221; when you&#8217;re done!') . '</p>'
  171. ) );
  172. get_current_screen()->set_help_sidebar(
  173. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  174. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Header_Screen" target="_blank">Documentation on Custom Header</a>' ) . '</p>' .
  175. '<p>' . __( '<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
  176. );
  177. }
  178. /**
  179. * Get the current step.
  180. *
  181. * @since 2.6.0
  182. *
  183. * @return int Current step
  184. */
  185. public function step() {
  186. if ( ! isset( $_GET['step'] ) )
  187. return 1;
  188. $step = (int) $_GET['step'];
  189. if ( $step < 1 || 3 < $step ||
  190. ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
  191. ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
  192. )
  193. return 1;
  194. return $step;
  195. }
  196. /**
  197. * Set up the enqueue for the JavaScript files.
  198. *
  199. * @since 2.1.0
  200. */
  201. public function js_includes() {
  202. $step = $this->step();
  203. if ( ( 1 == $step || 3 == $step ) ) {
  204. wp_enqueue_media();
  205. wp_enqueue_script( 'custom-header' );
  206. if ( current_theme_supports( 'custom-header', 'header-text' ) )
  207. wp_enqueue_script( 'wp-color-picker' );
  208. } elseif ( 2 == $step ) {
  209. wp_enqueue_script('imgareaselect');
  210. }
  211. }
  212. /**
  213. * Set up the enqueue for the CSS files
  214. *
  215. * @since 2.7.0
  216. */
  217. public function css_includes() {
  218. $step = $this->step();
  219. if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
  220. wp_enqueue_style( 'wp-color-picker' );
  221. elseif ( 2 == $step )
  222. wp_enqueue_style('imgareaselect');
  223. }
  224. /**
  225. * Execute custom header modification.
  226. *
  227. * @since 2.6.0
  228. */
  229. public function take_action() {
  230. if ( ! current_user_can('edit_theme_options') )
  231. return;
  232. if ( empty( $_POST ) )
  233. return;
  234. $this->updated = true;
  235. if ( isset( $_POST['resetheader'] ) ) {
  236. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  237. $this->reset_header_image();
  238. return;
  239. }
  240. if ( isset( $_POST['removeheader'] ) ) {
  241. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  242. $this->remove_header_image();
  243. return;
  244. }
  245. if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
  246. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  247. set_theme_mod( 'header_textcolor', 'blank' );
  248. } elseif ( isset( $_POST['text-color'] ) ) {
  249. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  250. $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
  251. $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
  252. if ( strlen($color) == 6 || strlen($color) == 3 )
  253. set_theme_mod('header_textcolor', $color);
  254. elseif ( ! $color )
  255. set_theme_mod( 'header_textcolor', 'blank' );
  256. }
  257. if ( isset( $_POST['default-header'] ) ) {
  258. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  259. $this->set_header_image( $_POST['default-header'] );
  260. return;
  261. }
  262. }
  263. /**
  264. * Process the default headers
  265. *
  266. * @since 3.0.0
  267. */
  268. public function process_default_headers() {
  269. global $_wp_default_headers;
  270. if ( !isset($_wp_default_headers) )
  271. return;
  272. if ( ! empty( $this->default_headers ) ) {
  273. return;
  274. }
  275. $this->default_headers = $_wp_default_headers;
  276. $template_directory_uri = get_template_directory_uri();
  277. $stylesheet_directory_uri = get_stylesheet_directory_uri();
  278. foreach ( array_keys($this->default_headers) as $header ) {
  279. $this->default_headers[$header]['url'] = sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );
  280. $this->default_headers[$header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
  281. }
  282. }
  283. /**
  284. * Display UI for selecting one of several default headers.
  285. *
  286. * Show the random image option if this theme has multiple header images.
  287. * Random image option is on by default if no header has been set.
  288. *
  289. * @since 3.0.0
  290. */
  291. public function show_header_selector( $type = 'default' ) {
  292. if ( 'default' == $type ) {
  293. $headers = $this->default_headers;
  294. } else {
  295. $headers = get_uploaded_header_images();
  296. $type = 'uploaded';
  297. }
  298. if ( 1 < count( $headers ) ) {
  299. echo '<div class="random-header">';
  300. echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
  301. echo __( '<strong>Random:</strong> Show a different image on each page.' );
  302. echo '</label>';
  303. echo '</div>';
  304. }
  305. echo '<div class="available-headers">';
  306. foreach ( $headers as $header_key => $header ) {
  307. $header_thumbnail = $header['thumbnail_url'];
  308. $header_url = $header['url'];
  309. $header_desc = empty( $header['description'] ) ? '' : $header['description'];
  310. echo '<div class="default-header">';
  311. echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
  312. $width = '';
  313. if ( !empty( $header['attachment_id'] ) )
  314. $width = ' width="230"';
  315. echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
  316. echo '</div>';
  317. }
  318. echo '<div class="clear"></div></div>';
  319. }
  320. /**
  321. * Execute JavaScript depending on step.
  322. *
  323. * @since 2.1.0
  324. */
  325. public function js() {
  326. $step = $this->step();
  327. if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
  328. $this->js_1();
  329. elseif ( 2 == $step )
  330. $this->js_2();
  331. }
  332. /**
  333. * Display JavaScript based on Step 1 and 3.
  334. *
  335. * @since 2.6.0
  336. */
  337. public function js_1() {
  338. $default_color = '';
  339. if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
  340. $default_color = get_theme_support( 'custom-header', 'default-text-color' );
  341. if ( $default_color && false === strpos( $default_color, '#' ) ) {
  342. $default_color = '#' . $default_color;
  343. }
  344. }
  345. ?>
  346. <script type="text/javascript">
  347. /* <![CDATA[ */
  348. (function($){
  349. var default_color = '<?php echo $default_color; ?>',
  350. header_text_fields;
  351. function pickColor(color) {
  352. $('#name').css('color', color);
  353. $('#desc').css('color', color);
  354. $('#text-color').val(color);
  355. }
  356. function toggle_text() {
  357. var checked = $('#display-header-text').prop('checked'),
  358. text_color;
  359. header_text_fields.toggle( checked );
  360. if ( ! checked )
  361. return;
  362. text_color = $('#text-color');
  363. if ( '' == text_color.val().replace('#', '') ) {
  364. text_color.val( default_color );
  365. pickColor( default_color );
  366. } else {
  367. pickColor( text_color.val() );
  368. }
  369. }
  370. $(document).ready(function() {
  371. var text_color = $('#text-color');
  372. header_text_fields = $('.displaying-header-text');
  373. text_color.wpColorPicker({
  374. change: function( event, ui ) {
  375. pickColor( text_color.wpColorPicker('color') );
  376. },
  377. clear: function() {
  378. pickColor( '' );
  379. }
  380. });
  381. $('#display-header-text').click( toggle_text );
  382. <?php if ( ! display_header_text() ) : ?>
  383. toggle_text();
  384. <?php endif; ?>
  385. });
  386. })(jQuery);
  387. /* ]]> */
  388. </script>
  389. <?php
  390. }
  391. /**
  392. * Display JavaScript based on Step 2.
  393. *
  394. * @since 2.6.0
  395. */
  396. public function js_2() { ?>
  397. <script type="text/javascript">
  398. /* <![CDATA[ */
  399. function onEndCrop( coords ) {
  400. jQuery( '#x1' ).val(coords.x);
  401. jQuery( '#y1' ).val(coords.y);
  402. jQuery( '#width' ).val(coords.w);
  403. jQuery( '#height' ).val(coords.h);
  404. }
  405. jQuery(document).ready(function() {
  406. var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>;
  407. var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>;
  408. var ratio = xinit / yinit;
  409. var ximg = jQuery('img#upload').width();
  410. var yimg = jQuery('img#upload').height();
  411. if ( yimg < yinit || ximg < xinit ) {
  412. if ( ximg / yimg > ratio ) {
  413. yinit = yimg;
  414. xinit = yinit * ratio;
  415. } else {
  416. xinit = ximg;
  417. yinit = xinit / ratio;
  418. }
  419. }
  420. jQuery('img#upload').imgAreaSelect({
  421. handles: true,
  422. keys: true,
  423. show: true,
  424. x1: 0,
  425. y1: 0,
  426. x2: xinit,
  427. y2: yinit,
  428. <?php
  429. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  430. ?>
  431. aspectRatio: xinit + ':' + yinit,
  432. <?php
  433. }
  434. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
  435. ?>
  436. maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
  437. <?php
  438. }
  439. if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  440. ?>
  441. maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
  442. <?php
  443. }
  444. ?>
  445. onInit: function () {
  446. jQuery('#width').val(xinit);
  447. jQuery('#height').val(yinit);
  448. },
  449. onSelectChange: function(img, c) {
  450. jQuery('#x1').val(c.x1);
  451. jQuery('#y1').val(c.y1);
  452. jQuery('#width').val(c.width);
  453. jQuery('#height').val(c.height);
  454. }
  455. });
  456. });
  457. /* ]]> */
  458. </script>
  459. <?php
  460. }
  461. /**
  462. * Display first step of custom header image page.
  463. *
  464. * @since 2.1.0
  465. */
  466. public function step_1() {
  467. $this->process_default_headers();
  468. ?>
  469. <div class="wrap">
  470. <h2><?php _e( 'Custom Header' ); ?></h2>
  471. <?php if ( current_user_can( 'customize' ) ) { ?>
  472. <div class="notice notice-info hide-if-no-customize">
  473. <p>
  474. <?php
  475. printf(
  476. __( 'You can now manage and live-preview Custom Header in the <a href="%1$s">Customizer</a>.' ),
  477. admin_url( 'customize.php?autofocus[control]=header_image' )
  478. );
  479. ?>
  480. </p>
  481. </div>
  482. <?php } ?>
  483. <?php if ( ! empty( $this->updated ) ) { ?>
  484. <div id="message" class="updated">
  485. <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
  486. </div>
  487. <?php } ?>
  488. <h3><?php _e( 'Header Image' ); ?></h3>
  489. <table class="form-table">
  490. <tbody>
  491. <?php if ( get_custom_header() || display_header_text() ) : ?>
  492. <tr>
  493. <th scope="row"><?php _e( 'Preview' ); ?></th>
  494. <td>
  495. <?php
  496. if ( $this->admin_image_div_callback ) {
  497. call_user_func( $this->admin_image_div_callback );
  498. } else {
  499. $custom_header = get_custom_header();
  500. $header_image_style = 'background-image:url(' . esc_url( get_header_image() ) . ');';
  501. if ( $custom_header->width )
  502. $header_image_style .= 'max-width:' . $custom_header->width . 'px;';
  503. if ( $custom_header->height )
  504. $header_image_style .= 'height:' . $custom_header->height . 'px;';
  505. ?>
  506. <div id="headimg" style="<?php echo $header_image_style; ?>">
  507. <?php
  508. if ( display_header_text() )
  509. $style = ' style="color:#' . get_header_textcolor() . ';"';
  510. else
  511. $style = ' style="display:none;"';
  512. ?>
  513. <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1>
  514. <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
  515. </div>
  516. <?php } ?>
  517. </td>
  518. </tr>
  519. <?php endif; ?>
  520. <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
  521. <tr>
  522. <th scope="row"><?php _e( 'Select Image' ); ?></th>
  523. <td>
  524. <p><?php _e( 'You can select an image to be shown at the top of your site by uploading from your computer or choosing from your media library. After selecting an image you will be able to crop it.' ); ?><br />
  525. <?php
  526. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  527. printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) );
  528. } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
  529. if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
  530. printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
  531. } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
  532. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
  533. printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
  534. }
  535. if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
  536. if ( current_theme_supports( 'custom-header', 'width' ) )
  537. printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
  538. if ( current_theme_supports( 'custom-header', 'height' ) )
  539. printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
  540. }
  541. ?></p>
  542. <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ) ?>">
  543. <p>
  544. <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
  545. <input type="file" id="upload" name="import" />
  546. <input type="hidden" name="action" value="save" />
  547. <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
  548. <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
  549. </p>
  550. <?php
  551. $modal_update_href = esc_url( add_query_arg( array(
  552. 'page' => 'custom-header',
  553. 'step' => 2,
  554. '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
  555. ), admin_url('themes.php') ) );
  556. ?>
  557. <p>
  558. <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
  559. <button id="choose-from-library-link" class="button"
  560. data-update-link="<?php echo esc_attr( $modal_update_href ); ?>"
  561. data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>"
  562. data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></button>
  563. </p>
  564. </form>
  565. </td>
  566. </tr>
  567. <?php endif; ?>
  568. </tbody>
  569. </table>
  570. <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ) ?>">
  571. <?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?>
  572. <table class="form-table">
  573. <tbody>
  574. <?php if ( get_uploaded_header_images() ) : ?>
  575. <tr>
  576. <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
  577. <td>
  578. <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
  579. <?php
  580. $this->show_header_selector( 'uploaded' );
  581. ?>
  582. </td>
  583. </tr>
  584. <?php endif;
  585. if ( ! empty( $this->default_headers ) ) : ?>
  586. <tr>
  587. <th scope="row"><?php _e( 'Default Images' ); ?></th>
  588. <td>
  589. <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
  590. <p><?php _e( 'If you don&lsquo;t want to upload your own image, you can use one of these cool headers, or show a random one.' ) ?></p>
  591. <?php else: ?>
  592. <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
  593. <?php endif; ?>
  594. <?php
  595. $this->show_header_selector( 'default' );
  596. ?>
  597. </td>
  598. </tr>
  599. <?php endif;
  600. if ( get_header_image() ) : ?>
  601. <tr>
  602. <th scope="row"><?php _e( 'Remove Image' ); ?></th>
  603. <td>
  604. <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
  605. <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?>
  606. </td>
  607. </tr>
  608. <?php endif;
  609. $default_image = get_theme_support( 'custom-header', 'default-image' );
  610. if ( $default_image && get_header_image() != $default_image ) : ?>
  611. <tr>
  612. <th scope="row"><?php _e( 'Reset Image' ); ?></th>
  613. <td>
  614. <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
  615. <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?>
  616. </td>
  617. </tr>
  618. <?php endif; ?>
  619. </tbody>
  620. </table>
  621. <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
  622. <h3><?php _e( 'Header Text' ); ?></h3>
  623. <table class="form-table">
  624. <tbody>
  625. <tr>
  626. <th scope="row"><?php _e( 'Header Text' ); ?></th>
  627. <td>
  628. <p>
  629. <label><input type="checkbox" name="display-header-text" id="display-header-text"<?php checked( display_header_text() ); ?> /> <?php _e( 'Show header text with your image.' ); ?></label>
  630. </p>
  631. </td>
  632. </tr>
  633. <tr class="displaying-header-text">
  634. <th scope="row"><?php _e( 'Text Color' ); ?></th>
  635. <td>
  636. <p>
  637. <?php
  638. $default_color = '';
  639. if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
  640. $default_color = get_theme_support( 'custom-header', 'default-text-color' );
  641. if ( $default_color && false === strpos( $default_color, '#' ) ) {
  642. $default_color = '#' . $default_color;
  643. }
  644. }
  645. $default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : '';
  646. $header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' );
  647. if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) {
  648. $header_textcolor = '#' . $header_textcolor;
  649. }
  650. echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />';
  651. if ( $default_color ) {
  652. echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>';
  653. }
  654. ?>
  655. </p>
  656. </td>
  657. </tr>
  658. </tbody>
  659. </table>
  660. <?php endif;
  661. /**
  662. * Fires just before the submit button in the custom header options form.
  663. *
  664. * @since 3.1.0
  665. */
  666. do_action( 'custom_header_options' );
  667. wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
  668. <?php submit_button( null, 'primary', 'save-header-options' ); ?>
  669. </form>
  670. </div>
  671. <?php }
  672. /**
  673. * Display second step of custom header image page.
  674. *
  675. * @since 2.1.0
  676. */
  677. public function step_2() {
  678. check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
  679. if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
  680. wp_die( __( 'Cheatin&#8217; uh?' ), 403 );
  681. if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
  682. $attachment_id = absint( $_GET['file'] );
  683. $file = get_attached_file( $attachment_id, true );
  684. $url = wp_get_attachment_image_src( $attachment_id, 'full' );
  685. $url = $url[0];
  686. } elseif ( isset( $_POST ) ) {
  687. $data = $this->step_2_manage_upload();
  688. $attachment_id = $data['attachment_id'];
  689. $file = $data['file'];
  690. $url = $data['url'];
  691. }
  692. if ( file_exists( $file ) ) {
  693. list( $width, $height, $type, $attr ) = getimagesize( $file );
  694. } else {
  695. $data = wp_get_attachment_metadata( $attachment_id );
  696. $height = isset( $data[ 'height' ] ) ? $data[ 'height' ] : 0;
  697. $width = isset( $data[ 'width' ] ) ? $data[ 'width' ] : 0;
  698. unset( $data );
  699. }
  700. $max_width = 0;
  701. // For flex, limit size of image displayed to 1500px unless theme says otherwise
  702. if ( current_theme_supports( 'custom-header', 'flex-width' ) )
  703. $max_width = 1500;
  704. if ( current_theme_supports( 'custom-header', 'max-width' ) )
  705. $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
  706. $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
  707. // If flexible height isn't supported and the image is the exact right size
  708. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
  709. && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
  710. {
  711. // Add the meta-data
  712. if ( file_exists( $file ) )
  713. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
  714. $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
  715. /**
  716. * Fires after the header image is set or an error is returned.
  717. *
  718. * @since 2.1.0
  719. *
  720. * @param string $file Path to the file.
  721. * @param int $attachment_id Attachment ID.
  722. */
  723. do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication
  724. return $this->finished();
  725. } elseif ( $width > $max_width ) {
  726. $oitar = $width / $max_width;
  727. $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
  728. if ( ! $image || is_wp_error( $image ) )
  729. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  730. /** This filter is documented in wp-admin/custom-header.php */
  731. $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication
  732. $url = str_replace(basename($url), basename($image), $url);
  733. $width = $width / $oitar;
  734. $height = $height / $oitar;
  735. } else {
  736. $oitar = 1;
  737. }
  738. ?>
  739. <div class="wrap">
  740. <h2><?php _e( 'Crop Header Image' ); ?></h2>
  741. <form method="post" action="<?php echo esc_url(add_query_arg('step', 3)); ?>">
  742. <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
  743. <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
  744. <div id="crop_image" style="position: relative">
  745. <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
  746. </div>
  747. <input type="hidden" name="x1" id="x1" value="0"/>
  748. <input type="hidden" name="y1" id="y1" value="0"/>
  749. <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
  750. <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
  751. <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
  752. <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
  753. <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
  754. <input type="hidden" name="create-new-attachment" value="true" />
  755. <?php } ?>
  756. <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
  757. <p class="submit">
  758. <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
  759. <?php
  760. if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) )
  761. submit_button( __( 'Skip Cropping, Publish Image as Is' ), 'secondary', 'skip-cropping', false );
  762. ?>
  763. </p>
  764. </form>
  765. </div>
  766. <?php
  767. }
  768. /**
  769. * Upload the file to be cropped in the second step.
  770. *
  771. * @since 3.4.0
  772. */
  773. public function step_2_manage_upload() {
  774. $overrides = array('test_form' => false);
  775. $uploaded_file = $_FILES['import'];
  776. $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false );
  777. if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
  778. wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
  779. $file = wp_handle_upload($uploaded_file, $overrides);
  780. if ( isset($file['error']) )
  781. wp_die( $file['error'], __( 'Image Upload Error' ) );
  782. $url = $file['url'];
  783. $type = $file['type'];
  784. $file = $file['file'];
  785. $filename = basename($file);
  786. // Construct the object array
  787. $object = array(
  788. 'post_title' => $filename,
  789. 'post_content' => $url,
  790. 'post_mime_type' => $type,
  791. 'guid' => $url,
  792. 'context' => 'custom-header'
  793. );
  794. // Save the data
  795. $attachment_id = wp_insert_attachment( $object, $file );
  796. return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
  797. }
  798. /**
  799. * Display third step of custom header image page.
  800. *
  801. * @since 2.1.0
  802. */
  803. public function step_3() {
  804. check_admin_referer( 'custom-header-crop-image' );
  805. if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
  806. wp_die( __( 'Cheatin&#8217; uh?' ), 403 );
  807. if ( ! empty( $_POST['skip-cropping'] ) && ! ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) )
  808. wp_die( __( 'Cheatin&#8217; uh?' ), 403 );
  809. if ( $_POST['oitar'] > 1 ) {
  810. $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
  811. $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
  812. $_POST['width'] = $_POST['width'] * $_POST['oitar'];
  813. $_POST['height'] = $_POST['height'] * $_POST['oitar'];
  814. }
  815. $attachment_id = absint( $_POST['attachment_id'] );
  816. $original = get_attached_file($attachment_id);
  817. $dimensions = $this->get_header_dimensions( array(
  818. 'height' => $_POST['height'],
  819. 'width' => $_POST['width'],
  820. ) );
  821. $height = $dimensions['dst_height'];
  822. $width = $dimensions['dst_width'];
  823. if ( empty( $_POST['skip-cropping'] ) )
  824. $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
  825. elseif ( ! empty( $_POST['create-new-attachment'] ) )
  826. $cropped = _copy_image_file( $attachment_id );
  827. else
  828. $cropped = get_attached_file( $attachment_id );
  829. if ( ! $cropped || is_wp_error( $cropped ) )
  830. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  831. /** This filter is documented in wp-admin/custom-header.php */
  832. $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
  833. $object = $this->create_attachment_object( $cropped, $attachment_id );
  834. if ( ! empty( $_POST['create-new-attachment'] ) )
  835. unset( $object['ID'] );
  836. // Update the attachment
  837. $attachment_id = $this->insert_attachment( $object, $cropped );
  838. $url = $object['guid'];
  839. $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
  840. // Cleanup.
  841. $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
  842. if ( file_exists( $medium ) ) {
  843. /**
  844. * Filter the path of the file to delete.
  845. *
  846. * @since 2.1.0
  847. *
  848. * @param string $medium Path to the file to delete.
  849. */
  850. @unlink( apply_filters( 'wp_delete_file', $medium ) );
  851. }
  852. if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
  853. /** This filter is documented in wp-admin/custom-header.php */
  854. @unlink( apply_filters( 'wp_delete_file', $original ) );
  855. }
  856. return $this->finished();
  857. }
  858. /**
  859. * Display last step of custom header image page.
  860. *
  861. * @since 2.1.0
  862. */
  863. public function finished() {
  864. $this->updated = true;
  865. $this->step_1();
  866. }
  867. /**
  868. * Display the page based on the current step.
  869. *
  870. * @since 2.1.0
  871. */
  872. public function admin_page() {
  873. if ( ! current_user_can('edit_theme_options') )
  874. wp_die(__('You do not have permission to customize headers.'));
  875. $step = $this->step();
  876. if ( 2 == $step )
  877. $this->step_2();
  878. elseif ( 3 == $step )
  879. $this->step_3();
  880. else
  881. $this->step_1();
  882. }
  883. /**
  884. * Unused since 3.5.0.
  885. *
  886. * @since 3.4.0
  887. */
  888. public function attachment_fields_to_edit( $form_fields ) {
  889. return $form_fields;
  890. }
  891. /**
  892. * Unused since 3.5.0.
  893. *
  894. * @since 3.4.0
  895. */
  896. public function filter_upload_tabs( $tabs ) {
  897. return $tabs;
  898. }
  899. /**
  900. * Choose a header image, selected from existing uploaded and default headers,
  901. * or provide an array of uploaded header data (either new, or from media library).
  902. *
  903. * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
  904. * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
  905. * among the uploaded images; the key of a default image registered for that theme; and
  906. * the key of an image uploaded for that theme (the basename of the URL).
  907. * Or an array of arguments: attachment_id, url, width, height. All are required.
  908. *
  909. * @since 3.4.0
  910. */
  911. final public function set_header_image( $choice ) {
  912. if ( is_array( $choice ) || is_object( $choice ) ) {
  913. $choice = (array) $choice;
  914. if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
  915. return;
  916. $choice['url'] = esc_url_raw( $choice['url'] );
  917. $header_image_data = (object) array(
  918. 'attachment_id' => $choice['attachment_id'],
  919. 'url' => $choice['url'],
  920. 'thumbnail_url' => $choice['url'],
  921. 'height' => $choice['height'],
  922. 'width' => $choice['width'],
  923. );
  924. update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
  925. set_theme_mod( 'header_image', $choice['url'] );
  926. set_theme_mod( 'header_image_data', $header_image_data );
  927. return;
  928. }
  929. if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) {
  930. set_theme_mod( 'header_image', $choice );
  931. remove_theme_mod( 'header_image_data' );
  932. return;
  933. }
  934. $uploaded = get_uploaded_header_images();
  935. if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  936. $header_image_data = $uploaded[ $choice ];
  937. } else {
  938. $this->process_default_headers();
  939. if ( isset( $this->default_headers[ $choice ] ) )
  940. $header_image_data = $this->default_headers[ $choice ];
  941. else
  942. return;
  943. }
  944. set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
  945. set_theme_mod( 'header_image_data', $header_image_data );
  946. }
  947. /**
  948. * Remove a header image.
  949. *
  950. * @since 3.4.0
  951. */
  952. final public function remove_header_image() {
  953. return $this->set_header_image( 'remove-header' );
  954. }
  955. /**
  956. * Reset a header image to the default image for the theme.
  957. *
  958. * This method does not do anything if the theme does not have a default header image.
  959. *
  960. * @since 3.4.0
  961. */
  962. final public function reset_header_image() {
  963. $this->process_default_headers();
  964. $default = get_theme_support( 'custom-header', 'default-image' );
  965. if ( ! $default )
  966. return $this->remove_header_image();
  967. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  968. $default_data = array();
  969. foreach ( $this->default_headers as $header => $details ) {
  970. if ( $details['url'] == $default ) {
  971. $default_data = $details;
  972. break;
  973. }
  974. }
  975. set_theme_mod( 'header_image', $default );
  976. set_theme_mod( 'header_image_data', (object) $default_data );
  977. }
  978. /**
  979. * Calculate width and height based on what the currently selected theme supports.
  980. *
  981. * @return array dst_height and dst_width of header image.
  982. */
  983. final public function get_header_dimensions( $dimensions ) {
  984. $max_width = 0;
  985. $width = absint( $dimensions['width'] );
  986. $height = absint( $dimensions['height'] );
  987. $theme_height = get_theme_support( 'custom-header', 'height' );
  988. $theme_width = get_theme_support( 'custom-header', 'width' );
  989. $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );
  990. $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
  991. $has_max_width = current_theme_supports( 'custom-header', 'max-width' ) ;
  992. $dst = array( 'dst_height' => null, 'dst_width' => null );
  993. // For flex, limit size of image displayed to 1500px unless theme says otherwise
  994. if ( $has_flex_width ) {
  995. $max_width = 1500;
  996. }
  997. if ( $has_max_width ) {
  998. $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
  999. }
  1000. $max_width = max( $max_width, $theme_width );
  1001. if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
  1002. $dst['dst_height'] = absint( $height * ( $max_width / $width ) );
  1003. }
  1004. elseif ( $has_flex_height && $has_flex_width ) {
  1005. $dst['dst_height'] = $height;
  1006. }
  1007. else {
  1008. $dst['dst_height'] = $theme_height;
  1009. }
  1010. if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
  1011. $dst['dst_width'] = absint( $width * ( $max_width / $width ) );
  1012. }
  1013. elseif ( $has_flex_width && $has_flex_height ) {
  1014. $dst['dst_width'] = $width;
  1015. }
  1016. else {
  1017. $dst['dst_width'] = $theme_width;
  1018. }
  1019. return $dst;
  1020. }
  1021. /**
  1022. * Create an attachment 'object'.
  1023. *
  1024. * @param string $cropped Cropped image URL.
  1025. * @param int $parent_attachment_id Attachment ID of parent image.
  1026. *
  1027. * @return array Attachment object.
  1028. */
  1029. final public function create_attachment_object( $cropped, $parent_attachment_id ) {
  1030. $parent = get_post( $parent_attachment_id );
  1031. $parent_url = $parent->guid;
  1032. $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
  1033. $size = @getimagesize( $cropped );
  1034. $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  1035. $object = array(
  1036. 'ID' => $parent_attachment_id,
  1037. 'post_title' => basename($cropped),
  1038. 'post_content' => $url,
  1039. 'post_mime_type' => $image_type,
  1040. 'guid' => $url,
  1041. 'context' => 'custom-header'
  1042. );
  1043. return $object;
  1044. }
  1045. /**
  1046. * Insert an attachment and its metadata.
  1047. *
  1048. * @param array $object Attachment object.
  1049. * @param string $cropped Cropped image URL.
  1050. *
  1051. * @return int Attachment ID.
  1052. */
  1053. final public function insert_attachment( $object, $cropped ) {
  1054. $attachment_id = wp_insert_attachment( $object, $cropped );
  1055. $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
  1056. /**
  1057. * Filter the header image attachment metadata.
  1058. *
  1059. * @since 3.9.0
  1060. *
  1061. * @see wp_generate_attachment_metadata()
  1062. *
  1063. * @param array $metadata Attachment metadata.
  1064. */
  1065. $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
  1066. wp_update_attachment_metadata( $attachment_id, $metadata );
  1067. return $attachment_id;
  1068. }
  1069. /**
  1070. * Gets attachment uploaded by Media Manager, crops it, then saves it as a
  1071. * new object. Returns JSON-encoded object details.
  1072. */
  1073. public function ajax_header_crop() {
  1074. check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );
  1075. if ( ! current_user_can( 'edit_theme_options' ) ) {
  1076. wp_send_json_error();
  1077. }
  1078. if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
  1079. wp_send_json_error();
  1080. }
  1081. $crop_details = $_POST['cropDetails'];
  1082. $dimensions = $this->get_header_dimensions( array(
  1083. 'height' => $crop_details['height'],
  1084. 'width' => $crop_details['width'],
  1085. ) );
  1086. $attachment_id = absint( $_POST['id'] );
  1087. $cropped = wp_crop_image(
  1088. $attachment_id,
  1089. (int) $crop_details['x1'],
  1090. (int) $crop_details['y1'],
  1091. (int) $crop_details['width'],
  1092. (int) $crop_details['height'],
  1093. (int) $dimensions['dst_width'],
  1094. (int) $dimensions['dst_height']
  1095. );
  1096. if ( ! $cropped || is_wp_error( $cropped ) ) {
  1097. wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
  1098. }
  1099. /** This filter is documented in wp-admin/custom-header.php */
  1100. $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
  1101. $object = $this->create_attachment_object( $cropped, $attachment_id );
  1102. unset( $object['ID'] );
  1103. $new_attachment_id = $this->insert_attachment( $object, $cropped );
  1104. $object['attachment_id'] = $new_attachment_id;
  1105. $object['width'] = $dimensions['dst_width'];
  1106. $object['height'] = $dimensions['dst_height'];
  1107. wp_send_json_success( $object );
  1108. }
  1109. /**
  1110. * Given an attachment ID for a header image, updates its "last used"
  1111. * timestamp to now.
  1112. *
  1113. * Triggered when the user tries adds a new header image from the
  1114. * Media Manager, even if s/he doesn't save that change.
  1115. */
  1116. public function ajax_header_add() {
  1117. check_ajax_referer( 'header-add', 'nonce' );
  1118. if ( ! current_user_can( 'edit_theme_options' ) ) {
  1119. wp_send_json_error();
  1120. }
  1121. $attachment_id = absint( $_POST['attachment_id'] );
  1122. if ( $attachment_id < 1 ) {
  1123. wp_send_json_error();
  1124. }
  1125. $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1126. update_post_meta( $attachment_id, $key, time() );
  1127. update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
  1128. wp_send_json_success();
  1129. }
  1130. /**
  1131. * Given an attachment ID for a header image, unsets it as a user-uploaded
  1132. * header image for the current theme.
  1133. *
  1134. * Triggered when the user clicks the overlay "X" button next to each image
  1135. * choice in the Customizer's Header tool.
  1136. */
  1137. public function ajax_header_remove() {
  1138. check_ajax_referer( 'header-remove', 'nonce' );
  1139. if ( ! current_user_can( 'edit_theme_options' ) ) {
  1140. wp_send_json_error();
  1141. }
  1142. $attachment_id = absint( $_POST['attachment_id'] );
  1143. if ( $attachment_id < 1 ) {
  1144. wp_send_json_error();
  1145. }
  1146. $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1147. delete_post_meta( $attachment_id, $key );
  1148. delete_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
  1149. wp_send_json_success();
  1150. }
  1151. public function customize_set_last_used( $wp_customize ) {
  1152. $data = $wp_customize->get_setting( 'header_image_data' )->post_value();
  1153. if ( ! isset( $data['attachment_id'] ) ) {
  1154. return;
  1155. }
  1156. $attachment_id = $data['attachment_id'];
  1157. $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1158. update_post_meta( $attachment_id, $key, time() );
  1159. }
  1160. public function get_default_header_images() {
  1161. $this->process_default_headers();
  1162. // Get the default image if there is one.
  1163. $default = get_theme_support( 'custom-header', 'default-image' );
  1164. if ( ! $default ) { // If not,
  1165. return $this->default_headers; // easy peasy.
  1166. }
  1167. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  1168. $already_has_default = false;
  1169. foreach ( $this->default_headers as $k => $h ) {
  1170. if ( $h['url'] === $default ) {
  1171. $already_has_default = true;
  1172. break;
  1173. }
  1174. }
  1175. if ( $already_has_default ) {
  1176. return $this->default_headers;
  1177. }
  1178. // If the one true image isn't included in the default set, prepend it.
  1179. $header_images = array();
  1180. $header_images['default'] = array(
  1181. 'url' => $default,
  1182. 'thumbnail_url' => $default,
  1183. 'description' => 'Default'
  1184. );
  1185. // The rest of the set comes after.
  1186. $header_images = array_merge( $header_images, $this->default_headers );
  1187. return $header_images;
  1188. }
  1189. public function get_uploaded_header_images() {
  1190. $header_images = get_uploaded_header_images();
  1191. $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1192. $alt_text_key = '_wp_attachment_image_alt';
  1193. foreach ( $header_images as &$header_image ) {
  1194. $header_meta = get_post_meta( $header_image['attachment_id'] );
  1195. $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
  1196. $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
  1197. }
  1198. return $header_images;
  1199. }
  1200. }