PluginProbe
Gutenberg / 23.3.0
Gutenberg v23.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-6.9 / post-data-block-bindings.php

post-data-block-bindings.php in Gutenberg 23.3.0, at lib/compat/wordpress-6.9/post-data-block-bindings.php

104 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Data source for the block bindings.
4 *
5 * @since 6.9.0
6 * @package gutenberg
7 * @subpackage Block Bindings
8 */
9
10 /**
11 * Gets value for Post Data source.
12 *
13 * @since 6.9.0
14 * @access private
15 *
16 * @param array $source_args Array containing source arguments used to look up the override value.
17 * Example: array( "field" => "foo" ).
18 * @param WP_Block $block_instance The block instance.
19 * @return mixed The value computed for the source.
20 */
21 function gutenberg_block_bindings_post_data_get_value( array $source_args, $block_instance ) {
22 if ( empty( $source_args['field'] ) ) {
23 // Backward compatibility for when the source argument was called `key` in Gutenberg plugin.
24 if ( empty( $source_args['key'] ) ) {
25 return null;
26 }
27 $field = $source_args['key'];
28 } else {
29 $field = $source_args['field'];
30 }
31
32 /*
33 * BACKWARDS COMPATIBILITY: Hardcoded exception for navigation blocks.
34 * Required for WordPress 6.9+ navigation blocks. DO NOT REMOVE.
35 */
36 $block_name = $block_instance->name ?? '';
37 $is_navigation_block = in_array(
38 $block_name,
39 array( 'core/navigation-link', 'core/navigation-submenu' ),
40 true
41 );
42
43 if ( $is_navigation_block ) {
44 // Navigation blocks: read from block attributes
45 $post_id = $block_instance->attributes['id'] ?? null;
46 } else {
47 // All other blocks: use context
48 $post_id = $block_instance->context['postId'] ?? null;
49 }
50
51 // If we don't have an entity ID, bail early.
52 if ( empty( $post_id ) ) {
53 return null;
54 }
55
56 // If a post isn't public, we need to prevent unauthorized users from accessing the post data.
57 $post = get_post( $post_id );
58 if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) {
59 return null;
60 }
61
62 if ( 'date' === $field ) {
63 return esc_attr( get_the_date( 'c', $post_id ) );
64 }
65
66 if ( 'modified' === $field ) {
67 // Only return the modified date if it is later than the publishing date.
68 if ( get_the_modified_date( 'U', $post_id ) > get_the_date( 'U', $post_id ) ) {
69 return esc_attr( get_the_modified_date( 'c', $post_id ) );
70 } else {
71 return '';
72 }
73 }
74
75 if ( 'link' === $field ) {
76 $permalink = get_permalink( $post_id );
77 return false === $permalink ? null : esc_url( $permalink );
78 }
79 }
80
81 /**
82 * Registers Post Data source in the block bindings registry.
83 *
84 * @since 6.9.0
85 * @access private
86 */
87 function gutenberg_register_block_bindings_post_data_source() {
88 if ( get_block_bindings_source( 'core/post-data' ) ) {
89 // The source is already registered.
90 return;
91 }
92
93 register_block_bindings_source(
94 'core/post-data',
95 array(
96 'label' => _x( 'Post Data', 'block bindings source' ),
97 'get_value_callback' => 'gutenberg_block_bindings_post_data_get_value',
98 'uses_context' => array( 'postId', 'postType' ),
99 )
100 );
101 }
102
103 add_action( 'init', 'gutenberg_register_block_bindings_post_data_source' );
104