forked from soflyy/breakdance-custom-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaifb-breakdance-elements-custom-metabox.php
More file actions
415 lines (364 loc) · 11 KB
/
aifb-breakdance-elements-custom-metabox.php
File metadata and controls
415 lines (364 loc) · 11 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* Plugin Name: AIFB Breakdance Elements Custom MetaBox
* Plugin URI: https://plugins.aifb.ch/aifb-breakdance-elements-custom-metabox
* Description: Advanced MetaBox integration for Breakdance Builder
* Version: 0.0.2
* Author: Edoardo Guzzi, AIFB
* Author URI: https://plugins.aifb.ch
* License: GPL-3.0+
* Text Domain: aifb-breakdance-metabox
* Domain Path: /languages
* Requires PHP: 8.1
* Requires at least: 6.0
*/
namespace AIFB\BreakdanceMetaBox;
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Manual Autoloader
spl_autoload_register(function ($class) {
// Project-specific namespace prefix
$prefix = 'AIFB\\BreakdanceMetaBox\\';
// Base directory for the namespace prefix
$base_dir = __DIR__ . '/';
// Check if the class uses the namespace prefix
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
// Get the relative class name
$relative_class = substr($class, $len);
// Replace namespace separators with directory separators
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// If the file exists, require it
if (file_exists($file)) {
require $file;
}
});
// Composer autoloader (if available)
if (\file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
/**
* Main Plugin Class
*
* @since 0.0.1
*/
final class Plugin
{
/**
* Plugin instance
*
* @var Plugin|null
*/
private static ?Plugin $instance = null;
/**
* Plugin version
*/
public const VERSION = '0.0.2';
/**
* Minimum PHP version required
*/
public const MIN_PHP_VERSION = '8.1';
/**
* Minimum WordPress version required
*/
public const MIN_WP_VERSION = '6.0';
/**
* Plugin basename
*
* @var string
*/
private string $plugin_basename;
/**
* Constructor
*/
private function __construct()
{
// Define basic constants
$this->define_constants();
// Initialize plugin_basename
$this->plugin_basename = \plugin_basename(AIFB_BMB_FILE);
define('AIFB_BMB_BASENAME', $this->plugin_basename);
// Check requirements before proceeding
if (!$this->checkRequirements()) {
return;
}
$this->initializePlugin();
$this->registerHooks();
}
/**
* Define plugin constants
*/
private function define_constants(): void
{
if (!defined('AIFB_BMB_VERSION')) {
define('AIFB_BMB_VERSION', self::VERSION);
}
if (!defined('AIFB_BMB_FILE')) {
define('AIFB_BMB_FILE', __FILE__);
}
if (!defined('AIFB_BMB_PATH')) {
define('AIFB_BMB_PATH', plugin_dir_path(__FILE__));
}
if (!defined('AIFB_BMB_URL')) {
define('AIFB_BMB_URL', plugin_dir_url(__FILE__));
}
}
/**
* Get plugin instance
*
* @return Plugin
*/
public static function getInstance(): Plugin
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Check if all requirements are met
*
* @return bool
*/
private function checkRequirements(): bool
{
$requirements_met = true;
// Check PHP version
if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<')) {
\add_action('admin_notices', function () {
$this->displayRequirementError('PHP', self::MIN_PHP_VERSION);
});
$requirements_met = false;
}
// Check WordPress version
if (version_compare($GLOBALS['wp_version'], self::MIN_WP_VERSION, '<')) {
add_action('admin_notices', function () {
$this->displayRequirementError('WordPress', self::MIN_WP_VERSION);
});
$requirements_met = false;
}
// Check if Meta Box plugin is active
if (!class_exists('RWMB_Loader')) {
add_action('admin_notices', function () {
$this->displayMetaBoxMissingError();
});
$requirements_met = false;
}
// Enhanced check for Breakdance Builder
$is_breakdance_active = false;
if (class_exists('\Breakdance\Plugin') || function_exists('breakdance_loaded')) {
$is_breakdance_active = true;
} else {
// Fallback check for plugin being active but not fully loaded
if (!function_exists('is_plugin_active')) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$is_breakdance_active = is_plugin_active('breakdance/plugin.php');
}
if (!$is_breakdance_active) {
add_action('admin_notices', function () {
$this->displayBreakdanceMissingError();
});
$requirements_met = false;
}
return $requirements_met;
}
/**
* Initialize plugin components
*
* @return void
*/
private function initializePlugin(): void
{
// Register text domain loading for init hook
add_action('init', [$this, 'loadTextDomain']);
// Initialize core components
Core\Loader::getInstance();
Core\MetaBoxIntegration::getInstance();
// Initialize admin if in admin area
if (is_admin()) {
Admin\AdminManager::getInstance();
}
// Register Element Studio locations when Breakdance is loaded
add_action('breakdance_loaded', [$this, 'registerElementLocations'], 9);
// Load elements when Breakdance is loaded
add_action('breakdance_loaded', [$this, 'loadElements'], 10);
}
/**
* Register plugin hooks
*
* @return void
*/
private function registerHooks(): void
{
// Add settings link on plugin page
add_filter('plugin_action_links_' . $this->plugin_basename, [$this, 'addPluginActionLinks']);
// Register activation hook
register_activation_hook(AIFB_BMB_FILE, [$this, 'activate']);
// Register deactivation hook
register_deactivation_hook(AIFB_BMB_FILE, [$this, 'deactivate']);
}
/**
* Plugin activation
*
* @return void
*/
public function activate(): void
{
// Create necessary database tables or options
update_option('aifb_bmb_version', self::VERSION);
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Plugin deactivation
*
* @return void
*/
public function deactivate(): void
{
// Clean up if needed
flush_rewrite_rules();
}
/**
* Load plugin text domain
*
* @return void
*/
public function loadTextDomain(): void
{
load_plugin_textdomain(
'aifb-breakdance-metabox',
false,
dirname($this->plugin_basename) . '/languages'
);
}
/**
* Add plugin action links
*
* @param array $links
* @return array
*/
public function addPluginActionLinks(array $links): array
{
$plugin_links = [
'<a href="' . admin_url('admin.php?page=breakdance_settings') . '">' .
__('Breakdance Settings', 'aifb-breakdance-metabox') . '</a>'
];
return array_merge($plugin_links, $links);
}
/**
* Register Element Studio locations
*
* @return void
*/
public function registerElementLocations(): void
{
// Allineato con l'esempio di Breakdance su GitHub
$locations = [
'elements' => [
'label' => __('Custom Elements', 'aifb-breakdance-metabox'),
'type' => 'element'
],
'macros' => [
'label' => __('Custom Macros', 'aifb-breakdance-metabox'),
'type' => 'macro'
],
'presets' => [
'label' => __('Custom Presets', 'aifb-breakdance-metabox'),
'type' => 'preset'
]
];
foreach ($locations as $dir => $config) {
\Breakdance\ElementStudio\registerSaveLocation(
$this->getRelativePath($dir),
'AIFB\\BreakdanceMetaBox',
$config['type'],
$config['label'],
false
);
}
}
/**
* Get relative path for Element Studio locations
*
* @param string $dir
* @return string
*/
private function getRelativePath(string $dir): string
{
return \Breakdance\Util\getDirectoryPathRelativeToPluginFolder(__DIR__) . '/' . $dir;
}
/**
* Display requirement error
*
* @param string $requirement
* @param string $version
* @return void
*/
private function displayRequirementError(string $requirement, string $version): void
{
$message = sprintf(
/* translators: 1: Requirement name 2: Required version */
\esc_html__('AIFB Breakdance MetaBox requires %1$s version %2$s or greater.', 'aifb-breakdance-metabox'),
$requirement,
$version
);
printf('<div class="notice notice-error"><p>%s</p></div>', $message);
}
/**
* Display Meta Box missing error
*
* @return void
*/
private function displayMetaBoxMissingError(): void
{
$message = esc_html__('AIFB Breakdance MetaBox requires Meta Box plugin to be installed and activated.', 'aifb-breakdance-metabox');
$install_url = wp_nonce_url(
self_admin_url('update.php?action=install-plugin&plugin=meta-box'),
'install-plugin_meta-box'
);
printf(
'<div class="notice notice-error"><p>%s <a href="%s">%s</a></p></div>',
$message,
esc_url($install_url),
esc_html__('Install Meta Box', 'aifb-breakdance-metabox')
);
}
/**
* Display Breakdance missing error
*
* @return void
*/
private function displayBreakdanceMissingError(): void
{
$message = esc_html__('AIFB Breakdance MetaBox requires Breakdance Builder to be installed and activated.', 'aifb-breakdance-metabox');
$install_url = wp_nonce_url(
self_admin_url('update.php?action=install-plugin&plugin=breakdance'),
'install-plugin_breakdance'
);
printf(
'<div class="notice notice-error"><p>%s <a href="%s">%s</a></p></div>',
$message,
esc_url($install_url),
esc_html__('Install Breakdance', 'aifb-breakdance-metabox')
);
}
/**
* Load custom elements
*
* @return void
*/
public function loadElements(): void
{
// Include the Meta Box Field element
require_once AIFB_BMB_PATH . 'elements/metabox/metabox-field.php';
}
}
// Initialize plugin
add_action('plugins_loaded', function () {
Plugin::getInstance();
});