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
|
|
|
$js_url = plugins_url( 'gutenberg/dist/blocks.build.js', dirname( __FILE__ ) ); |
36
|
|
|
|
37
|
|
|
// Scripts. |
38
|
|
|
wp_enqueue_script( |
39
|
|
|
'foogallery-block-js', // Handle. |
40
|
|
|
$js_url, // Block.build.js: We register the block here. Built with Webpack. |
41
|
|
|
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'foogallery-core' ), // Dependencies, defined above. |
42
|
|
|
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. |
|
|
|
|
43
|
|
|
true // Enqueue the script in the footer. |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
// Styles. |
47
|
|
|
wp_enqueue_style( |
48
|
|
|
'foogallery-block-editor-css', // Handle. |
49
|
|
|
plugins_url( 'gutenberg/dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS. |
50
|
|
|
array( 'wp-edit-blocks', 'foogallery-core' ) // Dependency to include the CSS after it. |
51
|
|
|
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: filemtime — Gets file modification time. |
|
|
|
|
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Enqueue Gutenberg block assets for both frontend + backend. |
57
|
|
|
* |
58
|
|
|
* `wp-blocks`: includes block type registration and related functions. |
59
|
|
|
* |
60
|
|
|
* @since 1.0.0 |
61
|
|
|
*/ |
62
|
|
|
function enqueue_block_assets() { |
|
|
|
|
63
|
|
|
// Styles. |
64
|
|
|
wp_enqueue_style( |
65
|
|
|
'foogallery-block-css', |
66
|
|
|
plugins_url( 'gutenberg/dist/blocks.style.build.css', dirname( __FILE__ ) ), |
67
|
|
|
array( 'wp-blocks' ) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Register our block and shortcode. |
73
|
|
|
*/ |
74
|
|
|
function php_block_init() { |
|
|
|
|
75
|
|
|
// Register our block, and explicitly define the attributes we accept. |
76
|
|
|
register_block_type( 'foogallery/responsive-gallery', array( |
77
|
|
|
'attributes' => array( |
78
|
|
|
'foo' => array( |
79
|
|
|
'type' => 'string', |
80
|
|
|
), |
81
|
|
|
), |
82
|
|
|
'render_callback' => array( $this, 'render_block' ), |
83
|
|
|
) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function render_block( $attributes ) { |
|
|
|
|
87
|
|
|
$foogallery_id = $attributes['foo']; |
88
|
|
|
$args = array( |
89
|
|
|
'id' => $foogallery_id |
90
|
|
|
); |
91
|
|
|
//create new instance of template engine |
92
|
|
|
$engine = new FooGallery_Template_Loader(); |
93
|
|
|
|
94
|
|
|
ob_start(); |
95
|
|
|
|
96
|
|
|
$engine->render_template( $args ); |
97
|
|
|
|
98
|
|
|
$output_string = ob_get_contents(); |
99
|
|
|
ob_end_clean(); |
100
|
|
|
return $output_string . |
101
|
|
|
"<script type='text/javascript'>setTimeout(function(){ jQuery('#foogallery-gallery-{$foogallery_id}').foogallery(); }, 5000);</script>"; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.