-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
215 lines (189 loc) · 5.33 KB
/
plugin.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Plugin Name: Mayflower Blocks (G4)
* Plugin URI: https://github.com/bellevuecollege/mayflower-blocks
* Description: Companion Gutenberg Blocks for BC Mayflower Theme
* Author: BC Integration (Taija, Angela, Elizabeth)
* Author URI: https://www.bellevuecollege.edu
* Version: 3.6.0 #{versionStamp}#
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Theme Names that are supported by this plugin.
*/
$mbg4_enabled_themes = array(
'Mayflower G4',
'Mayflower G5',
'Bellevue 2022',
'BC "Douglas Fir" Theme',
'BC "Sitka Spruce" Department Theme',
);
/**
* Enable this plugin for supported themes only.
*/
$mbg4_current_theme = wp_get_theme(); // gets the current theme!
foreach ( $mbg4_enabled_themes as $mbg4_enabled_theme ) {
if ( $mbg4_enabled_theme === $mbg4_current_theme->name || $mbg4_enabled_theme === $mbg4_current_theme->parent_theme ) {
/**
* Block Initializer.
*/
add_action( 'init', 'mg4_blocks_init' );
add_filter( 'block_categories_all', 'mbg4_block_categories', 10, 2 );
// Exit loop
break;
}
}
/**
* Load the blocks!
*
* Note- all blocks must be registered here.
*
* @return void
*/
function mg4_blocks_init() {
/** List of blocks - should match folder names */
// Dynamic Blocks.
if ( wp_get_theme()->name !== 'Bellevue 2022' ) {
mbg4_register_block( 'child-pages', true );
}
mbg4_register_block( 'course', true );
// Static Blocks.
mbg4_register_block( 'alert' );
mbg4_register_block( 'button' );
mbg4_register_block( 'button-group' );
mbg4_register_block( 'panel' );
mbg4_register_block( 'jumbotron' );
mbg4_register_block( 'lead' );
mbg4_register_block( 'well' );
// Multi-Block Structures
// Blocks for Row/Columns.
mbg4_register_block( 'row' );
mbg4_register_block( 'column' );
// All the blocks for the Tab block.
mbg4_register_block( 'tab-content-panel' );
mbg4_register_block( 'tab-content' );
mbg4_register_block( 'tab-list-tab' );
mbg4_register_block( 'tab-list' );
mbg4_register_block( 'tabs' );
// Blocks for the Collapse block.
mbg4_register_block( 'collapse' );
mbg4_register_block( 'collapsibles' );
// Specialized Blocks.
/**
* Register Staff List block if Mayflower is active and Staff List is enabled.
*/
if ( function_exists( 'mayflower_get_option' ) && true === mayflower_get_option( 'staff_toggle' ) ) {
mbg4_register_block( 'staff-list', true );
}
/**
* Only Register TablePress block if TablePress is active.
*/
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_plugin_active( 'tablepress/tablepress.php' ) ) {
mbg4_register_block( 'tablepress', true );
}
}
/**
* Register blocks
*
* @param string $block_name Name of the block.
* @param boolean $dynamic Is the block dynamic?
* @return void
*
* Registers static and dynamic blocks
*/
function mbg4_register_block( $block_name, $dynamic = false ) {
$path = dirname( __FILE__ ) . "/build/$block_name";
if ( $dynamic ) {
require "src/$block_name/block.php";
register_block_type(
"$path/block.json",
array(
'render_callback' => 'mbg4_' . str_replace( '-', '_', $block_name ) . '_callback',
)
);
} else {
register_block_type( "$path/block.json" );
}
}
/**
* Create a 'Bootstrap Blocks' category
*
* @param array $categories Array of categories.
* @param object $post Post object.
* @return string
*/
function mbg4_block_categories( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => 'bootstrap-blocks',
'title' => __( 'Bootstrap Blocks', 'bootstrap-blocks' ),
'icon' => 'editor-bold',
),
)
);
}
/**
* Filter WP KSES to allow required ARIA attributes.
*
* Allow aria-controls in post content
*
* @param array $tags Array of allowed tags.
* @param string $context Context of the tags.
* @return array
*/
function mbg4_allow_aria_attributes( $tags, $context ) {
$tags['a']['aria-controls'] = true;
$tags['a']['aria-expanded'] = true;
$tags['a']['aria-disabled'] = true;
$tags['button']['aria-controls'] = true;
$tags['button']['aria-expanded'] = true;
$tags['button']['aria-disabled'] = true;
return $tags;
}
add_filter( 'wp_kses_allowed_html', 'mbg4_allow_aria_attributes', 10, 2 );
// Make the `tablepress_tables option available via REST api to allow table lookup by post ID
add_action( 'rest_api_init', function () {
register_rest_route( 'mayflower-blocks/v1', '/tableid-by-postid/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'mbg4_table_callback',
'args' => array(
'id' => array(
'required' => true,
'type' => 'integer',
),
),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
}
) );
} );
/**
* Callback function for retrieving a TablePress table ID by post ID via REST API.
*
* @param array $request The REST API request data, should contain 'postid'.
* @return mixed The TablePress table ID corresponding to the given post ID, or null if not found.
*/
function mbg4_table_callback( $request ) {
$id = $request['id'];
if ( ! $id ) {
return null;
}
$table_option = json_decode( get_option( 'tablepress_tables' ) );
foreach ( $table_option->table_post as $key => $value ) {
if ( $value == $id ) {
return array( 'tableId' => $key );
}
}
return array( 'tableId' => false );
}