class-wp-site-icon.php 6.1 KB

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