Skip to content

Commit e92761c

Browse files
committed
cbxphpfpdf - improvisation add (load and loadable method)
1 parent cf42809 commit e92761c

File tree

1 file changed

+143
-192
lines changed

1 file changed

+143
-192
lines changed

cbxphpfpdf.php

Lines changed: 143 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
// If this file is called directly, abort.
2929
if (!defined('WPINC')) {
30-
die;
30+
die;
3131
}
3232
use Cbx\Phpfpdf\Hooks;
3333

@@ -38,206 +38,157 @@
3838
defined('CBXPHPFPDF_ROOT_URL') or define('CBXPHPFPDF_ROOT_URL', plugin_dir_url(__FILE__));
3939

4040
register_activation_hook(__FILE__, array('CBXPhpFpdf', 'activation'));
41-
require_once CBXPHPFPDF_ROOT_PATH . "lib/vendor/autoload.php";
4241

4342
/**
4443
* Class CBXPhpFpdf
4544
*/
4645
class CBXPhpFpdf
4746
{
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-
}
47+
function __construct()
48+
{
49+
//load text domain
50+
load_plugin_textdomain('cbxphpfpdf', false, dirname(plugin_basename(__FILE__)) . '/languages/');
51+
52+
add_filter('plugin_row_meta', array($this, 'plugin_row_meta'), 10, 2);
53+
add_action( 'admin_notices', [ $this, 'activation_error_display' ] );
54+
}
55+
56+
/**
57+
* Activation hook
58+
*/
59+
public static function activation()
60+
{
61+
$errors = [];
62+
63+
if ( ! self::php_version_check() ) {
64+
$errors[] = esc_html__('CBX PhpFPDF Library plugin requires PHP version 7.1 or newer', 'cbxphpfpdf');
65+
}
66+
67+
if ( ! self::extension_check( [ 'zip', 'xml', 'gd', 'mbstring', 'dom' ] ) ) {
68+
$errors[] = esc_html__( 'CBX PhpFPDF Library plugin requires PHP extensions: Zip, XML, and GD2.', 'cbxphpfpdf' );
69+
}
70+
71+
if ( sizeof( $errors ) > 0 ) {
72+
update_option( 'cbxphpfpdf_activation_error', $errors );
73+
//deactivate_plugins(plugin_basename(__FILE__));
74+
//wp_die('Plugin not activated due to dependency not fulfilled.');
75+
//die();
76+
}
77+
78+
}//end method activation
79+
80+
/**
81+
* Show error
82+
*
83+
* @return void
84+
*/
85+
public function activation_error_display() {
86+
// Only display on specific admin pages (e.g., plugins page)
87+
$screen = get_current_screen();
88+
if ( $screen && $screen->id === 'plugins' ) {
89+
$errors = get_option( 'cbxphpfpdf_activation_error' );
90+
if ( $errors ) {
91+
if ( is_array( $errors ) && sizeof( $errors ) > 0 ) {
92+
foreach ( $errors as $error ) {
93+
echo '<div class="notice notice-error is-dismissible"><p>' . esc_html( $error ) . '</p></div>';
94+
}
95+
}
96+
97+
delete_option( 'cbxphpfpdf_activation_error' );
98+
//deactivate_plugins('cbxphpfpdf/cbxphpfpdf.php');
99+
}
100+
}
101+
}//end method activation_error_display
102+
103+
/**
104+
* PHP version compatibility check
105+
*
106+
* @return bool
107+
*/
108+
private static function php_version_check(){
109+
return version_compare( PHP_VERSION, '7.1.0', '>=' );
110+
}//end method php_version_check
111+
112+
/**
113+
* Check if required PHP extensions are enabled
114+
*
115+
* @param array $extensions
116+
*
117+
* @return bool
118+
*/
119+
private static function extension_check( $extensions ) {
120+
foreach ( $extensions as $extension ) {
121+
if ( ! extension_loaded( $extension ) ) {
122+
return false;
123+
}
124+
}
125+
126+
return true;
127+
}//end method extension_check
128+
129+
/**
130+
* Is the environment ready for the phpfpdf package
131+
*
132+
* @return bool
133+
*/
134+
public static function environment_ready() {
135+
return self::php_version_check() && self::extension_check( [ 'zip', 'xml', 'gd', 'mbstring', 'dom' ]);
136+
}//end method environment_ready
137+
138+
/**
139+
* Plugin support and doc page url
140+
*
141+
* @param $links
142+
* @param $file
143+
*
144+
* @return array
145+
*/
146+
public function plugin_row_meta($links, $file)
147+
{
148+
if (strpos($file, 'cbxphpfpdf.php') !== false) {
149+
$new_links = array(
150+
'support' => '<a href="https://github.com/codeboxrcodehub/cbxphpfpdf" target="_blank">' . esc_html__('Support', 'cbxphpfpdf') . '</a>',
151+
'doc' => '<a href="https://github.com/dompdf/dompdf" target="_blank">' . esc_html__('PHP Dompdf Doc', 'cbxphpfpdf') . '</a>'
152+
);
153+
154+
$links = array_merge($links, $new_links);
155+
}
156+
157+
return $links;
158+
}
234159

235160
}//end method CBXPhpFpdf
236161

162+
/**
163+
* Initialize the plugin
164+
*/
165+
function cbxphpfpdf_load_plugin(){
166+
new CBXPhpFpdf();
167+
}
237168

238-
function cbxphpfpdf_load_plugin()
239-
{
240-
new CBXPhpFpdf();
169+
add_action('plugins_loaded', 'cbxphpfpdf_load_plugin', 5);
170+
171+
if(!function_exists('cbxphpfpdf_loadable')){
172+
/**
173+
* Check if the enviroment ready for phpfpdf library
174+
*
175+
* @return bool
176+
*/
177+
function cbxphpfpdf_loadable(){
178+
return CBXPhpFpdf::environment_ready();
179+
}//end function cbxphpfpdf_loadable
241180
}
242181

243-
add_action('plugins_loaded', 'cbxphpfpdf_load_plugin', 5);
182+
if(!function_exists('cbxphpfpdf_load')){
183+
/**
184+
* If the enviroment is ready then load the autoloaded
185+
*
186+
* @return void
187+
*/
188+
function cbxphpfpdf_load(){
189+
if(CBXPhpFpdf::environment_ready()){
190+
require_once CBXPHPFPDF_ROOT_PATH . "lib/vendor/autoload.php";
191+
new Hooks();
192+
}
193+
}//end function cbxphpfpdf_load
194+
}

0 commit comments

Comments
 (0)