Test Failed
Push — master ( f48a23...7b3a74 )
by Stiofan
66:46
created

helper_functions.php ➔ geodir_date()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 10
c 1
b 0
f 1
nc 3
nop 3
dl 0
loc 17
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
1
<?php
2
/**
3
 * Helper functions, this file contains functions made to make a developer's life easier.
4
 *
5
 * @since 1.4.6
6
 * @package GeoDirectory
7
 */
8
9
/**
10
 * Get the page ID of the add listing page.
11
 *
12
 * @package Geodirectory
13
 * @since 1.4.6
14
 * @return int|null Return the page ID if present or null if not.
15
 */
16
function geodir_add_listing_page_id(){
17 16
    $gd_page_id = get_option('geodir_add_listing_page');
18
19 16
    if (function_exists('icl_object_id')) {
20
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
21
    }
22
23 16
    return $gd_page_id;
24
}
25
26
/**
27
 * Get the page ID of the add listing preview page.
28
 *
29
 * @package Geodirectory
30
 * @since 1.4.6
31
 * @return int|null Return the page ID if present or null if not.
32
 */
33
function geodir_preview_page_id(){
34 6
    $gd_page_id = get_option('geodir_preview_page');
35
36 6
    if (function_exists('icl_object_id')) {
37
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
38
    }
39
40 6
    return $gd_page_id;
41
}
42
43
/**
44
 * Get the page ID of the add listing success page.
45
 *
46
 * @package Geodirectory
47
 * @since 1.4.6
48
 * @return int|null Return the page ID if present or null if not.
49
 */
50
function geodir_success_page_id(){
51 4
    $gd_page_id = get_option('geodir_success_page');
52
53 4
    if (function_exists('icl_object_id')) {
54
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
55
    }
56
57 4
    return $gd_page_id;
58
}
59
60
/**
61
 * Get the page ID of the add location page.
62
 *
63
 * @package Geodirectory
64
 * @since 1.4.6
65
 * @return int|null Return the page ID if present or null if not.
66
 */
67
function geodir_location_page_id(){
68 4
    $gd_page_id = get_option('geodir_location_page');
69
70 4
    if (function_exists('icl_object_id')) {
71
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
72
    }
73
74 4
    return $gd_page_id;
75
}
76
77
/**
78
 * Get the page ID of the GD home page.
79
 *
80
 * @package Geodirectory
81
 * @since 1.5.4
82
 * @return int|null Return the page ID if present or null if not.
83
 */
84
function geodir_home_page_id(){
85 5
    $gd_page_id = get_option('geodir_home_page');
86
87 5
    if (function_exists('icl_object_id')) {
88
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
89
    }
90
91 5
    return $gd_page_id;
92
}
93
94
/**
95
 * Get the page ID of the info page.
96
 *
97
 * @package Geodirectory
98
 * @since 1.5.3
99
 * @return int|null Return the page ID if present or null if not.
100
 */
101
function geodir_info_page_id(){
102
    $gd_page_id = get_option('geodir_info_page');
103
104
    if (function_exists('icl_object_id')) {
105
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
106
    }
107
108
    return $gd_page_id;
109
}
110
111
/**
112
 * Get the page ID of the login page.
113
 *
114
 * @package Geodirectory
115
 * @since 1.5.3
116
 * @return int|null Return the page ID if present or null if not.
117
 */
118
function geodir_login_page_id(){
119 5
    $gd_page_id = get_option('geodir_login_page');
120
121 5
    if (function_exists('icl_object_id')) {
122
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
123
    }
124
125 5
    return $gd_page_id;
126
}
127
128
129
/**
130
 * Get the page ID of the login page.
131
 *
132
 * @package Geodirectory
133
 * @since 1.5.3
134
 * @return int|null Return the page ID if present or null if not.
135
 */
