1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FooGallery Blocks Initializer |
4
|
|
|
* |
5
|
|
|
* Enqueue CSS/JS of all the FooGallery blocks. |
6
|
|
|
* |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
* @package CGB |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
if ( ! class_exists( 'FooGallery_Blocks' ) ) { |
12
|
|
|
class FooGallery_Blocks { |
|
|
|
|
13
|
|
|
|
14
|
|
|
function __construct() { |
|
|
|
|
15
|
|
|
//Frontend block assets. |
16
|
|
|
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); |
17
|
|
|
|
18
|
|
|
//Backend editor block assets. |
19
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
20
|
|
|
|
21
|
|
|
add_action( 'init', array( $this, 'php_block_init' ) ); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Enqueue Gutenberg block assets for backend editor. |
26
|
|
|
* |
27
|
|
|
* `wp-blocks`: includes block type registration and related functions. |
28
|
|
|
* `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks. |
29
|
|
|
* `wp-i18n`: To internationalize the block's text. |
30
|
|
|
* |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
*/ |
33
|
|
|
function enqueue_block_editor_assets() { |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ( !apply_filters( 'foogallery_gutenberg_enabled', true ) ) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
//enqueue foogallery dependencies |
40
|
|
|
wp_enqueue_script( 'masonry' ); |
41
|
|
|
foogallery_enqueue_core_gallery_template_script(); |
42
|
|
|
foogallery_enqueue_core_gallery_template_style(); |
43
|
|
|
|
44
|
|
|
$deps = array( |
45
|
|
|
'wp-blocks', |
46
|
|
|
'wp-i18n', |
47
|
|
|
'wp-element', |
48
|
|
|
'foogallery-core', |
49
|
|
|
'wp-components', |
50
|
|
|
'wp-editor', |
51
|
|
|
'underscore' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$js_url = plugins_url( 'gutenberg/dist/blocks.build.js', dirname( __FILE__ ) ); |
55
|
|
|
|
56
|
|
|
// Scripts. |
57
|
|
|
wp_enqueue_script( |
58
|
|
|
'foogallery-block-js', // Handle. |
59
|
|
|
$js_url, // Block.build.js: We register the block here. Built with Webpack. |
60
|
|
|
$deps, // Dependencies, defined above. |
61
|
|
|
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. |
|
|
|
|
62
|
|
|
true // Enqueue the script in the footer. |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
// Styles. |
66
|
|
|
wp_enqueue_style( |
67
|
|
|
'foogallery-block-editor-css', // Handle. |
68
|
|
|
plugins_url( 'gutenberg/dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS. |
69
|
|
|
array( 'wp-edit-blocks', 'foogallery-core' ) // Dependency to include the CSS after it. |
70
|
|
|
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: filemtime — Gets file modification time. |
|
|
|
|
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$local_data = false; |
74
|
|
|
|
75
|
|
|
if ( function_exists( 'wp_get_jed_locale_data' ) ) { |
76
|
|
|
$local_data = wp_get_jed_locale_data( 'foogallery' ); |
77
|
|
|
} else if ( function_exists( 'gutenberg_get_jed_locale_data' ) ) { |
78
|
|
|
$local_data = gutenberg_get_jed_locale_data( 'foogallery' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$block_js_data = apply_filters('foogallery_gutenberg_block_js_data', array( |
82
|
|
|
"editGalleryUrl" => $this->get_edit_gallery_url() |
83
|
|
|
)); |
84
|
|
|
|
85
|
|
|
$inline_script = 'window.FOOGALLERY_BLOCK = ' . json_encode( $block_js_data ) . ';'; |
86
|
|
|
if ( false !== $local_data ) { |
87
|
|
|
/* |
88
|
|
|
* Pass already loaded translations to our JavaScript. |
89
|
|
|
* |
90
|
|
|
* This happens _before_ our JavaScript runs, afterwards it's too late. |
91
|
|
|
*/ |
92
|
|
|
$inline_script .= PHP_EOL . 'wp.i18n.setLocaleData( ' . json_encode( $local_data ) . ', "foogallery" );'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
wp_add_inline_script( |
96
|
|
|
'foogallery-block-js', |
97
|
|
|
$inline_script, |
98
|
|
|
'before' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function get_edit_gallery_url() { |
|
|
|
|
103
|
|
|
$post_type_object = get_post_type_object( "foogallery" ); |
104
|
|
|
if ( !$post_type_object ) |
105
|
|
|
return ''; |
106
|
|
|
|
107
|
|
|
if ( $post_type_object->_edit_link ) { |
108
|
|
|
$link = admin_url( $post_type_object->_edit_link . '&action=edit' ); |
109
|
|
|
} else { |
110
|
|
|
$link = ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return apply_filters( 'foogallery_gutenberg_edit_gallery_url', $link ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Enqueue Gutenberg block assets for both frontend + backend. |
118
|
|
|
* |
119
|
|
|
* `wp-blocks`: includes block type registration and related functions. |
120
|
|
|
* |
121
|
|
|
* @since 1.0.0 |
122
|
|
|
*/ |
123
|
|
|
function enqueue_block_assets() { |
|
|
|
|
124
|
|
|
if ( !apply_filters( 'foogallery_gutenberg_enabled', true ) ) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Styles. |
129
|
|
|
wp_enqueue_style( |
130
|
|
|
'foogallery-block-css', |
131
|
|
|
plugins_url( 'gutenberg/dist/blocks.style.build.css', dirname( __FILE__ ) ), |
132
|
|
|
array( 'wp-blocks' ) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Register our block and shortcode. |
138
|
|
|
*/ |
139
|
|
|
function php_block_init() { |
|
|
|
|
140
|
|
|
if ( !apply_filters( 'foogallery_gutenberg_enabled', true ) ) { |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
//get out quickly if no Gutenberg |
145
|
|
|
if ( !function_exists( 'register_block_type' ) ) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Register our block, and explicitly define the attributes we accept. |
150
|
|
|
register_block_type( |
151
|
|
|
'fooplugins/foogallery', array( |
152
|
|
|
'attributes' => array( |
153
|
|
|
'id' => array( |
154
|
|
|
'type' => 'number', |
155
|
|
|
'default' => 0 |
156
|
|
|
), |
157
|
|
|
), |
158
|
|
|
'render_callback' => array( $this, 'render_block' ), |
159
|
|
|
)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
function render_block( $attributes ) { |
|
|
|
|
163
|
|
|
$foogallery_id = $attributes['id']; |
164
|
|
|
$args = array( |
165
|
|
|
'id' => $foogallery_id |
166
|
|
|
); |
167
|
|
|
//create new instance of template engine |
168
|
|
|
$engine = new FooGallery_Template_Loader(); |
169
|
|
|
|
170
|
|
|
ob_start(); |
171
|
|
|
|
172
|
|
|
$engine->render_template( $args ); |
173
|
|
|
|
174
|
|
|
$output_string = ob_get_contents(); |
175
|
|
|
ob_end_clean(); |
176
|
|
|
return !empty($output_string) ? $output_string : null; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.