class-wp-site-icon.php 6.0 KB

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