| 1 |
<?php |
| 2 |
/** |
| 3 |
* Selection Lite |
| 4 |
* Carefully selected Elementor addons bundle, for building the most awesome websites |
| 5 |
* |
| 6 |
* @encoding UTF-8 |
| 7 |
* @version 1.12 |
| 8 |
* @copyright (C) 2018-2024 Merkulove ( https://merkulov.design/ ). All rights reserved. |
| 9 |
* @license GPLv3 |
| 10 |
* @contributors merkulove, vladcherviakov, phoenixmkua, podolianochka, viktorialev01 |
| 11 |
* @support help@merkulov.design |
| 12 |
**/ |
| 13 |
|
| 14 |
namespace Merkulove\SelectionLite\Unity; |
| 15 |
|
| 16 |
/** Exit if accessed directly. */ |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
header( 'Status: 403 Forbidden' ); |
| 19 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* SINGLETON: Class used for check plugin compatibility on early phase. |
| 25 |
* |
| 26 |
* @since 1.0 |
| 27 |
* |
| 28 |
**/ |
| 29 |
final class CheckCompatibility { |
| 30 |
|
| 31 |
/** |
| 32 |
* Array of messages to show in admin area if some checks fails. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
**/ |
| 36 |
private $admin_messages = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* The one true CheckCompatibility. |
| 40 |
* |
| 41 |
* @since 1.0 |
| 42 |
* @var CheckCompatibility |
| 43 |
**/ |
| 44 |
private static $instance; |
| 45 |
|
| 46 |
/** |
| 47 |
* Do initial hosting environment check: PHP version and critical extensions. |
| 48 |
* |
| 49 |
* @param array $checks - List of critical initial checks to run. List of available checks: 'php56' |
| 50 |
* @param bool $show_message - Show or hide messages in admin area. |
| 51 |
* |
| 52 |
* @since 1.0 |
| 53 |
* @access public |
| 54 |
* |
| 55 |
* @return bool - True if all checks passed, false otherwise. |
| 56 |
**/ |
| 57 |
public function do_initial_checks( $checks, $show_message = true ) { |
| 58 |
|
| 59 |
/** Flag to indicate failed checks. */ |
| 60 |
$pass = true; |
| 61 |
|
| 62 |
/** Plugin require PHP 7.4 or higher. */ |
| 63 |
if ( in_array( 'php74', $checks, true ) ) { |
| 64 |
|
| 65 |
/** @noinspection NestedPositiveIfStatementsInspection */ |
| 66 |
if ( false === $this->check_php56_version( $show_message ) ) { $pass = false; } |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** Add handler to show admin messages. */ |
| 71 |
$this->admin_notices( $show_message ); |
| 72 |
|
| 73 |
return $pass; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Do environment checks for required extensions on plugin page, before show any settings. |
| 79 |
* |
| 80 |
* @param bool $show_message - Show or hide messages in admin area. |
| 81 |
* |
| 82 |
* @since 1.0 |
| 83 |
* @access public |
| 84 |
* |
| 85 |
* @return bool - true if all checks passed, false otherwise. |
| 86 |
**/ |
| 87 |
public function do_settings_checks( $show_message = true ) { |
| 88 |
|
| 89 |
/** Flag to indicate failed checks. */ |
| 90 |
$pass = true; |
| 91 |
|
| 92 |
/** Plugin require DOM extension. */ |
| 93 |
$dom = $this->check_dom( $show_message ); |
| 94 |
if ( false === $dom ) { $pass = false; } |
| 95 |
|
| 96 |
/** Plugin require XML extension. */ |
| 97 |
$xml = $this->check_xml( $show_message ); |
| 98 |
if ( false === $xml ) { $pass = false; } |
| 99 |
|
| 100 |
/** Add handler to show admin messages. */ |
| 101 |
$this->admin_notices( $show_message ); |
| 102 |
|
| 103 |
return $pass; |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Add handler to show admin messages. |
| 109 |
* |
| 110 |
* @param $show_message |
| 111 |
* |
| 112 |
* @since 1.0 |
| 113 |
* @access public |
| 114 |
* |
| 115 |
* @return void |
| 116 |
**/ |
| 117 |
private function admin_notices( $show_message ) { |
| 118 |
|
| 119 |
/** Do we need to show message in admin area. */ |
| 120 |
if ( ! $show_message ) { return; } |
| 121 |
|
| 122 |
/** Too early to call get_current_screen(). */ |
| 123 |
if ( ! function_exists( 'get_current_screen' ) ) { |
| 124 |
require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
| 125 |
} |
| 126 |
|
| 127 |
/** Detect Plugin Settings Page. */ |
| 128 |
$screen = get_current_screen(); // Get current screen. |
| 129 |
|
| 130 |
if ( null !== $screen && in_array( $screen->base, Plugin::get_menu_bases(), true ) ) { |
| 131 |
|
| 132 |
/** Show messages as snackbars. */ |
| 133 |
foreach ( $this->admin_messages as $message ) { |
| 134 |
$this->render_snackbar_message( $message, 'error' ); |
| 135 |
} |
| 136 |
|
| 137 |
} else { |
| 138 |
|
| 139 |
/** Show messages as WordPress admin messages. */ |
| 140 |
add_action( 'admin_notices', [ $this, 'show_admin_messages' ] ); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Show messages in Admin area. |
| 148 |
* |
| 149 |
* @since 1.0 |
| 150 |
* @access public |
| 151 |
* |
| 152 |
* @return void |
| 153 |
**/ |
| 154 |
public function show_admin_messages() { |
| 155 |
|
| 156 |
/** Show messages as WordPress admin messages. */ |
| 157 |
foreach ( $this->admin_messages as $message ) { |
| 158 |
$this->render_classic_message( $message, 'error' ); |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Render message in snackbar style. |
| 165 |
* |
| 166 |
* @param string $message - Message to show. |
| 167 |
* @param string $type - Type of message: info|error|warning |
| 168 |
* |
| 169 |
* @since 1.0 |
| 170 |
* @access public |
| 171 |
* |
| 172 |
* @return void |
| 173 |
**/ |
| 174 |
private function render_snackbar_message( $message, $type = 'warning' ) { |
| 175 |
|
| 176 |
/** Render message in snackbar style. */ |
| 177 |
UI::get_instance()->render_snackbar( |
| 178 |
$message, |
| 179 |
$type, |
| 180 |
-1, |
| 181 |
true |
| 182 |
); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Render message in classic WordPress style. |
| 188 |
* |
| 189 |
* @param string $message - Message to show |
| 190 |
* @param string $type - Type of message: info|error|warning |
| 191 |
* |
| 192 |
* @since 1.0 |
| 193 |
* @access public |
| 194 |
* |
| 195 |
* @return void |
| 196 |
**/ |
| 197 |
private function render_classic_message( $message, $type = 'warning' ) { |
| 198 |
|
| 199 |
/** Render message in old fashion style. */ |
| 200 |
?> |
| 201 |
<div class="settings-error notice notice-<?php echo esc_attr( $type ); ?>"> |
| 202 |
<h4><?php esc_html_e( 'Selection Lite', 'selection-lite' ); ?></h4> |
| 203 |
<p><?php echo esc_html( $message ); ?></p> |
| 204 |
</div> |
| 205 |
<?php |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Check minimal required php version. |
| 211 |
* |
| 212 |
* @param bool $show_message - Show or hide messages in admin area. |
| 213 |
* |
| 214 |
* @since 1.0 |
| 215 |
* @access private |
| 216 |
* |
| 217 |
* @return bool - true if php version is 7.4 or higher, false otherwise. |
| 218 |
**/ |
| 219 |
private function check_php56_version( $show_message = true ) { |
| 220 |
|
| 221 |
/** Plugin require PHP 7.4 or higher. */ |
| 222 |
$res = ! ( ! defined( 'PHP_VERSION_ID' ) || PHP_VERSION_ID < 70400 ); |
| 223 |
|
| 224 |
/** If we need to show message in admin area. */ |
| 225 |
if ( false === $res && $show_message ) { |
| 226 |
|
| 227 |
$this->admin_messages[] = esc_html__( 'The minimum PHP version required for Selection Lite plugin is 7.4.0.', 'selection-lite' ); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
return $res; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Check whether the DOM extension is installed. |
| 237 |
* |
| 238 |
* @param bool $show_message - Show or hide messages in admin area. |
| 239 |
* |
| 240 |
* @since 1.0 |
| 241 |
* @access private |
| 242 |
* |
| 243 |
* @return bool - true if DOM extension is loaded, false otherwise. |
| 244 |
**/ |
| 245 |
private function check_dom( $show_message = true ) { |
| 246 |
|
| 247 |
/** Whether the DOM extension is installed. */ |
| 248 |
$dom = ReporterServer::get_instance()->get_dom_installed(); |
| 249 |
$check = ! $dom['warning']; |
| 250 |
|
| 251 |
/** If we need to show message in admin area. */ |
| 252 |
if ( false === $check && $show_message ) { |
| 253 |
$this->admin_messages[] = $dom['recommendation']; |
| 254 |
} |
| 255 |
|
| 256 |
return $check; |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Check whether the xml extension is installed. |
| 262 |
* |
| 263 |
* @param bool $show_message - Show or hide messages in admin area. |
| 264 |
* |
| 265 |
* @since 1.0 |
| 266 |
* @access private |
| 267 |
* |
| 268 |
* @return bool - true if xml extension is loaded, false otherwise. |
| 269 |
**/ |
| 270 |
private function check_xml( $show_message = true ) { |
| 271 |
|
| 272 |
/** Whether the XML extension is installed. */ |
| 273 |
$xml = ReporterServer::get_instance()->get_xml_installed(); |
| 274 |
$check = ! $xml['warning']; |
| 275 |
|
| 276 |
/** If we need to show message in admin area. */ |
| 277 |
if ( false === $check && $show_message ) { |
| 278 |
$this->admin_messages[] = $xml['recommendation']; |
| 279 |
} |
| 280 |
|
| 281 |
return $check; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Main CheckCompatibility Instance. |
| 287 |
* Insures that only one instance of CheckCompatibility exists in memory at any one time. |
| 288 |
* |
| 289 |
* @since 1.0 |
| 290 |
* @static |
| 291 |
* |
| 292 |
* @return CheckCompatibility |
| 293 |
**/ |
| 294 |
public static function get_instance() { |
| 295 |
|
| 296 |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { |
| 297 |
|
| 298 |
self::$instance = new self; |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
return self::$instance; |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
} // End Class CheckCompatibility. |
| 307 |
|