-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwpdev-pro-extended.php
More file actions
130 lines (107 loc) · 4.16 KB
/
wpdev-pro-extended.php
File metadata and controls
130 lines (107 loc) · 4.16 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
<?php
/**
* Plugin Name: Pro Extended
* Plugin URI: https://github.com/renandadalte/wpdev-pro-extended
* Description: Extends Pro Theme / Cornerstone with MCP Server, Design Tokens, Element Defaults, and developer utilities.
* Version: 1.0.0-alpha
* Author: Renan Dadalte
* Author URI: https://github.com/renandadalte
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wpdev-pro-extended
* Requires at least: 6.5
* Requires PHP: 8.1
*/
declare(strict_types=1);
if (! defined('ABSPATH')) {
exit;
}
// ─── Constants ───────────────────────────────────────────────────────────────
define('PE_VERSION', '1.0.0-alpha');
define('PE_FILE', __FILE__);
define('PE_DIR', plugin_dir_path(__FILE__));
define('PE_URL', plugin_dir_url(__FILE__));
define('PE_BASENAME', plugin_basename(__FILE__));
// ─── Autoloader (built-in PSR-4) ─────────────────────────────────────────────
spl_autoload_register(function (string $class): void {
$prefix = 'ProExtended\\';
if (strncmp($class, $prefix, strlen($prefix)) !== 0) {
return;
}
$relativeClass = substr($class, strlen($prefix));
$file = PE_DIR . 'src/' . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
// ─── Theme Dependency Guard ──────────────────────────────────────────────────
/**
* Check if the Pro Theme (or a child theme of Pro) is active.
*/
function pe_is_pro_theme_active(): bool
{
$theme = wp_get_theme();
// Check current theme or parent theme
$template = $theme->get_template();
return $template === 'pro';
}
/**
* Deactivate this plugin and show an admin notice.
*/
function pe_deactivate_with_notice(): void
{
deactivate_plugins(PE_BASENAME);
add_action('admin_notices', function (): void {
echo '<div class="notice notice-error"><p>';
echo esc_html__(
'Pro Extended has been deactivated because the Pro Theme is not active. Please activate Pro Theme (or a child theme of Pro) first.',
'wpdev-pro-extended'
);
echo '</p></div>';
});
}
// Block activation if Pro Theme is not active.
register_activation_hook(__FILE__, function (): void {
if (! pe_is_pro_theme_active()) {
deactivate_plugins(PE_BASENAME);
wp_die(
esc_html__('Pro Extended requires the Pro Theme (or a child theme) to be active.', 'wpdev-pro-extended'),
esc_html__('Plugin Activation Error', 'wpdev-pro-extended'),
['back_link' => true]
);
}
});
// Deactivate if user switches away from Pro Theme.
add_action('switch_theme', function (): void {
if (! pe_is_pro_theme_active()) {
pe_deactivate_with_notice();
}
});
// ─── Bootstrap ───────────────────────────────────────────────────────────────
// Bail early if Pro Theme is not active (defensive check for edge cases).
if (! pe_is_pro_theme_active()) {
return;
}
/**
* Global accessor for the plugin instance.
*/
function pro_extended(): ProExtended\Plugin
{
static $instance = null;
if ($instance === null) {
$instance = new ProExtended\Plugin();
}
return $instance;
}
// Initialize on plugins_loaded (priority 20 to ensure Pro Theme is loaded).
add_action('plugins_loaded', function (): void {
// Extra safety check: ensure Cornerstone is available.
if (! function_exists('cornerstone')) {
add_action('admin_notices', function (): void {
echo '<div class="notice notice-warning"><p>';
echo esc_html__('Pro Extended: Cornerstone is not available. Some features will be disabled.', 'wpdev-pro-extended');
echo '</p></div>';
});
}
pro_extended()->boot();
}, 20);