Completed
Push — master ( e0e7fb...5987fc )
by
unknown
19:20
created

wpshop_tools   F

Complexity

Total Complexity 112

Size/Duplication

Total Lines 536
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 536
rs 1.5789
c 0
b 0
f 0
wmc 112
lcom 0
cbo 2

24 Methods

Rating   Name   Duplication   Size   Complexity  
C get_template_part() 0 55 12
A main_page() 0 3 1
A varSanitizer() 0 5 3
C forceDownload() 0 31 12
B is_sendsms_actived() 0 12 5
C search_all_possibilities() 0 33 7
A wpshop_get_currency() 0 14 3
A wpshop_get_sigle() 0 16 4
A wpshop_clean() 0 3 1
D slugify() 0 38 9
A trunk() 0 5 2
A wpshop_safe_redirect() 0 5 2
A create_custom_hook() 0 12 2
A get_plugin_validation_code() 0 15 2
A check_plugin_activation_code() 0 15 4
A formate_number() 0 9 2
B get_page_id() 0 12 5
A defineFieldType() 0 21 4
A is_phone() 0 3 1
A is_postcode() 0 3 1
A getMethode() 0 16 4
A minutes_to_time() 0 5 1
A number_format_hack() 0 3 1
C is_serialized() 0 53 24

How to fix   Complexity   

Complex Class

Complex classes like wpshop_tools often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use wpshop_tools, and based on these observations, apply Extract Interface, too.

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
	/**
18
	 * INTERNAL LIB - Check and get the template file path to use for a given display part
19
	 *
20
	 * @uses locate_template()
21
	 * @uses get_template_part()
22
	 *
23
	 * @param string $plugin_dir_name The main directory name containing the plugin
24
	 * @param string $main_template_dir THe main directory containing the templates used for display
25
	 * @param string $side The website part were the template will be displayed. Backend or frontend
26
	 * @param string $slug The slug name for the generic template.
27
	 * @param string $name The name of the specialised template.
28
	 *
29
	 * @return string The template file path to use
30
	 */
31
	public static function get_template_part( $plugin_dir_name, $main_template_dir, $side, $slug, $name=null, $debug = null ) {
32
		$path = '';
33
34
		$templates = array();
35
		$name = (string)$name;
36
		if ( '' !== $name )
37
			$templates[] = "{$side}/{$slug}-{$name}.php";
38
		$templates[] = "{$side}/{$slug}.php";
39
40
		/**	Check if required template exists into current theme	*/
41
		$check_theme_template = array();
0 ignored issues
show
Unused Code introduced by
$check_theme_template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
42
		foreach ( $templates as $template ) {
43
			$check_theme_template = $plugin_dir_name . "/" . $template;
44
			$path = locate_template( $check_theme_template, false );
45
			if( !empty($path) ) {
46
				break;
47
			}
48
		}
49
50
		/**	Allow debugging	*/
51
		if ( !empty( $debug ) ) {
52
			echo '--- Debug mode ON - Start ---<br/>';
53
			echo __FILE__ . '<br/>';
54
			echo 'Debug for display method<br/>';
55
			echo 'Asked path ' . $path . '<br/>';
56
		}
57
58
		if ( empty( $path ) ) {
59
			foreach ( (array) $templates as $template_name ) {
60
				if ( !$template_name )
61
					continue;
62
63
				if ( !empty( $debug ) ) {
64
					echo __LINE__ . ' - ' . $main_template_dir . $template_name . '<hr/>';
65
				}
66
67
				$file_exists = file_exists( $main_template_dir . $template_name );
68
				if ( $file_exists ) {
69
					$path = $main_template_dir . $template_name;
70
					break;
71
				}
72
73
				if ( !empty( $debug ) ) {
74
					echo __LINE__ . ' - ' . (bool)$file_exists . '<hr/>';
75
				}
76
			}
77
		}
78
79
		/**	Allow debugging	*/
80
		if ( !empty( $debug ) ) {
81
			echo '--- Debug mode ON - END ---<br/><br/>';
82
		}
83
84
		return $path;
85
	}
86
87
	/**
88
	 *	Define the tools main page
89
	 */
90
	public static function main_page() {
91
		echo wpshop_display::display_template_element('wpshop_admin_tools_main_page', array(), array(), 'admin');
92
	}
93
94
	/**
95
	 *	Return a variable with some basic treatment
96
	 *
97
	 *	@param mixed $varToSanitize The variable we want to treat for future use
98
	 *	@param mixed $varDefaultValue The default value to set to the variable if the different test are not successfull
99
	 *	@param string $varType optionnal The type of the var for better verification
100
	 *
101
	 *	@return mixed $sanitizedVar The var after treatment
102
	 */
103
	public static function varSanitizer($varToSanitize, $varDefaultValue = '', $varType = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $varType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
		$sanitizedVar = is_string( $varToSanitize ) && (trim(strip_tags(stripslashes($varToSanitize))) != '') ? trim(strip_tags(stripslashes(($varToSanitize)))) : $varDefaultValue ;
105
106
		return $sanitizedVar;
107
	}
108
109
	/**
110
	 * Permit to force download a file
111
	 * @param string $Fichier_a_telecharger
112
	 * @param boolean $delete_after_download
113
	 */
114
	public static function forceDownload($Fichier_a_telecharger, $delete_after_download = false) {
115
116
		$nom_fichier = basename($Fichier_a_telecharger);
117
		switch(strrchr($nom_fichier, ".")) {
118
			case ".gz": $type = "application/x-gzip"; break;
119
			case ".tgz": $type = "application/x-gzip"; break;
120
			case ".zip": $type = "application/zip"; break;
121
			case ".pdf": $type = "application/pdf"; break;
122
			case ".png": $type = "image/png"; break;
123
			case ".gif": $type = "image/gif"; break;
124
			case ".jpg": $type = "image/jpeg"; break;
125
			case ".txt": $type = "text/plain"; break;
126
			case ".htm": $type = "text/html"; break;
127
			case ".html": $type = "text/html"; break;
128
			default: $type = "application/octet-stream"; break;
129
		}
130
131
		header("Content-disposition: attachment; filename=$nom_fichier");
132
		header("Content-Type: application/force-download");
133
		header("Content-Transfer-Encoding: $type\n"); // Surtout ne pas enlever le \n
134
		header("Content-Length: ".filesize($Fichier_a_telecharger));
135
		header("Pragma: no-cache");
136
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
137
		header("Expires: 0");
138
		ob_end_clean();
139
		readfile($Fichier_a_telecharger);
140
		if ( $delete_after_download ) {
141
			unlink( $Fichier_a_telecharger );
142
		}
143
		exit;
144
	}
145
146
	/**
147
	 * Check if Send SMS is actived
148
	 * @return boolean
149
	 */
150
	public static function is_sendsms_actived() {
151
		if(is_plugin_active('wordpress-send-sms/Send-SMS.php')) {
152
			$configOption = get_option('sendsms_config', '');
153
			$ligne = unserialize($configOption);
154
			$nicOVH = $ligne['nicOVH'];
155
			$passOVH = $ligne['passOVH'];
156
			$compteSMS = $ligne['compteSMS'];
157
			$tel_admin = $ligne['tel_admin'];
158
			return !empty($nicOVH) && !empty($passOVH) && !empty($compteSMS) && !empty($tel_admin);
159
		}
160
		return false;
161
	}
162
163
	/**
164
	 * Search all variations possibilities
165
	 * @param unknown_type $input
166
	 * @return Ambigous <multitype:, multitype:multitype:unknown  >
167
	 */
168
	public static function search_all_possibilities( $input ) {
169
		$result = array();
170
171
		while (list($key, $values) = each($input)) {
172
			if (empty($values)) {
173
				continue;
174
			}
175
176
			if (empty($result)) {
177
				foreach($values as $value) {
178
					$result[] = array($key => $value);
179
				}
180
			}
181
			else {
182
				$append = array();
183
				foreach($result as &$product) {
184
					$product[$key] = array_shift($values);
185
					$copy = $product;
186
187
					foreach($values as $item) {
188
						$copy[$key] = $item;
189
						$append[] = $copy;
190
					}
191
192
					array_unshift($values, $product[$key]);
193
				}
194
195
				$result = array_merge($result, $append);
196
			}
197
		}
198
199
		return $result;
200
	}
201
202
	/**
203
	 * Return Default currency
204
	 * @param boolean $code : false return sigle, true return code (€ or EUR)
205
	 * @return string currency code or sigle
206
	 */
