'site-icon-upload',
) );
}
/**
* Removes the site icon.
*
* @since 4.3.0
* @access public
*/
public function remove_site_icon() {
check_admin_referer( 'remove-site-icon' );
$this->delete_site_icon();
add_settings_error( 'site-icon', 'icon-removed', __( 'Site Icon removed.' ), 'updated' );
}
/**
* Handle uploading a site icon.
*
* Uploading a site_icon is a 3 step process
*
* 1. Select the file to upload
* 2. Crop the file
* 3. Confirmation
*
* @since 4.3.0
* @access public
*/
public function upload_site_icon_page() {
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'select_site_icon';
switch ( $action ) {
case 'select_site_icon':
$this->select_page();
break;
case 'crop_site_icon':
$this->crop_page();
break;
default:
wp_safe_redirect( admin_url( 'options-general.php#site-icon' ) );
exit;
}
}
/**
* Displays the site_icon form to upload the image.
*
* @since 4.3.0
* @access public
*/
public function select_page() {
?>
'site-icon',
'action' => 'crop_site_icon',
), wp_nonce_url( admin_url( 'options-general.php' ), 'crop-site-icon' ) ) );
?>
get_upload_data();
if ( $image_size[0] == $image_size[1] && $image_size[0] == $this->min_size ) {
// No cropping required.
$url = add_query_arg( array(
'attachment_id' => $attachment_id,
'skip-cropping' => true,
'create-new-attachment' => true,
'action' => 'set_site_icon',
), wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' ) );
wp_safe_redirect( $url );
die();
}
}
/**
* Handles the image crop admin view.
*
* @since 4.3.0
* @access public
*/
public function crop_page() {
check_admin_referer( 'crop-site-icon' );
list( $attachment_id, $url, $image_size ) = $this->get_upload_data();
if ( $image_size[0] < $this->min_size ) {
add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) );
// back to step one
$this->select_page();
return;
}
if ( $image_size[1] < $this->min_size ) {
add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in height.' ), $this->min_size ) );
// Back to step one.
$this->select_page();
return;
}
wp_localize_script( 'site-icon-crop', 'wpSiteIconCropData', array(
'init_x' => 0,
'init_y' => 0,
'init_size' => $this->min_size,
'min_size' => $this->min_size,
'width' => $image_size[0],
'height' => $image_size[1],
) );
?>
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' );
}
/**
* Handles uploading the file to be cropped in the second step.
*
* @since 4.3.0
* @access public
*/
public function handle_upload() {
$uploaded_file = $_FILES['site-icon'];
$file_type = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
if ( ! wp_match_mime_types( 'image', $file_type['type'] ) ) {
wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
}
$file = wp_handle_upload( $uploaded_file, array( 'test_form' => false ) );
if ( isset( $file['error'] ) ) {
wp_die( $file['error'], __( 'Image Upload Error' ) );
}
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$filename = basename( $file );
// Construct the object array
$object = array(
'post_title' => $filename,
'post_content' => $url,
'post_mime_type' => $type,
'guid' => $url,
'context' => 'site-icon',
);
// Save the data
$attachment_id = wp_insert_attachment( $object, $file );
return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
}
/**
* Creates an attachment 'object'.
*
* @since 4.3.0
*
* @param string $cropped Cropped image URL.
* @param int $parent_attachment_id Attachment ID of parent image.
* @return array Attachment object.
*/
public function create_attachment_object( $cropped, $parent_attachment_id ) {
$parent = get_post( $parent_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';
$object = array(
'ID' => $parent_attachment_id,
'post_title' => basename( $cropped ),
'post_content' => $url,
'post_mime_type' => $image_type,
'guid' => $url,
'context' => 'site-icon'
);
return $object;
}
/**
* Inserts an attachment.
*
* @since 4.3.0
* @access public
*
* @param array $object Attachment object.
* @param string $file File path of the attached image.
* @return int Attachment ID
*/
public function insert_attachment( $object, $file ) {
$attachment_id = wp_insert_attachment( $object, $file );
$this->update_attachment_metadata( $attachment_id, $file );
return $attachment_id;
}
/**
* Handles updating the metadata of an attachment.
*
* @since 4.3.0
*
* @param int $attachment_id Attachment ID
* @param string $file File path of the attached image.
*/
public function update_attachment_metadata( $attachment_id, $file ) {
$metadata = wp_generate_attachment_metadata( $attachment_id, $file );
/**
* Filter the site icon attachment metadata.
*
* @since 4.3.0
*
* @see wp_generate_attachment_metadata()
*
* @param array $metadata Attachment metadata.
*/
$metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
wp_update_attachment_metadata( $attachment_id, $metadata );
}
/**
* Adds additional sizes to be made when creating the site_icon images.
*
* @since 4.3.0
* @access public
*
* @param array $sizes List of additional sizes.
* @return array Additional image sizes.
*/
public function additional_sizes( $sizes = array() ) {
$only_crop_sizes = array();
/**
* Filter the different dimensions that a site icon is saved in.
*
* @since 4.3.0
*
* @param array $site_icon_sizes Sizes available for the Site Icon.
*/
$this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
// Use a natural sort of numbers.
natsort( $this->site_icon_sizes );
$this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
// ensure that we only resize the image into
foreach ( $sizes as $name => $size_array ) {
if ( isset( $size_array['crop'] ) ) {
$only_crop_sizes[ $name ] = $size_array;
}
}
foreach ( $this->site_icon_sizes as $size ) {
if ( $size < $this->min_size ) {
$only_crop_sizes[ 'site_icon-' . $size ] = array(
'width ' => $size,
'height' => $size,
'crop' => true,
);
}
}
return $only_crop_sizes;
}
/**
* Adds Site Icon sizes to the array of image sizes on demand.
*
* @since 4.3.0
* @access public
*
* @param array $sizes List of image sizes.
* @return array List of intermediate image sizes.
*/
public function intermediate_image_sizes( $sizes = array() ) {
/** This filter is documented in wp-admin/includes/site-icon.php */
$this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
foreach ( $this->site_icon_sizes as $size ) {
$sizes[] = 'site_icon-' . $size;
}
return $sizes;
}
/**
* Deletes all additional image sizes, used for site icons.
*
* @since 4.3.0
* @access public
*
* @return bool Whether the site icon was successfully deleted.
*/
public function delete_site_icon() {
// We add the filter to make sure that we also delete all the added images.
add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
wp_delete_attachment( get_option( 'site_icon' ), true );
remove_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
return delete_option( 'site_icon' );
}
/**
* Deletes the Site Icon when the image file is deleted.
*
* @since 4.3.0
* @access public
*
* @param int $post_id Attachment ID.
*/
public function delete_attachment_data( $post_id ) {
$site_icon_id = get_option( 'site_icon' );
if ( $site_icon_id && $post_id == $site_icon_id ) {
delete_option( 'site_icon' );
}
}
/**
* Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
*
* @since 4.3.0
* @access public
*
* @param null|array|string $value The value get_metadata() should return a single metadata value, or an
* array of values.
* @param int $post_id Post ID.
* @param string $meta_key Meta key.
* @param string|array $single Meta value, or an array of values.
* @return array|null|string The attachment metadata value, array of values, or null.
*/
public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
$site_icon_id = get_option( 'site_icon' );
if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) {
add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
}
return $value;
}
/**
* Gets the data required to work with the uploaded image
*
* @since 4.3.0
* @access private
*
* @return array containing the collected data
*/
private function get_upload_data() {
if ( isset( $_GET['file'] ) ) {
$attachment_id = absint( $_GET['file'] );
$file = get_attached_file( $attachment_id, true );
$url = wp_get_attachment_image_src( $attachment_id, 'full' );
$url = $url[0];
} else {
$upload = $this->handle_upload();
$attachment_id = $upload['attachment_id'];
$file = $upload['file'];
$url = $upload['url'];
}
$image_size = getimagesize( $file );
return array( $attachment_id, $url, $image_size );
}
}
/**
* @global WP_Site_Icon $wp_site_icon
*/
$GLOBALS['wp_site_icon'] = new WP_Site_Icon;