-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclass-cliowp-blocks-boilerplate.php
110 lines (95 loc) · 3.6 KB
/
class-cliowp-blocks-boilerplate.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
<?php
/**
* Plugin Name: ClioWP Blocks Boilerplate
* Plugin URI: https://github.com/pontikis/cliowp-blocks-boilerplate
* Description: Free WordPress Gutenberg block-type Plugin Boilerplate for Developers
* Version: 1.0.0
* Author: Christos Pontikis
* Author URI: https://pontikis.net
* Text Domain: td-cliowp-blocks-boilerplate
* Domain Path: /languages
* Requires PHP: 5.6.20
* Tested up to: 6.1.1
* License: GPLv2 or later
*
* @package ClioWP_Blocks_Boilerplate
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* ClioWP_Blocks_Boilerplate plugin main class
*
* @link https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/writing-your-first-block-type/
* @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/
*/
class ClioWP_Blocks_Boilerplate {
/**
* Constructor
*/
public function __construct() {
add_action( 'init', array( $this, 'register_block' ) );
}
/**
* Register assets and block
*
* It is recommended to use block.json file after version 5.8
* However if you don't use it, register_block would be like
* the following register_block_without_block_json() function
*/
public function register_block() {
register_block_type_from_metadata(
__DIR__,
array(
'render_callback' => array( $this, 'render_callback' ),
)
);
// i18n - PHP Localization.
load_plugin_textdomain( 'td-cliowp-blocks-boilerplate', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// i18n - Load JSON files for JS.
$script_handle = generate_block_asset_handle( 'cliowp-blocks-boilerplate', 'editorScript' );
wp_set_script_translations( $script_handle, 'td-cliowp-blocks-boilerplate', plugin_dir_path( __FILE__ ) . 'languages' );
}
/**
* Register assets and block (without using block.json file)
* Unused fucntion - just for reference
*/
public function register_block_without_block_json() {
wp_register_script( 'editor_js', plugin_dir_url( __FILE__ ) . 'build/editor.js', array( 'wp-blocks', 'wp-i18n', 'wp-editor' ), '1.0.0', true );
wp_register_script( 'frontend_js', plugin_dir_url( __FILE__ ) . 'build/frontend.js', array( 'wp-i18n' ), '1.0.0', true );
wp_register_style( 'editor_css', plugin_dir_url( __FILE__ ) . 'build/editor.css', array(), '1.0.0' );
wp_register_style( 'frontend_css', plugin_dir_url( __FILE__ ) . 'build/frontend.css', array(), '1.0.0' );
register_block_type(
'cliowp-blocks/boilerplate',
array(
'render_callback' => array( $this, 'render_callback' ),
'editor_script' => 'editor_js',
'editor_style' => 'editor_css',
'script' => 'frontend_js',
'style' => 'frontend_css',
)
);
// i18n - PHP Localization.
load_plugin_textdomain( 'td-cliowp-blocks-boilerplate', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// i18n - Load JSON files for JS.
wp_set_script_translations( 'editor_js', 'td-cliowp-blocks-boilerplate', plugin_dir_path( __FILE__ ) . 'languages' );
}
/**
* PHP render callback.
*
* @param array $attributes The attributes to pass to PHP.
*/
public function render_callback( $attributes ) {
if ( isset( $attributes['headline'] ) && $attributes['headline'] ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$attributes_encoded = base64_encode( wp_json_encode( $attributes ) );
ob_start(); ?>
<div class="cliowp-block-instance" data-blockprops="<?php echo esc_attr( $attributes_encoded ); ?>"></div>
<?php
return ob_get_clean();
}
return null;
}
}
$cliowp_blocks_boilerplate = new ClioWP_Blocks_Boilerplate();