136
function geodir_login_url($args=array()){
137 21
    $gd_page_id = get_option('geodir_login_page');
138
139 21
    if (function_exists('icl_object_id')) {
140
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
141
    }
142
143 21
    if (function_exists('geodir_location_geo_home_link')) {
144
        remove_filter('home_url', 'geodir_location_geo_home_link', 100000);
145
    }
146 21
    $home_url = get_home_url();
147 21
    if (function_exists('geodir_location_geo_home_link')) {
148
        add_filter('home_url', 'geodir_location_geo_home_link', 100000, 2);
149
    }
150
151 21 View Code Duplication
    if($gd_page_id){
152 21
        $post = get_post($gd_page_id);
153 21
        $slug = $post->post_name;
154
        //$login_url = get_permalink($gd_page_id );// get_permalink can only be user after theme-Setup hook, any earlier and it errors
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155 21
        $login_url = trailingslashit($home_url)."$slug/";
156 21
    }else{
157
        $login_url = trailingslashit($home_url)."?geodir_signup=true";
158
    }
159
160 21
    if($args){
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
161 10
        $login_url = add_query_arg($args,$login_url );
162 10
    }
163
164
    /**
165
     * Filter the GeoDirectory login page url.
166
     *
167
     * This filter can be used to change the GeoDirectory page url.
168
     *
169
     * @since 1.5.3
170
     * @package GeoDirectory
171
     * @param string $login_url The url of the login page.
172
     * @param array $args The array of query args used.
173
     * @param int $gd_page_id The page id of the GD login page.
174
     */
175 21
    return apply_filters('geodir_login_url',$login_url,$args,$gd_page_id);
176
}
177
178
/**
179
 * Returns info page url
180
 *
181
 * @package Geodirectory
182
 * @since 1.5.4
183
 * @return string Info page url.
184
 */
185
function geodir_info_url($args=array()){
186
    $gd_page_id = get_option('geodir_info_page');
187
188
    if (function_exists('icl_object_id')) {
189
        $gd_page_id =  icl_object_id($gd_page_id, 'page', true);
190
    }
191
192
    if (function_exists('geodir_location_geo_home_link')) {
193
        remove_filter('home_url', 'geodir_location_geo_home_link', 100000);
194
    }
195
    $home_url = get_home_url();
196
    if (function_exists('geodir_location_geo_home_link')) {
197
        add_filter('home_url', 'geodir_location_geo_home_link', 100000, 2);
198
    }
199
200 View Code Duplication
    if($gd_page_id){
201
        $post = get_post($gd_page_id);
202
        $slug = $post->post_name;
203
        //$login_url = get_permalink($gd_page_id );// get_permalink can only be user after theme-Setup hook, any earlier and it errors
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
204
        $info_url = trailingslashit($home_url)."$slug/";
205
    }else{
206
        $info_url = trailingslashit($home_url);
207
    }
208
209
    if($args){
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
210
        $info_url = add_query_arg($args,$info_url );
211
    }
212
213
    return $info_url;
214
}
215
216
/**
217
 * Converts string to title case.
218
 *
219
 * This function converts string to title case. Ex: hello world -> Hello World.
220
 * When mbstring php extension available this function supports all unicode characters.
221
 *
222
 * @package Geodirectory
223
 * @since 1.5.4
224
 * @param string $string String to convert.
225
 * @param string $charset Character set to use for conversion.
226
 * @return string Returns converted string.
227
 */
228
function geodir_ucwords($string, $charset='UTF-8') {
229 8
    if (function_exists('mb_convert_case')) {
230 8
        return mb_convert_case($string, MB_CASE_TITLE, $charset);
231
    } else {
232
        return ucwords($string);
233
    }
234
}
235
236
/**
237
 * Converts string to lower case.
238
 *
239
 * This function converts string to lower case. Ex: HelLO WorLd -> hello world.
240
 * When mbstring php extension available this function supports all unicode characters.
241
 *
242
 * @package Geodirectory
243
 * @since 1.5.4
244
 * @param string $string String to convert.
245
 * @param string $charset Character set to use for conversion.
246
 * @return string Returns converted string.
247
 */
248
function geodir_strtolower($string, $charset='UTF-8') {
249 16
    if (function_exists('mb_convert_case')) {
250 16
        return mb_convert_case($string, MB_CASE_LOWER, $charset);
251
    } else {
252
        return strtolower($string);
253
    }
254
}
255
256
/**
257
 * Converts string to upper case.
258
 *
259
 * This function converts string to upper case. Ex: HelLO WorLd -> HELLO WORLD.
260
 * When mbstring php extension available this function supports all unicode characters.
261
 *
262
 * @package Geodirectory
263
 * @since 1.5.4
264
 * @param string $string String to convert.
265
 * @param string $charset Character set to use for conversion.
266
 * @return string Returns converted string.
267
 */
