-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBlock_Base.php
More file actions
147 lines (121 loc) · 3.91 KB
/
Block_Base.php
File metadata and controls
147 lines (121 loc) · 3.91 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php declare(strict_types=1);
namespace Tribe\Plugin\Blocks;
use Tribe\Plugin\Assets\Traits\Assets;
abstract class Block_Base {
use Assets;
protected string $assets_path;
protected string $assets_path_uri;
abstract public function get_block_name(): string;
public function __construct( string $assets_folder = 'dist/assets/' ) {
$this->assets_path = trailingslashit( get_stylesheet_directory() ) . $assets_folder;
$this->assets_path_uri = trailingslashit( get_stylesheet_directory_uri() ) . $assets_folder;
}
public function get_block_handle(): string {
return sanitize_title( $this->get_block_name() );
}
public function get_block_path(): string {
return $this->get_block_name();
}
public function get_block_style_handle(): string {
return str_replace( 'core/', 'wp-block-', $this->get_block_name() );
}
/**
* Block styles to be defined in extending class
*/
public function get_block_styles(): array {
return [];
}
/**
* Block dependencies to be defined in extending class
*/
public function get_block_dependencies(): array {
return [];
}
/**
* Allows registration of additional block variations (called "Block Styles")
* Not to be confused with CSS Styles
*/
public function register_core_block_variations(): void {
if ( ! function_exists( 'register_block_style' ) ) {
return;
}
foreach ( $this->get_block_styles() as $name => $label ) {
register_block_style( $this->get_block_name(), [
'name' => $name,
'label' => $label,
] );
}
}
/**
* Enqueue core block styles
*
* Adds the core block styles to both the public site and to the editor.
* On the public site, styles are inlined into the document inside a `<style>` element.
* Styles are added as a `<link>` for both block & site editor regardless of if it is inline or iframed.
* Additionally, the selectors are prefixed with `.editor-styles-wrapper` in the editors.
*/
public function enqueue_core_block_public_styles(): void {
$block = $this->get_block_handle();
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
$src_path = get_theme_file_path( "dist/blocks/$path/style-index.css" );
$src = get_theme_file_uri( "dist/blocks/$path/style-index.css" );
if ( ! file_exists( $src_path ) ) {
return;
}
wp_enqueue_style(
"tribe-$block",
$src,
[],
$args['version'] ?? false,
'all'
);
}
/**
* Enqueue editor-specific styles
*
* These are the editor specific style overrides for the block.
* Styles are added as a `<link>` file for both block & site editor regardless of if it is inline or iframed.
* Additionally, the selectors are prefixed with `.editor-styles-wrapper` in the editors.
*/
public function enqueue_core_block_editor_styles(): void {
if ( ! is_admin() ) {
return;
}
$block = $this->get_block_handle();
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
$src_path = get_theme_file_path( "dist/blocks/$path/editor.css" );
$src = get_theme_file_uri( "dist/blocks/$path/editor.css" );
if ( ! file_exists( $src_path ) ) {
return;
}
wp_enqueue_style(
"tribe-editor-$block",
$src,
[],
$args['version'] ?? false,
'all'
);
}
public function enqueue_core_block_editor_scripts(): void {
if ( ! is_admin() ) {
return;
}
$block = $this->get_block_handle();
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
$src_path = get_theme_file_path( "dist/blocks/$path/editor.js" );
$src = get_theme_file_uri( "dist/blocks/$path/editor.js" );
if ( ! file_exists( $src_path ) ) {
return;
}
wp_enqueue_script(
"tribe-editor-$block",
$src,
$args['dependencies'] ?? [],
$args['version'] ?? false,
true
);
}
}