/wp-admin/includes/class-wp-site-icon.php

  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. * @access public 
  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. * @access public 
  31. * @var int 
  32. */ 
  33. public $page_crop = 512; 
  34.  
  35. /** 
  36. * List of site icon sizes. 
  37. * 
  38. * @since 4.3.0 
  39. * @access public 
  40. * @var array 
  41. */ 
  42. public $site_icon_sizes = array( 
  43. /** 
  44. * Square, medium sized tiles for IE11+. 
  45. * 
  46. * See https://msdn.microsoft.com/library/dn455106(v=vs.85).aspx 
  47. */ 
  48. 270,  
  49.  
  50. /** 
  51. * App icon for Android/Chrome. 
  52. * 
  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 
  55. */ 
  56. 192,  
  57.  
  58. /** 
  59. * App icons up to iPhone 6 Plus. 
  60. * 
  61. * See https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html 
  62. */ 
  63. 180,  
  64.  
  65. // Our regular Favicon. 
  66. 32,  
  67. ); 
  68.  
  69. /** 
  70. * Registers actions and filters. 
  71. * 
  72. * @since 4.3.0 
  73. * @access public 
  74. */ 
  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 ); 
  78.  
  79. /** 
  80. * Creates an attachment 'object'. 
  81. * 
  82. * @since 4.3.0 
  83. * 
  84. * @param string $cropped Cropped image URL. 
  85. * @param int $parent_attachment_id Attachment ID of parent image. 
  86. * @return array Attachment object. 
  87. */ 
  88. public function create_attachment_object( $cropped, $parent_attachment_id ) { 
  89. $parent = get_post( $parent_attachment_id ); 
  90. $parent_url = wp_get_attachment_url( $parent->ID ); 
  91. $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); 
  92.  
  93. $size = @getimagesize( $cropped ); 
  94. $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; 
  95.  
  96. $object = array( 
  97. 'ID' => $parent_attachment_id,  
  98. 'post_title' => basename( $cropped ),  
  99. 'post_content' => $url,  
  100. 'post_mime_type' => $image_type,  
  101. 'guid' => $url,  
  102. 'context' => 'site-icon' 
  103. ); 
  104.  
  105. return $object; 
  106.  
  107. /** 
  108. * Inserts an attachment. 
  109. * 
  110. * @since 4.3.0 
  111. * @access public 
  112. * 
  113. * @param array $object Attachment object. 
  114. * @param string $file File path of the attached image. 
  115. * @return int Attachment ID 
  116. */ 
  117. public function insert_attachment( $object, $file ) { 
  118. $attachment_id = wp_insert_attachment( $object, $file ); 
  119. $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); 
  120.  
  121. /** 
  122. * Filters the site icon attachment metadata. 
  123. * 
  124. * @since 4.3.0 
  125. * 
  126. * @see wp_generate_attachment_metadata() 
  127. * 
  128. * @param array $metadata Attachment metadata. 
  129. */ 
  130. $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata ); 
  131. wp_update_attachment_metadata( $attachment_id, $metadata ); 
  132.  
  133. return $attachment_id; 
  134.  
  135. /** 
  136. * Adds additional sizes to be made when creating the site_icon images. 
  137. * 
  138. * @since 4.3.0 
  139. * @access public 
  140. * 
  141. * @param array $sizes List of additional sizes. 
  142. * @return array Additional image sizes. 
  143. */ 
  144. public function additional_sizes( $sizes = array() ) { 
  145. $only_crop_sizes = array(); 
  146.  
  147. /** 
  148. * Filters the different dimensions that a site icon is saved in. 
  149. * 
  150. * @since 4.3.0 
  151. * 
  152. * @param array $site_icon_sizes Sizes available for the Site Icon. 
  153. */ 
  154. $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes ); 
  155.  
  156. // Use a natural sort of numbers. 
  157. natsort( $this->site_icon_sizes ); 
  158. $this->site_icon_sizes = array_reverse( $this->site_icon_sizes ); 
  159.  
  160. // ensure that we only resize the image into 
  161. foreach ( $sizes as $name => $size_array ) { 
  162. if ( isset( $size_array['crop'] ) ) { 
  163. $only_crop_sizes[ $name ] = $size_array; 
  164.  
  165. foreach ( $this->site_icon_sizes as $size ) { 
  166. if ( $size < $this->min_size ) { 
  167. $only_crop_sizes[ 'site_icon-' . $size ] = array( 
  168. 'width ' => $size,  
  169. 'height' => $size,  
  170. 'crop' => true,  
  171. ); 
  172.  
  173. return $only_crop_sizes; 
  174.  
  175. /** 
  176. * Adds Site Icon sizes to the array of image sizes on demand. 
  177. * 
  178. * @since 4.3.0 
  179. * @access public 
  180. * 
  181. * @param array $sizes List of image sizes. 
  182. * @return array List of intermediate image sizes. 
  183. */ 
  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; 
  189.  
  190. return $sizes; 
  191.  
  192. /** 
  193. * Deletes the Site Icon when the image file is deleted. 
  194. * 
  195. * @since 4.3.0 
  196. * @access public 
  197. * 
  198. * @param int $post_id Attachment ID. 
  199. */ 
  200. public function delete_attachment_data( $post_id ) { 
  201. $site_icon_id = get_option( 'site_icon' ); 
  202.  
  203. if ( $site_icon_id && $post_id == $site_icon_id ) { 
  204. delete_option( 'site_icon' ); 
  205.  
  206. /** 
  207. * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon. 
  208. * 
  209. * @since 4.3.0 
  210. * @access public 
  211. * 
  212. * @param null|array|string $value The value get_metadata() should return a single metadata value, or an 
  213. * array of values. 
  214. * @param int $post_id Post ID. 
  215. * @param string $meta_key Meta key. 
  216. * @param string|array $single Meta value, or an array of values. 
  217. * @return array|null|string The attachment metadata value, array of values, or null. 
  218. */ 
  219. public function get_post_metadata( $value, $post_id, $meta_key, $single ) { 
  220. if ( $single && '_wp_attachment_backup_sizes' === $meta_key ) { 
  221. $site_icon_id = get_option( 'site_icon' ); 
  222.  
  223. if ( $post_id == $site_icon_id ) { 
  224. add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) ); 
  225.  
  226. return $value; 
.