3 * Administration API: WP_Site_Icon class
6 * @subpackage Administration
11 * Core class used to implement site icon functionality.
18 * The minimum size of the site icon.
24 public $min_size = 512;
27 * The size to which to crop the image so that we can display it in the UI nicely.
33 public $page_crop = 512;
36 * List of site icon sizes.
42 public $site_icon_sizes = array(
44 * Square, medium sized tiles for IE11+.
46 * See https://msdn.microsoft.com/library/dn455106(v=vs.85).aspx
51 * App icon for Android/Chrome.
53 * @link https://developers.google.com/web/updates/2014/11/Support-for-theme-color-in-Chrome-39-for-Android
54 * @link https://developer.chrome.com/multidevice/android/installtohomescreen
59 * App icons up to iPhone 6 Plus.
61 * See https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
65 // Our regular Favicon.
70 * Registers actions and filters.
75 public function __construct() {
76 add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ) );
77 add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 );
81 * Creates an attachment 'object'.
85 * @param string $cropped Cropped image URL.
86 * @param int $parent_attachment_id Attachment ID of parent image.
87 * @return array Attachment object.
89 public function create_attachment_object( $cropped, $parent_attachment_id ) {
90 $parent = get_post( $parent_attachment_id );
91 $parent_url = wp_get_attachment_url( $parent->ID );
92 $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
94 $size = @getimagesize( $cropped );
95 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
98 'ID' => $parent_attachment_id,
99 'post_title' => basename( $cropped ),
100 'post_content' => $url,
101 'post_mime_type' => $image_type,
103 'context' => 'site-icon'
110 * Inserts an attachment.
115 * @param array $object Attachment object.
116 * @param string $file File path of the attached image.
117 * @return int Attachment ID
119 public function insert_attachment( $object, $file ) {
120 $attachment_id = wp_insert_attachment( $object, $file );
121 $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
124 * Filter the site icon attachment metadata.
128 * @see wp_generate_attachment_metadata()
130 * @param array $metadata Attachment metadata.
132 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
133 wp_update_attachment_metadata( $attachment_id, $metadata );
135 return $attachment_id;
139 * Adds additional sizes to be made when creating the site_icon images.
144 * @param array $sizes List of additional sizes.
145 * @return array Additional image sizes.
147 public function additional_sizes( $sizes = array() ) {
148 $only_crop_sizes = array();
151 * Filter the different dimensions that a site icon is saved in.
155 * @param array $site_icon_sizes Sizes available for the Site Icon.
157 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
159 // Use a natural sort of numbers.
160 natsort( $this->site_icon_sizes );
161 $this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
163 // ensure that we only resize the image into
164 foreach ( $sizes as $name => $size_array ) {
165 if ( isset( $size_array['crop'] ) ) {
166 $only_crop_sizes[ $name ] = $size_array;
170 foreach ( $this->site_icon_sizes as $size ) {
171 if ( $size < $this->min_size ) {
172 $only_crop_sizes[ 'site_icon-' . $size ] = array(
180 return $only_crop_sizes;
184 * Adds Site Icon sizes to the array of image sizes on demand.
189 * @param array $sizes List of image sizes.
190 * @return array List of intermediate image sizes.
192 public function intermediate_image_sizes( $sizes = array() ) {
193 /** This filter is documented in wp-admin/includes/class-wp-site-icon.php */
194 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
195 foreach ( $this->site_icon_sizes as $size ) {
196 $sizes[] = 'site_icon-' . $size;
203 * Deletes the Site Icon when the image file is deleted.
208 * @param int $post_id Attachment ID.
210 public function delete_attachment_data( $post_id ) {
211 $site_icon_id = get_option( 'site_icon' );
213 if ( $site_icon_id && $post_id == $site_icon_id ) {
214 delete_option( 'site_icon' );
219 * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
224 * @param null|array|string $value The value get_metadata() should return a single metadata value, or an
226 * @param int $post_id Post ID.
227 * @param string $meta_key Meta key.
228 * @param string|array $single Meta value, or an array of values.
229 * @return array|null|string The attachment metadata value, array of values, or null.
231 public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
232 if ( $single && '_wp_attachment_backup_sizes' === $meta_key ) {
233 $site_icon_id = get_option( 'site_icon' );
235 if ( $post_id == $site_icon_id ) {
236 add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
245 * @global WP_Site_Icon $wp_site_icon
247 $GLOBALS['wp_site_icon'] = new WP_Site_Icon;