+ $attachment_id = absint( $_POST['attachment_id'] );
+ $original = get_attached_file($attachment_id);
+
+ $dimensions = $this->get_header_dimensions( array(
+ 'height' => $_POST['height'],
+ 'width' => $_POST['width'],
+ ) );
+ $height = $dimensions['dst_height'];
+ $width = $dimensions['dst_width'];
+
+ if ( empty( $_POST['skip-cropping'] ) )
+ $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
+ elseif ( ! empty( $_POST['create-new-attachment'] ) )
+ $cropped = _copy_image_file( $attachment_id );
+ else
+ $cropped = get_attached_file( $attachment_id );
+
+ if ( ! $cropped || is_wp_error( $cropped ) )
+ wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
+
+ /** This filter is documented in wp-admin/custom-header.php */
+ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
+
+ $object = $this->create_attachment_object( $cropped, $attachment_id );
+
+ if ( ! empty( $_POST['create-new-attachment'] ) )
+ unset( $object['ID'] );
+
+ // Update the attachment
+ $attachment_id = $this->insert_attachment( $object, $cropped );
+
+ $url = $object['guid'];
+ $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
+
+ // Cleanup.
+ $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
+ if ( file_exists( $medium ) ) {
+ /**
+ * Filter the path of the file to delete.
+ *
+ * @since 2.1.0
+ *
+ * @param string $medium Path to the file to delete.
+ */
+ @unlink( apply_filters( 'wp_delete_file', $medium ) );
+ }
+
+ if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
+ /** This filter is documented in wp-admin/custom-header.php */
+ @unlink( apply_filters( 'wp_delete_file', $original ) );
+ }
+
+ return $this->finished();
+ }
+
+ /**
+ * Display last step of custom header image page.
+ *
+ * @since 2.1.0
+ */
+ public function finished() {
+ $this->updated = true;
+ $this->step_1();
+ }
+
+ /**
+ * Display the page based on the current step.
+ *
+ * @since 2.1.0
+ */
+ public function admin_page() {
+ if ( ! current_user_can('edit_theme_options') )
+ wp_die(__('You do not have permission to customize headers.'));
+ $step = $this->step();
+ if ( 2 == $step )
+ $this->step_2();
+ elseif ( 3 == $step )
+ $this->step_3();
+ else
+ $this->step_1();
+ }
+
+ /**
+ * Unused since 3.5.0.
+ *
+ * @since 3.4.0
+ */
+ public function attachment_fields_to_edit( $form_fields ) {
+ return $form_fields;
+ }
+
+ /**
+ * Unused since 3.5.0.
+ *
+ * @since 3.4.0
+ */
+ public function filter_upload_tabs( $tabs ) {
+ return $tabs;
+ }
+
+ /**
+ * Choose a header image, selected from existing uploaded and default headers,
+ * or provide an array of uploaded header data (either new, or from media library).
+ *
+ * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
+ * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
+ * among the uploaded images; the key of a default image registered for that theme; and
+ * the key of an image uploaded for that theme (the basename of the URL).
+ * Or an array of arguments: attachment_id, url, width, height. All are required.
+ *
+ * @since 3.4.0
+ */
+ final public function set_header_image( $choice ) {
+ if ( is_array( $choice ) || is_object( $choice ) ) {
+ $choice = (array) $choice;
+ if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
+ return;
+
+ $choice['url'] = esc_url_raw( $choice['url'] );
+
+ $header_image_data = (object) array(
+ 'attachment_id' => $choice['attachment_id'],
+ 'url' => $choice['url'],
+ 'thumbnail_url' => $choice['url'],
+ 'height' => $choice['height'],
+ 'width' => $choice['width'],
+ );
+
+ update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
+ set_theme_mod( 'header_image', $choice['url'] );
+ set_theme_mod( 'header_image_data', $header_image_data );
+ return;
+ }
+
+ if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) {
+ set_theme_mod( 'header_image', $choice );
+ remove_theme_mod( 'header_image_data' );
+ return;
+ }
+
+ $uploaded = get_uploaded_header_images();
+ if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
+ $header_image_data = $uploaded[ $choice ];