1717 * Plugin URI: https://codeboxr.com/php-spreadsheet-library-wordpress-plugin/
1818 * Description: A pure PHP library for reading and writing spreadsheet files https://phpspreadsheet.readthedocs.io/
1919 * Version: 1.0.6
20+ * Requires PHP: 7.4
2021 * Author: Codeboxr
2122 * Author URI: https://github.com/PHPOffice/PhpSpreadsheet
2223 * License: GPL-2.0+
2526 * Domain Path: /languages
2627 */
2728
28- // If this file is called directly, abort.
29-
3029use Cbx \Phpspreadsheet \Hooks ;
3130
3231if (!defined ('WPINC ' )) {
33- die;
32+ die;
3433}
3534
3635defined ('CBXPHPSPREADSHEET_PLUGIN_NAME ' ) or define ('CBXPHPSPREADSHEET_PLUGIN_NAME ' , 'cbxphpspreadsheet ' );
3938defined ('CBXPHPSPREADSHEET_ROOT_PATH ' ) or define ('CBXPHPSPREADSHEET_ROOT_PATH ' , plugin_dir_path (__FILE__ ));
4039defined ('CBXPHPSPREADSHEET_ROOT_URL ' ) or define ('CBXPHPSPREADSHEET_ROOT_URL ' , plugin_dir_url (__FILE__ ));
4140
42-
43- register_activation_hook (__FILE__ , array ('CBXPhpSpreadSheet ' , 'activation ' ));
41+ register_activation_hook (__FILE__ , ['CBXPhpSpreadSheet ' , 'activation ' ]);
4442require_once CBXPHPSPREADSHEET_ROOT_PATH . "lib/vendor/autoload.php " ;
4543
46-
44+ add_action ( ' admin_notices ' , [ ' CBXPhpSpreadSheet ' , ' activation_error_display ' ]);
4745
4846/**
4947 * Class CBXPhpSpreadSheet
5048 */
5149class CBXPhpSpreadSheet
5250{
53- function __construct ()
54- {
55- //load text domain
56- load_plugin_textdomain ('cbxphpspreadsheet ' , false , dirname (plugin_basename (__FILE__ )) . '/languages/ ' );
51+ public function __construct ()
52+ {
53+ // Load text domain
54+ load_plugin_textdomain ('cbxphpspreadsheet ' , false , dirname (plugin_basename (__FILE__ )) . '/languages/ ' );
5755
58- add_filter ('plugin_row_meta ' , array ($ this , 'plugin_row_meta ' ), 10 , 2 );
56+ // Add custom row meta links
57+ add_filter ('plugin_row_meta ' , [$ this , 'plugin_row_meta ' ], 10 , 2 );
5958
60- new Hooks ();
61- }
59+ new Hooks ();
60+ } //end constructor
6261
6362 /**
6463 * 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
12264 *
123- * @return bool
65+ * @return void
12466 */
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
67+ public static function activation ()
68+ {
69+ $ errors = [];
70+
71+ if (!self ::php_version_check ()) {
72+ $ errors [] = __ ('This plugin requires PHP version 7.4 or newer. ' , 'cbxphpspreadsheet ' );
73+ }
13374
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- }
75+ if (!self ::extension_check (['zip ' , 'xml ' , 'gd ' ])) {
76+ $ errors [] = __ ('This plugin requires PHP extensions: Zip, XML, and GD2. ' , 'cbxphpspreadsheet ' );
77+ }
14478
145- return false ;
146- }//end method php_zip_enabled_check
79+ if (sizeof ($ errors ) > 0 ) {
80+ update_option ('cbxphpspreadsheet_activation_error ' , $ errors );
81+ deactivate_plugins (plugin_basename (__FILE__ ));
14782
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- }
83+ //wp_die('Plugin not activated due to dependency not fulfilled.');
15884
159- return false ;
160- }//end method php_xml_enabled_check
85+ //die();
86+ }
87+ }//end method activation
16188
16289 /**
163- * php_gd2 enabled check
90+ * Show error
16491 *
165- * @return bool
92+ * @return void
16693 */
167- public static function php_gd_enabled_check ()
168- {
169- if (extension_loaded ('gd ' )) {
170- return true ;
94+ public static function activation_error_display (){
95+ // Only display on specific admin pages (e.g., plugins page)
96+ $ screen = get_current_screen ();
97+ if ($ screen && $ screen ->id === 'plugins ' ) {
98+ $ errors = get_option ('cbxphpspreadsheet_activation_error ' );
99+ if ($ errors ) {
100+ if (is_array ($ errors ) && sizeof ($ errors ) > 0 ){
101+ foreach ($ errors as $ error ){
102+ echo '<div class="notice notice-error is-dismissible"><p> ' . esc_html ($ error ) . '</p></div> ' ;
103+ }
104+ }
105+
106+ delete_option ('cbxphpspreadsheet_activation_error ' );
107+ deactivate_plugins ('cbxphpspreadsheet/cbxphpspreadsheet.php ' );
108+ }
171109 }
110+ }//end method activation_error_display
111+
112+ /**
113+ * Check PHP version compatibility
114+ *
115+ * @return bool
116+ */
117+ private static function php_version_check ()
118+ {
119+ return version_compare (PHP_VERSION , '7.4.0 ' , '>= ' );
120+ }//end method php_version_check
121+
122+ /**
123+ * Check if required PHP extensions are enabled
124+ *
125+ * @param array $extensions
126+ * @return bool
127+ */
128+ private static function extension_check ($ extensions )
129+ {
130+ foreach ($ extensions as $ extension ) {
131+ if (!extension_loaded ($ extension )) {
132+ return false ;
133+ }
134+ }
135+ return true ;
136+ }//end method extension_check
137+
138+ /**
139+ * Add support and documentation links to the plugin row meta
140+ *
141+ * @param array $links
142+ * @param string $file
143+ * @return array
144+ */
145+ public function plugin_row_meta ($ links , $ file )
146+ {
147+ if (strpos ($ file , 'cbxphpspreadsheet.php ' ) !== false ) {
148+ $ new_links = [
149+ 'support ' => '<a href="https://codeboxr.com/php-spreadsheet-library-wordpress-plugin/" target="_blank"> ' . esc_html__ ('Support ' , 'cbxphpspreadsheet ' ) . '</a> ' ,
150+ 'doc ' => '<a href="https://phpspreadsheet.readthedocs.io/en/latest/" target="_blank"> ' . esc_html__ ('PHP Spreadsheet Doc ' , 'cbxphpspreadsheet ' ) . '</a> ' ,
151+ ];
152+
153+ $ links = array_merge ($ links , $ new_links );
154+ }
155+
156+ return $ links ;
157+ }
158+ }
172159
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-
207-
160+ /**
161+ * Initialize the plugin
162+ */
208163function cbxphpspreadsheet_load_plugin ()
209164{
210- new CBXPhpSpreadSheet ();
165+ new CBXPhpSpreadSheet ();
211166}
212167
213- add_action ('plugins_loaded ' , 'cbxphpspreadsheet_load_plugin ' , 5 );
168+ add_action ('plugins_loaded ' , 'cbxphpspreadsheet_load_plugin ' , 5 );
0 commit comments