Wordpress 4.6
[autoinstalls/wordpress.git] / 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         /**
81          * Creates an attachment 'object'.
82          *
83          * @since 4.3.0
84          *
85          * @param string $cropped              Cropped image URL.
86          * @param int    $parent_attachment_id Attachment ID of parent image.
87          * @return array Attachment object.
88          */
89         public function create_attachment_object( $cropped, $parent_attachment_id ) {
90                 $parent     = get_post( $parent_attachment_id );
91                 $parent_url = wp_get_attachment_url( $parent->ID );
92                 $url        = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
93
94                 $size       = @getimagesize( $cropped );
95                 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
96
97                 $object = array(
98                         'ID'             => $parent_attachment_id,
99                         'post_title'     => basename( $cropped ),
100                         'post_content'   => $url,
101                         'post_mime_type' => $image_type,
102                         'guid'           => $url,
103                         'context'        => 'site-icon'
104                 );
105
106                 return $object;
107         }
108
109         /**
110          * Inserts an attachment.
111          *
112          * @since 4.3.0
113          * @access public
114          *
115          * @param array  $object Attachment object.
116          * @param string $file   File path of the attached image.
117          * @return int           Attachment ID
118          */
119         public function insert_attachment( $object, $file ) {
120                 $attachment_id = wp_insert_attachment( $object, $file );
121                 $metadata      = wp_generate_attachment_metadata( $attachment_id, $file );
122
123                 /**
124                  * Filters the site icon attachment metadata.
125                  *
126                  * @since 4.3.0
127                  *
128                  * @see wp_generate_attachment_metadata()
129                  *
130                  * @param array $metadata Attachment metadata.
131                  */
132                 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
133                 wp_update_attachment_metadata( $attachment_id, $metadata );
134
135                 return $attachment_id;
136         }
137
138         /**
139          * Adds additional sizes to be made when creating the site_icon images.
140          *
141          * @since 4.3.0
142          * @access public
143          *
144          * @param array $sizes List of additional sizes.
145          * @return array Additional image sizes.
146          */
147         public function additional_sizes( $sizes = array() ) {
148                 $only_crop_sizes = array();
149
150                 /**
151                  * Filters the different dimensions that a site icon is saved in.
152                  *
153                  * @since 4.3.0
154                  *
155                  * @param array $site_icon_sizes Sizes available for the Site Icon.
156                  */
157                 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
158
159                 // Use a natural sort of numbers.
160                 natsort( $this->site_icon_sizes );
161                 $this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
162
163                 // ensure that we only resize the image into
164                 foreach ( $sizes as $name => $size_array ) {
165                         if ( isset( $size_array['crop'] ) ) {
166                                 $only_crop_sizes[ $name ] = $size_array;
167                         }
168                 }
169
170                 foreach ( $this->site_icon_sizes as $size ) {
171                         if ( $size < $this->min_size ) {
172                                 $only_crop_sizes[ 'site_icon-' . $size ] = array(
173                                         'width ' => $size,
174                                         'height' => $size,
175                                         'crop'   => true,
176                                 );
177                         }
178                 }
179
180                 return $only_crop_sizes;
181         }
182
183         /**
184          * Adds Site Icon sizes to the array of image sizes on demand.
185          *
186          * @since 4.3.0
187          * @access public
188          *
189          * @param array $sizes List of image sizes.
190          * @return array List of intermediate image sizes.
191          */
192         public function intermediate_image_sizes( $sizes = array() ) {
193                 /** This filter is documented in wp-admin/includes/class-wp-site-icon.php */
194                 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
195                 foreach ( $this->site_icon_sizes as $size ) {
196                         $sizes[] = 'site_icon-' . $size;
197                 }
198
199                 return $sizes;
200         }
201
202         /**
203          * Deletes the Site Icon when the image file is deleted.
204          *
205          * @since 4.3.0
206          * @access public
207          *
208          * @param int $post_id Attachment ID.
209          */
210         public function delete_attachment_data( $post_id ) {
211                 $site_icon_id = get_option( 'site_icon' );
212
213                 if ( $site_icon_id && $post_id == $site_icon_id ) {
214                         delete_option( 'site_icon' );
215                 }
216         }
217
218         /**
219          * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
220          *
221          * @since 4.3.0
222          * @access public
223          *
224          * @param null|array|string $value    The value get_metadata() should return a single metadata value, or an
225          *                                    array of values.
226          * @param int               $post_id  Post ID.
227          * @param string            $meta_key Meta key.
228          * @param string|array      $single   Meta value, or an array of values.
229          * @return array|null|string The attachment metadata value, array of values, or null.
230          */
231         public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
232                 if ( $single && '_wp_attachment_backup_sizes' === $meta_key ) {
233                         $site_icon_id = get_option( 'site_icon' );
234
235                         if ( $post_id == $site_icon_id ) {
236                                 add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
237                         }
238                 }
239
240                 return $value;
241         }
242 }
243
244 /**
245  * @global WP_Site_Icon $wp_site_icon
246  */
247 $GLOBALS['wp_site_icon'] = new WP_Site_Icon;