| 1 |
<?php |
| 2 |
namespace Templately\Utils; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use WP_Error; |
| 6 |
use WP_REST_Response; |
| 7 |
use function get_plugins; |
| 8 |
use function is_plugin_active; |
| 9 |
/** |
| 10 |
* Utility Helper for Templately |
| 11 |
* |
| 12 |
* This class contains some helper functions for easy access. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class Helper extends Base { |
| 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 |
} |
| 32 |
|
| 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 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Collect IP from request. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public static function get_ip() { |
| 50 |
$ip = '127.0.0.1'; // Local IP |
| 51 |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 52 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
| 53 |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 54 |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 55 |
} else { |
| 56 |
$ip = ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : $ip; |
| 57 |
} |
| 58 |
|
| 59 |
return sanitize_text_field( $ip ); |
| 60 |
} |
| 61 |
|
| 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 = [] ){ |
| 70 |
extract( $data ); |
| 71 |
$helper = self::class; |
| 72 |
$file = TEMPLATELY_PATH . 'views/' . $name . '.php'; |
| 73 |
|
| 74 |
if( is_readable( $file ) ) { |
| 75 |
include_once $file; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Sanitize Helper |
| 81 |
* |
| 82 |
* @param mixed $value |
| 83 |
* @param string $type |
| 84 |
* |
| 85 |
* @return bool|string |
| 86 |
*/ |
| 87 |
public static function sanitize( $value, $type = 'text' ){ |
| 88 |
switch ( $type ) { |
| 89 |
case 'boolean': |
| 90 |
$sanitized_value = rest_sanitize_boolean( $value ); |
| 91 |
break; |
| 92 |
default: |
| 93 |
$sanitized_value = sanitize_text_field( $value ); |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
return $sanitized_value; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* API Error Formatter |
| 102 |
* |
| 103 |
* @param int $error_code |
| 104 |
* @param mixed $error_message |
| 105 |
* @param string $endpoint |
| 106 |
* @param integer $status |
| 107 |
* @param array $additional_data |
| 108 |
* @return WP_Error |
| 109 |
*/ |
| 110 |
public static function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) { |
| 111 |
$additional_data['status'] = $status; |
| 112 |
if ( ! empty( $endpoint ) ) { |
| 113 |
$additional_data['endpoint'] = $endpoint; |
| 114 |
} |
| 115 |
|
| 116 |
return new WP_Error( $error_code, $error_message, $additional_data ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* API Response Formatter |
| 121 |
* |
| 122 |
* @param mixed $data |
| 123 |
* @return WP_REST_Response |
| 124 |
*/ |
| 125 |
public static function success( $data ) { |
| 126 |
return new WP_REST_Response( $data, 200 ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Normalize Favourites Data |
| 131 |
* |
| 132 |
* @param array $favourites |
| 133 |
* @param array $_favourites |
| 134 |
* @param boolean $undo |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
public function normalizeFavourites( $favourites, $_favourites = [], $undo = false ){ |
| 139 |
if( $undo ) { |
| 140 |
$_favourites[ $favourites['type'] ] = array_values(array_filter( $_favourites[ $favourites['type'] ], function( $item ) use( $favourites ) { |
| 141 |
return $item != $favourites['id']; |
| 142 |
} )); |
| 143 |
return $_favourites; |
| 144 |
} |
| 145 |
|
| 146 |
array_map( function( $item ) use ( &$_favourites) { |
| 147 |
if( ! is_null( $item ) ) { |
| 148 |
$item = (array) $item; |
| 149 |
if ( isset( $_favourites[ $item['type'] ] ) ){ |
| 150 |
$_favourites[ $item['type'] ][] = $item['id']; |
| 151 |
} else { |
| 152 |
$_favourites[ $item['type'] ] = [ $item['id'] ]; |
| 153 |
} |
| 154 |
} |
| 155 |
return $_favourites; |
| 156 |
}, $favourites ); |
| 157 |
|
| 158 |
return $_favourites; |
| 159 |
} |
| 160 |
|
| 161 |
public function normalizeReviews( $favourites, $_favourites = [], $undo = false ){ |
| 162 |
array_map( function( $item ) use ( &$_favourites) { |
| 163 |
if( ! is_null( $item ) ) { |
| 164 |
$item = (array) $item; |
| 165 |
if ( !isset( $_favourites[ $item['type'] ] ) ){ |
| 166 |
$_favourites[ $item['type'] ] = []; |
| 167 |
} |
| 168 |
$_favourites[ $item['type'] ][$item['type_id']] = $item['rating']; |
| 169 |
} |
| 170 |
return $_favourites; |
| 171 |
}, $favourites ); |
| 172 |
|
| 173 |
return $_favourites; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Trigger Error |
| 178 |
* |
| 179 |
* @param object $triggered_by |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public static function trigger_error( $triggered_by, $method = 'get_instance' ){ |
| 183 |
$class = get_class( $triggered_by ); |
| 184 |
$trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
| 185 |
$file = $trace[0]['file']; |
| 186 |
$line = $trace[0]['line']; |
| 187 |
trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Printing Error Logs in debug.log file. |
| 192 |
* |
| 193 |
* @param mixed $log |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
public static function log( $log ){ |
| 197 |
if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG ) { |
| 198 |
if ( is_array( $log ) || is_object( $log ) ) { |
| 199 |
error_log( print_r( $log, true ) ); |
| 200 |
} else { |
| 201 |
error_log( $log ?: '' ); |
| 202 |
} |
| 203 |
} |
| 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); |
| 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 |
} |