268
function geodir_strtoupper($string, $charset='UTF-8') {
269 4
    if (function_exists('mb_convert_case')) {
270 4
        return mb_convert_case($string, MB_CASE_UPPER, $charset);
271
    } else {
272
        return strtoupper($string);
273
    }
274
}
275
276
/**
277
 * Returns parsed url and title.
278
 *
279
 * This function converts string to url and title if there is "|" separator used in url.
280
 * Ex: "http://wpgeodirectory.com|GeoDirectory" will return array( url => http://wpgeodirectory.com, label => GeoDirectory ).
281
 *
282
 * @package Geodirectory
283
 * @since 1.5.7
284
 * @param string $url The website url.
285
 * @param bool $formatted True if returns formatted url. False if not. Default true.
286
 * @return array Parsed url and title.
287
 */
288
function geodir_parse_custom_field_url($url, $formatted = true) {
289 2
	if ($url == '' || !is_string($url)) {
290
		return NULL;
291
	}
292 2
	$original_url = $url;
0 ignored issues
show
Unused Code introduced by
$original_url 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...
293
	
294 2
	$url = stripcslashes($url);
295 2
	$parts = explode('|', $url, 2);
296
	
297 2
	$url = trim($parts[0]);
298 2
	if ($formatted && $url != '') {
299 2
		$url = str_replace( ' ', '%20', $url );
300 2
		$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url);
301
		
302 2
		if (0 !== stripos($url, 'mailto:')) {
303 2
			$strip = array('%0d', '%0a', '%0D', '%0A');
304 2
			$url = _deep_replace($strip, $url);
305 2
		}
306
		
307 2
		$url = str_replace(';//', '://', $url);
308
		
309 2
		if (strpos($url, ':') === false && ! in_array($url[0], array('/', '#', '?')) && !preg_match('/^[a-z0-9-]+?\.php/i', $url)) {
310
			$url = 'http://' . $url;
311
		}
312
		
313 2
		$url = wp_kses_normalize_entities($url);
314 2
		$url = str_replace('&amp;', '&#038;', $url);
315 2
		$url = str_replace("'", '&#039;', $url);
316 2
	}
317
	
318 2
	$return = array();
319 2
	$return['url'] = $url;
320 2
	if (!empty($parts[1]) && trim($parts[1]) != '') {
321
		$return['label'] = trim($parts[1]);
322
	}
323
324 2
	return $return;
325
}
326
327
/**
328
 * Set parent categories to fix categories tree structure.
329
 *
330
 * @since 1.5.7
331
 * @package GeoDirectory
332
 *
333
 * @param array $request_info Listing request info.
334
 * @return array Modified listing request info.
335
 */
336
function geodir_attach_parent_categories($request_info) {
337 6
	if (!empty($request_info['post_category']) && is_array($request_info['post_category'])) {
338 1
		foreach ($request_info['post_category'] as $taxomony => $term_ids) {			
339 1
			$attach_term_ids = array();
0 ignored issues
show
Unused Code introduced by
$attach_term_ids 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...
340
			
341 1
			if (!empty($term_ids) && is_array($term_ids) && taxonomy_exists($taxomony) && strpos($taxomony, 'category') !== false) {
342 1
				$attach_term_ids = geodir_add_parent_terms($term_ids, $taxomony);
343
				
344 1
				if (!empty($attach_term_ids)) {
345 1
					if (!isset($request_info['post_default_category'])) {
346 1
						$request_info['post_default_category'] = $attach_term_ids[0];
347 1
					}
348 1
					$request_info['post_category'][$taxomony] = $attach_term_ids;
349 1
				}
350 1
			}
351 1
		}
352 1
	}
353
	
354 6
	return $request_info;
355
}
356
357
/**
358
 * Add the parent terms in current terms.
359
 *
360
 * @since 1.5.7
361
 * @package GeoDirectory
362
 *
363
 * @param int|array $term_ids Term id or Terms array.
364
 * @param string $taxomony Category taxonomy of post type.
365
 * @return array Modified term ids.
366
 */
