[ 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          // High-density (2x) site icon for the admin bar and post embeds.
  64          64,
  65  
  66          // Our regular Favicon.
  67          32,
  68      );
  69  
  70      /**
  71       * Registers actions and filters.
  72       *
  73       * @since 4.3.0
  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       * @deprecated 6.5.0
  85       *
  86       * @param string $cropped              Cropped image URL.
  87       * @param int    $parent_attachment_id Attachment ID of parent image.
  88       * @return array An array with attachment object data.
  89       */
  90  	public function create_attachment_object( $cropped, $parent_attachment_id ) {
  91          _deprecated_function( __METHOD__, '6.5.0', 'wp_copy_parent_attachment_properties()' );
  92  
  93          $parent     = get_post( $parent_attachment_id );
  94          $parent_url = wp_get_attachment_url( $parent->ID );
  95          $url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
  96  
  97          $size       = wp_getimagesize( $cropped );
  98          $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  99  
 100          $attachment = array(
 101              'ID'             => $parent_attachment_id,
 102              'post_title'     => wp_basename( $cropped ),
 103              'post_content'   => $url,
 104              'post_mime_type' => $image_type,
 105              'guid'           => $url,
 106              'context'        => 'site-icon',
 107          );
 108  
 109          return $attachment;
 110      }
 111  
 112      /**
 113       * Inserts an attachment.
 114       *
 115       * @since 4.3.0
 116       *
 117       * @param array  $attachment An array with attachment object data.
 118       * @param string $file       File path of the attached image.
 119       * @return int               Attachment ID.
 120       */
 121  	public function insert_attachment( $attachment, $file ) {
 122          $attachment_id = wp_insert_attachment( $attachment, $file );
 123          $metadata      = wp_generate_attachment_metadata( $attachment_id, $file );
 124  
 125          /**
 126           * Filters the site icon attachment metadata.
 127           *
 128           * @since 4.3.0
 129           *
 130           * @see wp_generate_attachment_metadata()
 131           *
 132           * @param array $metadata Attachment metadata.
 133           */
 134          $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
 135          wp_update_attachment_metadata( $attachment_id, $metadata );
 136  
 137          return $attachment_id;
 138      }
 139  
 140      /**
 141       * Adds additional sizes to be made when creating the site icon images.
 142       *
 143       * @since 4.3.0
 144       *
 145       * @param array[] $sizes Array of arrays containing information for additional sizes.
 146       * @return array[] Array of arrays containing additional image sizes.
 147       */
 148  	public function additional_sizes( $sizes = array() ) {
 149          $only_crop_sizes = array();
 150  
 151          /**
 152           * Filters the different dimensions that a site icon is saved in.
 153           *
 154           * @since 4.3.0
 155           *
 156           * @param int[] $site_icon_sizes Array of sizes available for the Site Icon.
 157           */
 158          $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
 159  
 160          // Use a natural sort of numbers.
 161          natsort( $this->site_icon_sizes );
 162          $this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
 163  
 164          // Ensure that we only resize the image into sizes that allow cropping.
 165          foreach ( $sizes as $name => $size_array ) {
 166              if ( isset( $size_array['crop'] ) ) {
 167                  $only_crop_sizes[ $name ] = $size_array;
 168              }
 169          }
 170  
 171          foreach ( $this->site_icon_sizes as $size ) {
 172              if ( $size < $this->min_size ) {
 173                  $only_crop_sizes[ 'site_icon-' . $size ] = array(
 174                      'width ' => $size,
 175                      'height' => $size,
 176                      'crop'   => true,
 177                  );
 178              }
 179          }
 180  
 181          return $only_crop_sizes;
 182      }
 183  
 184      /**
 185       * Adds Site Icon sizes to the array of image sizes on demand.
 186       *
 187       * @since 4.3.0
 188       *
 189       * @param string[] $sizes Array of image size names.
 190       * @return string[] Array of image size names.
 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       *
 207       * @param int $post_id Attachment ID.
 208       */
 209  	public function delete_attachment_data( $post_id ) {
 210          $site_icon_id = (int) get_option( 'site_icon' );
 211  
 212          if ( $site_icon_id && $post_id === $site_icon_id ) {
 213              delete_option( 'site_icon' );
 214          }
 215      }
 216  
 217      /**
 218       * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
 219       *
 220       * @since 4.3.0
 221       *
 222       * @param null|array|string $value    The value get_metadata() should return a single metadata value, or an
 223       *                                    array of values.
 224       * @param int               $post_id  Post ID.
 225       * @param string            $meta_key Meta key.
 226       * @param bool              $single   Whether to return only the first value of the specified `$meta_key`.
 227       * @return array|null|string The attachment metadata value, array of values, or null.
 228       */
 229  	public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
 230          if ( $single && '_wp_attachment_backup_sizes' === $meta_key ) {
 231              $site_icon_id = (int) get_option( 'site_icon' );
 232  
 233              if ( $post_id === $site_icon_id ) {
 234                  add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
 235              }
 236          }
 237  
 238          return $value;
 239      }
 240  }


Generated : Thu Jul 30 08:20:17 2026 Cross-referenced by PHPXref