PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.2.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.2.1
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Utils / Helper.php

Helper.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.2.1, at includes/Utils/Helper.php

310 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately\Utils;
3
4 use WP_Error;
5 use WP_REST_Response;
6 use function get_plugins;
7 use function is_plugin_active;
8 /**
9 * Utility Helper for Templately
10 *
11 * This class contains some helper functions for easy access.
12 *
13 * @since 1.0.0
14 */
15 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 }
26 public static function is_plugins_installed($plugin_file){
27 $_plugins = self::get_plugins();
28 $is_installed = isset( $_plugins[ $plugin_file ] );
29 return $is_installed;
30 }
31
32 /**
33 * Get installed WordPress Plugin List
34 * @return boolean
35 */
36 public static function is_plugin_active( $plugin ){
37 if( ! function_exists( 'is_plugin_active' ) ) {
38 require_once ABSPATH . 'wp-admin/includes/plugin.php';
39 }
40 return is_plugin_active( $plugin );
41 }
42
43 /**
44 * Collect IP from request.
45 *
46 * @return string
47 */
48 public static function get_ip() {
49 $ip = '127.0.0.1'; // Local IP
50 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
51 $ip = $_SERVER['HTTP_CLIENT_IP'];
52 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
53 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
54 } else {
55 $ip = ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : $ip;
56 }
57
58 return sanitize_text_field( $ip );
59 }
60
61 /**
62 * Get views for front-end display
63 *
64 * @param string $name it will be file name only from the view's folder.
65 * @param array $data
66 * @return void
67 */
68 public static function views( $name, $data = [] ){
69 extract( $data );
70 $helper = self::class;
71 $file = TEMPLATELY_PATH . 'views/' . $name . '.php';
72
73 if( is_readable( $file ) ) {
74 include_once $file;
75 }
76 }
77
78 /**
79 * Sanitize Helper
80 *
81 * @param mixed $value
82 * @param string $type
83 *
84 * @return bool|string
85 */
86 public static function sanitize( $value, $type = 'text' ){
87 switch ( $type ) {
88 case 'boolean':
89 $sanitized_value = rest_sanitize_boolean( $value );
90 break;
91 default:
92 $sanitized_value = sanitize_text_field( $value );
93 break;
94 }
95
96 return $sanitized_value;
97 }
98
99 /**
100 * API Error Formatter
101 *
102 * @param int $error_code
103 * @param mixed $error_message
104 * @param string $endpoint
105 * @param integer $status
106 * @param array $additional_data
107 * @return WP_Error
108 */
109 public static function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) {
110 $additional_data['status'] = $status;
111 if ( ! empty( $endpoint ) ) {
112 $additional_data['endpoint'] = $endpoint;
113 }
114
115 return new WP_Error( $error_code, $error_message, $additional_data );
116 }
117
118 /**
119 * API Response Formatter
120 *
121 * @param mixed $data
122 * @return WP_REST_Response
123 */
124 public static function success( $data ) {
125 return new WP_REST_Response( $data, 200 );
126 }
127
128 /**
129 * Normalize Favourites Data
130 *
131 * @param array $favourites
132 * @param array $_favourites
133 * @param boolean $undo
134 *
135 * @return array
136 */
137 public function normalizeFavourites( $favourites, $_favourites = [], $undo = false ){
138 if( $undo ) {
139 $_favourites[ $favourites['type'] ] = array_values(array_filter( $_favourites[ $favourites['type'] ], function( $item ) use( $favourites ) {
140 return $item != $favourites['id'];
141 } ));
142 return $_favourites;
143 }
144
145 array_map( function( $item ) use ( &$_favourites) {
146 if( ! is_null( $item ) ) {
147 $item = (array) $item;
148 if ( isset( $_favourites[ $item['type'] ] ) ){
149 $_favourites[ $item['type'] ][] = $item['id'];
150 } else {
151 $_favourites[ $item['type'] ] = [ $item['id'] ];
152 }
153 }
154 return $_favourites;
155 }, $favourites );
156
157 return $_favourites;
158 }
159
160 public function normalizeReviews( $favourites, $_favourites = [], $undo = false ){
161 array_map( function( $item ) use ( &$_favourites) {
162 if( ! is_null( $item ) ) {
163 $item = (array) $item;
164 if ( !isset( $_favourites[ $item['type'] ] ) ){
165 $_favourites[ $item['type'] ] = [];
166 }
167 $_favourites[ $item['type'] ][$item['type_id']] = $item['rating'];
168 }
169 return $_favourites;
170 }, $favourites );
171
172 return $_favourites;
173 }
174
175 /**
176 * Trigger Error
177 *
178 * @param object $triggered_by
179 * @return void
180 */
181 public static function trigger_error( $triggered_by, $method = 'get_instance' ){
182 $class = get_class( $triggered_by );
183 $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
184 $file = $trace[0]['file'];
185 $line = $trace[0]['line'];
186 trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR);
187 }
188
189 /**
190 * Printing Error Logs in debug.log file.
191 *
192 * @param mixed $log
193 * @return void
194 */
195 public static function log( $log ){
196 if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG ) {
197 if ( is_array( $log ) || is_object( $log ) ) {
198 error_log( print_r( $log, true ) );
199 } else {
200 error_log( $log ?: '' );
201 }
202 }
203 }
204
205 public static function should_flush(){
206 if(isset($_GET['is_lightspeed']) && $_GET['is_lightspeed'] === 'true'){
207 return false;
208 }
209 return (!defined('TEMPLATELY_IGNORE_FLUSH_ALL') || !TEMPLATELY_IGNORE_FLUSH_ALL) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') === false;
210 }
211
212 public static function get_block_by_name($blocks, $search) {
213 $queue = $blocks;
214
215 while (!empty($queue)) {
216 $current_block = array_shift($queue);
217
218 if($search === $current_block['blockName']){
219 return $current_block;
220 }
221
222 if (isset($current_block['innerBlocks'])) {
223 // Add nested blocks to the end of the queue for processing
224 $queue = array_merge($queue, $current_block['innerBlocks']);
225 }
226 }
227
228 return false;
229 }
230
231 /**
232 * Only checks if user can install/activate plugins
233 *
234 * @param [type] $cap
235 * @param [type] ...$args
236 * @return void
237 */
238 public static function current_user_can($cap, ...$args) {
239 $user = wp_get_current_user();
240
241 // Multisite super admin has all caps by definition, Unless specifically denied.
242 if ( is_multisite() && is_super_admin( $user->ID ) ) {
243 return true;
244 }
245
246 $caps = map_meta_cap( $cap, $user->ID, ...$args );
247
248 switch ($cap) {
249 case 'install_plugins':
250 case 'upload_plugins':
251 $caps = ['install_plugins'];
252 break;
253 case 'install_themes':
254 case 'upload_themes':
255 $caps = ['install_themes'];
256 break;
257 case 'activate_plugins':
258 case 'deactivate_plugins':
259 case 'activate_plugin':
260 case 'deactivate_plugin':
261 $caps = ['activate_plugins'];
262 break;
263 default:
264 break;
265 }
266
267 // Maintain BC for the argument passed to the "user_has_cap" filter.
268 $args = array_merge( array( $cap, $user->ID ), $args );
269
270 /**
271 * See WP_User::has_cap() for description.
272 */
273 $capabilities = apply_filters( 'user_has_cap', $user->allcaps, $caps, $args, $user );
274
275 // Everyone is allowed to exist.
276 $capabilities['exist'] = true;
277
278 // Nobody is allowed to do things they are not allowed to do.
279 unset( $capabilities['do_not_allow'] );
280
281 // Must have ALL requested caps.
282 foreach ( (array) $caps as $cap ) {
283 if ( empty( $capabilities[ $cap ] ) ) {
284 return false;
285 }
286 }
287
288 return true;
289 }
290
291 /**
292 * Calculates the elapsed time and checks if it is close to the maximum execution time.
293 * Returns true if the script should exit to avoid exceeding the limit.
294 *
295 * @return bool True if the script should exit, false otherwise.
296 */
297 public static function fsi_should_exit() {
298 if (defined('TEMPLATELY_START_TIME') && ini_get('max_execution_time')) {
299 $max_time = ini_get('max_execution_time');
300 $elapsed = microtime(true) - TEMPLATELY_START_TIME;
301 $delay = max(5, $max_time / 20);
302
303 // Check if elapsed time is close to max execution time
304 if ($max_time - $elapsed <= $delay) {
305 return ['max_time' => $max_time, 'elapsed' => $elapsed, 'delay' => $delay];
306 }
307 }
308 return false;
309 }
310 }