| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Templately\Utils; |
| 3 | 3 | |
| 4 | +use Elementor\Plugin; | |
| 4 | 5 | use WP_Error; |
| 5 | 6 | use WP_REST_Response; |
| 6 | 7 | use function get_plugins; |
| 7 | 8 | use function is_plugin_active; |
| @@ -12,31 +13,36 @@ | ||
| 12 | 13 | * |
| 13 | 14 | * @since 1.0.0 |
| 14 | 15 | */ |
| 15 | 16 | class Helper extends Base { |
| 16 | - /** | |
| 17 | - * Get installed WordPress Plugin List | |
| 18 | - * @return array | |
| 19 | - */ | |
| 20 | - public static function get_plugins(){ | |
| 21 | - if( ! function_exists( 'get_plugins' ) ) { | |
| 22 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 23 | - } | |
| 24 | - return get_plugins(); | |
| 25 | - } | |
| 17 | + /** | |
| 18 | + * Get installed WordPress Plugin List | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | + public static function get_plugins(){ | |
| 22 | + if( ! function_exists( 'get_plugins' ) ) { | |
| 23 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 24 | + } | |
| 25 | + return get_plugins(); | |
| 26 | + } | |
| 27 | + public static function is_plugins_installed($plugin_file){ | |
| 28 | + $_plugins = self::get_plugins(); | |
| 29 | + $is_installed = isset( $_plugins[ $plugin_file ] ); | |
| 30 | + return $is_installed; | |
| 31 | + } | |
| 26 | 32 | |
| 27 | - /** | |
| 28 | - * Get installed WordPress Plugin List | |
| 29 | - * @return boolean | |
| 30 | - */ | |
| 31 | - public static function is_plugin_active( $plugin ){ | |
| 32 | - if( ! function_exists( 'is_plugin_active' ) ) { | |
| 33 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 34 | - } | |
| 35 | - return is_plugin_active( $plugin ); | |
| 36 | - } | |
| 33 | + /** | |
| 34 | + * Get installed WordPress Plugin List | |
| 35 | + * @return boolean | |
| 36 | + */ | |
| 37 | + public static function is_plugin_active( $plugin ){ | |
| 38 | + if( ! function_exists( 'is_plugin_active' ) ) { | |
| 39 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 40 | + } | |
| 41 | + return is_plugin_active( $plugin ); | |
| 42 | + } | |
| 37 | 43 | |
| 38 | - /** | |
| 44 | + /** | |
| 39 | 45 | * Collect IP from request. |
| 40 | 46 | * |
| 41 | 47 | * @return string |
| 42 | 48 | */ |
| @@ -52,24 +58,24 @@ | ||
| 52 | 58 | |
| 53 | 59 | return sanitize_text_field( $ip ); |
| 54 | 60 | } |
| 55 | 61 | |
| 56 | - /** | |
| 57 | - * Get views for front-end display | |
| 58 | - * | |
| 59 | - * @param string $name it will be file name only from the view's folder. | |
| 60 | - * @param array $data | |
| 61 | - * @return void | |
| 62 | - */ | |
| 63 | - public static function views( $name, $data = [] ){ | |
| 62 | + /** | |
| 63 | + * Get views for front-end display | |
| 64 | + * | |
| 65 | + * @param string $name it will be file name only from the view's folder. | |
| 66 | + * @param array $data | |
| 67 | + * @return void | |
| 68 | + */ | |
| 69 | + public static function views( $name, $data = [] ){ | |
| 64 | 70 | extract( $data ); |
| 65 | - $helper = self::class; | |
| 66 | - $file = TEMPLATELY_PATH . 'views/' . $name . '.php'; | |
| 71 | + $helper = self::class; | |
| 72 | + $file = TEMPLATELY_PATH . 'views/' . $name . '.php'; | |
| 67 | 73 | |
| 68 | - if( is_readable( $file ) ) { | |
| 69 | - include_once $file; | |
| 70 | - } | |
| 71 | - } | |
| 74 | + if( is_readable( $file ) ) { | |
| 75 | + include_once $file; | |
| 76 | + } | |
| 77 | + } | |
| 72 | 78 | |
| 73 | 79 | /** |
| 74 | 80 | * Sanitize Helper |
| 75 | 81 | * |
| @@ -187,13 +193,160 @@ | ||
| 187 | 193 | * @param mixed $log |
| 188 | 194 | * @return void |
| 189 | 195 | */ |
| 190 | 196 | public static function log( $log ){ |
| 191 | - if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG === true ) { | |
| 197 | + if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG ) { | |
| 192 | 198 | if ( is_array( $log ) || is_object( $log ) ) { |
| 193 | 199 | error_log( print_r( $log, true ) ); |
| 194 | 200 | } else { |
| 195 | - error_log( $log ); | |
| 201 | + error_log( $log ?: '' ); | |
| 196 | 202 | } |
| 197 | 203 | } |
| 198 | 204 | } |
| 205 | + | |
| 206 | + public static function should_flush(){ | |
| 207 | + if(isset($_GET['is_lightspeed']) && $_GET['is_lightspeed'] === 'true'){ | |
| 208 | + return false; | |
| 209 | + } | |
| 210 | + return (!defined('TEMPLATELY_IGNORE_FLUSH_ALL') || !TEMPLATELY_IGNORE_FLUSH_ALL) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') === false; | |
| 211 | + } | |
| 212 | + | |
| 213 | + public static function get_block_by_name($blocks, $search) { | |
| 214 | + $queue = $blocks; | |
| 215 | + | |
| 216 | + while (!empty($queue)) { | |
| 217 | + $current_block = array_shift($queue); | |
| 218 | + | |
| 219 | + if($search === $current_block['blockName']){ | |
| 220 | + return $current_block; | |
| 221 | + } | |
| 222 | + | |
| 223 | + if (isset($current_block['innerBlocks'])) { | |
| 224 | + // Add nested blocks to the end of the queue for processing | |
| 225 | + $queue = array_merge($queue, $current_block['innerBlocks']); | |
| 226 | + } | |
| 227 | + } | |
| 228 | + | |
| 229 | + return false; | |
| 230 | + } | |
| 231 | + | |
| 232 | + /** | |
| 233 | + * Only checks if user can install/activate plugins | |
| 234 | + * | |
| 235 | + * @param [type] $cap | |
| 236 | + * @param [type] ...$args | |
| 237 | + * @return void | |
| 238 | + */ | |
| 239 | + public static function current_user_can($cap, ...$args) { | |
| 240 | + $user = wp_get_current_user(); | |
| 241 | + | |
| 242 | + // Multisite super admin has all caps by definition, Unless specifically denied. | |
| 243 | + if ( is_multisite() && is_super_admin( $user->ID ) ) { | |
| 244 | + return true; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $caps = map_meta_cap( $cap, $user->ID, ...$args ); | |
| 248 | + | |
| 249 | + switch ($cap) { | |
| 250 | + case 'install_plugins': | |
| 251 | + case 'upload_plugins': | |
| 252 | + $caps = ['install_plugins']; | |
| 253 | + break; | |
| 254 | + case 'install_themes': | |
| 255 | + case 'upload_themes': | |
| 256 | + $caps = ['install_themes']; | |
| 257 | + break; | |
| 258 | + case 'activate_plugins': | |
| 259 | + case 'deactivate_plugins': | |
| 260 | + case 'activate_plugin': | |
| 261 | + case 'deactivate_plugin': | |
| 262 | + $caps = ['activate_plugins']; | |
| 263 | + break; | |
| 264 | + default: | |
| 265 | + break; | |
| 266 | + } | |
| 267 | + | |
| 268 | + // Maintain BC for the argument passed to the "user_has_cap" filter. | |
| 269 | + $args = array_merge( array( $cap, $user->ID ), $args ); | |
| 270 | + | |
| 271 | + /** | |
| 272 | + * See WP_User::has_cap() for description. | |
| 273 | + */ | |
| 274 | + $capabilities = apply_filters( 'user_has_cap', $user->allcaps, $caps, $args, $user ); | |
| 275 | + | |
| 276 | + // Everyone is allowed to exist. | |
| 277 | + $capabilities['exist'] = true; | |
| 278 | + | |
| 279 | + // Nobody is allowed to do things they are not allowed to do. | |
| 280 | + unset( $capabilities['do_not_allow'] ); | |
| 281 | + | |
| 282 | + // Must have ALL requested caps. | |
| 283 | + foreach ( (array) $caps as $cap ) { | |
| 284 | + if ( empty( $capabilities[ $cap ] ) ) { | |
| 285 | + return false; | |
| 286 | + } | |
| 287 | + } | |
| 288 | + | |
| 289 | + return true; | |
| 290 | + } | |
| 291 | + | |
| 292 | + /** | |
| 293 | + * Calculates the elapsed time and checks if it is close to the maximum execution time. | |
| 294 | + * Returns true if the script should exit to avoid exceeding the limit. | |
| 295 | + * | |
| 296 | + * @return bool True if the script should exit, false otherwise. | |
| 297 | + */ | |
| 298 | + public static function fsi_should_exit() { | |
| 299 | + if (defined('TEMPLATELY_START_TIME') && ini_get('max_execution_time')) { | |
| 300 | + $max_time = ini_get('max_execution_time'); | |
| 301 | + $elapsed = microtime(true) - TEMPLATELY_START_TIME; | |
| 302 | + $delay = max(5, $max_time * 20 / 100); | |
| 303 | + | |
| 304 | + // Check if elapsed time is close to max execution time | |
| 305 | + if ($max_time - $elapsed <= $delay) { | |
| 306 | + return ['max_time' => $max_time, 'elapsed' => $elapsed, 'delay' => $delay]; | |
| 307 | + } | |
| 308 | + } | |
| 309 | + return false; | |
| 310 | + } | |
| 311 | + | |
| 312 | + /** | |
| 313 | + * Enable Elementor Container | |
| 314 | + * This function will enable the Elementor Container feature. | |
| 315 | + * Without this feature, some of the templates may not work properly. | |
| 316 | + * | |
| 317 | + * @return boolean | |
| 318 | + */ | |
| 319 | + public static function enable_elementor_container(){ | |
| 320 | + if(class_exists('Elementor\Plugin')) { | |
| 321 | + $control_name = Plugin::instance()->experiments->get_feature_option_key( 'container' ); | |
| 322 | + if(get_option( $control_name ) === 'inactive') { | |
| 323 | + update_option( $control_name, 'active' ); | |
| 324 | + return true; | |
| 325 | + } | |
| 326 | + } | |
| 327 | + return false; | |
| 328 | + } | |
| 329 | + | |
| 330 | + /** | |
| 331 | + * Undocumented function | |
| 332 | + * | |
| 333 | + * @param [type] $args | |
| 334 | + * @param [type] $defaults | |
| 335 | + * @return array | |
| 336 | + */ | |
| 337 | + public static function recursive_wp_parse_args( $args, $defaults ) { | |
| 338 | + $args = (array) $args; | |
| 339 | + $defaults = (array) $defaults; | |
| 340 | + $r = $defaults; | |
| 341 | + foreach ( $args as $key => $value ) { | |
| 342 | + if ( is_array( $value ) && isset( $r[ $key ] ) ) { | |
| 343 | + $r[ $key ] = self::recursive_wp_parse_args( $value, $r[ $key ] ); | |
| 344 | + } | |
| 345 | + else { | |
| 346 | + $r[$key] = $value; | |
| 347 | + } | |
| 348 | + } | |
| 349 | + return $r; | |
| 350 | + } | |
| 351 | + | |
| 199 | 352 | } |