1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit; |
2
|
|
|
/** |
3
|
|
|
* Main controller file for product mass modification module |
4
|
|
|
* |
5
|
|
|
* @author Eoxia development team <[email protected]> |
6
|
|
|
* @version 1.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Main controller class for product mass modification module |
11
|
|
|
* |
12
|
|
|
* @author Eoxia development team <[email protected]> |
13
|
|
|
* @version 1.0 |
14
|
|
|
*/ |
15
|
|
|
class wpshop_tools { |
16
|
|
|
|
17
|
|
|
public static $currency_cache = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* INTERNAL LIB - Check and get the template file path to use for a given display part |
21
|
|
|
* |
22
|
|
|
* @uses locate_template() |
23
|
|
|
* @uses get_template_part() |
24
|
|
|
* |
25
|
|
|
* @param string $plugin_dir_name The main directory name containing the plugin |
26
|
|
|
* @param string $main_template_dir THe main directory containing the templates used for display |
27
|
|
|
* @param string $side The website part were the template will be displayed. Backend or frontend |
28
|
|
|
* @param string $slug The slug name for the generic template. |
29
|
|
|
* @param string $name The name of the specialised template. |
30
|
|
|
* |
31
|
|
|
* @return string The template file path to use |
32
|
|
|
*/ |
33
|
|
|
public static function get_template_part( $plugin_dir_name, $main_template_dir, $side, $slug, $name=null, $debug = null ) { |
34
|
|
|
$path = ''; |
35
|
|
|
|
36
|
|
|
$templates = array(); |
37
|
|
|
$name = (string)$name; |
38
|
|
|
if ( '' !== $name ) |
39
|
|
|
$templates[] = "{$side}/{$slug}-{$name}.php"; |
40
|
|
|
$templates[] = "{$side}/{$slug}.php"; |
41
|
|
|
|
42
|
|
|
/** Check if required template exists into current theme */ |
43
|
|
|
$check_theme_template = array(); |
|
|
|
|
44
|
|
|
foreach ( $templates as $template ) { |
45
|
|
|
$check_theme_template = $plugin_dir_name . "/" . $template; |
46
|
|
|
$path = locate_template( $check_theme_template, false ); |
47
|
|
|
if( !empty($path) ) { |
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** Allow debugging */ |
53
|
|
|
if ( !empty( $debug ) ) { |
54
|
|
|
echo '--- Debug mode ON - Start ---<br/>'; |
55
|
|
|
echo __FILE__ . '<br/>'; |
56
|
|
|
echo 'Debug for display method<br/>'; |
57
|
|
|
echo 'Asked path ' . $path . '<br/>'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ( empty( $path ) ) { |
61
|
|
|
foreach ( (array) $templates as $template_name ) { |
62
|
|
|
if ( !$template_name ) |
63
|
|
|
continue; |
64
|
|
|
|
65
|
|
|
if ( !empty( $debug ) ) { |
66
|
|
|
echo __LINE__ . ' - ' . $main_template_dir . $template_name . '<hr/>'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$file_exists = file_exists( $main_template_dir . $template_name ); |
70
|
|
|
if ( $file_exists ) { |
71
|
|
|
$path = $main_template_dir . $template_name; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( !empty( $debug ) ) { |
76
|
|
|
echo __LINE__ . ' - ' . (bool)$file_exists . '<hr/>'; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** Allow debugging */ |
82
|
|
|
if ( !empty( $debug ) ) { |
83
|
|
|
echo '--- Debug mode ON - END ---<br/><br/>'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $path; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Define the tools main page |
91
|
|
|
*/ |
92
|
|
|
public static function main_page() { |
93
|
|
|
echo wpshop_display::display_template_element('wpshop_admin_tools_main_page', array(), array(), 'admin'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return a variable with some basic treatment |
98
|
|
|
* |
99
|
|
|
* @param mixed $varToSanitize The variable we want to treat for future use |
100
|
|
|
* @param mixed $varDefaultValue The default value to set to the variable if the different test are not successfull |
101
|
|
|
* @param string $varType optionnal The type of the var for better verification |
102
|
|
|
* |
103
|
|
|
* @return mixed $sanitizedVar The var after treatment |
104
|
|
|
*/ |
105
|
|
|
public static function varSanitizer($varToSanitize, $varDefaultValue = '', $varType = '') { |
|
|
|
|
106
|
|
|
$sanitizedVar = is_string( $varToSanitize ) && (trim(strip_tags(stripslashes($varToSanitize))) != '') ? trim(strip_tags(stripslashes(($varToSanitize)))) : $varDefaultValue ; |
107
|
|
|
|
108
|
|
|
return $sanitizedVar; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Permit to force download a file |
113
|
|
|
* @param string $Fichier_a_telecharger |
114
|
|
|
* @param boolean $delete_after_download |
115
|
|
|
*/ |
116
|
|
|
public static function forceDownload($Fichier_a_telecharger, $delete_after_download = false) { |
117
|
|
|
|
118
|
|
|
$nom_fichier = basename($Fichier_a_telecharger); |
119
|
|
|
switch(strrchr($nom_fichier, ".")) { |
120
|
|
|
case ".gz": $type = "application/x-gzip"; break; |
121
|
|
|
case ".tgz": $type = "application/x-gzip"; break; |
122
|
|
|
case ".zip": $type = "application/zip"; break; |
123
|
|
|
case ".pdf": $type = "application/pdf"; break; |
124
|
|
|
case ".png": $type = "image/png"; break; |
125
|
|
|
case ".gif": $type = "image/gif"; break; |
126
|
|
|
case ".jpg": $type = "image/jpeg"; break; |
127
|
|
|
case ".txt": $type = "text/plain"; break; |
128
|
|
|
case ".htm": $type = "text/html"; break; |
129
|
|
|
case ".html": $type = "text/html"; break; |
130
|
|
|
default: $type = "application/octet-stream"; break; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
header("Content-disposition: attachment; filename=$nom_fichier"); |
134
|
|
|
header("Content-Type: application/force-download"); |
135
|
|
|
header("Content-Transfer-Encoding: $type\n"); // Surtout ne pas enlever le \n |
136
|
|
|
header("Content-Length: ".filesize($Fichier_a_telecharger)); |
137
|
|
|
header("Pragma: no-cache"); |
138
|
|
|
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public"); |
139
|
|
|
header("Expires: 0"); |
140
|
|
|
ob_end_clean(); |
141
|
|
|
readfile($Fichier_a_telecharger); |
142
|
|
|
if ( $delete_after_download ) { |
143
|
|
|
unlink( $Fichier_a_telecharger ); |
144
|
|
|
} |
145
|
|
|
exit; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Check if Send SMS is actived |
150
|
|
|
* @return boolean |
151
|
|
|
*/ |
152
|
|
|
public static function is_sendsms_actived() { |
153
|
|
|
if(is_plugin_active('wordpress-send-sms/Send-SMS.php')) { |
154
|
|
|
$configOption = get_option('sendsms_config', ''); |
155
|
|
|
$ligne = unserialize($configOption); |
156
|
|
|
$nicOVH = $ligne['nicOVH']; |
157
|
|
|
$passOVH = $ligne['passOVH']; |
158
|
|
|
$compteSMS = $ligne['compteSMS']; |
159
|
|
|
$tel_admin = $ligne['tel_admin']; |
160
|
|
|
return !empty($nicOVH) && !empty($passOVH) && !empty($compteSMS) && !empty($tel_admin); |
161
|
|
|
} |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Search all variations possibilities |
167
|
|
|
* @param unknown_type $input |
168
|
|
|
* @return Ambigous <multitype:, multitype:multitype:unknown > |
169
|
|
|
*/ |
170
|
|
|
public static function search_all_possibilities( $input ) { |
171
|
|
|
$result = array(); |
172
|
|
|
|
173
|
|
|
while (list($key, $values) = each($input)) { |
174
|
|
|
if (empty($values)) { |
175
|
|
|
continue; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (empty($result)) { |
179
|
|
|
foreach($values as $value) { |
180
|
|
|
$result[] = array($key => $value); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
else { |
184
|
|
|
$append = array(); |
185
|
|
|
foreach($result as &$product) { |
186
|
|
|
$product[$key] = array_shift($values); |
187
|
|
|
$copy = $product; |
188
|
|
|
|
189
|
|
|
foreach($values as $item) { |
190
|
|
|
$copy[$key] = $item; |
191
|
|
|
$append[] = $copy; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
array_unshift($values, $product[$key]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$result = array_merge($result, $append); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $result; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Return Default currency |
206
|
|
|
* @param boolean $code : false return sigle, true return code (€ or EUR) |
207
|
|
|
* @return string currency code or sigle |
208
|
|
|
*/ |
209
|
|
|
public static function wpshop_get_currency($code=false) { |
210
|
|
|
// Currency |
211
|
|
|
if( is_null( self::$currency_cache ) ) { |
212
|
|
|
global $wpdb; |
213
|
|
|
$current_currency = get_option('wpshop_shop_default_currency'); |
214
|
|
|
$query = $wpdb->prepare('SELECT * FROM ' .WPSHOP_DBT_ATTRIBUTE_UNIT. ' WHERE id =%d ', $current_currency ); |
215
|
|
|
self::$currency_cache = $wpdb->get_row( $query ); |
216
|
|
|
} |
217
|
|
|
if ( !empty(self::$currency_cache) ) { |
218
|
|
|
$code = ($code) ? self::$currency_cache->name : self::$currency_cache->unit; |
219
|
|
|
return $code; |
220
|
|
|
} |
221
|
|
|
else { |
222
|
|
|
return ''; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Return unit sigle |
228
|
|
|
* @param unknown_type $code |
229
|
|
|
* @param unknown_type $column_to_return |
230
|
|
|
*/ |
231
|
|
|
public static function wpshop_get_sigle($code, $column_to_return = "unit") { |
232
|
|
|
$tmp_code = (int)$code; |
233
|
|
|
$key_to_get = 'name'; |
234
|
|
|
if ( is_int($tmp_code) && !empty($tmp_code) ) { |
235
|
|
|
$key_to_get = 'id'; |
236
|
|
|
} |
237
|
|
|
$old_way_currencies = unserialize(WPSHOP_SHOP_CURRENCIES); |
238
|
|
|
if ( array_key_exists( $code, $old_way_currencies)) { |
239
|
|
|
$code = $old_way_currencies[$code]; |
240
|
|
|
$key_to_get = 'name'; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$current_currency = wpshop_attributes_unit::getElement($code, "'valid'", $key_to_get); |
244
|
|
|
|
245
|
|
|
return $current_currency->$column_to_return; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Clean variable |
250
|
|
|
* @param string $var : variable to clean |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public static function wpshop_clean( $var ) { |
254
|
|
|
return trim(strip_tags(stripslashes($var))); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Check if string have phone number structure |
259
|
|
|
* @param string phone number |
260
|
|
|
* @return boolean |
261
|
|
|
*/ |
262
|
|
|
public static function is_phone( $phone ) { |
263
|
|
|
return preg_match( '/(?=.*[0-9])([ 0-9\-\+\(\)]+)/', $phone ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Check if string have postcode valid structure |
268
|
|
|
* @param string postcode |
269
|
|
|
* @return boolean |
270
|
|
|
*/ |
271
|
|
|
public static function is_postcode( $postcode ) { |
272
|
|
|
return preg_match( '/(?=.*[0-9A-Za-z])([ \-A-Za-z0-9]+)/', $postcode ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Return a form field type from a database field type |
277
|
|
|
* |
278
|
|
|
* @param string $dataFieldType The database field type we want to get the form field type for |
279
|
|
|
* |
280
|
|
|
* @return string $type The form input type to use for the given field |
281
|
|
|
*/ |
282
|
|
|
public static function defineFieldType($dataFieldType, $input_type, $frontend_verification){ |
283
|
|
|
$type = 'text'; |
|
|
|
|
284
|
|
|
|
285
|
|
|
if ( $dataFieldType == 'datetime' ) { |
286
|
|
|
$type = 'text'; |
287
|
|
|
} |
288
|
|
|
else { |
289
|
|
|
switch ( $frontend_verification ) { |
290
|
|
|
case 'phone': |
291
|
|
|
$type = 'tel'; |
292
|
|
|
break; |
293
|
|
|
case 'email': |
294
|
|
|
$type = 'email'; |
295
|
|
|
break; |
296
|
|
|
default: |
297
|
|
|
$type = $input_type; |
298
|
|
|
break; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
return $type; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the method through which the data are transferred (POST OR GET) |
306
|
|
|
* |
307
|
|
|
* @return array The different element send by request method |
308
|
|
|
*/ |
309
|
|
|
public static function getMethode(){ |
310
|
|
|
$request_method = null; |
311
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "GET") { |
312
|
|
|
$request_method = (array)$_GET; |
313
|
|
|
} |
314
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") { |
315
|
|
|
$request_method = (array)$_POST; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if ( null === $request_method ) { |
319
|
|
|
die ('Invalid REQUEST_METHOD (not GET, not POST).'); |
320
|
|
|
} |
321
|
|
|
else { |
322
|
|
|
return $request_method; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Transform a given text with a specific pattern, send by the second parameter |
328
|
|
|
* |
329
|
|
|
* @param string $toSlugify The string we want to "clean" for future use |
330
|
|
|
* @param array|string $slugifyType The type of cleaning we are going to do on the input text |
331
|
|
|
* |
332
|
|
|
* @return string $slugified The input string that was slugified with the selected method |
333
|
|
|
*/ |
334
|
|
|
public static function slugify($toSlugify, $slugifyType){ |
335
|
|
|
$slugified = ''; |
336
|
|
|
|
337
|
|
|
if($toSlugify != '') |
338
|
|
|
{ |
339
|
|
|
$slugified = $toSlugify; |
340
|
|
|
foreach($slugifyType as $type) |
|
|
|
|
341
|
|
|
{ |
342
|
|
|
if($type == 'noAccent') |
343
|
|
|
{ |
344
|
|
|
$pattern = array("/é/", "/è/", "/ê/", "/ç/", "/à/", "/â/", "/î/", "/ï/", "/û/", "/ô/", "/È/", "/É/", "/Ê/", "/Ë/", "/Ì/", "/Í/", "/Î/", "/Ï/", "/Ö/", "/Ù/", "/Û/", "/Ü/","/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/"); |
345
|
|
|
$rep_pat = array("e", "e", "e", "c", "a", "a", "i", "i", "u", "o", "E", "E", "E", "E", "I", "I", "I", "I", "O", "U", "U", "U","e", "e", "e", "c", "a", "a", "i", "i", "u", "o", "E", "E", "E", "E", "I", "I", "I", "I", "O", "U", "U", "U"); |
346
|
|
|
} |
347
|
|
|
elseif($type == 'noSpaces') |
348
|
|
|
{ |
349
|
|
|
$pattern = array('/\s/'); |
350
|
|
|
$rep_pat = array('_'); |
351
|
|
|
$slugified = trim($slugified); |
352
|
|
|
} |
353
|
|
|
elseif($type == 'lowerCase') |
354
|
|
|
{ |
355
|
|
|
$slugified = strtolower($slugified); |
356
|
|
|
} |
357
|
|
|
elseif($type == 'noPunctuation') |
358
|
|
|
{ |
359
|
|
|
$pattern = array("/#/", "/\{/", "/\[/", "/\(/", "/\)/", "/\]/", "/\}/", "/&/", "/~/", "/�/", "/`/", "/\^/", "/@/", "/=/", "/�/", "/�/", "/%/", "/�/", "/!/", "/�/", "/:/", "/\$/", "/;/", "/\./", "/,/", "/\?/", "/\\\/", "/\//"); |
360
|
|
|
$rep_pat = array("_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
if(is_array($pattern) && is_array($rep_pat)) |
364
|
|
|
{ |
365
|
|
|
$slugified = preg_replace($pattern, $rep_pat, utf8_decode($slugified)); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $slugified; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Trunk a string too long |
375
|
|
|
* |
376
|
|
|
* @param string $string The string we want to "trunk" |
377
|
|
|
* @param int $maxlength The max length of the result string |
378
|
|
|
* |
379
|
|
|
* @return string $string The output string that was trunk if necessary |
380
|
|
|
*/ |
381
|
|
|
public static function trunk($string, $maxlength) { |
382
|
|
|
if(strlen($string)>$maxlength+3) |
383
|
|
|
return substr($string,0,$maxlength).'...'; |
384
|
|
|
else return $string; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Run a safe redirect in javascript |
389
|
|
|
*/ |
390
|
|
|
public static function wpshop_safe_redirect($url='') { |
391
|
|
|
$url = empty($url) ? admin_url('admin.php?page='.WPSHOP_URL_SLUG_DASHBOARD) : $url; |
392
|
|
|
echo '<script type="text/javascript">window.top.location.href = "'.$url.'"</script>'; |
393
|
|
|
exit; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Create a custom hook action |
398
|
|
|
* @param string $hook_name |
399
|
|
|
* @param array $args : Hook arguments |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
public static function create_custom_hook ($hook_name, $args = '') { |
403
|
|
|
ob_start(); |
404
|
|
|
if ( !empty($args) ) { |
405
|
|
|
do_action($hook_name, $args); |
406
|
|
|
} |
407
|
|
|
else { |
408
|
|
|
do_action($hook_name); |
409
|
|
|
} |
410
|
|
|
$content = ob_get_contents(); |
411
|
|
|
ob_end_clean(); |
412
|
|
|
return $content; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Return a plug-in activation code |
417
|
|
|
* @param string $plugin_name |
418
|
|
|
* @param string $encrypt_base_attribute |
419
|
|
|
* @return string |
420
|
|
|
*/ |
421
|
|
|
public static function get_plugin_validation_code($plugin_name, $encrypt_base_attribute) { |
422
|
|
|
$code = ''; |
|
|
|
|
423
|
|
|
if ( !function_exists( 'get_plugin_data') ) |
424
|
|
|
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
425
|
|
|
$plug = get_plugin_data( WP_PLUGIN_DIR . '/' . WPSHOP_PLUGIN_DIR . '/wpshop.php' ); |
426
|
|
|
$code_part = array(); |
427
|
|
|
$code_part[] = substr(hash ( "sha256" , $plugin_name ), WPSHOP_ADDONS_KEY_IS, 5); |
428
|
|
|
$code_part[] = substr(hash ( "sha256" , $plug['Name'] ), WPSHOP_ADDONS_KEY_IS, 5); |
429
|
|
|
$code_part[] = substr(hash ( "sha256" , 'addons' ), WPSHOP_ADDONS_KEY_IS, 5); |
430
|
|
|
$code = $code_part[1] . '-' . $code_part[2] . '-' . $code_part[0]; |
431
|
|
|
$att = $encrypt_base_attribute; |
432
|
|
|
$code .= '-' . substr(hash ( "sha256" , $att ), WPSHOP_ADDONS_KEY_IS, 5); |
433
|
|
|
|
434
|
|
|
return $code; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Check the WPShop Add-ons encrypt code validity |
439
|
|
|
* @param string $plugin_name |
440
|
|
|
* @param string $encrypt_base_attribute |
441
|
|
|
* @return boolean |
442
|
|
|
*/ |
443
|
|
|
public static function check_plugin_activation_code( $plugin_name, $encrypt_base_attribute, $from = 'file') { |
|
|
|
|
444
|
|
|
$is_valid = false; |
445
|
|
|
$valid_activation_code = self::get_plugin_validation_code($plugin_name, $encrypt_base_attribute); |
446
|
|
|
$activation_code_filename = WP_PLUGIN_DIR .'/'. $plugin_name.'/encoder.txt'; |
447
|
|
|
if ( is_file($activation_code_filename) ) { |
448
|
|
|
$activation_code_file = fopen($activation_code_filename, 'r' ); |
449
|
|
|
if ( $activation_code_file !== false) { |
450
|
|
|
$activation_code = fread( $activation_code_file, filesize($activation_code_filename)); |
451
|
|
|
if ( $activation_code == $valid_activation_code ) { |
452
|
|
|
$is_valid = true; |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
return $is_valid; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Formate number, Add span on cents on hide cents if equals zero |
461
|
|
|
* @param unknown_type $number |
462
|
|
|
* @return string |
463
|
|
|
*/ |
464
|
|
|
public static function formate_number( $number ) { |
465
|
|
|
$number = number_format( $number, 2, '.', '' ); |
466
|
|
|
$exploded_number = explode( '.', $number ); |
467
|
|
|
$number = $exploded_number[0]; |
468
|
|
|
if( $exploded_number[1] != '00' ) { |
469
|
|
|
$number .= '<span class="wps_number_cents">,' . $exploded_number[1]. '</span>'; |
470
|
|
|
} |
471
|
|
|
return $number; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Return the translated element id of a page |
476
|
|
|
* @param int $page_id |
477
|
|
|
* @return int |
478
|
|
|
*/ |
479
|
|
|
public static function get_page_id( $page_id ) { |
480
|
|
|
if( !empty($page_id) ) { |
481
|
|
|
if ( function_exists( 'icl_object_id' ) && defined('ICL_LANGUAGE_CODE') ) { |
482
|
|
|
$element_post_type = get_post_type( $page_id ); |
483
|
|
|
$translated_element_id = icl_object_id( $page_id, $element_post_type, true, ICL_LANGUAGE_CODE ); |
484
|
|
|
if( !empty($translated_element_id) ) { |
485
|
|
|
$page_id = $translated_element_id; |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
return $page_id; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
public static function minutes_to_time( $minutes, $format = '%hh %imin' ) { |
493
|
|
|
$dtF = new \DateTime( '@0' ); |
494
|
|
|
$dtT = new \DateTime( '@' . ( $minutes * 60 ) ); |
495
|
|
|
return $dtF->diff($dtT)->format( $format ); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
public static function number_format_hack($n) { |
499
|
|
|
return number_format($n, 5, '.', ''); |
500
|
|
|
} |
501
|
|
|
public static function is_serialized( $data, $strict = true ) { |
502
|
|
|
if ( ! is_string( $data ) ) { |
503
|
|
|
return false; |
504
|
|
|
} |
505
|
|
|
$data = trim( $data ); |
506
|
|
|
if ( 'N;' == $data ) { |
507
|
|
|
return true; |
508
|
|
|
} |
509
|
|
|
if ( strlen( $data ) < 4 ) { |
510
|
|
|
return false; |
511
|
|
|
} |
512
|
|
|
if ( ':' !== $data[1] ) { |
513
|
|
|
return false; |
514
|
|
|
} |
515
|
|
|
if ( $strict ) { |
516
|
|
|
$lastc = substr( $data, -1 ); |
517
|
|
|
if ( ';' !== $lastc && '}' !== $lastc ) { |
518
|
|
|
return false; |
519
|
|
|
} |
520
|
|
|
} else { |
521
|
|
|
$semicolon = strpos( $data, ';' ); |
522
|
|
|
$brace = strpos( $data, '}' ); |
523
|
|
|
if ( false === $semicolon && false === $brace ) { |
524
|
|
|
return false; |
525
|
|
|
} |
526
|
|
|
if ( false !== $semicolon && $semicolon < 3 ) { |
527
|
|
|
return false; |
528
|
|
|
} |
529
|
|
|
if ( false !== $brace && $brace < 4 ) { |
530
|
|
|
return false; |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
$token = $data[0]; |
534
|
|
|
switch ( $token ) { |
535
|
|
|
case 's' : |
536
|
|
|
if ( $strict ) { |
537
|
|
|
if ( '"' !== substr( $data, -2, 1 ) ) { |
538
|
|
|
return false; |
539
|
|
|
} |
540
|
|
|
} elseif ( false === strpos( $data, '"' ) ) { |
541
|
|
|
return false; |
542
|
|
|
} |
543
|
|
|
case 'a' : |
544
|
|
|
case 'O' : |
545
|
|
|
return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); |
546
|
|
|
case 'b' : |
547
|
|
|
case 'i' : |
548
|
|
|
case 'd' : |
549
|
|
|
$end = $strict ? '$' : ''; |
550
|
|
|
return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data ); |
551
|
|
|
} |
552
|
|
|
return false; |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.