-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeatured-image-binding.php
99 lines (85 loc) · 2.31 KB
/
featured-image-binding.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
91
92
93
94
95
96
97
98
99
<?php
/**
* Plugin Name: Featured Image Lightbox Block
* Description: Example of the block bindings API that creates a new "Image Block" that pulls the featured image from the post and gives it a lightbox effect.
* Version: 1.0.0
* Author: Brian Coords
* Author URI: https://briancoords.com
* Requires at least: 6.6
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Register the custom block bindings source.
*
* @return void
*/
function bc_register_custom_block_bindings() {
register_block_bindings_source(
'bc/bindings',
array(
'label' => __( 'Custom Bindings', 'bc' ),
'get_value_callback' => 'bc_get_custom_source_value',
)
);
}
add_action( 'init', 'bc_register_custom_block_bindings' );
/**
* Get the value for the custom block binding.
*
* @param array $source_args Array of source arguments.
* @param object $block_instance The block instance.
*
* @return mixed|null
*/
function bc_get_custom_source_value( array $source_args, $block_instance ) {
if ( ! isset( $block_instance->context ) || 'featured_image_url' !== $source_args['key'] ) {
return null;
}
$post_id = absint( $block_instance->context['postId'] );
if ( ! $post_id ) {
return null;
}
$featured_image_id = get_post_thumbnail_id( $post_id );
if ( ! $featured_image_id ) {
return null;
}
$featured_image = wp_get_attachment_image_src( $featured_image_id, 'full' );
if ( ! $featured_image ) {
return null;
}
return $featured_image[0];
}
/**
* Register a block variation for the core/image block.
*
* @param array $variations Array of block variations.
* @param object $block_type The block type.
*
* @return array
*/
function bc_register_block_variations( $variations, $block_type ) {
if ( 'core/image' === $block_type->name ) {
$variations[] = array(
'name' => 'bc_feat_image',
'title' => __( 'Featured Image with Lightbox', 'bc' ),
'attributes' => array(
'lightbox' => array(
'enabled' => true,
),
'metadata' => array(
'bindings' => array(
'url' => array(
'source' => 'bc/bindings',
'args' => array(
'key' => 'featured_image_url',
),
),
),
),
),
);
}
return $variations;
}
add_filter( 'get_block_type_variations', 'bc_register_block_variations', 10, 2 );