[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-wp-site-icon.php (source)

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


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref