3 * Class for implementing site icon functionality.
10 * The minimum size of the site icon.
16 public $min_size = 512;
19 * The size to which to crop the image so that we can display it in the UI nicely.
25 public $page_crop = 512;
28 * List of site icon sizes.
34 public $site_icon_sizes = array(
36 * Square, medium sized tiles for IE11+.
38 * See https://msdn.microsoft.com/library/dn455106(v=vs.85).aspx
43 * App icon for Android/Chrome.
45 * @link https://developers.google.com/web/updates/2014/11/Support-for-theme-color-in-Chrome-39-for-Android
46 * @link https://developer.chrome.com/multidevice/android/installtohomescreen
51 * App icons up to iPhone 6 Plus.
53 * See https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
57 // Our regular Favicon.
62 * Registers actions and filters.
67 public function __construct() {
68 add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ) );
69 add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 );
73 * Creates an attachment 'object'.
77 * @param string $cropped Cropped image URL.
78 * @param int $parent_attachment_id Attachment ID of parent image.
79 * @return array Attachment object.
81 public function create_attachment_object( $cropped, $parent_attachment_id ) {
82 $parent = get_post( $parent_attachment_id );
83 $parent_url = $parent->guid;
84 $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
86 $size = @getimagesize( $cropped );
87 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
90 'ID' => $parent_attachment_id,
91 'post_title' => basename( $cropped ),
92 'post_content' => $url,
93 'post_mime_type' => $image_type,
95 'context' => 'site-icon'
102 * Inserts an attachment.
107 * @param array $object Attachment object.
108 * @param string $file File path of the attached image.
109 * @return int Attachment ID
111 public function insert_attachment( $object, $file ) {
112 $attachment_id = wp_insert_attachment( $object, $file );
113 $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
116 * Filter the site icon attachment metadata.
120 * @see wp_generate_attachment_metadata()
122 * @param array $metadata Attachment metadata.
124 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
125 wp_update_attachment_metadata( $attachment_id, $metadata );
127 return $attachment_id;
131 * Adds additional sizes to be made when creating the site_icon images.
136 * @param array $sizes List of additional sizes.
137 * @return array Additional image sizes.
139 public function additional_sizes( $sizes = array() ) {
140 $only_crop_sizes = array();
143 * Filter the different dimensions that a site icon is saved in.
147 * @param array $site_icon_sizes Sizes available for the Site Icon.
149 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
151 // Use a natural sort of numbers.
152 natsort( $this->site_icon_sizes );
153 $this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
155 // ensure that we only resize the image into
156 foreach ( $sizes as $name => $size_array ) {
157 if ( isset( $size_array['crop'] ) ) {
158 $only_crop_sizes[ $name ] = $size_array;
162 foreach ( $this->site_icon_sizes as $size ) {
163 if ( $size < $this->min_size ) {
164 $only_crop_sizes[ 'site_icon-' . $size ] = array(
172 return $only_crop_sizes;
176 * Adds Site Icon sizes to the array of image sizes on demand.
181 * @param array $sizes List of image sizes.
182 * @return array List of intermediate image sizes.
184 public function intermediate_image_sizes( $sizes = array() ) {
185 /** This filter is documented in wp-admin/includes/class-wp-site-icon.php */
186 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
187 foreach ( $this->site_icon_sizes as $size ) {
188 $sizes[] = 'site_icon-' . $size;
195 * Deletes the Site Icon when the image file is deleted.
200 * @param int $post_id Attachment ID.
202 public function delete_attachment_data( $post_id ) {
203 $site_icon_id = get_option( 'site_icon' );
205 if ( $site_icon_id && $post_id == $site_icon_id ) {
206 delete_option( 'site_icon' );
211 * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
216 * @param null|array|string $value The value get_metadata() should return a single metadata value, or an
218 * @param int $post_id Post ID.
219 * @param string $meta_key Meta key.
220 * @param string|array $single Meta value, or an array of values.
221 * @return array|null|string The attachment metadata value, array of values, or null.
223 public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
224 $site_icon_id = get_option( 'site_icon' );
226 if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) {
227 add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
235 * @global WP_Site_Icon $wp_site_icon
237 $GLOBALS['wp_site_icon'] = new WP_Site_Icon;