forked from philbuchanan/Accordion-Blocks
/
accordion-blocks.php
233 lines (172 loc) · 5.53 KB
/
accordion-blocks.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Plugin Name: Accordion Blocks
* Plugin URI: https://github.com/philbuchanan/Accordion-Blocks
* Description: Gutenberg blocks for creating responsive accordion drop-downs.
* Version: 1.1.3
* Requires at least: 5.0
* Author: Phil Buchanan
* Author URI: https://philbuchanan.com
* License: GPLv2 or later
*/
// Make sure to not redeclare the class
if (!class_exists('PB_Accordion_Blocks')) :
class PB_Accordion_Blocks {
/**
* Current plugin version number
* Set from parent plugin file
*/
public $plugin_version;
/**
* Class constructor
* Sets up the plugin, including: textdomain and registering scripts.
*/
function __construct() {
$basename = plugin_basename(__FILE__);
$this->plugin_version = $this->get_plugin_version();
// Register block
add_action('init', array($this, 'register_block'));
// Register frontend JavaScript
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
if (is_admin()) {
// Add link to documentation on plugin page
add_filter("plugin_action_links_$basename", array($this, 'add_documentation_link'));
}
// Add API endpoint to get defaults
add_action('rest_api_init', array($this, 'register_rest_routes'));
}
/**
* Current plugin version number
*/
private function get_plugin_version() {
$plugin_data = get_file_data(__FILE__, array('Version' => 'Version'), false);
return (defined('WP_DEBUG') && WP_DEBUG) ? time() : $plugin_data['Version'];
}
/**
* Register the block's assets for the editor
*/
public function register_block() {
// Automatically load dependencies and version
$asset_file = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');
wp_register_script(
'pb-accordion-blocks-script',
plugins_url('build/index.js', __FILE__),
$asset_file['dependencies'],
$asset_file['version']
);
wp_register_style(
'pb-accordion-blocks-style',
plugins_url('css/accordion-blocks.css', __FILE__),
array(),
$this->plugin_version
);
register_block_type('pb/accordion-item', array(
'editor_script' => 'pb-accordion-blocks-script',
'style' => 'pb-accordion-blocks-style',
));
}
/**
* Enqueue the block's assets for the frontend
*/
public function enqueue_frontend_assets() {
$min = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
wp_enqueue_script(
'pb-accordion-blocks-frontend-script',
plugins_url("js/accordion-blocks$min.js", __FILE__),
array('jquery'),
$this->plugin_version,
true
);
}
/**
* Add documentation link on plugin page
*/
public function add_documentation_link($links) {
array_push($links, sprintf('<a href="%s">%s</a>',
'http://wordpress.org/plugins/accordion-blocks/',
_x('Documentation', 'link to documentation on wordpress.org site', 'accordion-blocks')
));
return $links;
}
/**
* Register rest endpoint to get plugin defaults
*/
public function register_rest_routes() {
register_rest_route('accordion-blocks/v1', '/defaults', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'api_get_defaults'),
));
register_rest_route('accordion-blocks/v1', '/defaults', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'api_set_defaults'),
'permission_callback' => array($this, 'check_permissions'),
));
}
/**
* Get accordion block default settings
*
* @return object Default accordion block settings object
*/
public function api_get_defaults() {
$defaults = $this->get_defaults();
/*
* If there are no defaults set yet, set them now
* This will likely only happen when users upgrade from an older version
* of the plugin.
*/
if (!$defaults) {
$defaults = (object) array(
'initiallyOpen' => false,
'clickToClose' => true,
'autoClose' => true,
'scroll' => false,
'scrollOffset' => 0,
);
$this->set_defaults($defaults);
}
$response = new WP_REST_Response($defaults);
$response->set_status(200);
return $response;
}
/**
* Set accordion block default settings
*
* @param data object The date passed from the API
* @return object Default accordion block settings object
*/
public function api_set_defaults($request) {
$old_defaults = $this->get_defaults();
$new_defaults = json_decode($request->get_body());
$new_defaults = (object) array(
'initiallyOpen' => isset($new_defaults->initiallyOpen) ? $new_defaults->initiallyOpen : $old_defaults->initiallyOpen,
'clickToClose' => isset($new_defaults->clickToClose) ? $new_defaults->clickToClose : $old_defaults->clickToClose,
'autoClose' => isset($new_defaults->autoClose) ? $new_defaults->autoClose : $old_defaults->autoClose,
'scroll' => isset($new_defaults->scroll) ? $new_defaults->scroll : $old_defaults->scroll,
'scrollOffset' => isset($new_defaults->scrollOffset) ? $new_defaults->scrollOffset : $old_defaults->scrollOffset,
);
$this->set_defaults($new_defaults);
$response = new WP_REST_Response($new_defaults);
$response->set_status(201);
return $response;
}
/**
* Ensure user has permission to set defaults
*/
public function check_permissions() {
return current_user_can('edit_posts');
}
/**
* Get default settings from `wp_options` table
*/
private function get_defaults() {
return get_option('accordion_blocks_defaults');
}
/**
* Save default settings in `wp_options` table
*/
private function set_defaults($settings) {
return update_option('accordion_blocks_defaults', $settings);
}
}
$PB_Accordion_Blocks = new PB_Accordion_Blocks;
endif;