-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-apiinnerblocks.php
More file actions
148 lines (133 loc) · 3.69 KB
/
Copy pathclass-apiinnerblocks.php
File metadata and controls
148 lines (133 loc) · 3.69 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
148
<?php
/**
* Plugin Name: UBC WP API Inner Blocks
* Description: Provide inner blocks that consumes data from external API.
* Requires at least: 6.5
* Requires PHP: 8.2
* Version: 2.0.0
* Author: Kelvin Xu
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: ubc-wp-api-innerblocks
*
* @package ubc-wp-api-innerblocks
*/
namespace UBC\CTLT\Block\APIInnerBlocks;
if ( defined( 'UBC_WP_API_INNER_BLOCKS_PLUGIN_URL' ) ) {
wp_die( 'UBC WP API Inner Blocks is already activated. The new version is not compatible with the plugins that requires the older version of the UBC WP API InnerBlocks plugin.' );
}
define( 'UBC_WP_API_INNER_BLOCKS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'UBC_WP_API_INNER_BLOCKS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
/**
* API Inner Blocks
*/
class APIInnerBlocks {
/**
* Blocks
*
* @var array
*/
private static $blocks = array(
'resource-template' => array(
'name' => 'ubc/api-template',
'blocks' => array(),
),
'resource-pagination' => array(
'name' => 'ubc/api-pagination',
'blocks' => array(),
),
'resource-no-results' => array(
'name' => 'ubc/api-no-results',
'blocks' => array(),
),
'resource-title' => array(
'name' => 'ubc/api-title',
'blocks' => array(),
),
'resource-content' => array(
'name' => 'ubc/api-content',
'blocks' => array(),
),
'resource-excerpt' => array(
'name' => 'ubc/api-excerpt',
'blocks' => array(),
),
'resource-terms' => array(
'name' => 'ubc/api-terms',
'blocks' => array(),
),
'resource-datetime' => array(
'name' => 'ubc/api-datetime',
'blocks' => array(),
),
'resource-custom-field' => array(
'name' => 'ubc/api-custom-field',
'blocks' => array(),
),
'resource-image' => array(
'name' => 'ubc/api-image',
'blocks' => array(),
),
);
/**
* Initialize hooks
*/
public static function init() {
self::setup_hooks();
add_action( 'init', array( __CLASS__, 'register_core_blocks' ), 5 );
}
/**
* Setup hooks
*/
public static function setup_hooks() {
add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_block_registration_scripts' ) );
}
/**
* Enqueue block registration scripts
*/
public static function enqueue_block_registration_scripts() {
wp_enqueue_script(
'ubc_wp_api_inner_blocks_script',
UBC_WP_API_INNER_BLOCKS_PLUGIN_URL . 'blocks/build/register-block.js',
array( 'wp-hooks', 'wp-blocks' ),
filemtime( UBC_WP_API_INNER_BLOCKS_PLUGIN_DIR . 'blocks/build/register-block.js' ),
true
);
wp_localize_script(
'ubc_wp_api_inner_blocks_script',
'ubc_wp_api_inner_blocks',
array(
'blocks' => self::$blocks,
)
);
}
/**
* Register core blocks
*/
public static function register_core_blocks() {
foreach ( self::$blocks as $block_path => $duplicate_blocks ) {
register_block_type( UBC_WP_API_INNER_BLOCKS_PLUGIN_DIR . 'blocks/build/' . $block_path, array() );
}
}
/**
* Register block
*
* @param string $block_folder_name Block folder name.
* @param string $new_block_name New block name.
* @param array $args Block arguments.
*/
public static function register_block( $block_folder_name, $new_block_name, $args = array() ) {
register_block_type(
UBC_WP_API_INNER_BLOCKS_PLUGIN_DIR . 'blocks/build/' . $block_folder_name,
array_merge(
$args,
array(
'name' => $new_block_name,
)
)
);
self::$blocks[ $block_folder_name ]['blocks'][] = $new_block_name;
}
}
/* ----------------------------------------------------------------------------- */
APIInnerBlocks::init();