207
	public static function wpshop_get_currency($code=false) {
208
		// Currency
209
		global $wpdb;
210
		$current_currency = get_option('wpshop_shop_default_currency');
211
		$query = $wpdb->prepare('SELECT * FROM ' .WPSHOP_DBT_ATTRIBUTE_UNIT. ' WHERE id =%d ', $current_currency );
212
		$currency_infos = $wpdb->get_row( $query );
213
		if ( !empty($currency_infos) ) {
214
			$code = ($code) ?  $currency_infos->name : $currency_infos->unit;
215
			return $code;
216
		}
217
		else {
218
			return '';
219
		}
220
	}
221
222
	/**
223
	 * Return unit sigle
224
	 * @param unknown_type $code
225
	 * @param unknown_type $column_to_return
226
	 */
227
	public static function wpshop_get_sigle($code, $column_to_return = "unit") {
228
		$tmp_code = (int)$code;
229
		$key_to_get = 'name';
230
		if ( is_int($tmp_code) && !empty($tmp_code) ) {
231
			$key_to_get = 'id';
232
		}
233
		$old_way_currencies = unserialize(WPSHOP_SHOP_CURRENCIES);
234
		if ( array_key_exists( $code, $old_way_currencies)) {
235
			$code = $old_way_currencies[$code];
236
			$key_to_get = 'name';
237
		}
238
239
		$current_currency = wpshop_attributes_unit::getElement($code, "'valid'", $key_to_get);
240
241
		return $current_currency->$column_to_return;
242
	}
243
244
	/**
245
	 * Clean variable
246
	 * @param string $var : variable to clean
247
	 * @return string
248
	 */
249
	public static function wpshop_clean( $var ) {
250
		return trim(strip_tags(stripslashes($var)));
251
	}
252
253
	/**
254
	 * Check if string have phone number structure
255
	 * @param   string	phone number
256
	 * @return  boolean
257
	 */
258
	public static function is_phone( $phone ) {
259
		return preg_match( '/(?=.*[0-9])([ 0-9\-\+\(\)]+)/', $phone );
260
	}
261
262
	/**
263
	 * Check if string have postcode valid structure
264
	 * @param   string	postcode
265
	 * @return  boolean
266
	 */
267
	public static function is_postcode( $postcode ) {
268
		return preg_match( '/(?=.*[0-9A-Za-z])([ \-A-Za-z0-9]+)/', $postcode );
269
	}
270
271
	/**
272
	 *	Return a form field type from a database field type
273
	 *
274
	 *	@param string $dataFieldType The database field type we want to get the form field type for
275
	 *
276
	 *	@return string $type The form input type to use for the given field
277
	 */
278
	public static function defineFieldType($dataFieldType, $input_type, $frontend_verification){
279
		$type = 'text';
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
280
281
		if ( $dataFieldType == 'datetime' ) {
282
			$type = 'text';
283
		}
284
		else {
285
			switch ( $frontend_verification ) {
286
				case 'phone':
287
					$type = 'tel';
288
					break;
289
				case 'email':
290
					$type = 'email';
291
					break;
292
				default:
293
					$type = $input_type;
294
					break;
295
			}
296
		}
297
		return $type;
298
	}
299
300
	/**
301
	 * Get the method through which the data are transferred (POST OR GET)
302
	 *
303
	 * @return array The different element send by request method
304
	 */
305
	public static function getMethode(){
306
		$request_method = null;
307
		if ($_SERVER["REQUEST_METHOD"] == "GET") {
308
			$request_method = (array)$_GET;
309
		}
310
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
311
			$request_method = (array)$_POST;
312
		}
313
314
		if ( null === $request_method ) {
315
			die ('Invalid REQUEST_METHOD (not GET, not POST).');
316
		}
317
		else {
318
			return $request_method;
319
		}
320
	}
321
322
	/**
323
	 *	Transform a given text with a specific pattern, send by the second parameter
324
	 *
325
	 *	@param string $toSlugify The string we want to "clean" for future use
326
	 *	@param array|string $slugifyType The type of cleaning we are going to do on the input text
327
	 *
328
	 *	@return string $slugified The input string that was slugified with the selected method
329
	 */