367
function geodir_add_parent_terms($term_ids, $taxomony) {	
368 1
	if (is_int($term_ids)) {
369
		$term_ids = array($term_ids);
370
	}
371
	
372 1
	$parent_terms = array();
373
	
374 1
	foreach ($term_ids as $term_id) {
375 1
		$parent_terms[] = $term_id;
376 1
		$term_parents = geodir_get_category_parents($term_id, $taxomony, $parent_terms);
377
		
378 1
		if (!empty($term_parents)) {
379
			$parent_terms = array_merge($parent_terms, $term_parents);
380
		}
381 1
	}
382
383 1
	return $parent_terms;
384
}
385
386
/**
387
 * Get the parent categories of current id.
388
 *
389
 * @since 1.5.7
390
 * @package GeoDirectory
391
 *
392
 * @param int $id Category id.
393
 * @param string $taxomony Category taxonomy of post type.
394
 * @param array $visited Array of category ids already included.
395
 * @param array $parents Array of category ids.
396
 * @return array Category ids.
397
 */
398
function geodir_get_category_parents($id, $taxomony, $visited = array(), $parents = array()) {
399 1
	$parent = get_term($id, $taxomony);
400 1
	if (is_wp_error($parent)) {
401
		return $parents;
402
	}
403
404 1
	if (isset($parent->parent) && $parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
405
		$visited[] = $parent->parent;
406
		$parents[] = $parent->parent;
407
		$parents = geodir_get_category_parents($parent->parent, $taxomony, $visited, $parents);
408
	}
409
410 1
	return $parents;
411
}
412
413
if (!function_exists('geodir_get_ip')) {
414
/**
415
 * Get the visitor's IP address.
416
 *
417
 * @since 1.5.7
418
 * @package GeoDirectory
419
 *
420
 * @return string The IP address.
421
 */
422
function geodir_get_ip() {
423
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
424
		//check ip from share internet
425
		$ip = $_SERVER['HTTP_CLIENT_IP'];
426
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
427
		//to check ip is pass from proxy
428
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
429
	} else {
430
		$ip = $_SERVER['REMOTE_ADDR'];
431
	}
432
	
433
	/**
434
	 * Filter the the visitor's IP address.
435
	 *
436
	 * @since 1.5.7
437
	 * @package GeoDirectory
438
	 *
439
	 * @param string $ip The IP address.
440
	 */
441
	return apply_filters('geodir_get_ip', $ip);
442
}
443
}
444
445
/**
446
 * Register die handler for gd_die()
447
 *
448
 * @since 1.5.9
449
 * @package GeoDirectory
450
 */
451
function _gd_die_handler() {
452 15
    if ( defined( 'GD_TESTING_MODE' ) ) {
453 15
        return '_gd_die_handler';
454
    } else {
455
        die();
456
    }
457
}
458
459
/**
460
 * Wrapper function for wp_die(). This function adds filters for wp_die() which
461
 * kills execution of the script using wp_die(). This allows us to then to work
462
 * with functions using gd_die() in the unit tests.
463
 *
464
 * @since 1.5.9
465
 * @package GeoDirectory
466
 * @param string $message Optional. Error message.
467
 * @param string $title   Optional. Error title.
468
 * @param int $status     Optional. Status code.
469
 */
470
function gd_die( $message = '', $title = '', $status = 400 ) {
471 15
    add_filter( 'wp_die_ajax_handler', '_gd_die_handler', 10, 3 );
472 15
    add_filter( 'wp_die_handler', '_gd_die_handler', 10, 3 );
473 15
    wp_die( $message, $title, array( 'response' => $status ));
474 15
}
475
476
/*
477
 * Matches each symbol of PHP date format standard with jQuery equivalent codeword
478
 *
479
 * @since 1.6.5
480
 * @param string $php_format The PHP date format.
481
 * @return string The jQuery format date string.
482
 */
