PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.6
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.6
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.1.6, at includes/Utils/Helper.php

283 lines 6.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
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 }
37
38 /**
39 * Collect IP from request.
40 *
41 * @return string
42 */
43 public static function get_ip() {
44 $ip = '127.0.0.1'; // Local IP
45 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
46 $ip = $_SERVER['HTTP_CLIENT_IP'];
47 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
48 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
49 } else {
50 $ip = ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : $ip;
51 }
52
53 return sanitize_text_field( $ip );
54 }
55
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 = [] ){
64 extract( $data );
65 $helper = self::class;
66 $file = TEMPLATELY_PATH . 'views/' . $name . '.php';
67
68 if( is_readable( $file ) ) {
69 include_once $file;
70 }
71 }
72
73 /**
74 * Sanitize Helper
75 *
76 * @param mixed $value
77 * @param string $type
78 *
79 * @return bool|string
80 */
81 public static function sanitize( $value, $type = 'text' ){
82 switch ( $type ) {
83 case 'boolean':
84 $sanitized_value = rest_sanitize_boolean( $value );
85 break;
86 default:
87 $sanitized_value = sanitize_text_field( $value );
88 break;
89 }
90
91 return $sanitized_value;
92 }
93
94 /**
95 * API Error Formatter
96 *
97 * @param int $error_code
98 * @param mixed $error_message
99 * @param string $endpoint
100 * @param integer $status
101 * @param array $additional_data
102 * @return WP_Error
103 */
104 public static function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) {
105 $additional_data['status'] = $status;
106 if ( ! empty( $endpoint ) ) {
107 $additional_data['endpoint'] = $endpoint;
108 }
109
110 return new WP_Error( $error_code, $error_message, $additional_data );
111 }
112
113 /**
114 * API Response Formatter
115 *
116 * @param mixed $data
117 * @return WP_REST_Response
118 */
119 public static function success( $data ) {
120 return new WP_REST_Response( $data, 200 );
121 }
122
123 /**
124 * Normalize Favourites Data
125 *
126 * @param array $favourites
127 * @param array $_favourites
128 * @param boolean $undo
129 *
130 * @return array
131 */
132 public function normalizeFavourites( $favourites, $_favourites = [], $undo = false ){
133 if( $undo ) {
134 $_favourites[ $favourites['type'] ] = array_values(array_filter( $_favourites[ $favourites['type'] ], function( $item ) use( $favourites ) {
135 return $item != $favourites['id'];
136 } ));
137 return $_favourites;
138 }
139
140 array_map( function( $item ) use ( &$_favourites) {
141 if( ! is_null( $item ) ) {
142 $item = (array) $item;
143 if ( isset( $_favourites[ $item['type'] ] ) ){
144 $_favourites[ $item['type'] ][] = $item['id'];
145 } else {
146 $_favourites[ $item['type'] ] = [ $item['id'] ];
147 }
148 }
149 return $_favourites;
150 }, $favourites );
151
152 return $_favourites;
153 }
154
155 public function normalizeReviews( $favourites, $_favourites = [], $undo = false ){
156 array_map( function( $item ) use ( &$_favourites) {
157 if( ! is_null( $item ) ) {
158 $item = (array) $item;
159 if ( !isset( $_favourites[ $item['type'] ] ) ){
160 $_favourites[ $item['type'] ] = [];
161 }
162 $_favourites[ $item['type'] ][$item['type_id']] = $item['rating'];
163 }
164 return $_favourites;
165 }, $favourites );
166
167 return $_favourites;
168 }
169
170 /**
171 * Trigger Error
172 *
173 * @param object $triggered_by
174 * @return void
175 */
176 public static function trigger_error( $triggered_by, $method = 'get_instance' ){
177 $class = get_class( $triggered_by );
178 $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
179 $file = $trace[0]['file'];
180 $line = $trace[0]['line'];
181 trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR);
182 }
183
184 /**
185 * Printing Error Logs in debug.log file.
186 *
187 * @param mixed $log
188 * @return void
189 */
190 public static function log( $log ){
191 if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG === true ) {
192 if ( is_array( $log ) || is_object( $log ) ) {
193 error_log( print_r( $log, true ) );
194 } else {
195 error_log( $log ?: '' );
196 }
197 }
198 }
199
200 public static function should_flush(){
201 return (!defined('TEMPLATELY_IGNORE_FLUSH_ALL') || !TEMPLATELY_IGNORE_FLUSH_ALL) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') === false;
202 }
203
204 public static function get_block_by_name($blocks, $search) {
205 $queue = $blocks;
206
207 while (!empty($queue)) {
208 $current_block = array_shift($queue);
209
210 if($search === $current_block['blockName']){
211 return $current_block;
212 }
213
214 if (isset($current_block['innerBlocks'])) {
215 // Add nested blocks to the end of the queue for processing
216 $queue = array_merge($queue, $current_block['innerBlocks']);
217 }
218 }
219
220 return false;
221 }
222
223 /**
224 * Only checks if user can install/activate plugins
225 *
226 * @param [type] $cap
227 * @param [type] ...$args
228 * @return void
229 */
230 public static function current_user_can($cap, ...$args) {
231 $user = wp_get_current_user();
232
233 // Multisite super admin has all caps by definition, Unless specifically denied.
234 if ( is_multisite() && is_super_admin( $user->ID ) ) {
235 return true;
236 }
237
238 $caps = map_meta_cap( $cap, $user->ID, ...$args );
239
240 switch ($cap) {
241 case 'install_plugins':
242 case 'upload_plugins':
243 $caps = ['install_plugins'];
244 break;
245 case 'install_themes':
246 case 'upload_themes':
247 $caps = ['install_themes'];
248 break;
249 case 'activate_plugins':
250 case 'deactivate_plugins':
251 case 'activate_plugin':
252 case 'deactivate_plugin':
253 $caps = ['activate_plugins'];
254 break;
255 default:
256 break;
257 }
258
259 // Maintain BC for the argument passed to the "user_has_cap" filter.
260 $args = array_merge( array( $cap, $user->ID ), $args );
261
262 /**
263 * See WP_User::has_cap() for description.
264 */
265 $capabilities = apply_filters( 'user_has_cap', $user->allcaps, $caps, $args, $user );
266
267 // Everyone is allowed to exist.
268 $capabilities['exist'] = true;
269
270 // Nobody is allowed to do things they are not allowed to do.
271 unset( $capabilities['do_not_allow'] );
272
273 // Must have ALL requested caps.
274 foreach ( (array) $caps as $cap ) {
275 if ( empty( $capabilities[ $cap ] ) ) {
276 return false;
277 }
278 }
279
280 return true;
281 }
282
283 }