]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-site-icon.php
WordPress 4.3.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-site-icon.php
1 <?php
2 /**
3  * Class for implementing site icon functionality.
4  *
5  * @since 4.3.0
6  */
7 class WP_Site_Icon {
8
9         /**
10          * The minimum size of the site icon.
11          *
12          * @since 4.3.0
13          * @access public
14          * @var int
15          */
16         public $min_size  = 512;
17
18         /**
19          * The size to which to crop the image so that we can display it in the UI nicely.
20          *
21          * @since 4.3.0
22          * @access public
23          * @var int
24          */
25         public $page_crop = 512;
26
27         /**
28          * List of site icon sizes.
29          *
30          * @since 4.3.0
31          * @access public
32          * @var array
33          */
34         public $site_icon_sizes = array(
35                 /*
36                  * Square, medium sized tiles for IE11+.
37                  *
38                  * See https://msdn.microsoft.com/library/dn455106(v=vs.85).aspx
39                  */
40                 270,
41
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                 /*
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
57                 // Our regular Favicon.
58                 32,
59         );
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         /**
73          * Creates an attachment 'object'.
74          *
75          * @since 4.3.0
76          *
77          * @param string $cropped              Cropped image URL.
78          * @param int    $parent_attachment_id Attachment ID of parent image.
79          * @return array Attachment object.
80          */
81         public function create_attachment_object( $cropped, $parent_attachment_id ) {
82                 $parent     = get_post( $parent_attachment_id );
83                 $parent_url = $parent->guid;
84                 $url        = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
85
86                 $size       = @getimagesize( $cropped );
87                 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
88
89                 $object = array(
90                         'ID'             => $parent_attachment_id,
91                         'post_title'     => basename( $cropped ),
92                         'post_content'   => $url,
93                         'post_mime_type' => $image_type,
94                         'guid'           => $url,
95                         'context'        => 'site-icon'
96                 );
97
98                 return $object;
99         }
100
101         /**
102          * Inserts an attachment.
103          *
104          * @since 4.3.0
105          * @access public
106          *
107          * @param array  $object Attachment object.
108          * @param string $file   File path of the attached image.
109          * @return int           Attachment ID
110          */
111         public function insert_attachment( $object, $file ) {
112                 $attachment_id = wp_insert_attachment( $object, $file );
113                 $metadata      = wp_generate_attachment_metadata( $attachment_id, $file );
114
115                 /**
116                  * Filter the site icon attachment metadata.
117                  *
118                  * @since 4.3.0
119                  *
120                  * @see wp_generate_attachment_metadata()
121                  *
122                  * @param array $metadata Attachment metadata.
123                  */
124                 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
125                 wp_update_attachment_metadata( $attachment_id, $metadata );
126
127                 return $attachment_id;
128         }
129
130         /**
131          * Adds additional sizes to be made when creating the site_icon images.
132          *
133          * @since 4.3.0
134          * @access public
135          *
136          * @param array $sizes List of additional sizes.
137          * @return array Additional image sizes.
138          */
139         public function additional_sizes( $sizes = array() ) {
140                 $only_crop_sizes = array();
141
142                 /**
143                  * Filter the different dimensions that a site icon is saved in.
144                  *
145                  * @since 4.3.0
146                  *
147                  * @param array $site_icon_sizes Sizes available for the Site Icon.
148                  */
149                 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
150
151                 // Use a natural sort of numbers.
152                 natsort( $this->site_icon_sizes );
153                 $this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
154
155                 // ensure that we only resize the image into
156                 foreach ( $sizes as $name => $size_array ) {
157                         if ( isset( $size_array['crop'] ) ) {
158                                 $only_crop_sizes[ $name ] = $size_array;
159                         }
160                 }
161
162                 foreach ( $this->site_icon_sizes as $size ) {
163                         if ( $size < $this->min_size ) {
164                                 $only_crop_sizes[ 'site_icon-' . $size ] = array(
165                                         'width ' => $size,
166                                         'height' => $size,
167                                         'crop'   => true,
168                                 );
169                         }
170                 }
171
172                 return $only_crop_sizes;
173         }
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
191                 return $sizes;
192         }
193
194         /**
195          * Deletes the Site Icon when the image file is deleted.
196          *
197          * @since 4.3.0
198          * @access public
199          *
200          * @param int $post_id Attachment ID.
201          */
202         public function delete_attachment_data( $post_id ) {
203                 $site_icon_id = get_option( 'site_icon' );
204
205                 if ( $site_icon_id && $post_id == $site_icon_id ) {
206                         delete_option( 'site_icon' );
207                 }
208         }
209
210         /**
211          * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
212          *
213          * @since 4.3.0
214          * @access public
215          *
216          * @param null|array|string $value    The value get_metadata() should return a single metadata value, or an
217          *                                    array of values.
218          * @param int               $post_id  Post ID.
219          * @param string            $meta_key Meta key.
220          * @param string|array      $single   Meta value, or an array of values.
221          * @return array|null|string The attachment metadata value, array of values, or null.
222          */
223         public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
224                 $site_icon_id = get_option( 'site_icon' );
225
226                 if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) {
227                         add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
228                 }
229
230                 return $value;
231         }
232 }
233
234 /**
235  * @global WP_Site_Icon $wp_site_icon
236  */
237 $GLOBALS['wp_site_icon'] = new WP_Site_Icon;