Skip to content

Commit 31c1106

Browse files
committed
plugin activation check
1 parent 3b42533 commit 31c1106

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

cbxphpspreadsheet.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
register_activation_hook(__FILE__, ['CBXPhpSpreadSheet', 'activation']);
4141
require_once CBXPHPSPREADSHEET_ROOT_PATH . "lib/vendor/autoload.php";
4242

43+
add_action('admin_notices', ['CBXPhpSpreadSheet', 'activation_error_display']);
44+
4345
/**
4446
* Class CBXPhpSpreadSheet
4547
*/
@@ -54,11 +56,13 @@ public function __construct()
5456
add_filter('plugin_row_meta', [$this, 'plugin_row_meta'], 10, 2);
5557

5658
new Hooks();
57-
}
59+
}//end constructor
5860

59-
/**
60-
* Activation hook
61-
*/
61+
/**
62+
* Activation hook
63+
*
64+
* @return void
65+
*/
6266
public static function activation()
6367
{
6468
$errors = [];
@@ -71,11 +75,38 @@ public static function activation()
7175
$errors[] = __('This plugin requires PHP extensions: Zip, XML, and GD2.', 'cbxphpspreadsheet');
7276
}
7377

74-
if (!empty($errors)) {
75-
deactivate_plugins(plugin_basename(__FILE__));
76-
wp_die(implode('<br>', $errors), __('Plugin Activation Error', 'cbxphpspreadsheet'), ['back_link' => true]);
78+
if (sizeof($errors) > 0) {
79+
update_option('cbxphpspreadsheet_activation_error', $errors);
80+
deactivate_plugins(plugin_basename(__FILE__));
81+
82+
//wp_die('Plugin not activated due to dependency not fulfilled.');
83+
84+
//die();
7785
}
78-
}
86+
}//end method activation
87+
88+
/**
89+
* Show error
90+
*
91+
* @return void
92+
*/
93+
public static function activation_error_display(){
94+
// Only display on specific admin pages (e.g., plugins page)
95+
$screen = get_current_screen();
96+
if ($screen && $screen->id === 'plugins') {
97+
$errors = get_option('cbxphpspreadsheet_activation_error');
98+
if ($errors) {
99+
if(is_array($errors) && sizeof($errors) > 0){
100+
foreach ($errors as $error){
101+
echo '<div class="notice notice-error is-dismissible"><p>' . esc_html($error) . '</p></div>';
102+
}
103+
}
104+
105+
delete_option('cbxphpspreadsheet_activation_error');
106+
deactivate_plugins('cbxphpspreadsheet/cbxphpspreadsheet.php');
107+
}
108+
}
109+
}//end method activation_error_display
79110

80111
/**
81112
* Check PHP version compatibility
@@ -85,7 +116,7 @@ public static function activation()
85116
private static function php_version_check()
86117
{
87118
return version_compare(PHP_VERSION, '7.4.0', '>=');
88-
}
119+
}//end method php_version_check
89120

90121
/**
91122
* Check if required PHP extensions are enabled
@@ -101,7 +132,7 @@ private static function extension_check($extensions)
101132
}
102133
}
103134
return true;
104-
}
135+
}//end method extension_check
105136

106137
/**
107138
* Add support and documentation links to the plugin row meta

0 commit comments

Comments
 (0)