-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsive-core-group-block.php
126 lines (108 loc) · 3.75 KB
/
responsive-core-group-block.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace SethRubenstein;
use WP_HTML_Tag_Processor;
/**
* Plugin Name: Responsive Core Group Block
* Description: Responsive controls for the core/group block.
* Version: 1.0.0
* Requires at least: 6.3
* Requires PHP: 8.1
* Author: Seth Rubenstein
* Author URI: https://sethrubenstein.info
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: sethrubenstein-responsive-core-group-block
*
*/
class Responsive_Core_Group_Block {
public static $block_name = 'core/group';
public $block_json;
public $editor_script_handle;
public $style_handle;
public function __construct() {
$block_json_file = __DIR__ . '/build/block.json';
$this->block_json = \wp_json_file_decode( $block_json_file, array( 'associative' => true ) );
$this->block_json['file'] = wp_normalize_path( realpath( $block_json_file ) );
add_action('init', array($this, 'init_assets'));
add_action('enqueue_block_editor_assets', array($this, 'enqueue_editor_script'));
add_action('enqueue_block_assets', array($this, 'enqueue_style'));
add_filter( 'block_type_metadata', array($this, 'add_responsive_attributes'), 100, 1 );
add_filter( 'render_block', array($this, 'render_responsive_group_block'), 100, 2 );
}
/**
* @hook init
* @return void
*/
public function init_assets() {
$this->editor_script_handle = register_block_script_handle( $this->block_json, 'editorScript' );
$this->style_handle = register_block_style_handle( $this->block_json, 'style' );
}
/**
* @hook enqueue_block_editor_assets
* @return void
*/
public function enqueue_editor_script() {
wp_enqueue_script( $this->editor_script_handle );
}
/**
* @hook enqueue_block_assets
* @return void
*/
public function enqueue_style() {
wp_enqueue_style( $this->style_handle );
}
/**
* Register additional attributes for the core-group block.
* @hook block_type_metadata 100, 1
* @param mixed $metadata
* @return mixed
*/
public function add_responsive_attributes( $metadata ) {
if ( self::$block_name !== $metadata['name'] ) {
return $metadata;
}
if ( ! array_key_exists( 'responsiveContainerQuery', $metadata['attributes'] ) ) {
$metadata['attributes']['responsiveContainerQuery'] = array(
'type' => 'object',
'default' => array(
'hideOnDesktop' => false,
'hideOnTablet' => false,
'hideOnMobile' => false,
),
);
}
return $metadata;
}
/**
* @hook render_block 100, 2
* @param mixed $block_content
* @param mixed $block
* @return mixed
*/
public function render_responsive_group_block( $block_content, $block ) {
if ( self::$block_name !== $block['blockName'] || is_admin() ) {
return $block_content;
}
$responsive_options = array_key_exists('responsiveContainerQuery', $block['attrs']) ? $block['attrs']['responsiveContainerQuery'] : array();
if ( empty( $responsive_options ) ) {
return $block_content;
}
$hide_on_desktop = array_key_exists('hideOnDesktop', $responsive_options) ? $responsive_options['hideOnDesktop'] : false;
$hide_on_tablet = array_key_exists('hideOnTablet', $responsive_options) ? $responsive_options['hideOnTablet'] : false;
$hide_on_mobile = array_key_exists('hideOnMobile', $responsive_options) ? $responsive_options['hideOnMobile'] : false;
$w = new WP_HTML_Tag_Processor( $block_content );
if ( $w->next_tag() ) {
if ( $hide_on_desktop ) {
$w->set_attribute( 'data-hide-on-desktop', 'true' );
}
if ( $hide_on_tablet ) {
$w->set_attribute( 'data-hide-on-tablet', 'true' );
}
if ( $hide_on_mobile ) {
$w->set_attribute( 'data-hide-on-mobile', 'true' );
}
}
return $w->get_updated_html();
}
}
$Responsive_Core_Group_Block = new Responsive_Core_Group_Block();