PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/custom-header.php

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