Skip to content

Commit 3b42533

Browse files
committed
improvements
1 parent d05a40f commit 3b42533

File tree

1 file changed

+86
-163
lines changed

1 file changed

+86
-163
lines changed

cbxphpspreadsheet.php

Lines changed: 86 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
* Domain Path: /languages
2626
*/
2727

28-
// If this file is called directly, abort.
29-
3028
use Cbx\Phpspreadsheet\Hooks;
3129

3230
if (!defined('WPINC')) {
33-
die;
31+
die;
3432
}
3533

3634
defined('CBXPHPSPREADSHEET_PLUGIN_NAME') or define('CBXPHPSPREADSHEET_PLUGIN_NAME', 'cbxphpspreadsheet');
@@ -39,175 +37,100 @@
3937
defined('CBXPHPSPREADSHEET_ROOT_PATH') or define('CBXPHPSPREADSHEET_ROOT_PATH', plugin_dir_path(__FILE__));
4038
defined('CBXPHPSPREADSHEET_ROOT_URL') or define('CBXPHPSPREADSHEET_ROOT_URL', plugin_dir_url(__FILE__));
4139

42-
43-
register_activation_hook(__FILE__, array('CBXPhpSpreadSheet', 'activation'));
40+
register_activation_hook(__FILE__, ['CBXPhpSpreadSheet', 'activation']);
4441
require_once CBXPHPSPREADSHEET_ROOT_PATH . "lib/vendor/autoload.php";
4542

