1616 * Plugin Name: CBX PHPFPDF Library
1717 * Plugin URI: https://github.com/codeboxrcodehub/cbxphpfpdf
1818 * Description: fpdf library as WordPress plugin based on https://github.com/fawno/FPDF
19- * Version: 1.0.0
19+ * Version: 1.0.1
2020 * Author: Codeboxr
2121 * Author URI: https://github.com/PHPOffice/PhpDomPDF
22- * License: GPL-2.0+
23- * License URI: http ://www.gnu.org/licenses/gpl-2.0 .txt
22+ * License: MIT
23+ * License URI: https ://github.com/codeboxrcodehub/cbxphpfpdf/blob/master/LICENSE .txt
2424 * Text Domain: cbxphpfpdf
2525 * Domain Path: /languages
2626 */
2727
2828// If this file is called directly, abort.
2929if (!defined ('WPINC ' )) {
30- die;
30+ die;
3131}
32- use Cbx \Phpfpdf \Hooks ;
3332
3433defined ('CBXPHPFPDF_PLUGIN_NAME ' ) or define ('CBXPHPFPDF_PLUGIN_NAME ' , 'cbxphpfpdf ' );
35- defined ('CBXPHPFPDF_PLUGIN_VERSION ' ) or define ('CBXPHPFPDF_PLUGIN_VERSION ' , '1.0.0 ' );
34+ defined ('CBXPHPFPDF_PLUGIN_VERSION ' ) or define ('CBXPHPFPDF_PLUGIN_VERSION ' , '1.0. ' );
3635defined ('CBXPHPFPDF_BASE_NAME ' ) or define ('CBXPHPFPDF_BASE_NAME ' , plugin_basename (__FILE__ ));
3736defined ('CBXPHPFPDF_ROOT_PATH ' ) or define ('CBXPHPFPDF_ROOT_PATH ' , plugin_dir_path (__FILE__ ));
3837defined ('CBXPHPFPDF_ROOT_URL ' ) or define ('CBXPHPFPDF_ROOT_URL ' , plugin_dir_url (__FILE__ ));
3938
4039register_activation_hook (__FILE__ , array ('CBXPhpFpdf ' , 'activation ' ));
41- require_once CBXPHPFPDF_ROOT_PATH . "lib/vendor/autoload.php " ;
4240
4341/**
4442 * Class CBXPhpFpdf
4543 */
4644class CBXPhpFpdf
4745{
48- function __construct ()
49- {
50- //load text domain
51- load_plugin_textdomain ('cbxphpfpdf ' , false , dirname (plugin_basename (__FILE__ )) . '/languages/ ' );
52-
53- add_filter ('plugin_row_meta ' , array ($ this , 'plugin_row_meta ' ), 10 , 2 );
54- new Hooks ();
55- }
56-
57- /**
58- * Activation hook
59- */
60- public static function activation ()
61- {
62- /*$requirements = array(
63- 'PHP 7.1.0' => version_compare(PHP_VERSION, '7.1.0', '>='),
64- 'PHP extension XML' => extension_loaded('xml'),
65- 'PHP extension xmlwriter' => extension_loaded('xmlwriter'),
66- 'PHP extension mbstring' => extension_loaded('mbstring'),
67- 'PHP extension ZipArchive' => extension_loaded('zip'),
68- 'PHP extension GD (optional)' => extension_loaded('gd'),
69- 'PHP extension dom (optional)' => extension_loaded('dom'),
70- );*/
71-
72- if (!CBXPhpFpdf::php_version_check ()) {
73-
74- // Deactivate the plugin
75- deactivate_plugins (__FILE__ );
76-
77- // Throw an error in the wordpress admin console
78- $ error_message = esc_html__ ('This plugin requires PHP version 7.1 or newer ' , 'cbxphpfpdf ' );
79- die ($ error_message );
80- }
81-
82- if (!CBXPhpFpdf::php_zip_enabled_check ()) {
83-
84- // Deactivate the plugin
85- deactivate_plugins (__FILE__ );
86-
87- // Throw an error in the wordpress admin console
88- $ error_message = esc_html__ ('This plugin requires PHP php_zip extension installed and enabled ' , 'cbxphpfpdf ' );
89- die ($ error_message );
90- }
91-
92- if (!CBXPhpFpdf::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 = esc_html__ ('This plugin requires PHP php_xml extension installed and enabled ' , 'cbxphpfpdf ' );
99- die ($ error_message );
100- }
101-
102- if (!CBXPhpFpdf::php_gd_enabled_check ()) {
103-
104- // Deactivate the plugin
105- deactivate_plugins (__FILE__ );
106-
107- // Throw an error in the wordpress admin console
108- $ error_message = esc_html__ ('This plugin requires PHP php_gd2 extension installed and enabled ' , 'cbxphpfpdf ' );
109- die ($ error_message );
110- }
111-
112- if (!CBXPhpFpdf::php_mbstring_enabled_check ()) {
113-
114- // Deactivate the plugin
115- deactivate_plugins (__FILE__ );
116-
117- // Throw an error in the wordpress admin console
118- $ error_message = esc_html__ ('This plugin requires PHP php_MBString extension installed and enabled ' , 'cbxphpfpdf ' );
119- die ($ error_message );
120- }
121-
122- if (!CBXPhpFpdf::php_dom_enabled_check ()) {
123-
124- // Deactivate the plugin
125- deactivate_plugins (__FILE__ );
126-
127- // Throw an error in the wordpress admin console
128- $ error_message = esc_html__ ('This plugin requires PHP DOM extension installed and enabled ' , 'cbxphpfpdf ' );
129- die ($ error_message );
130- }
131-
132- }//end method activation
133-
134- /**
135- * PHP version compatibility check
136- *
137- * @return bool
138- */
139- public static function php_version_check ()
140- {
141- if (version_compare (PHP_VERSION , '7.1.0 ' , '< ' )) {
142- return false ;
143- }
144-
145- return true ;
146- }//end method php_version_check
147-
148- /**
149- * php_zip enabled check
150- *
151- * @return bool
152- */
153- public static function php_zip_enabled_check ()
154- {
155- if (extension_loaded ('zip ' )) {
156- return true ;
157- }
158- return false ;
159- }//end method php_zip_enabled_check
160-
161- /**
162- * php_xml enabled check
163- *
164- * @return bool
165- */
166- public static function php_xml_enabled_check ()
167- {
168- if (extension_loaded ('xml ' )) {
169- return true ;
170- }
171- return false ;
172- }//end method php_xml_enabled_check
173-
174- /**
175- * php_gd2 enabled check
176- *
177- * @return bool
178- */
179- public static function php_gd_enabled_check ()
180- {
181- if (extension_loaded ('gd ' )) {
182- return true ;
183- }
184- return false ;
185- }//end method php_gd_enabled_check
186-
187- /**
188- * php_mbstring enabled check
189- *
190- * @return bool
191- */
192- public static function php_mbstring_enabled_check ()
193- {
194- if (extension_loaded ('mbstring ' )) {
195- return true ;
196- }
197- return false ;
198- }//end method php_mbstring_enabled_check
199-
200- /**
201- * php_dom enabled check
202- *
203- * @return bool
204- */
205- public static function php_dom_enabled_check ()
206- {
207- if (extension_loaded ('dom ' )) {
208- return true ;
209- }
210- return false ;
211- }//end method php_dom_enabled_check
212-
213- /**
214- * Plugin support and doc page url
215- *
216- * @param $links
217- * @param $file
218- *
219- * @return array
220- */
221- public function plugin_row_meta ($ links , $ file )
222- {
223- if (strpos ($ file , 'cbxphpfpdf.php ' ) !== false ) {
224- $ new_links = array (
225- 'support ' => '<a href="https://github.com/codeboxrcodehub/cbxphpfpdf" target="_blank"> ' . esc_html__ ('Support ' , 'cbxphpfpdf ' ) . '</a> ' ,
226- 'doc ' => '<a href="https://github.com/dompdf/dompdf" target="_blank"> ' . esc_html__ ('PHP Dompdf Doc ' , 'cbxphpfpdf ' ) . '</a> '
227- );
228-
229- $ links = array_merge ($ links , $ new_links );
230- }
231-
232- return $ links ;
233- }
46+ function __construct ()
47+ {
48+ //load text domain
49+ load_plugin_textdomain ('cbxphpfpdf ' , false , dirname (plugin_basename (__FILE__ )) . '/languages/ ' );
50+
51+ add_filter ('plugin_row_meta ' , array ($ this , 'plugin_row_meta ' ), 10 , 2 );
52+ add_action ( 'admin_notices ' , [ $ this , 'activation_error_display ' ] );
53+ }
54+
55+ /**
56+ * Activation hook
57+ */
58+ public static function activation ()
59+ {
60+ $ errors = [];
61+
62+ if ( ! self ::php_version_check () ) {
63+ $ errors [] = esc_html__ ('CBX PhpFPDF Library plugin requires PHP version 7.1 or newer ' , 'cbxphpfpdf ' );
64+ }
65+
66+ if ( ! self ::extension_check ( [ 'zip ' , 'xml ' , 'gd ' , 'mbstring ' , 'dom ' ] ) ) {
67+ $ errors [] = esc_html__ ( 'CBX PhpFPDF Library plugin requires PHP extensions: Zip, XML, and GD2. ' , 'cbxphpfpdf ' );
68+ }
69+
70+ if ( sizeof ( $ errors ) > 0 ) {
71+ update_option ( 'cbxphpfpdf_activation_error ' , $ errors );
72+ //deactivate_plugins(plugin_basename(__FILE__));
73+ //wp_die('Plugin not activated due to dependency not fulfilled.');
74+ //die();
75+ }
76+
77+ }//end method activation
78+
79+ /**
80+ * Show error
81+ *
82+ * @return void
83+ */
84+ public function activation_error_display () {
85+ // Only display on specific admin pages (e.g., plugins page)
86+ $ screen = get_current_screen ();
87+ if ( $ screen && $ screen ->id === 'plugins ' ) {
88+ $ errors = get_option ( 'cbxphpfpdf_activation_error ' );
89+ if ( $ errors ) {
90+ if ( is_array ( $ errors ) && sizeof ( $ errors ) > 0 ) {
91+ foreach ( $ errors as $ error ) {
92+ echo '<div class="notice notice-error is-dismissible"><p> ' . esc_html ( $ error ) . '</p></div> ' ;
93+ }
94+ }
95+
96+ delete_option ( 'cbxphpfpdf_activation_error ' );
97+ //deactivate_plugins('cbxphpfpdf/cbxphpfpdf.php');
98+ }
99+ }
100+ }//end method activation_error_display
101+
102+ /**
103+ * PHP version compatibility check
104+ *
105+ * @return bool
106+ */
107+ private static function php_version_check (){
108+ return version_compare ( PHP_VERSION , '7.4 ' , '>= ' );
109+ }//end method php_version_check
110+
111+ /**
112+ * Check if required PHP extensions are enabled
113+ *
114+ * @param array $extensions
115+ *
116+ * @return bool
117+ */
118+ private static function extension_check ( $ extensions ) {
119+ foreach ( $ extensions as $ extension ) {
120+ if ( ! extension_loaded ( $ extension ) ) {
121+ return false ;
122+ }
123+ }
124+
125+ return true ;
126+ }//end method extension_check
127+
128+ /**
129+ * Is the environment ready for the phpfpdf package
130+ *
131+ * @return bool
132+ */
133+ public static function environment_ready () {
134+ return self ::php_version_check () && self ::extension_check ( [ 'zip ' , 'xml ' , 'gd ' , 'mbstring ' , 'dom ' ]);
135+ }//end method environment_ready
136+
137+ /**
138+ * Plugin support and doc page url
139+ *
140+ * @param $links
141+ * @param $file
142+ *
143+ * @return array
144+ */
145+ public function plugin_row_meta ($ links , $ file )
146+ {
147+ if (strpos ($ file , 'cbxphpfpdf.php ' ) !== false ) {
148+ $ new_links = array (
149+ 'support ' => '<a href="https://github.com/codeboxrcodehub/cbxphpfpdf" target="_blank"> ' . esc_html__ ('Support ' , 'cbxphpfpdf ' ) . '</a> ' ,
150+ 'doc ' => '<a href="https://github.com/dompdf/dompdf" target="_blank"> ' . esc_html__ ('PHP Dompdf Doc ' , 'cbxphpfpdf ' ) . '</a> '
151+ );
152+
153+ $ links = array_merge ($ links , $ new_links );
154+ }
155+
156+ return $ links ;
157+ }
234158
235159}//end method CBXPhpFpdf
236160
161+ /**
162+ * Initialize the plugin
163+ */
164+ function cbxphpfpdf_load_plugin (){
165+ new CBXPhpFpdf ();
166+ }
237167
238- function cbxphpfpdf_load_plugin ()
239- {
240- new CBXPhpFpdf ();
168+ add_action ('plugins_loaded ' , 'cbxphpfpdf_load_plugin ' , 5 );
169+
170+ if (!function_exists ('cbxphpfpdf_loadable ' )){
171+ /**
172+ * Check if the enviroment ready for phpfpdf library
173+ *
174+ * @return bool
175+ */
176+ function cbxphpfpdf_loadable (){
177+ return CBXPhpFpdf::environment_ready ();
178+ }//end function cbxphpfpdf_loadable
241179}
242180
243- add_action ('plugins_loaded ' , 'cbxphpfpdf_load_plugin ' , 5 );
181+ if (!function_exists ('cbxphpfpdf_load ' )){
182+ /**
183+ * If the enviroment is ready then load the autoloaded
184+ *
185+ * @return void
186+ */
187+ function cbxphpfpdf_load (){
188+ if (CBXPhpFpdf::environment_ready ()){
189+ require_once CBXPHPFPDF_ROOT_PATH . "lib/vendor/autoload.php " ;
190+ }
191+ }//end function cbxphpfpdf_load
192+ }
0 commit comments