330
	public static function slugify($toSlugify, $slugifyType){
331
		$slugified = '';
332
333
		if($toSlugify != '')
334
		{
335
			$slugified = $toSlugify;
336
			foreach($slugifyType as $type)
0 ignored issues
show
Bug introduced by
The expression $slugifyType of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
337
			{
338
				if($type == 'noAccent')
339
				{
340
					$pattern = array("/&eacute;/", "/&egrave;/", "/&ecirc;/", "/&ccedil;/", "/&agrave;/", "/&acirc;/", "/&icirc;/", "/&iuml;/", "/&ucirc;/", "/&ocirc;/", "/&Egrave;/", "/&Eacute;/", "/&Ecirc;/", "/&Euml;/", "/&Igrave;/", "/&Iacute;/", "/&Icirc;/", "/&Iuml;/", "/&Ouml;/", "/&Ugrave;/", "/&Ucirc;/", "/&Uuml;/","/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/", "/�/");
341
					$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");
342
				}
343
				elseif($type == 'noSpaces')
344
				{
345
					$pattern = array('/\s/');
346
					$rep_pat = array('_');
347
					$slugified = trim($slugified);
348
				}
349
				elseif($type == 'lowerCase')
350
				{
351
					$slugified = strtolower($slugified);
352
				}
353
				elseif($type == 'noPunctuation')
354
				{
355
					$pattern = array("/#/", "/\{/", "/\[/", "/\(/", "/\)/", "/\]/", "/\}/", "/&/", "/~/", "/�/", "/`/", "/\^/", "/@/", "/=/", "/�/", "/�/", "/%/", "/�/", "/!/", "/�/", "/:/", "/\$/", "/;/", "/\./", "/,/", "/\?/", "/\\\/", "/\//");
356
					$rep_pat = array("_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_");
357
				}
358
359
				if(is_array($pattern) && is_array($rep_pat))
360
				{
361
					$slugified = preg_replace($pattern, $rep_pat, utf8_decode($slugified));
0 ignored issues
show
Bug introduced by
The variable $pattern does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $rep_pat does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
362
				}
363
			}
364
		}
365
366
		return $slugified;
367
	}
368
369
	/**
370
	 *	Trunk a string too long
371
	 *
372
	 *	@param string $string The string we want to "trunk"
373
	 *	@param int $maxlength The max length of the result string
374
	 *
375
	 *	@return string $string The output string that was trunk if necessary
376
	 */
377
	public static function trunk($string, $maxlength) {
378
		if(strlen($string)>$maxlength+3)
379
			return substr($string,0,$maxlength).'...';
380
		else return $string;
381
	}
382
383
	/**
384
	 * Run a safe redirect in javascript
385
	 */
386
	public static function wpshop_safe_redirect($url='') {
387
		$url = empty($url) ? admin_url('admin.php?page='.WPSHOP_URL_SLUG_DASHBOARD) : $url;
388
		echo '<script type="text/javascript">window.top.location.href = "'.$url.'"</script>';
389
		exit;
390
	}
391
392
	/**
393
	 * Create a custom hook action
394
	 * @param string $hook_name
395
	 * @param array $args : Hook arguments
396
	 * @return string
397
	 */
398
	public static function create_custom_hook ($hook_name, $args = '') {
399
		ob_start();
400
		if ( !empty($args) ) {
401
			do_action($hook_name, $args);
402
		}
403
		else {
404
			do_action($hook_name);
405
		}
406
		$content = ob_get_contents();
407
		ob_end_clean();
408
		return $content;
409
	}
410
411
	/**
412
	 * Return a plug-in activation code
413
	 * @param string $plugin_name
414
	 * @param string $encrypt_base_attribute
415
	 * @return string
416
	 */
417
	public static function get_plugin_validation_code($plugin_name, $encrypt_base_attribute) {
418
		$code = '';
0 ignored issues
show
Unused Code introduced by
$code is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
419
		if ( !function_exists( 'get_plugin_data') )
420
			require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
421
		$plug = get_plugin_data( WP_PLUGIN_DIR . '/' . WPSHOP_PLUGIN_DIR . '/wpshop.php' );
422
		$code_part = array();
423
		$code_part[] = substr(hash ( "sha256" , $plugin_name ), WPSHOP_ADDONS_KEY_IS, 5);
424
		$code_part[] = substr(hash ( "sha256" , $plug['Name'] ), WPSHOP_ADDONS_KEY_IS, 5);
425
		$code_part[] = substr(hash ( "sha256" , 'addons' ), WPSHOP_ADDONS_KEY_IS, 5);
426
		$code = $code_part[1] . '-' . $code_part[2] . '-' . $code_part[0];
427
		$att = $encrypt_base_attribute;
428
		$code .= '-' . substr(hash ( "sha256" , $att ),  WPSHOP_ADDONS_KEY_IS, 5);
429
430
		return $code;
431
	}
