PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/custom-header.php

https://github.com/muskmelon/Greemo
PHP | 791 lines | 532 code | 101 blank | 158 comment | 122 complexity | 04ef8b09d107911327493b62b6f595fb MD5 | raw file
  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. var $admin_header_callback;
  24. /**
  25. * Callback for header div.
  26. *
  27. * @var callback
  28. * @since 3.0.0
  29. * @access private
  30. */
  31. var $admin_image_div_callback;
  32. /**
  33. * Holds default headers.
  34. *
  35. * @var array
  36. * @since 3.0.0
  37. * @access private
  38. */
  39. var $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. var $uploaded_headers = array();
  48. /**
  49. * Holds the page menu hook.
  50. *
  51. * @var string
  52. * @since 3.0.0
  53. * @access private
  54. */
  55. var $page = '';
  56. /**
  57. * Constructor - Register administration header callback.
  58. *
  59. * @since 2.1.0
  60. * @param callback $admin_header_callback
  61. * @param callback $admin_image_div_callback Optional custom image div output callback.
  62. * @return Custom_Image_Header
  63. */
  64. function __construct($admin_header_callback, $admin_image_div_callback = '') {
  65. $this->admin_header_callback = $admin_header_callback;
  66. $this->admin_image_div_callback = $admin_image_div_callback;
  67. }
  68. /**
  69. * Set up the hooks for the Custom Header admin page.
  70. *
  71. * @since 2.1.0
  72. */
  73. function init() {
  74. if ( ! current_user_can('edit_theme_options') )
  75. return;
  76. $this->page = $page = add_theme_page(__('Header'), __('Header'), 'edit_theme_options', 'custom-header', array(&$this, 'admin_page'));
  77. add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
  78. add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
  79. add_action("admin_head-$page", array(&$this, 'help') );
  80. add_action("admin_head-$page", array(&$this, 'take_action'), 50);
  81. add_action("admin_head-$page", array(&$this, 'js'), 50);
  82. add_action("admin_head-$page", $this->admin_header_callback, 51);
  83. }
  84. /**
  85. * Adds contextual help.
  86. *
  87. * @since 3.0.0
  88. */
  89. function help() {
  90. add_contextual_help( $this->page, '<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.' ) . '</p>' .
  91. '<p>' . __( 'If you want to discard your custom header and go back to the default included in your theme, click on the buttons to remove the custom image and restore the original header image.' ) . '</p>' .
  92. '<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 Save Changes button.' ) . '</p>' .
  93. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  94. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Header_Screen" target="_blank">Documentation on Custom Header</a>' ) . '</p>' .
  95. '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>' );
  96. }
  97. /**
  98. * Get the current step.
  99. *
  100. * @since 2.6.0
  101. *
  102. * @return int Current step
  103. */
  104. function step() {
  105. if ( ! isset( $_GET['step'] ) )
  106. return 1;
  107. $step = (int) $_GET['step'];
  108. if ( $step < 1 || 3 < $step )
  109. $step = 1;
  110. return $step;
  111. }
  112. /**
  113. * Set up the enqueue for the JavaScript files.
  114. *
  115. * @since 2.1.0
  116. */
  117. function js_includes() {
  118. $step = $this->step();
  119. if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
  120. wp_enqueue_script('farbtastic');
  121. elseif ( 2 == $step )
  122. wp_enqueue_script('imgareaselect');
  123. }
  124. /**
  125. * Set up the enqueue for the CSS files
  126. *
  127. * @since 2.7
  128. */
  129. function css_includes() {
  130. $step = $this->step();
  131. if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
  132. wp_enqueue_style('farbtastic');
  133. elseif ( 2 == $step )
  134. wp_enqueue_style('imgareaselect');
  135. }
  136. /**
  137. * Check if header text is allowed
  138. *
  139. * @since 3.0.0
  140. */
  141. function header_text() {
  142. if ( defined( 'NO_HEADER_TEXT' ) && NO_HEADER_TEXT )
  143. return false;
  144. return true;
  145. }
  146. /**
  147. * Execute custom header modification.
  148. *
  149. * @since 2.6.0
  150. */
  151. function take_action() {
  152. if ( ! current_user_can('edit_theme_options') )
  153. return;
  154. if ( empty( $_POST ) )
  155. return;
  156. $this->updated = true;
  157. if ( isset( $_POST['resetheader'] ) ) {
  158. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  159. remove_theme_mod( 'header_image' );
  160. return;
  161. }
  162. if ( isset( $_POST['resettext'] ) ) {
  163. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  164. remove_theme_mod('header_textcolor');
  165. return;
  166. }
  167. if ( isset( $_POST['removeheader'] ) ) {
  168. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  169. set_theme_mod( 'header_image', 'remove-header' );
  170. return;
  171. }
  172. if ( isset( $_POST['text-color'] ) ) {
  173. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  174. $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
  175. if ( 'blank' == $_POST['text-color'] ) {
  176. set_theme_mod( 'header_textcolor', 'blank' );
  177. } else {
  178. $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
  179. if ( strlen($color) == 6 || strlen($color) == 3 )
  180. set_theme_mod('header_textcolor', $color);
  181. }
  182. }
  183. if ( isset( $_POST['default-header'] ) ) {
  184. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  185. if ( 'random-default-image' == $_POST['default-header'] ) {
  186. set_theme_mod( 'header_image', 'random-default-image' );
  187. } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) {
  188. set_theme_mod( 'header_image', 'random-uploaded-image' );
  189. } else {
  190. $this->process_default_headers();
  191. $uploaded = get_uploaded_header_images();
  192. if ( isset( $uploaded[$_POST['default-header']] ) )
  193. set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
  194. elseif ( isset( $this->default_headers[$_POST['default-header']] ) )
  195. set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
  196. }
  197. }
  198. }
  199. /**
  200. * Process the default headers
  201. *
  202. * @since 3.0.0
  203. */
  204. function process_default_headers() {
  205. global $_wp_default_headers;
  206. if ( !empty($this->headers) )
  207. return;
  208. if ( !isset($_wp_default_headers) )
  209. return;
  210. $this->default_headers = $_wp_default_headers;
  211. foreach ( array_keys($this->default_headers) as $header ) {
  212. $this->default_headers[$header]['url'] = sprintf( $this->default_headers[$header]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
  213. $this->default_headers[$header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
  214. }
  215. }
  216. /**
  217. * Display UI for selecting one of several default headers.
  218. *
  219. * Show the random image option if this theme has multiple header images.
  220. * Random image option is on by default if no header has been set.
  221. *
  222. * @since 3.0.0
  223. */
  224. function show_header_selector( $type = 'default' ) {
  225. if ( 'default' == $type ) {
  226. $headers = $this->default_headers;
  227. } else {
  228. $headers = get_uploaded_header_images();
  229. $type = 'uploaded';
  230. }
  231. if ( 1 < count( $headers ) ) {
  232. echo '<div class="random-header">';
  233. echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
  234. echo __( '<strong>Random:</strong> Show a different image on each page.' );
  235. echo '</label>';
  236. echo '</div>';
  237. }
  238. echo '<div class="available-headers">';
  239. foreach ( $headers as $header_key => $header ) {
  240. $header_thumbnail = $header['thumbnail_url'];
  241. $header_url = $header['url'];
  242. $header_desc = empty( $header['description'] ) ? '' : $header['description'];
  243. echo '<div class="default-header">';
  244. echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
  245. $width = '';
  246. if ( !empty( $header['uploaded'] ) )
  247. $width = ' width="230"';
  248. echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
  249. echo '</div>';
  250. }
  251. echo '<div class="clear"></div></div>';
  252. }
  253. /**
  254. * Execute Javascript depending on step.
  255. *
  256. * @since 2.1.0
  257. */
  258. function js() {
  259. $step = $this->step();
  260. if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
  261. $this->js_1();
  262. elseif ( 2 == $step )
  263. $this->js_2();
  264. }
  265. /**
  266. * Display Javascript based on Step 1 and 3.
  267. *
  268. * @since 2.6.0
  269. */
  270. function js_1() { ?>
  271. <script type="text/javascript">
  272. /* <![CDATA[ */
  273. var text_objects = ['#name', '#desc', '#text-color-row'];
  274. var farbtastic;
  275. var default_color = '#<?php echo HEADER_TEXTCOLOR; ?>';
  276. var old_color = null;
  277. function pickColor(color) {
  278. jQuery('#name').css('color', color);
  279. jQuery('#desc').css('color', color);
  280. jQuery('#text-color').val(color);
  281. farbtastic.setColor(color);
  282. }
  283. function toggle_text(s) {
  284. if (jQuery(s).attr('id') == 'showtext' && jQuery('#text-color').val() != 'blank')
  285. return;
  286. if (jQuery(s).attr('id') == 'hidetext' && jQuery('#text-color').val() == 'blank')
  287. return;
  288. if (jQuery('#text-color').val() == 'blank') {
  289. //Show text
  290. if (old_color == '#blank')
  291. old_color = default_color;
  292. jQuery( text_objects.toString() ).show();
  293. jQuery('#text-color').val(old_color);
  294. jQuery('#name').css('color', old_color);
  295. jQuery('#desc').css('color', old_color);
  296. pickColor(old_color);
  297. } else {
  298. //Hide text
  299. jQuery( text_objects.toString() ).hide();
  300. old_color = jQuery('#text-color').val();
  301. jQuery('#text-color').val('blank');
  302. }
  303. }
  304. jQuery(document).ready(function() {
  305. jQuery('#pickcolor').click(function() {
  306. jQuery('#color-picker').show();
  307. });
  308. jQuery('input[name="hidetext"]').click(function() {
  309. toggle_text(this);
  310. });
  311. jQuery('#defaultcolor').click(function() {
  312. pickColor(default_color);
  313. jQuery('#text-color').val(default_color)
  314. });
  315. jQuery('#text-color').keyup(function() {
  316. var _hex = jQuery('#text-color').val();
  317. var hex = _hex;
  318. if ( hex[0] != '#' )
  319. hex = '#' + hex;
  320. hex = hex.replace(/[^#a-fA-F0-9]+/, '');
  321. if ( hex != _hex )
  322. jQuery('#text-color').val(hex);
  323. if ( hex.length == 4 || hex.length == 7 )
  324. pickColor( hex );
  325. });
  326. jQuery(document).mousedown(function(){
  327. jQuery('#color-picker').each( function() {
  328. var display = jQuery(this).css('display');
  329. if (display == 'block')
  330. jQuery(this).fadeOut(2);
  331. });
  332. });
  333. farbtastic = jQuery.farbtastic('#color-picker', function(color) { pickColor(color); });
  334. <?php if ( $color = get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
  335. pickColor('#<?php echo $color; ?>');
  336. <?php } ?>
  337. <?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() ) { ?>
  338. toggle_text();
  339. <?php } ?>
  340. });
  341. /* ]]> */
  342. </script>
  343. <?php
  344. }
  345. /**
  346. * Display Javascript based on Step 2.
  347. *
  348. * @since 2.6.0
  349. */
  350. function js_2() { ?>
  351. <script type="text/javascript">
  352. /* <![CDATA[ */
  353. function onEndCrop( coords ) {
  354. jQuery( '#x1' ).val(coords.x);
  355. jQuery( '#y1' ).val(coords.y);
  356. jQuery( '#width' ).val(coords.w);
  357. jQuery( '#height' ).val(coords.h);
  358. }
  359. jQuery(document).ready(function() {
  360. var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
  361. var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
  362. var ratio = xinit / yinit;
  363. var ximg = jQuery('img#upload').width();
  364. var yimg = jQuery('img#upload').height();
  365. if ( yimg < yinit || ximg < xinit ) {
  366. if ( ximg / yimg > ratio ) {
  367. yinit = yimg;
  368. xinit = yinit * ratio;
  369. } else {
  370. xinit = ximg;
  371. yinit = xinit / ratio;
  372. }
  373. }
  374. jQuery('img#upload').imgAreaSelect({
  375. handles: true,
  376. keys: true,
  377. aspectRatio: xinit + ':' + yinit,
  378. show: true,
  379. x1: 0,
  380. y1: 0,
  381. x2: xinit,
  382. y2: yinit,
  383. maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
  384. maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
  385. onInit: function () {
  386. jQuery('#width').val(xinit);
  387. jQuery('#height').val(yinit);
  388. },
  389. onSelectChange: function(img, c) {
  390. jQuery('#x1').val(c.x1);
  391. jQuery('#y1').val(c.y1);
  392. jQuery('#width').val(c.width);
  393. jQuery('#height').val(c.height);
  394. }
  395. });
  396. });
  397. /* ]]> */
  398. </script>
  399. <?php
  400. }
  401. /**
  402. * Display first step of custom header image page.
  403. *
  404. * @since 2.1.0
  405. */
  406. function step_1() {
  407. $this->process_default_headers();
  408. ?>
  409. <div class="wrap">
  410. <?php screen_icon(); ?>
  411. <h2><?php _e('Custom Header'); ?></h2>
  412. <?php if ( ! empty( $this->updated ) ) { ?>
  413. <div id="message" class="updated">
  414. <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
  415. </div>
  416. <?php } ?>
  417. <table class="form-table">
  418. <tbody>
  419. <tr valign="top">
  420. <th scope="row"><?php _e( 'Preview' ); ?></th>
  421. <td >
  422. <?php if ( $this->admin_image_div_callback ) {
  423. call_user_func( $this->admin_image_div_callback );
  424. } else {
  425. ?>
  426. <div id="headimg" style="max-width:<?php echo HEADER_IMAGE_WIDTH; ?>px;height:<?php echo HEADER_IMAGE_HEIGHT; ?>px;background-image:url(<?php esc_url ( header_image() ) ?>);">
  427. <?php
  428. if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() )
  429. $style = ' style="display:none;"';
  430. else
  431. $style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
  432. ?>
  433. <h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>"><?php bloginfo( 'name' ); ?></a></h1>
  434. <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
  435. </div>
  436. <?php } ?>
  437. </td>
  438. </tr>
  439. <?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
  440. <tr valign="top">
  441. <th scope="row"><?php _e( 'Upload Image' ); ?></th>
  442. <td>
  443. <p><?php _e( 'You can upload a custom header image to be shown at the top of your site instead of the default one. On the next screen you will be able to crop the image.' ); ?><br />
  444. <?php printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p>
  445. <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
  446. <p>
  447. <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
  448. <input type="file" id="upload" name="import" />
  449. <input type="hidden" name="action" value="save" />
  450. <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ) ?>
  451. <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
  452. </p>
  453. </form>
  454. </td>
  455. </tr>
  456. <?php endif; ?>
  457. </tbody>
  458. </table>
  459. <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
  460. <table class="form-table">
  461. <tbody>
  462. <?php if ( get_uploaded_header_images() ) : ?>
  463. <tr valign="top">
  464. <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
  465. <td>
  466. <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
  467. <?php
  468. $this->show_header_selector( 'uploaded' );
  469. ?>
  470. </td>
  471. </tr>
  472. <?php endif;
  473. if ( ! empty( $this->default_headers ) ) : ?>
  474. <tr valign="top">
  475. <th scope="row"><?php _e( 'Default Images' ); ?></th>
  476. <td>
  477. <?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
  478. <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>
  479. <?php else: ?>
  480. <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
  481. <?php endif; ?>
  482. <?php
  483. $this->show_header_selector( 'default' );
  484. ?>
  485. </td>
  486. </tr>
  487. <?php endif;
  488. if ( get_header_image() ) : ?>
  489. <tr valign="top">
  490. <th scope="row"><?php _e( 'Remove Image' ); ?></th>
  491. <td>
  492. <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
  493. <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?>
  494. </td>
  495. </tr>
  496. <?php endif;
  497. if ( defined( 'HEADER_IMAGE' ) && '' != HEADER_IMAGE ) : ?>
  498. <tr valign="top">
  499. <th scope="row"><?php _e( 'Reset Image' ); ?></th>
  500. <td>
  501. <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
  502. <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?>
  503. </td>
  504. </tr>
  505. <?php endif; ?>
  506. </tbody>
  507. </table>
  508. <?php if ( $this->header_text() ) : ?>
  509. <table class="form-table">
  510. <tbody>
  511. <tr valign="top" class="hide-if-no-js">
  512. <th scope="row"><?php _e( 'Display Text' ); ?></th>
  513. <td>
  514. <p>
  515. <?php $hidetext = get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>
  516. <label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( ( 'blank' == $hidetext || empty( $hidetext ) ) ? true : false ); ?> /> <?php _e( 'No' ); ?></label>
  517. <label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( ( 'blank' == $hidetext || empty( $hidetext ) ) ? false : true ); ?> /> <?php _e( 'Yes' ); ?></label>
  518. </p>
  519. </td>
  520. </tr>
  521. <tr valign="top" id="text-color-row">
  522. <th scope="row"><?php _e( 'Text Color' ); ?></th>
  523. <td>
  524. <p>
  525. <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) ); ?>" />
  526. <span class="description hide-if-js"><?php _e( 'If you want to hide header text, add <strong>#blank</strong> as text color.' );?></span>
  527. <input type="button" class="button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" />
  528. </p>
  529. <div id="color-picker" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
  530. </td>
  531. </tr>
  532. <?php if ( defined('HEADER_TEXTCOLOR') && get_theme_mod('header_textcolor') ) { ?>
  533. <tr valign="top">
  534. <th scope="row"><?php _e('Reset Text Color'); ?></th>
  535. <td>
  536. <p><?php _e( 'This will restore the original header text. You will not be able to restore any customizations.' ) ?></p>
  537. <?php submit_button( __( 'Restore Original Header Text' ), 'button', 'resettext', false ); ?>
  538. </td>
  539. </tr>
  540. <?php } ?>
  541. </tbody>
  542. </table>
  543. <?php endif;
  544. do_action( 'custom_header_options' );
  545. wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
  546. <?php submit_button( null, 'primary', 'save-header-options' ); ?>
  547. </form>
  548. </div>
  549. <?php }
  550. /**
  551. * Display second step of custom header image page.
  552. *
  553. * @since 2.1.0
  554. */
  555. function step_2() {
  556. check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
  557. if ( ! current_theme_supports( 'custom-header-uploads' ) )
  558. wp_die( 'Cheatin&#8217; uh?' );
  559. $overrides = array('test_form' => false);
  560. $file = wp_handle_upload($_FILES['import'], $overrides);
  561. if ( isset($file['error']) )
  562. wp_die( $file['error'], __( 'Image Upload Error' ) );
  563. $url = $file['url'];
  564. $type = $file['type'];
  565. $file = $file['file'];
  566. $filename = basename($file);
  567. // Construct the object array
  568. $object = array(
  569. 'post_title' => $filename,
  570. 'post_content' => $url,
  571. 'post_mime_type' => $type,
  572. 'guid' => $url,
  573. 'context' => 'custom-header'
  574. );
  575. // Save the data
  576. $id = wp_insert_attachment($object, $file);
  577. list($width, $height, $type, $attr) = getimagesize( $file );
  578. if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
  579. // Add the meta-data
  580. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  581. update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
  582. set_theme_mod('header_image', esc_url($url));
  583. do_action('wp_create_file_in_uploads', $file, $id); // For replication
  584. return $this->finished();
  585. } elseif ( $width > HEADER_IMAGE_WIDTH ) {
  586. $oitar = $width / HEADER_IMAGE_WIDTH;
  587. $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
  588. if ( is_wp_error( $image ) )
  589. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  590. $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
  591. $url = str_replace(basename($url), basename($image), $url);
  592. $width = $width / $oitar;
  593. $height = $height / $oitar;
  594. } else {
  595. $oitar = 1;
  596. }
  597. ?>
  598. <div class="wrap">
  599. <?php screen_icon(); ?>
  600. <h2><?php _e( 'Crop Header Image' ); ?></h2>
  601. <form method="post" action="<?php echo esc_attr(add_query_arg('step', 3)); ?>">
  602. <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
  603. <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
  604. <div id="crop_image" style="position: relative">
  605. <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
  606. </div>
  607. <input type="hidden" name="x1" id="x1" value="0"/>
  608. <input type="hidden" name="y1" id="y1" value="0"/>
  609. <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
  610. <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
  611. <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
  612. <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
  613. <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
  614. <?php submit_button( __( 'Crop and Publish' ) ); ?>
  615. </p>
  616. </form>
  617. </div>
  618. <?php
  619. }
  620. /**
  621. * Display third step of custom header image page.
  622. *
  623. * @since 2.1.0
  624. */
  625. function step_3() {
  626. check_admin_referer('custom-header-crop-image');
  627. if ( ! current_theme_supports( 'custom-header-uploads' ) )
  628. wp_die( 'Cheatin&#8217; uh?' );
  629. if ( $_POST['oitar'] > 1 ) {
  630. $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
  631. $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
  632. $_POST['width'] = $_POST['width'] * $_POST['oitar'];
  633. $_POST['height'] = $_POST['height'] * $_POST['oitar'];
  634. }
  635. $attachment_id = absint( $_POST['attachment_id'] );
  636. $original = get_attached_file($attachment_id);
  637. $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
  638. if ( is_wp_error( $cropped ) )
  639. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  640. $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication
  641. $parent = get_post($attachment_id);
  642. $parent_url = $parent->guid;
  643. $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
  644. // Construct the object array
  645. $object = array(
  646. 'ID' => $attachment_id,
  647. 'post_title' => basename($cropped),
  648. 'post_content' => $url,
  649. 'post_mime_type' => 'image/jpeg',
  650. 'guid' => $url,
  651. 'context' => 'custom-header'
  652. );
  653. // Update the attachment
  654. wp_insert_attachment($object, $cropped);
  655. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
  656. update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
  657. set_theme_mod('header_image', $url);
  658. // cleanup
  659. $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
  660. @unlink( apply_filters( 'wp_delete_file', $medium ) );
  661. @unlink( apply_filters( 'wp_delete_file', $original ) );
  662. return $this->finished();
  663. }
  664. /**
  665. * Display last step of custom header image page.
  666. *
  667. * @since 2.1.0
  668. */
  669. function finished() {
  670. $this->updated = true;
  671. $this->step_1();
  672. }
  673. /**
  674. * Display the page based on the current step.
  675. *
  676. * @since 2.1.0
  677. */
  678. function admin_page() {
  679. if ( ! current_user_can('edit_theme_options') )
  680. wp_die(__('You do not have permission to customize headers.'));
  681. $step = $this->step();
  682. if ( 1 == $step )
  683. $this->step_1();
  684. elseif ( 2 == $step )
  685. $this->step_2();
  686. elseif ( 3 == $step )
  687. $this->step_3();
  688. }
  689. }
  690. ?>