-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathcompatibility-checker.php
More file actions
238 lines (200 loc) · 8.94 KB
/
compatibility-checker.php
File metadata and controls
238 lines (200 loc) · 8.94 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Get all possible Pro package slugs dynamically
function wedevs_pm_get_pro_packages() {
return [
'wedevs-project-manager-pro',
'wedevs-project-manager-business',
'wedevs-project-manager-professional',
'pm-pro' // Legacy package name
];
}
// Hook into any Pro plugin activation dynamically to prevent incompatible activation
add_action( 'activate_plugin', 'wedevs_pm_prevent_old_pro_activation', 1, 2 );
// Deactivate Pro BEFORE plugins_loaded to prevent fatal errors
wedevs_pm_deactivate_incompatible_pro();
// Check Pro version compatibility
add_action( 'plugins_loaded', 'wedevs_pm_check_pro_compatibility', 100 );
function wedevs_pm_prevent_old_pro_activation( $plugin, $network_wide = false ) {
// Get all possible Pro package slugs
$pro_packages = wedevs_pm_get_pro_packages();
// Check if the plugin being activated is one of the Pro packages
$is_pro_package = false;
$pro_slug = '';
foreach ( $pro_packages as $package ) {
if ( strpos( $plugin, $package ) !== false ) {
$is_pro_package = true;
$pro_slug = $package;
break;
}
}
if ( ! $is_pro_package ) {
return; // Not a Pro package, ignore
}
// Get the full path to the Pro plugin file
$pro_file = WP_PLUGIN_DIR . '/' . $plugin;
if ( file_exists( $pro_file ) ) {
// Read the Pro plugin file to get version
$pro_data = get_file_data( $pro_file, [ 'Version' => 'Version' ] );
$pro_version = $pro_data['Version'] ?? null;
// If Pro version is < 3.0.0, prevent activation
if ( $pro_version && version_compare( $pro_version, '3.0.0', '<' ) ) {
// Deactivate it immediately
deactivate_plugins( $plugin, true );
// Store version for notice
update_option( 'wedevs_pm_pro_activation_blocked', $pro_version );
// Redirect back to plugins page with error
wp_die(
sprintf(
/* translators: 1: Pro version number, 2: Free version number */
esc_html__( 'WP Project Manager Pro version %1$s is not compatible with WP Project Manager Free version %2$s. Please update WP Project Manager Pro to version 3.0.0 or higher before activating.', 'wedevs-project-manager' ),
esc_html( $pro_version ),
esc_html( PM_VERSION )
),
esc_html__( 'Plugin Activation Error', 'wedevs-project-manager' ),
[ 'back_link' => true ]
);
}
}
}
function wedevs_pm_deactivate_incompatible_pro() {
// Get the stored version from database
$previous_version = get_option( 'cpm_version' );
// Check if Pro is active
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Get all active plugins
$active_plugins = get_option( 'active_plugins', [] );
$pro_packages = wedevs_pm_get_pro_packages();
// Check each active plugin to see if it's a Pro package
foreach ( $active_plugins as $plugin ) {
$is_pro_package = false;
foreach ( $pro_packages as $package ) {
if ( strpos( $plugin, $package ) !== false ) {
$is_pro_package = true;
break;
}
}
if ( ! $is_pro_package ) {
continue;
}
// Get the full path to the Pro plugin file
$pro_file = WP_PLUGIN_DIR . '/' . $plugin;
if ( ! file_exists( $pro_file ) ) {
continue;
}
// Read the Pro plugin file to get version
$pro_data = get_file_data( $pro_file, [ 'Version' => 'Version' ] );
$pro_version = $pro_data['Version'] ?? null;
// If updating from 2.x to 3.0 OR Pro version is < 3.0.0, deactivate Pro
$is_updating = $previous_version && version_compare( $previous_version, '3.0.0', '<' );
$pro_is_old = $pro_version && version_compare( $pro_version, '3.0.0', '<' );
if ( $is_updating || $pro_is_old || ! $pro_version ) {
// Deactivate Pro immediately to prevent fatal errors
deactivate_plugins( $plugin, true ); // silent deactivation
update_option( 'wedevs_pm_pro_deactivated_on_update', $pro_version ?: 'unknown' );
// Add notice for next page load
add_action( 'admin_notices', 'wedevs_pm_pro_deactivated_notice' );
}
}
// Show notice if Pro was deactivated
if ( get_option( 'wedevs_pm_pro_deactivated_on_update' ) ) {
add_action( 'admin_notices', 'wedevs_pm_pro_deactivated_notice' );
}
}
function wedevs_pm_pro_deactivated_notice() {
$pro_version = get_option( 'wedevs_pm_pro_deactivated_on_update' );
if ( ! $pro_version ) {
return;
}
?>
<div class="notice notice-warning is-dismissible">
<p>
<strong><?php esc_html_e( 'WP Project Manager Pro Deactivated', 'wedevs-project-manager' ); ?></strong><br>
<?php
echo esc_html( sprintf(
/* translators: 1: Free version number, 2: Pro version number */
__( 'WP Project Manager has been updated to version %1$s. Your Pro version %2$s was automatically deactivated to prevent compatibility errors. Please update WP Project Manager Pro to version 3.0.0 or higher before reactivating it.', 'wedevs-project-manager' ),
PM_VERSION,
$pro_version
) );
?>
</p>
</div>
<?php
// Clear the notice after showing it
delete_option( 'wedevs_pm_pro_deactivated_on_update' );
}
function wedevs_pm_check_pro_compatibility() {
// Check if Pro version is active
$required_version = '3.0.0';
// Check if Pro is active
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Get all active plugins
$active_plugins = get_option( 'active_plugins', [] );
$pro_packages = wedevs_pm_get_pro_packages();
// Check each active plugin to see if it's a Pro package
foreach ( $active_plugins as $plugin ) {
$is_pro_package = false;
foreach ( $pro_packages as $package ) {
if ( strpos( $plugin, $package ) !== false ) {
$is_pro_package = true;
break;
}
}
if ( ! $is_pro_package ) {
continue;
}
// Get the full path to the Pro plugin file
$pro_file = WP_PLUGIN_DIR . '/' . $plugin;
if ( ! file_exists( $pro_file ) ) {
continue;
}
// Read the Pro plugin file to get version
$pro_data = get_file_data( $pro_file, [ 'Version' => 'Version' ] );
$pro_version = $pro_data['Version'] ?? null;
// Check if Pro version is compatible (>= 3.0.0)
if ( $pro_version && version_compare( $pro_version, $required_version, '<' ) ) {
add_action( 'admin_notices', 'wedevs_pm_pro_incompatible_notice' );
// Deactivate Pro plugin on admin_init to ensure proper context
add_action( 'admin_init', function() use ( $plugin ) {
deactivate_plugins( $plugin );
} );
}
}
}
function wedevs_pm_pro_incompatible_notice() {
$pro_version = defined( 'PM_PRO_VERSION' ) ? PM_PRO_VERSION : ( function_exists( 'pm_pro_config' ) ? pm_pro_config('app.version') : wedevs_pm_pro_config('app.version') );
$deactivated_on_update = get_option( 'wedevs_pm_pro_deactivated_on_update' );
?>
<div class="error">
<p>
<strong><?php esc_html_e( 'WP Project Manager Pro Incompatible!', 'wedevs-project-manager' ); ?></strong><br>
<?php
if ( $deactivated_on_update ) {
echo esc_html( sprintf(
/* translators: 1: Free version number, 2: Pro version number */
__( 'WP Project Manager has been updated to version %1$s. Your Pro version %2$s is not compatible and was automatically deactivated to prevent errors. Please update WP Project Manager Pro to version 3.0.0 or higher before reactivating it.', 'wedevs-project-manager' ),
PM_VERSION,
$pro_version
) );
// Clear the flag after showing the notice once
delete_option( 'wedevs_pm_pro_deactivated_on_update' );
} else {
echo esc_html( sprintf(
/* translators: 1: Free version number, 2: Pro version number */
__( 'WP Project Manager Free version %1$s requires WP Project Manager Pro version 3.0.0 or higher. Your Pro version %2$s is not compatible and has been deactivated. Please update WP Project Manager Pro to version 3.0.0 or higher.', 'wedevs-project-manager' ),
PM_VERSION,
$pro_version
) );
}
?>
</p>
</div>
<?php
}