46-
47-
4843
/**
4944
* Class CBXPhpSpreadSheet
5045
*/
5146
class CBXPhpSpreadSheet
5247
{
53-
function __construct()
54-
{
55-
//load text domain
56-
load_plugin_textdomain('cbxphpspreadsheet', false, dirname(plugin_basename(__FILE__)) . '/languages/');
57-
58-
add_filter('plugin_row_meta', array($this, 'plugin_row_meta'), 10, 2);
59-
60-
new Hooks();
61-
}
62-
63-
/**
64-
* Activation hook
65-
*/
66-
public static function activation()
67-
{
68-
if (!CBXPhpSpreadSheet::php_version_check()) {
69-
70-
// Deactivate the plugin
71-
deactivate_plugins(__FILE__);
72-
73-
// Throw an error in the wordpress admin console
74-
$error_message = __('This plugin requires PHP version 7.4 or newer', 'cbxphpspreadsheet');
75-
die($error_message);
76-
}
77-
78-
79-
if (!CBXPhpSpreadSheet::php_zip_enabled_check()) {
80-
81-
// Deactivate the plugin
82-
deactivate_plugins(__FILE__);
83-
84-
// Throw an error in the wordpress admin console
85-
$error_message = __(
86-
'This plugin requires PHP php_zip extension installed and enabled',
87-
'cbxphpspreadsheet'
88-
);
89-
die($error_message);
90-
}
91-
92-
if (!CBXPhpSpreadSheet::php_xml_enabled_check()) {
93-
94-
// Deactivate the plugin
95-
deactivate_plugins(__FILE__);
96-
97-
// Throw an error in the wordpress admin console
98-
$error_message = __(
99-
'This plugin requires PHP php_xml extension installed and enabled',
100-
'cbxphpspreadsheet'
101-
);
102-
die($error_message);
103-
}
104-
105-
if (!CBXPhpSpreadSheet::php_gd_enabled_check()) {
106-
107-
// Deactivate the plugin
108-
deactivate_plugins(__FILE__);
109-
110-
// Throw an error in the wordpress admin console
111-
$error_message = __(
112-
'This plugin requires PHP php_gd2 extension installed and enabled',
113-
'cbxphpspreadsheet'
114-
);
115-
die($error_message);
116-
}
117-
118-
}//end method activation
119-
120-
/**
121-
* PHP version compatibility check
122-
*
123-
* @return bool
124-
*/
125-
public static function php_version_check()
126-
{
127-
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
128-
return false;
129-
}
130-
131-
return true;
132-
}//end method php_version_check
133-
134-
/**
135-
* php_zip enabled check
136-
*
137-
* @return bool
138-
*/
139-
public static function php_zip_enabled_check()
140-
{
141-
if (extension_loaded('zip')) {
142-
return true;
143-
}
144-
145-
return false;
146-
}//end method php_zip_enabled_check
147-
148-
/**
149-
* php_xml enabled check
150-
*
151-
* @return bool
152-
*/
153-
public static function php_xml_enabled_check()
154-
{
155-
if (extension_loaded('xml')) {
156-
return true;
157-
}
158-
159-
return false;
160-
}//end method php_xml_enabled_check
161-
162-
/**
163-
* php_gd2 enabled check
164-
*
165-
* @return bool
166-
*/
167-
public static function php_gd_enabled_check()
168-
{
169-
if (extension_loaded('gd')) {
170-
return true;
171-
}
172-
173-
return false;
174-
}//end method php_gd_enabled_check
175-
176-
/**
177-
* Plugin support and doc page url
178-
*
179-
* @param $links
180-
* @param $file
181-
*
182-
* @return array
183-
*/
184-
public function plugin_row_meta($links, $file)
185-
{
186-
187-
if (strpos($file, 'cbxphpspreadsheet.php') !== false) {
188-
$new_links = array(
189-
'support' => '<a href="https://codeboxr.com/php-spreadsheet-library-wordpress-plugin/" target="_blank">' . esc_html__(
190-
'Support',
191-
'cbxphpspreadsheet'
192-
) . '</a>',
193-
'doc' => '<a href="https://phpspreadsheet.readthedocs.io/en/latest/" target="_blank">' . esc_html__(
194-
'PHP Spreadsheet Doc',
195-
'cbxphpspreadsheet'
196-
) . '</a>'
197-
);
198-
199-
$links = array_merge($links, $new_links);
200-
}
201-
202-
return $links;
203-
}
204-
205-
}//end method CBXPhpSpreadSheet
206-
48+
public function __construct()
49+
{
50+
// Load text domain
51+
load_plugin_textdomain('cbxphpspreadsheet', false, dirname(plugin_basename(__FILE__)) . '/languages/');
52+
53+
// Add custom row meta links
54+
add_filter('plugin_row_meta', [$this, 'plugin_row_meta'], 10, 2);
55+
56+
new Hooks();
57+
}
58+
59+
/**
60+
* Activation hook
61+
*/
62+
public static function activation()
63+
{
64+
$errors = [];
65+
66+
if (!self::php_version_check()) {
67+
$errors[] = __('This plugin requires PHP version 7.4 or newer.', 'cbxphpspreadsheet');
68+
}
69+
70+
if (!self::extension_check(['zip', 'xml', 'gd'])) {
71+
$errors[] = __('This plugin requires PHP extensions: Zip, XML, and GD2.', 'cbxphpspreadsheet');
72+
}
73+
74+
if (!empty($errors)) {
75+
deactivate_plugins(plugin_basename(__FILE__));
76+
wp_die(implode('<br>', $errors), __('Plugin Activation Error', 'cbxphpspreadsheet'), ['back_link' => true]);
77+
}
78+
}
79+
80+
/**
81+
* Check PHP version compatibility
82+
*
83+
* @return bool
84+
*/
85+
private static function php_version_check()
86+
{
87+
return version_compare(PHP_VERSION, '7.4.0', '>=');
88+
}
89+
90+
/**
91+
* Check if required PHP extensions are enabled
92+
*
93+
* @param array $extensions
94+
* @return bool
95+
*/
96+
private static function extension_check($extensions)
97+
{
98+
foreach ($extensions as $extension) {
99+
if (!extension_loaded($extension)) {
100+
return false;
101+
}
102+
}
103+
return true;
104+
}
105+
106+
/**
107+
* Add support and documentation links to the plugin row meta
108+
*
109+
* @param array $links
110+
* @param string $file
111+
* @return array
112+
*/
113+
public function plugin_row_meta($links, $file)
114+
{
115+
if (strpos($file, 'cbxphpspreadsheet.php') !== false) {
116+
$new_links = [
117+
'support' => '<a href="https://codeboxr.com/php-spreadsheet-library-wordpress-plugin/" target="_blank">' . esc_html__('Support', 'cbxphpspreadsheet') . '</a>',
118+
'doc' => '<a href="https://phpspreadsheet.readthedocs.io/en/latest/" target="_blank">' . esc_html__('PHP Spreadsheet Doc', 'cbxphpspreadsheet') . '</a>',
119+
];
120+
121+
$links = array_merge($links, $new_links);
122+
}
123+
124+
return $links;
125+
}
126+
}
207127

128+
/**
129+
* Initialize the plugin
130+
*/
208131
function cbxphpspreadsheet_load_plugin()
209132
{
210-
new CBXPhpSpreadSheet();
133+
new CBXPhpSpreadSheet();
211134
}
212135

213-
add_action('plugins_loaded', 'cbxphpspreadsheet_load_plugin', 5);
136+
add_action('plugins_loaded', 'cbxphpspreadsheet_load_plugin', 5);

0 commit comments

Comments
 (0)