483
function geodir_date_format_php_to_jqueryui( $php_format ) {
484
	$symbols = array(
485
		// Day
486
		'd' => 'dd',
487
		'D' => 'D',
488
		'j' => 'd',
489
		'l' => 'DD',
490
		'N' => '',
491
		'S' => '',
492
		'w' => '',
493
		'z' => 'o',
494
		// Week
495
		'W' => '',
496
		// Month
497
		'F' => 'MM',
498
		'm' => 'mm',
499
		'M' => 'M',
500
		'n' => 'm',
501
		't' => '',
502
		// Year
503
		'L' => '',
504
		'o' => '',
505
		'Y' => 'yy',
506
		'y' => 'y',
507
		// Time
508
		'a' => 'tt',
509
		'A' => 'TT',
510
		'B' => '',
511
		'g' => 'h',
512
		'G' => 'H',
513
		'h' => 'hh',
514
		'H' => 'HH',
515
		'i' => 'mm',
516
		's' => '',
517
		'u' => ''
518
	);
519
520
	$jqueryui_format = "";
521
	$escaping = false;
522
523
	for ( $i = 0; $i < strlen( $php_format ); $i++ ) {
524
		$char = $php_format[$i];
525
526
		// PHP date format escaping character
527
		if ( $char === '\\' ) {
528
			$i++;
529
530
			if ( $escaping ) {
531
				$jqueryui_format .= $php_format[$i];
532
			} else {
533
				$jqueryui_format .= '\'' . $php_format[$i];
534
			}
535
536
			$escaping = true;
537
		} else {
538
			if ( $escaping ) {
539
				$jqueryui_format .= "'";
540
				$escaping = false;
541
			}
542
543
			if ( isset( $symbols[$char] ) ) {
544
				$jqueryui_format .= $symbols[$char];
545
			} else {
546
				$jqueryui_format .= $char;
547
			}
548
		}
549
	}
550
551
	return $jqueryui_format;
552
}
553
554
/**
555
 * Maybe untranslate date string for saving to the database.
556
 *
557
 * @param string $date The date string.
558
 *
559
 * @return string The untranslated date string.
560
 * @since 1.6.5
561
 */
562
function geodir_maybe_untranslate_date($date){
563
	$english_long_months = array(
564
		'January',
565
		'February',
566
		'March',
567
		'April',
568
		'May',
569
		'June',
570
		'July',
571
		'August',
572
		'September',
573
		'October',
574
		'November',
575
		'December',
576
	);
577
578
	$non_english_long_months  = array(
579
		__('January'),
580
		__('February'),
581
		__('March'),
582
		__('April'),
583
		__('May'),
584
		__('June'),
585
		__('July'),
586
		__('August'),
587
		__('September'),
588
		__('October'),
589
		__('November'),
590
		__('December'),
591
	);
592
	$date = str_replace($non_english_long_months,$english_long_months,$date);
593
594
595
	$english_short_months = array(
596
		' Jan ',
597
		' Feb ',
598
		' Mar ',
599
		' Apr ',
600
		' May ',
601
		' Jun ',
602
		' Jul ',
603
		' Aug ',
604
		' Sep ',
605
		' Oct ',
606
		' Nov ',
607
		' Dec ',
608
	);
609
610
	$non_english_short_months = array(
611
		' '._x( 'Jan', 'January abbreviation' ).' ',
612
		' '._x( 'Feb', 'February abbreviation' ).' ',
613
		' '._x( 'Mar', 'March abbreviation' ).' ',
614
		' '._x( 'Apr', 'April abbreviation' ).' ',
615
		' '._x( 'May', 'May abbreviation' ).' ',
616
		' '._x( 'Jun', 'June abbreviation' ).' ',
617
		' '._x( 'Jul', 'July abbreviation' ).' ',
618
		' '._x( 'Aug', 'August abbreviation' ).' ',
619
		' '._x( 'Sep', 'September abbreviation' ).' ',
620
		' '._x( 'Oct', 'October abbreviation' ).' ',
621
		' '._x( 'Nov', 'November abbreviation' ).' ',
622
		' '._x( 'Dec', 'December abbreviation' ).' ',
623
	);
624
625
	$date = str_replace($non_english_short_months,$english_short_months,$date);
626
627
628
	return $date;
629
}
630
631
/**
632
 * Convert date to given format.
633
 *
634
 * @since 1.6.7
635
 *
636
 * @param string $date The date string.
637
 * @param string $date_to The destination date format.
638
 * @param string $date_from The source date format.
639
 * @return string The formatted date.
640
 */
641
function geodir_date($date, $date_to, $date_from) {
642
    if (empty($date)) {
643
        return $date;
644
    }
645
    
646
    $datetime = date_create_from_format($date_from, $date);
647
    
648
    if (empty($datetime)) {
649
        $date = date_i18n($date_to, strtotime($date));
650
    } else {
651
        $date = $datetime->format($date_to);
652
    }
653
    
654
    $date = geodir_maybe_untranslate_date($date);
655
    
656
    return $date;
657
}