class-wp-site-icon.php 6.1 KB

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