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