-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCoreImage.php
More file actions
72 lines (66 loc) · 1.61 KB
/
CoreImage.php
File metadata and controls
72 lines (66 loc) · 1.61 KB
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
<?php
/**
* Core Image Block
*
* @package WPGraphQL\ContentBlocks\Blocks
*/
namespace WPGraphQL\ContentBlocks\Blocks;
use WPGraphQL\ContentBlocks\Registry\Registry;
use WP_Block_Type;
/**
* Class CoreImage
*/
class CoreImage extends Block {
/**
* {@inheritDoc}
*
* @var array|null
*/
protected ?array $additional_block_attributes = [
'cssClassName' => [
'type' => 'string',
'selector' => 'figure',
'source' => 'attribute',
'attribute' => 'class',
],
'src' => [
'type' => 'string',
'selector' => 'img',
'source' => 'attribute',
'attribute' => 'src',
],
];
/**
* Block constructor.
*
* @param \WP_Block_Type $block The Block Type.
* @param \WPGraphQL\ContentBlocks\Registry\Registry $block_registry The instance of the WPGraphQL block registry.
*/
public function __construct( WP_Block_Type $block, Registry $block_registry ) {
parent::__construct( $block, $block_registry );
register_graphql_field(
$this->type_name,
'mediaDetails',
[
'type' => 'MediaDetails',
'description' => sprintf(
// translators: %s is the block type name.
__( 'Media Details of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
$attrs = $block['attrs'];
$id = $attrs['id'] ?? null;
if ( $id ) {
$media_details = wp_get_attachment_metadata( $id );
if ( ! empty( $media_details ) ) {
$media_details['ID'] = $id;
return $media_details;
}
}
return null;
},
]
);
}
}