432
433
	/**
434
	 * Check the WPShop Add-ons encrypt code validity
435
	 * @param string $plugin_name
436
	 * @param string $encrypt_base_attribute
437
	 * @return boolean
438
	 */
439
	public static function check_plugin_activation_code( $plugin_name, $encrypt_base_attribute, $from = 'file') {
0 ignored issues
show
Unused Code introduced by
The parameter $from is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
440
		$is_valid = false;
441
		$valid_activation_code = self::get_plugin_validation_code($plugin_name, $encrypt_base_attribute);
442
		$activation_code_filename = WP_PLUGIN_DIR .'/'. $plugin_name.'/encoder.txt';
443
		if ( is_file($activation_code_filename) ) {
444
			$activation_code_file = fopen($activation_code_filename, 'r' );
445
			if ( $activation_code_file !== false) {
446
				$activation_code = fread( $activation_code_file, filesize($activation_code_filename));
447
				if ( $activation_code == $valid_activation_code ) {
448
					$is_valid = true;
449
				}
450
			}
451
		}
452
		return $is_valid;
453
	}
454
455
	/**
456
	 * Formate number, Add span on cents on hide cents if equals zero
457
	 * @param unknown_type $number
458
	 * @return string
459
	 */
460
	public static function formate_number( $number ) {
461
		$number = number_format( $number, 2, '.', '' );
462
		$exploded_number = explode( '.', $number );
463
		$number = $exploded_number[0];
464
		if( $exploded_number[1] != '00' ) {
465
			$number .= '<span class="wps_number_cents">,' . $exploded_number[1]. '</span>';
466
		}
467
		return $number;
468
	}
469
470
	/**
471
	 * Return the translated element id of a page
472
	 * @param int $page_id
473
	 * @return int
474
	 */
475
	public static function get_page_id( $page_id ) {
476
		if( !empty($page_id) ) {
477
			if ( function_exists( 'icl_object_id' ) && defined('ICL_LANGUAGE_CODE') ) {
478
				$element_post_type = get_post_type( $page_id );
479
				$translated_element_id = icl_object_id( $page_id, $element_post_type, true, ICL_LANGUAGE_CODE );
480
				if( !empty($translated_element_id) ) {
481
					$page_id = $translated_element_id;
482
				}
483
			}
484
		}
485
		return $page_id;
486
	}
487
488
	public static function minutes_to_time( $minutes, $format = '%hh %imin' ) {
489
		$dtF = new \DateTime( '@0' );
490
		$dtT = new \DateTime( '@' . ( $minutes * 60 ) );
491
		return $dtF->diff($dtT)->format( $format );
492
	}
493
494
	public static function number_format_hack($n) {
495
		return number_format($n, 5, '.', '');
496
	}
497
	public static function is_serialized( $data, $strict = true ) {
498
		if ( ! is_string( $data ) ) {
499
			return false;
500
		}
501
		$data = trim( $data );
502
		if ( 'N;' == $data ) {
503
			return true;
504
		}
505
		if ( strlen( $data ) < 4 ) {
506
			return false;
507
		}
508
		if ( ':' !== $data[1] ) {
509
			return false;
510
		}
511
		if ( $strict ) {
512
			$lastc = substr( $data, -1 );
513
			if ( ';' !== $lastc && '}' !== $lastc ) {
514
				return false;
515
			}
516
		} else {
517
			$semicolon = strpos( $data, ';' );
518
			$brace = strpos( $data, '}' );
519
			if ( false === $semicolon && false === $brace ) {
520
				return false;
521
			}
522
			if ( false !== $semicolon && $semicolon < 3 ) {
523
				return false;
524
			}
525
			if ( false !== $brace && $brace < 4 ) {
526
				return false;
527
			}
528
		}
529
		$token = $data[0];
530
		switch ( $token ) {
531
			case 's' :
532
				if ( $strict ) {
533
					if ( '"' !== substr( $data, -2, 1 ) ) {
534
						return false;
535
					}
536
				} elseif ( false === strpos( $data, '"' ) ) {
537
					return false;
538
				}
539
			case 'a' :
540
			case 'O' :
541
				return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
542
			case 'b' :
543
			case 'i' :
544
			case 'd' :
545
				$end = $strict ? '$' : '';
546
				return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data );
547
		}
548
		return false;
549
	}
550
}
551