-
Notifications
You must be signed in to change notification settings - Fork 7
/
core-image.php
90 lines (73 loc) · 2.36 KB
/
core-image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Enhancements for the core/image block
*
* @package vip-block-data-api
*/
namespace WPCOMVIP\BlockDataApi\ContentParser\BlockAdditions;
defined( 'ABSPATH' ) || die();
/**
* Enhance the core/image block with attributes related to its size.
*/
class CoreImage {
/**
* Initialize the CoreImage class.
*
* @access private
*/
public static function init() {
add_filter( 'vip_block_data_api__sourced_block_result', [ __CLASS__, 'add_image_metadata' ], 5, 2 );
}
/**
* Add size metadata to core/image blocks
*
* @param array $sourced_block Sourced block result.
* @param string $block_name Name of the block.
*
* @access private
*
* @return array Updated sourced block with new metadata information
*/
public static function add_image_metadata( $sourced_block, $block_name ) {
if ( 'core/image' !== $block_name ) {
return $sourced_block;
}
$attachment_id = $sourced_block['attributes']['id'] ?? null;
if ( empty( $attachment_id ) ) {
return $sourced_block;
}
$attachment_metadata = wp_get_attachment_metadata( $attachment_id );
if ( empty( $attachment_metadata ) ) {
return $sourced_block;
}
$size_metadata = self::get_size_metadata( $sourced_block['attributes'], $attachment_metadata );
$sourced_block['attributes'] = array_merge( $sourced_block['attributes'], $size_metadata );
return $sourced_block;
}
/**
* Get the size metadata for an image block
*
* @param array $attributes Attributes of the block.
* @param array $attachment_metadata Metadata of the attachment.
*
* @return array the size metadata
*/
private static function get_size_metadata( $attributes, $attachment_metadata ) {
$size_metadata = [];
if ( isset( $attachment_metadata['width'] ) ) {
$size_metadata['width'] = $attachment_metadata['width'];
}
if ( isset( $attachment_metadata['height'] ) ) {
$size_metadata['height'] = $attachment_metadata['height'];
}
// If the attached image uses a thumbnail size, find the altered width and height.
$size_slug = $attributes['sizeSlug'] ?? null;
if ( null !== $size_slug && isset( $attachment_metadata['sizes'][ $size_slug ] ) ) {
$size = $attachment_metadata['sizes'][ $size_slug ];
$size_metadata['width'] = $size['width'];
$size_metadata['height'] = $size['height'];
}
return $size_metadata;
}
}
CoreImage::init();