[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Administration API: WP_Site_Icon class 4 * 5 * @package WordPress 6 * @subpackage Administration 7 * @since 4.3.0 8 */ 9 10 /** 11 * Core class used to implement site icon functionality. 12 * 13 * @since 4.3.0 14 */ 15 class WP_Site_Icon { 16 17 /** 18 * The minimum size of the site icon. 19 * 20 * @since 4.3.0 21 * @var int 22 */ 23 public $min_size = 512; 24 25 /** 26 * The size to which to crop the image so that we can display it in the UI nicely. 27 * 28 * @since 4.3.0 29 * @var int 30 */ 31 public $page_crop = 512; 32 33 /** 34 * List of site icon sizes. 35 * 36 * @since 4.3.0 37 * @var int[] 38 */ 39 public $site_icon_sizes = array( 40 /* 41 * Square, medium sized tiles for IE11+. 42 * 43 * See https://msdn.microsoft.com/library/dn455106(v=vs.85).aspx 44 */ 45 270, 46 47 /* 48 * App icon for Android/Chrome. 49 * 50 * @link https://developers.google.com/web/updates/2014/11/Support-for-theme-color-in-Chrome-39-for-Android 51 * @link https://developer.chrome.com/multidevice/android/installtohomescreen 52 */ 53 192, 54 55 /* 56 * App icons up to iPhone 6 Plus. 57 * 58 * See https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html 59 */ 60 180, 61 62 // Our regular Favicon. 63 32, 64 ); 65 66 /** 67 * Registers actions and filters. 68 * 69 * @since 4.3.0 70 */ 71 public function __construct() { 72 add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ) ); 73 add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 ); 74 } 75 76 /** 77 * Creates an attachment 'object'. 78 * 79 * @since 4.3.0 80 * 81 * @param string $cropped Cropped image URL. 82 * @param int $parent_attachment_id Attachment ID of parent image. 83 * @return array An array with attachment object data. 84 */ 85 public function create_attachment_object( $cropped, $parent_attachment_id ) { 86 $parent = get_post( $parent_attachment_id ); 87 $parent_url = wp_get_attachment_url( $parent->ID ); 88 $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url ); 89 90 $size = wp_getimagesize( $cropped ); 91 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; 92 93 $attachment = array( 94 'ID' => $parent_attachment_id, 95 'post_title' => wp_basename( $cropped ), 96 'post_content' => $url, 97 'post_mime_type' => $image_type, 98 'guid' => $url, 99 'context' => 'site-icon', 100 ); 101 102 return $attachment; 103 } 104 105 /** 106 * Inserts an attachment. 107 * 108 * @since 4.3.0 109 * 110 * @param array $attachment An array with attachment object data. 111 * @param string $file File path of the attached image. 112 * @return int Attachment ID. 113 */ 114 public function insert_attachment( $attachment, $file ) { 115 $attachment_id = wp_insert_attachment( $attachment, $file ); 116 $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); 117 118 /** 119 * Filters the site icon attachment metadata. 120 * 121 * @since 4.3.0 122 * 123 * @see wp_generate_attachment_metadata() 124 * 125 * @param array $metadata Attachment metadata. 126 */ 127 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata ); 128 wp_update_attachment_metadata( $attachment_id, $metadata ); 129 130 return $attachment_id; 131 } 132 133 /** 134 * Adds additional sizes to be made when creating the site icon images. 135 * 136 * @since 4.3.0 137 * 138 * @param array[] $sizes Array of arrays containing information for additional sizes. 139 * @return array[] Array of arrays containing additional image sizes. 140 */ 141 public function additional_sizes( $sizes = array() ) { 142 $only_crop_sizes = array(); 143 144 /** 145 * Filters the different dimensions that a site icon is saved in. 146 * 147 * @since 4.3.0 148 * 149 * @param int[] $site_icon_sizes Array of sizes available for the Site Icon. 150 */ 151 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes ); 152 153 // Use a natural sort of numbers. 154 natsort( $this->site_icon_sizes ); 155 $this->site_icon_sizes = array_reverse( $this->site_icon_sizes ); 156 157 // Ensure that we only resize the image into sizes that allow cropping. 158 foreach ( $sizes as $name => $size_array ) { 159 if ( isset( $size_array['crop'] ) ) { 160 $only_crop_sizes[ $name ] = $size_array; 161 } 162 } 163 164 foreach ( $this->site_icon_sizes as $size ) { 165 if ( $size < $this->min_size ) { 166 $only_crop_sizes[ 'site_icon-' . $size ] = array( 167 'width ' => $size, 168 'height' => $size, 169 'crop' => true, 170 ); 171 } 172 } 173 174 return $only_crop_sizes; 175 } 176 177 /** 178 * Adds Site Icon sizes to the array of image sizes on demand. 179 * 180 * @since 4.3.0 181 * 182 * @param string[] $sizes Array of image size names. 183 * @return string[] Array of image size names. 184 */ 185 public function intermediate_image_sizes( $sizes = array() ) { 186 /** This filter is documented in wp-admin/includes/class-wp-site-icon.php */ 187 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes ); 188 foreach ( $this->site_icon_sizes as $size ) { 189 $sizes[] = 'site_icon-' . $size; 190 } 191 192 return $sizes; 193 } 194 195 /** 196 * Deletes the Site Icon when the image file is deleted. 197 * 198 * @since 4.3.0 199 * 200 * @param int $post_id Attachment ID. 201 */ 202 public function delete_attachment_data( $post_id ) { 203 $site_icon_id = get_option( 'site_icon' ); 204 205 if ( $site_icon_id && $post_id == $site_icon_id ) { 206 delete_option( 'site_icon' ); 207 } 208 } 209 210 /** 211 * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon. 212 * 213 * @since 4.3.0 214 * 215 * @param null|array|string $value The value get_metadata() should return a single metadata value, or an 216 * array of values. 217 * @param int $post_id Post ID. 218 * @param string $meta_key Meta key. 219 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 220 * @return array|null|string The attachment metadata value, array of values, or null. 221 */ 222 public function get_post_metadata( $value, $post_id, $meta_key, $single ) { 223 if ( $single && '_wp_attachment_backup_sizes' === $meta_key ) { 224 $site_icon_id = get_option( 'site_icon' ); 225 226 if ( $post_id == $site_icon_id ) { 227 add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) ); 228 } 229 } 230 231 return $value; 232 } 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 10 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |