-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist-block.php
90 lines (79 loc) · 2.49 KB
/
gist-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
<?php
/**
* Plugin Name: Gist Block
* Description: A block for displaying GitHub gists in Gutenberg.
* Version: 0.1.0
* Author: Ashar Irfan
* Author URI: https://asharirfan.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: gist-block
*
* @package AsharIrfan\GistBlock
* @since 0.1.0
*/
namespace AsharIrfan\GistBlock;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Register the block with WordPress.
*
* @author Ashar Irfan
* @since 0.1.0
*/
function register_block() {
// Define our assets.
$editor_script = 'build/index.js';
$editor_style = 'build/index.css';
$frontend_style = 'build/style-index.css';
$frontend_script = 'build/frontend.js';
// Verify we have an editor script.
if ( ! file_exists( plugin_dir_path( __FILE__ ) . $editor_script ) ) {
wp_die( esc_html__( 'Whoops! You need to run `npm run build` for the AsharIrfan Gist Block first.', 'gist-block' ) );
}
// Autoload dependencies and version.
$asset_file = require plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
// Register editor script.
wp_register_script(
'ashar-irfan-gist-block-editor-script',
plugins_url( $editor_script, __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);
// Register editor style.
if ( file_exists( plugin_dir_path( __FILE__ ) . $editor_style ) ) {
wp_register_style(
'ashar-irfan-gist-block-editor-style',
plugins_url( $editor_style, __FILE__ ),
[ 'wp-edit-blocks' ],
filemtime( plugin_dir_path( __FILE__ ) . $editor_style )
);
}
// Register frontend style.
if ( file_exists( plugin_dir_path( __FILE__ ) . $frontend_style ) ) {
wp_register_style(
'ashar-irfan-gist-block-style',
plugins_url( $frontend_style, __FILE__ ),
[],
filemtime( plugin_dir_path( __FILE__ ) . $frontend_style )
);
}
// Register block with WordPress.
register_block_type( 'ashar-irfan/gist-block', array(
'editor_script' => 'ashar-irfan-gist-block-editor-script',
'editor_style' => 'ashar-irfan-gist-block-editor-style',
'style' => 'ashar-irfan-gist-block-style',
) );
// Register frontend script.
if ( file_exists( plugin_dir_path( __FILE__ ) . $frontend_script ) ) {
wp_enqueue_script(
'ashar-irfan-gist-block-frontend-script',
plugins_url( $frontend_script, __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);
}
}
add_action( 'init', __NAMESPACE__ . '\register_block' );