/** * Saves a new Site Icon. * * @since 4.3.0 */ public function set_site_icon() { check_admin_referer('set-site-icon'); $attachment_id = absint($_REQUEST['attachment_id']); $create_new_attachement = !empty($_REQUEST['create-new-attachment']); /* * If the current attachment as been set as site icon don't delete it. */ if (get_option('site_icon') == $attachment_id) { // Get the file path. $image_url = get_attached_file($attachment_id); // Update meta data and possibly regenerate intermediate sizes. add_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes')); $this->update_attachment_metadata($attachment_id, $image_url); remove_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes')); } else { // Delete any existing site icon images. $this->delete_site_icon(); if (empty($_REQUEST['skip-cropping'])) { $cropped = wp_crop_image($attachment_id, $_REQUEST['crop-x'], $_REQUEST['crop-y'], $_REQUEST['crop-w'], $_REQUEST['crop-h'], $this->min_size, $this->min_size); } elseif ($create_new_attachement) { $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')); } $object = $this->create_attachment_object($cropped, $attachment_id); if ($create_new_attachement) { unset($object['ID']); } // Update the attachment. add_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes')); $attachment_id = $this->insert_attachment($object, $cropped); remove_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes')); // Save the site_icon data into option update_option('site_icon', $attachment_id); } add_settings_error('site-icon', 'icon-updated', __('Site Icon updated.'), 'updated'); }
/** * Display third step of custom header image page. * * @since 2.1.0 */ function step_3() { check_admin_referer('custom-header-crop-image'); if (!current_theme_supports('custom-header', 'uploads')) { wp_die(__('Cheatin’ uh?')); } if (!empty($_POST['skip-cropping']) && !(current_theme_supports('custom-header', 'flex-height') || current_theme_supports('custom-header', 'flex-width'))) { wp_die(__('Cheatin’ uh?')); } if ($_POST['oitar'] > 1) { $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; $_POST['width'] = $_POST['width'] * $_POST['oitar']; $_POST['height'] = $_POST['height'] * $_POST['oitar']; } $attachment_id = absint($_POST['attachment_id']); $original = get_attached_file($attachment_id); $max_width = 0; // For flex, limit size of image displayed to 1500px unless theme says otherwise if (current_theme_supports('custom-header', 'flex-width')) { $max_width = 1500; } if (current_theme_supports('custom-header', 'max-width')) { $max_width = max($max_width, get_theme_support('custom-header', 'max-width')); } $max_width = max($max_width, get_theme_support('custom-header', 'width')); if (current_theme_supports('custom-header', 'flex-height') && !current_theme_supports('custom-header', 'flex-width') || $_POST['width'] > $max_width) { $dst_height = absint($_POST['height'] * ($max_width / $_POST['width'])); } elseif (current_theme_supports('custom-header', 'flex-height') && current_theme_supports('custom-header', 'flex-width')) { $dst_height = absint($_POST['height']); } else { $dst_height = get_theme_support('custom-header', 'height'); } if (current_theme_supports('custom-header', 'flex-width') && !current_theme_supports('custom-header', 'flex-height') || $_POST['width'] > $max_width) { $dst_width = absint($_POST['width'] * ($max_width / $_POST['width'])); } elseif (current_theme_supports('custom-header', 'flex-width') && current_theme_supports('custom-header', 'flex-height')) { $dst_width = absint($_POST['width']); } else { $dst_width = get_theme_support('custom-header', 'width'); } if (empty($_POST['skip-cropping'])) { $cropped = wp_crop_image($attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_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')); } $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication $parent = get_post($attachment_id); $parent_url = $parent->guid; $url = str_replace(basename($parent_url), basename($cropped), $parent_url); $size = @getimagesize($cropped); $image_type = $size ? $size['mime'] : 'image/jpeg'; // Construct the object array $object = array('ID' => $attachment_id, 'post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => $image_type, 'guid' => $url, 'context' => 'custom-header'); if (!empty($_POST['create-new-attachment'])) { unset($object['ID']); } // Update the attachment $attachment_id = wp_insert_attachment($object, $cropped); wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $cropped)); $width = $dst_width; $height = $dst_height; $this->set_header_image(compact('url', 'attachment_id', 'width', 'height')); // cleanup $medium = str_replace(basename($original), 'midsize-' . basename($original), $original); if (file_exists($medium)) { @unlink(apply_filters('wp_delete_file', $medium)); } if (empty($_POST['create-new-attachment']) && empty($_POST['skip-cropping'])) { @unlink(apply_filters('wp_delete_file', $original)); } return $this->finished(); }
/** * Display third step of custom header image page. * * @since 2.1.0 */ public function step_3() { check_admin_referer('custom-header-crop-image'); if (!current_theme_supports('custom-header', 'uploads')) { wp_die(__('Cheatin’ uh?'), 403); } if (!empty($_POST['skip-cropping']) && !(current_theme_supports('custom-header', 'flex-height') || current_theme_supports('custom-header', 'flex-width'))) { wp_die(__('Cheatin’ uh?'), 403); } if ($_POST['oitar'] > 1) { $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; $_POST['width'] = $_POST['width'] * $_POST['oitar']; $_POST['height'] = $_POST['height'] * $_POST['oitar']; } $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)) { wp_delete_file($medium); } if (empty($_POST['create-new-attachment']) && empty($_POST['skip-cropping'])) { wp_delete_file($original); } return $this->finished(); }
/** * Saves a new Site Icon. * * @since 4.3.0 */ public function set_site_icon() { check_admin_referer('set-site-icon'); // Delete any existing site icon images. $this->delete_site_icon(); $attachment_id = absint($_POST['attachment_id']); // TODO if (empty($_POST['skip-cropping'])) { $crop_ratio = (double) $_POST['crop_ratio']; $crop_data = $this->convert_coordinates_from_resized_to_full($_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio); $cropped = wp_crop_image($attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->min_size, $this->min_size); } 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')); } $object = $this->create_attachment_object($cropped, $attachment_id); if (!empty($_POST['create-new-attachment'])) { unset($object['ID']); } // Update the attachment add_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes')); $attachment_id = $this->insert_attachment($object, $cropped); remove_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes')); // Save the site_icon data into option update_option('site_icon', $attachment_id); add_settings_error('site-icon', 'icon-updated', __('Site Icon updated.'), 'updated'); }