| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/query` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Modifies the static `core/query` block on the server. |
| 10 |
* |
| 11 |
* @since X.X.X |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* @param string $content Block default content. |
| 15 |
* @param string $block Block instance. |
| 16 |
* |
| 17 |
* @return string Returns the modified output of the query block. |
| 18 |
*/ |
| 19 |
function gutenberg_render_block_core_query( $attributes, $content, $block ) { |
| 20 |
if ( $attributes['enhancedPagination'] ) { |
| 21 |
$p = new WP_HTML_Tag_Processor( $content ); |
| 22 |
if ( $p->next_tag() ) { |
| 23 |
// Add the necessary directives. |
| 24 |
$p->set_attribute( 'data-wp-interactive', true ); |
| 25 |
$p->set_attribute( 'data-wp-navigation-id', 'query-' . $attributes['queryId'] ); |
| 26 |
$p->set_attribute( |
| 27 |
'data-wp-context', |
| 28 |
wp_json_encode( array( 'core' => array( 'query' => (object) array() ) ) ) |
| 29 |
); |
| 30 |
$content = $p->get_updated_html(); |
| 31 |
|
| 32 |
// Mark the block as interactive. |
| 33 |
$block->block_type->supports['interactivity'] = true; |
| 34 |
|
| 35 |
// Add a div to announce messages using `aria-live`. |
| 36 |
$last_div_position = strripos( $content, '</div>' ); |
| 37 |
$content = substr_replace( |
| 38 |
$content, |
| 39 |
'<div |
| 40 |
class="wp-block-query__enhanced-pagination-navigation-announce" |
| 41 |
aria-live="polite" |
| 42 |
data-wp-text="context.core.query.message" |
| 43 |
></div> |
| 44 |
<div |
| 45 |
class="wp-block-query__enhanced-pagination-animation" |
| 46 |
data-wp-class--start-animation="selectors.core.query.startAnimation" |
| 47 |
data-wp-class--finish-animation="selectors.core.query.finishAnimation" |
| 48 |
></div>', |
| 49 |
$last_div_position, |
| 50 |
0 |
| 51 |
); |
| 52 |
|
| 53 |
// Use state to send translated strings. |
| 54 |
wp_store( |
| 55 |
array( |
| 56 |
'state' => array( |
| 57 |
'core' => array( |
| 58 |
'query' => array( |
| 59 |
'loadingText' => __( 'Loading page, please wait.' ), |
| 60 |
'loadedText' => __( 'Page Loaded.' ), |
| 61 |
), |
| 62 |
), |
| 63 |
), |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
$view_asset = 'wp-block-query-view'; |
| 70 |
if ( ! wp_script_is( $view_asset ) ) { |
| 71 |
$script_handles = $block->block_type->view_script_handles; |
| 72 |
// If the script is not needed, and it is still in the `view_script_handles`, remove it. |
| 73 |
if ( ! $attributes['enhancedPagination'] && in_array( $view_asset, $script_handles, true ) ) { |
| 74 |
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_asset ) ); |
| 75 |
} |
| 76 |
// If the script is needed, but it was previously removed, add it again. |
| 77 |
if ( $attributes['enhancedPagination'] && ! in_array( $view_asset, $script_handles, true ) ) { |
| 78 |
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_asset ) ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$style_asset = 'wp-block-query'; |
| 83 |
if ( ! wp_style_is( $style_asset ) ) { |
| 84 |
$style_handles = $block->block_type->style_handles; |
| 85 |
// If the styles are not needed, and they are still in the `style_handles`, remove them. |
| 86 |
if ( ! $attributes['enhancedPagination'] && in_array( $style_asset, $style_handles, true ) ) { |
| 87 |
$block->block_type->style_handles = array_diff( $style_handles, array( $style_asset ) ); |
| 88 |
} |
| 89 |
// If the styles are needed, but they were previously removed, add them again. |
| 90 |
if ( $attributes['enhancedPagination'] && ! in_array( $style_asset, $style_handles, true ) ) { |
| 91 |
$block->block_type->style_handles = array_merge( $style_handles, array( $style_asset ) ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $content; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Registers the `core/query` block on the server. |
| 100 |
*/ |
| 101 |
function gutenberg_register_block_core_query() { |
| 102 |
register_block_type_from_metadata( |
| 103 |
__DIR__ . '/query', |
| 104 |
array( |
| 105 |
'render_callback' => 'gutenberg_render_block_core_query', |
| 106 |
) |
| 107 |
); |
| 108 |
} |
| 109 |
add_action( 'init', 'gutenberg_register_block_core_query', 20 ); |
| 110 |
|