WP_Site_Icon

Core class used to implement site icon functionality.

Defined (1)

The class is defined in the following location(s).

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