Test Failed
Push — master ( d47f34...2b52fe )
by Stiofan
08:45
created

general_functions.php ➔ geodir_getlink()   D

Complexity

Conditions 9
Paths 26

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11.3597

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 21
c 1
b 0
f 0
nc 26
nop 3
dl 0
loc 32
ccs 9
cts 13
cp 0.6923
crap 11.3597
rs 4.909
1
<?php
2
/**
3
 * Plugin general functions
4
 *
5
 * @since   1.0.0
6
 * @package GeoDirectory
7
 */
8
9
10
/**
11
 * Get All Plugin functions from WordPress
12
 */
13
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
14
15
/*-----------------------------------------------------------------------------------*/
16
/* Helper functions */
17
/*-----------------------------------------------------------------------------------*/
18
19
/**
20
 * Return the plugin url.
21
 *
22
 * Return the plugin folder url WITHOUT TRAILING SLASH.
23
 *
24
 * @since   1.0.0
25
 * @package GeoDirectory
26
 * @return string example url eg: http://wpgeo.directory/wp-content/plugins/geodirectory
27
 */
28
function geodir_plugin_url() {
29
30
	if ( is_ssl() ) :
31 21
		return str_replace( 'http://', 'https://', WP_PLUGIN_URL ) . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) );
32
	else :
33
		return WP_PLUGIN_URL . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) );
34 21
	endif;
35
}
36
37
38
/**
39
 * Return the plugin path.
40
 *
41
 * Return the plugin folder path WITHOUT TRAILING SLASH.
42
 *
43
 * @since   1.0.0
44
 * @package GeoDirectory
45
 * @return string example url eg: /home/geo/public_html/wp-content/plugins/geodirectory
46
 */
47
function geodir_plugin_path() {
48
	if ( defined( 'GD_TESTING_MODE' ) && GD_TESTING_MODE ) {
49
		return dirname( dirname( __FILE__ ) );
50 34
	} else {
51 34
		return WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) );
52
	}
53
}
54
55
/**
56
 * Check if a GeoDirectory addon plugin is active.
57
 *
58
 * @since   1.0.0
59
 * @package GeoDirectory
60
 *
61
 * @param string $plugin plugin uri.
62
 *
63
 * @return bool true or false.
64
 * @todo    check if this is faster than normal WP check and remove if not.
65
 */
66
function geodir_is_plugin_active( $plugin ) {
67
	$active_plugins = get_option( 'active_plugins' );
68
	foreach ( $active_plugins as $key => $active_plugin ) {
69
		if ( strstr( $active_plugin, $plugin ) ) {
70
			return true;
71
		}
72
	}
73
74
	return false;
75
}
76
77
78
/**
79
 * Return the formatted date.
80
 *
81
 * Return a formatted date from a date/time string according to WordPress date format. $date must be in format : 'Y-m-d
82
 * H:i:s'.
83
 *
84
 * @since   1.0.0
85
 * @package GeoDirectory
86
 *
87
 * @param string $date must be in format: 'Y-m-d H:i:s'.
88
 *
89
 * @return bool|int|string the formatted date.
90
 */
91
function geodir_get_formated_date( $date ) {
0 ignored issues
show
Best Practice introduced by
The function geodir_get_formated_date() has been defined more than once; this definition is ignored, only the first definition in geodirectory-functions/custom_fields_functions.php (L2331-2334) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
92
	return mysql2date( get_option( 'date_format' ), $date );
93
}
94
95
/**
96
 * Return the formatted time.
97
 *
98
 * Return a formatted time from a date/time string according to WordPress time format. $time must be in format : 'Y-m-d
99
 * H:i:s'.
100
 *
101
 * @since   1.0.0
102
 * @package GeoDirectory
103
 *
104
 * @param string $time must be in format: 'Y-m-d H:i:s'.
105
 *
106
 * @return bool|int|string the formatted time.
107
 */
108
function geodir_get_formated_time( $time ) {
0 ignored issues
show
Best Practice introduced by
The function geodir_get_formated_time() has been defined more than once; this definition is ignored, only the first definition in geodirectory-functions/custom_fields_functions.php (L2346-2349) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
109
	return mysql2date( get_option( 'time_format' ), $time, $translate = true );
110
}
111
112
113
/**
114
 * Return a formatted link with parameters.
115
 *
116
 * Returns a link with new parameters and currently used parameters regardless of ? or & in the $url parameter.
117
 *
118
 * @since   1.0.0
119
 * @package GeoDirectory
120
 *
121 15
 * @param string $url                  The main url to be used.
122 15
 * @param array $params                The arguments array.
123 15
 * @param bool $use_existing_arguments Do you want to use existing arguments? Default: false.
124 15
 *
125 15
 * @return string Formatted link.
126 15
 */
127
function geodir_getlink( $url, $params = array(), $use_existing_arguments = false ) {
128 15
	if ( $use_existing_arguments ) {
129 15
		$params = $params + $_GET;
130 15
	}
131
	if ( ! $params ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params 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...
132
		return $url;
133
	}
134
	$link = $url;
135 15
	if ( strpos( $link, '?' ) === false ) {
136
		$link .= '?';
137 15
	} //If there is no '?' add one at the end
138 15
	elseif ( strpos( $link, '//maps.google.com/maps/api/js?language=' ) ) {
139
		$link .= '&amp;';
140 15
	} //If there is no '&' at the END, add one.
141
	elseif ( ! preg_match( '/(\?|\&(amp;)?)$/', $link ) ) {
142
		$link .= '&';
143
	} //If there is no '&' at the END, add one.
144
145
	$params_arr = array();
146
	foreach ( $params as $key => $value ) {
147
		if ( gettype( $value ) == 'array' ) { //Handle array data properly
148
			foreach ( $value as $val ) {
149
				$params_arr[] = $key . '[]=' . urlencode( $val );
150
			}
151
		} else {
152
			$params_arr[] = $key . '=' . urlencode( $value );
153
		}
154
	}
155 3
	$link .= implode( '&', $params_arr );
156
157
	return $link;
158 3
}
159 3
160
161 3
/**
162
 * Returns add listing page link.
163 3
 *
164
 * @since   1.0.0
165
 * @package GeoDirectory
166
 * @global object $wpdb     WordPress Database object.
167
 *
168
 * @param string $post_type The post type.
169
 *
170
 * @return string Listing page url if valid. Otherwise home url will be returned.
171
 */
172
function geodir_get_addlisting_link( $post_type = '' ) {
173
	global $wpdb;
174
175
	//$check_pkg  = $wpdb->get_var("SELECT pid FROM ".GEODIR_PRICE_TABLE." WHERE post_type='".$post_type."' and status != '0'");
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
176
	$check_pkg = 1;
177
	if ( post_type_exists( $post_type ) && $check_pkg ) {
178 9
179 9
		$add_listing_link = get_page_link( geodir_add_listing_page_id() );
180
181
		return esc_url( add_query_arg( array( 'listing_type' => $post_type ), $add_listing_link ) );
182 9
	} else {
183 9
		return get_bloginfo( 'url' );
184
	}
185
}
186
187
/**
188
 * Get the current page URL.
189
 *
190 9
 * @since   1.0.0
191
 * @package GeoDirectory
192
 * @since   1.4.2 Removed the port number from the URL if port 80 is not being used.
193
 * @return string The current URL.
194
 */
195
function geodir_curPageURL() {
196
	$pageURL = 'http';
197
	if ( isset( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] == "on" ) {
198
		$pageURL .= "s";
199
	}
200
	$pageURL .= "://";
201
	$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
202
203
	/**
204
	 * Filter the current page URL returned by function geodir_curPageURL().
205
	 *
206
	 * @since 1.4.1
207
	 *
208
	 * @param string $pageURL The URL of the current page.
209
	 */
210
	return apply_filters( 'geodir_curPageURL', $pageURL );
211
}
212
213
214
/**
215
 * Clean variables.
216
 *
217
 * This function is used to create posttype, posts, taxonomy and terms slug.
218
 *
219
 * @since   1.0.0
220
 * @package GeoDirectory
221
 *
222
 * @param string $string The variable to clean.
223
 *
224
 * @return string Cleaned variable.
225
 */
226
function geodir_clean( $string ) {
227
228
	$string = trim( strip_tags( stripslashes( $string ) ) );
229
	$string = str_replace( " ", "-", $string ); // Replaces all spaces with hyphens.
230
	$string = preg_replace( '/[^A-Za-z0-9\-\_]/', '', $string ); // Removes special chars.
231
	$string = preg_replace( '/-+/', '-', $string ); // Replaces multiple hyphens with single one.
232
233
	return $string;
234
}
235
236
/**
237
 * Get Week Days list.
238
 *
239
 * @since   1.0.0
240
 * @package GeoDirectory
241
 * @return array Week days.
242
 */
243
function geodir_get_weekday() {
244
	return array(
245
		__( 'Sunday', 'geodirectory' ),
246
		__( 'Monday', 'geodirectory' ),
247
		__( 'Tuesday', 'geodirectory' ),
248
		__( 'Wednesday', 'geodirectory' ),
249
		__( 'Thursday', 'geodirectory' ),
250
		__( 'Friday', 'geodirectory' ),
251
		__( 'Saturday', 'geodirectory' )
252
	);
253
}
254
255 28
/**
256
 * Get Weeks lists.
257
 *
258
 * @since   1.0.0
259 28
 * @package GeoDirectory
260
 * @return array Weeks.
261 25
 */
262 1
function geodir_get_weeks() {
263 24
	return array(
264
		__( 'First', 'geodirectory' ),
265
		__( 'Second', 'geodirectory' ),
266
		__( 'Third', 'geodirectory' ),
267 24
		__( 'Fourth', 'geodirectory' ),
268 27
		__( 'Last', 'geodirectory' )
269 22
	);
270 22
}
271 1
272 22
273 21
/**
274 27
 * Check that page is.
275 6
 *
276 6
 * @since   1.0.0
277 5
 * @since   1.5.6 Added to check GD invoices and GD checkout pages.
278 27
 * @since   1.5.7 Updated to validate buddypress dashboard listings page as a author page.
279 12
 * @package GeoDirectory
280 12
 * @global object $wp_query WordPress Query object.
281 12
 * @global object $post     The current post object.
282 12
 *
283 10
 * @param string $gdpage    The page type.
284 26
 *
285 7
 * @return bool If valid returns true. Otherwise false.
286 7
 */
287 7
function geodir_is_page( $gdpage = '' ) {
288 7
289
	global $wp_query, $post, $wp;
290 7
	//if(!is_admin()):
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% 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...
291 26
292 12
	switch ( $gdpage ):
293
		case 'add-listing':
294
295
			if ( is_page() && get_query_var( 'page_id' ) == geodir_add_listing_page_id() ) {
296
				return true;
297 12
			} elseif ( is_page() && isset( $post->post_content ) && has_shortcode( $post->post_content, 'gd_add_listing' ) ) {
298 12
				return true;
299 12
			}
300 12
301
			break;
302 12
		case 'preview':
303 25
			if ( ( is_page() && get_query_var( 'page_id' ) == geodir_preview_page_id() ) && isset( $_REQUEST['listing_type'] )
304
			     && in_array( $_REQUEST['listing_type'], geodir_get_posttypes() )
305 8
			) {
306 8
				return true;
307
			}
308 9
			break;
309 25
		case 'listing-success':
310 7
			if ( is_page() && get_query_var( 'page_id' ) == geodir_success_page_id() ) {
311 7
				return true;
312 6
			}
313 25
			break;
314 24 View Code Duplication
		case 'detail':
315 24
			$post_type = get_query_var( 'post_type' );
316
			if ( is_array( $post_type ) ) {
317 24
				$post_type = reset( $post_type );
318
			}
319
			if ( is_single() && in_array( $post_type, geodir_get_posttypes() ) ) {
320
				return true;
321
			}
322 24
			break;
323 25 View Code Duplication
		case 'pt':
324 24
			$post_type = get_query_var( 'post_type' );
325 24
			if ( is_array( $post_type ) ) {
326 23
				$post_type = reset( $post_type );
327 10
			}
328 1
			if ( is_post_type_archive() && in_array( $post_type, geodir_get_posttypes() ) && ! is_tax() ) {
329 1
				return true;
330 1
			}
331 9
332 9
			break;
333 9
		case 'listing':
334 9
			if ( is_tax() && geodir_get_taxonomy_posttype() ) {
335 7
				global $current_term, $taxonomy, $term;
336
337
				return true;
338
			}
339 7
			$post_type = get_query_var( 'post_type' );
340
			if ( is_array( $post_type ) ) {
341
				$post_type = reset( $post_type );
342
			}
343 7
			if ( is_post_type_archive() && in_array( $post_type, geodir_get_posttypes() ) ) {
344 7
				return true;
345
			}
346
347 7
			break;
348
		case 'home':
349
350
			if ( ( is_page() && get_query_var( 'page_id' ) == geodir_home_page_id() ) || is_page_geodir_home() ) {
351 28
				return true;
352
			}
353
354
			break;
355
		case 'location':
356
			if ( is_page() && get_query_var( 'page_id' ) == geodir_location_page_id() ) {
357
				return true;
358
			}
359
			break;
360
		case 'author':
361
			if ( is_author() && isset( $_REQUEST['geodir_dashbord'] ) ) {
362
				return true;
363
			}
364
365
			if ( function_exists( 'bp_loggedin_user_id' ) && function_exists( 'bp_displayed_user_id' ) && $my_id = (int) bp_loggedin_user_id() ) {
366
				if ( ( (bool) bp_is_current_component( 'listings' ) || (bool) bp_is_current_component( 'favorites' ) ) && $my_id > 0 && $my_id == (int) bp_displayed_user_id() ) {
367
					return true;
368
				}
369
			}
370
			break;
371
		case 'search':
372
			if ( is_search() && isset( $_REQUEST['geodir_search'] ) ) {
373
				return true;
374
			}
375
			break;
376
		case 'info':
377
			if ( is_page() && get_query_var( 'page_id' ) == geodir_info_page_id() ) {
378
				return true;
379
			}
380
			break;
381
		case 'login':
382
			if ( is_page() && get_query_var( 'page_id' ) == geodir_login_page_id() ) {
383
				return true;
384
			}
385
			break;
386
		case 'checkout':
387 View Code Duplication
			if ( is_page() && function_exists( 'geodir_payment_checkout_page_id' ) && get_query_var( 'page_id' ) == geodir_payment_checkout_page_id() ) {
388
				return true;
389
			}
390
			break;
391
		case 'invoices':
392 View Code Duplication
			if ( is_page() && function_exists( 'geodir_payment_invoices_page_id' ) && get_query_var( 'page_id' ) == geodir_payment_invoices_page_id() ) {
393
				return true;
394
			}
395
			break;
396
		default:
397
			return false;
398
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
399
400
	endswitch;
401
402
	//endif;
403
404
	return false;
405
}
406
407
/**
408
 * Sets a key and value in $wp object if the current page is a geodir page.
409
 *
410
 * @since   1.0.0
411
 * @since   1.5.4 Added check for new style GD homepage.
412
 * @since   1.5.6 Added check for GD invoices and GD checkout page.
413
 * @package GeoDirectory
414
 *
415
 * @param object $wp WordPress object.
416
 */
417
function geodir_set_is_geodir_page( $wp ) {
418
	if ( ! is_admin() ) {
419
		//$wp->query_vars['gd_is_geodir_page'] = false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
420
		//print_r()
421
		if ( empty( $wp->query_vars ) || ! array_diff( array_keys( $wp->query_vars ), array(
422
				'preview',
423
				'page',
424
				'paged',
425
				'cpage'
426
			) )
427
		) {
428
			if ( get_option( 'geodir_set_as_home' ) ) {
429
				$wp->query_vars['gd_is_geodir_page'] = true;
430
			}
431
			if ( geodir_is_page( 'home' ) ) {
432
				$wp->query_vars['gd_is_geodir_page'] = true;
433
			}
434
435
436
		}
437
438
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] ) && isset( $wp->query_vars['page_id'] ) ) {
439
			if (
440
				$wp->query_vars['page_id'] == geodir_add_listing_page_id()
441
				|| $wp->query_vars['page_id'] == geodir_preview_page_id()
442
				|| $wp->query_vars['page_id'] == geodir_success_page_id()
443
				|| $wp->query_vars['page_id'] == geodir_location_page_id()
444
				|| $wp->query_vars['page_id'] == geodir_home_page_id()
445
				|| $wp->query_vars['page_id'] == geodir_info_page_id()
446
				|| $wp->query_vars['page_id'] == geodir_login_page_id()
447
				|| ( function_exists( 'geodir_payment_checkout_page_id' ) && $wp->query_vars['page_id'] == geodir_payment_checkout_page_id() )
448
				|| ( function_exists( 'geodir_payment_invoices_page_id' ) && $wp->query_vars['page_id'] == geodir_payment_invoices_page_id() )
449
			) {
450
				$wp->query_vars['gd_is_geodir_page'] = true;
451
			}
452
		}
453
454
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] ) && isset( $wp->query_vars['pagename'] ) ) {
455
			$page = get_page_by_path( $wp->query_vars['pagename'] );
456
457
			if ( ! empty( $page ) && (
458
					$page->ID == geodir_add_listing_page_id()
459
					|| $page->ID == geodir_preview_page_id()
460
					|| $page->ID == geodir_success_page_id()
461
					|| $page->ID == geodir_location_page_id()
462
					|| ( isset( $wp->query_vars['page_id'] ) && $wp->query_vars['page_id'] == geodir_home_page_id() )
463
					|| ( isset( $wp->query_vars['page_id'] ) && $wp->query_vars['page_id'] == geodir_info_page_id() )
464
					|| ( isset( $wp->query_vars['page_id'] ) && $wp->query_vars['page_id'] == geodir_login_page_id() )
465
					|| ( isset( $wp->query_vars['page_id'] ) && function_exists( 'geodir_payment_checkout_page_id' ) && $wp->query_vars['page_id'] == geodir_payment_checkout_page_id() )
466
					|| ( isset( $wp->query_vars['page_id'] ) && function_exists( 'geodir_payment_invoices_page_id' ) && $wp->query_vars['page_id'] == geodir_payment_invoices_page_id() )
467
				)
468 30
			) {
469 30
				$wp->query_vars['gd_is_geodir_page'] = true;
470 30
			}
471
		}
472 30
473
474
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] ) && isset( $wp->query_vars['post_type'] ) && $wp->query_vars['post_type'] != '' ) {
475
			$requested_post_type = $wp->query_vars['post_type'];
476
			// check if this post type is geodirectory post types
477
			$post_type_array = geodir_get_posttypes();
478
			if ( in_array( $requested_post_type, $post_type_array ) ) {
479
				$wp->query_vars['gd_is_geodir_page'] = true;
480
			}
481
		}
482
483
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] ) ) {
484
			$geodir_taxonomis = geodir_get_taxonomies( '', true );
485
			if ( ! empty( $geodir_taxonomis ) ) {
486
				foreach ( $geodir_taxonomis as $taxonomy ) {
0 ignored issues
show
Bug introduced by
The expression $geodir_taxonomis of type array|boolean 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...
487 9
					if ( array_key_exists( $taxonomy, $wp->query_vars ) ) {
488 9
						$wp->query_vars['gd_is_geodir_page'] = true;
489 9
						break;
490 9
					}
491 9
				}
492
			}
493
494
		}
495
496
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] ) && isset( $wp->query_vars['author_name'] ) && isset( $_REQUEST['geodir_dashbord'] ) ) {
497
			$wp->query_vars['gd_is_geodir_page'] = true;
498
		}
499 9
500
501 9
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] ) && isset( $_REQUEST['geodir_search'] ) ) {
502
			$wp->query_vars['gd_is_geodir_page'] = true;
503
		}
504
505
506
//check if homepage
507
		if ( ! isset( $wp->query_vars['gd_is_geodir_page'] )
508 9
		     && ! isset( $wp->query_vars['page_id'] )
509
		     && ! isset( $wp->query_vars['pagename'] )
510
		     && is_page_geodir_home()
511
		) {
512
			$wp->query_vars['gd_is_geodir_page'] = true;
513
		}
514
		//echo $wp->query_vars['gd_is_geodir_page'] ;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
515
		/*echo "<pre>" ;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
516
		print_r($wp) ;
517
		echo "</pre>" ;
518
	//	exit();
519
			*/
520
	} // end of is admin
521
}
522
523
/**
524
 * Checks whether the current page is a GD page or not.
525
 *
526
 * @since   1.0.0
527
 * @package GeoDirectory
528
 * @global object $wp WordPress object.
529
 * @return bool If the page is GD page returns true. Otherwise false.
530
 */
531
function geodir_is_geodir_page() {
532
	global $wp;
533
	if ( isset( $wp->query_vars['gd_is_geodir_page'] ) && $wp->query_vars['gd_is_geodir_page'] ) {
534
		return true;
535
	} else {
536
		return false;
537
	}
538
}
539
540
if ( ! function_exists( 'geodir_get_imagesize' ) ) {
541
	/**
542
	 * Get image size using the size key .
543
	 *
544
	 * @since   1.0.0
545
	 * @package GeoDirectory
546
	 *
547
	 * @param string $size The image size key.
548
	 *
549
	 * @return array|mixed|void|WP_Error If valid returns image size. Else returns error.
550
	 */
551
	function geodir_get_imagesize( $size = '' ) {
552
553
		$imagesizes = array(
554
			'list-thumb'   => array( 'w' => 283, 'h' => 188 ),
555
			'thumbnail'    => array( 'w' => 125, 'h' => 125 ),
556
			'widget-thumb' => array( 'w' => 50, 'h' => 50 ),
557
			'slider-thumb' => array( 'w' => 100, 'h' => 100 )
558
		);
559
560
		/**
561
		 * Filter the image sizes array.
562
		 *
563
		 * @since 1.0.0
564
		 *
565
		 * @param array $imagesizes Image size array.
566
		 */
567
		$imagesizes = apply_filters( 'geodir_imagesizes', $imagesizes );
568
569
		if ( ! empty( $size ) && array_key_exists( $size, $imagesizes ) ) {
570
			/**
571
			 * Filters image size of the passed key.
572 1
			 *
573 1
			 * @since 1.0.0
574
			 *
575
			 * @param array $imagesizes [$size] Image size array of the passed key.
576 1
			 */
577 1
			return apply_filters( 'geodir_get_imagesize_' . $size, $imagesizes[ $size ] );
578
579
		} elseif ( ! empty( $size ) ) {
580 1
581 1
			return new WP_Error( 'geodir_no_imagesize', __( "Given image size is not valid", 'geodirectory' ) );
582 1
583
		}
584
585
		return $imagesizes;
586
	}
587
}
588
589
/**
590
 * Get an image size
591
 *
592
 * Variable is filtered by geodir_get_image_size_{image_size}
593
 */
594
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
595
function geodir_get_image_size( $image_size ) {
596
	$return = '';
597 1
	switch ($image_size) :
598 1
		case "list_thumbnail_size" : $return = get_option('geodirectory_list_thumbnail_size'); break;
599
	endswitch;
600
	return apply_filters( 'geodir_get_image_size_'.$image_size, $return );
601
}
602
*/
603
604
605
if ( ! function_exists( 'createRandomString' ) ) {
606
	/**
607
	 * Creates random string.
608
	 *
609
	 * @since   1.0.0
610
	 * @package GeoDirectory
611
	 * @return string Random string.
612
	 */
613
	function createRandomString() {
614
		$chars = "abcdefghijkmlnopqrstuvwxyz1023456789";
615
		srand( (double) microtime() * 1000000 );
616
		$i       = 0;
617
		$rstring = '';
618 1
		while ( $i <= 25 ) {
619
			$num     = rand() % 33;
620 1
			$tmp     = substr( $chars, $num, 1 );
621 1
			$rstring = $rstring . $tmp;
622 1
			$i ++;
623 1
		}
624 1
625 1
		return $rstring;
626 1
	}
627 1
}
628
629
if ( ! function_exists( 'geodir_getDistanceRadius' ) ) {
630
	/**
631
	 * Calculates the distance radius.
632
	 *
633
	 * @since   1.0.0
634
	 * @package GeoDirectory
635
	 *
636
	 * @param string $uom Measurement unit type.
637
	 *
638
	 * @return float The mean radius.
639
	 */
640
	function geodir_getDistanceRadius( $uom = 'km' ) {
641
//	Use Haversine formula to calculate the great circle distance between two points identified by longitude and latitude
642
		switch ( geodir_strtolower( $uom ) ):
643
			case 'km'    :
644
				$earthMeanRadius = 6371.009; // km
645
				break;
646
			case 'm'    :
647
			case 'meters'    :
648
				$earthMeanRadius = 6371.009 * 1000; // km
649
				break;
650
			case 'miles'    :
651
				$earthMeanRadius = 3958.761; // miles
652 9
				break;
653
			case 'yards'    :
654
			case 'yds'    :
655 9
				$earthMeanRadius = 3958.761 * 1760; // yards
656 9
				break;
657
			case 'feet'    :
658 9
			case 'ft'    :
659 2
				$earthMeanRadius = 3958.761 * 1760 * 3; // feet
660 2
				break;
661 9
			case 'nm'    :
662 2
				$earthMeanRadius = 3440.069; //  miles
663 2
				break;
664 7
			default:
665 1
				$earthMeanRadius = 3958.761; // miles
666 1
				break;
667 1
		endswitch;
668 5
669 2
		return $earthMeanRadius;
670 2
	}
671 2
}
672 4
673 1
674 1
if ( ! function_exists( 'geodir_calculateDistanceFromLatLong' ) ) {
675 2
	/**
676 1
	 * Calculate the great circle distance between two points identified by longitude and latitude.
677 1
	 *
678 1
	 * @since   1.0.0
679
	 * @package GeoDirectory
680
	 *
681
	 * @param array $point1 Latitude and Longitude of point 1.
682
	 * @param array $point2 Latitude and Longitude of point 2.
683 9
	 * @param string $uom   Unit of measurement.
684 9
	 *
685 9
	 * @return float The distance.
686
	 */
687 9
	function geodir_calculateDistanceFromLatLong( $point1, $point2, $uom = 'km' ) {
688 9
//	Use Haversine formula to calculate the great circle distance between two points identified by longitude and latitude
689 9
690
		$earthMeanRadius = geodir_getDistanceRadius( $uom );
691 9
692 9
		$deltaLatitude  = deg2rad( (float) $point2['latitude'] - (float) $point1['latitude'] );
693 9
		$deltaLongitude = deg2rad( (float) $point2['longitude'] - (float) $point1['longitude'] );
694 9
		$a              = sin( $deltaLatitude / 2 ) * sin( $deltaLatitude / 2 ) +
695
		                  cos( deg2rad( (float) $point1['latitude'] ) ) * cos( deg2rad( (float) $point2['latitude'] ) ) *
696 9
		                  sin( $deltaLongitude / 2 ) * sin( $deltaLongitude / 2 );
697 9
		$c              = 2 * atan2( sqrt( $a ), sqrt( 1 - $a ) );
698 1
		$distance       = $earthMeanRadius * $c;
699 1
700
		return $distance;
701 9
702 9
	}
703
}
704 9
705
706 9
if ( ! function_exists( 'geodir_sendEmail' ) ) {
707 2
	/**
708 2
	 * The main function that send transactional emails using the args provided.
709 2
	 *
710 9
	 * @since   1.0.0
711 9
	 * @since   1.5.7 Added db translations for notifications subject and content.
712 9
	 * @package GeoDirectory
713 9
	 *
714
	 * @param string $fromEmail     Sender email address.
715 9
	 * @param string $fromEmailName Sender name.
716 9
	 * @param string $toEmail       Receiver email address.
717 9
	 * @param string $toEmailName   Receiver name.
718
	 * @param string $to_subject    Email subject.
719 9
	 * @param string $to_message    Email content.
720 6
	 * @param string $extra         Not being used.
721 6
	 * @param string $message_type  The message type. Can be send_friend, send_enquiry, forgot_password, registration, post_submit, listing_published.
722
	 * @param string $post_id       The post ID.
723 9
	 * @param string $user_id       The user ID.
724 6
	 */
725 6
	function geodir_sendEmail( $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra = '', $message_type, $post_id = '', $user_id = '' ) {
726
		$login_details = '';
727 9
728 9
		// strip slashes from subject & message text
729 9
		$to_subject = stripslashes_deep( $to_subject );
730
		$to_message = stripslashes_deep( $to_message );
731 9
732 9
		if ( $message_type == 'send_friend' ) {
733 9
			$subject = get_option( 'geodir_email_friend_subject' );
734
			$message = get_option( 'geodir_email_friend_content' );
735 9
		} elseif ( $message_type == 'send_enquiry' ) {
736 9
			$subject = get_option( 'geodir_email_enquiry_subject' );
737 9
			$message = get_option( 'geodir_email_enquiry_content' );
738 9
		} elseif ( $message_type == 'forgot_password' ) {
739
			$subject       = get_option( 'geodir_forgot_password_subject' );
740 9
			$message       = get_option( 'geodir_forgot_password_content' );
741
			$login_details = $to_message;
742
		} elseif ( $message_type == 'registration' ) {
743
			$subject       = get_option( 'geodir_registration_success_email_subject' );
744
			$message       = get_option( 'geodir_registration_success_email_content' );
745
			$login_details = $to_message;
746
		} elseif ( $message_type == 'post_submit' ) {
747
			$subject = get_option( 'geodir_post_submited_success_email_subject' );
748
			$message = get_option( 'geodir_post_submited_success_email_content' );
749
		} elseif ( $message_type == 'listing_published' ) {
750
			$subject = get_option( 'geodir_post_published_email_subject' );
751
			$message = get_option( 'geodir_post_published_email_content' );
752
		} elseif ( $message_type == 'listing_edited' ) {
753
			$subject = get_option( 'geodir_post_edited_email_subject_admin' );
754
			$message = get_option( 'geodir_post_edited_email_content_admin' );
755
		}
756
757
		if ( ! empty( $subject ) ) {
758
			$subject = __( stripslashes_deep( $subject ), 'geodirectory' );
759 9
		}
760
761
		if ( ! empty( $message ) ) {
762
			$message = __( stripslashes_deep( $message ), 'geodirectory' );
763
		}
764
765
		$to_message        = nl2br( $to_message );
766
		$sitefromEmail     = get_option( 'site_email' );
767
		$sitefromEmailName = get_site_emailName();
768
		$productlink       = get_permalink( $post_id );
769
770
		$user_login = '';
771
		if ( $user_id > 0 && $user_info = get_userdata( $user_id ) ) {
772
			$user_login = $user_info->user_login;
773
		}
774
775
		$posted_date = '';
776
		$listingLink = '';
777 9
778
		$post_info = get_post( $post_id );
779
780
		if ( $post_info ) {
781
			$posted_date = $post_info->post_date;
782
			$listingLink = '<a href="' . $productlink . '"><b>' . $post_info->post_title . '</b></a>';
783
		}
784
		$siteurl       = home_url();
785
		$siteurl_link  = '<a href="' . $siteurl . '">' . $siteurl . '</a>';
786
		$loginurl      = geodir_login_url();
787
		$loginurl_link = '<a href="' . $loginurl . '">login</a>';
788
789
		$post_author_id   = ! empty( $post_info ) ? $post_info->post_author : 0;
790
		$post_author_name = geodir_get_client_name( $post_author_id );
791
		$current_date     = date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) );
792
793
		if ( $fromEmail == '' ) {
794
			$fromEmail = get_option( 'site_email' );
795 9
		}
796
797
		if ( $fromEmailName == '' ) {
798
			$fromEmailName = get_option( 'site_email_name' );
799
		}
800
801
		$search_array  = array(
802
			'[#listing_link#]',
803
			'[#site_name_url#]',
804
			'[#post_id#]',
805
			'[#site_name#]',
806
			'[#to_name#]',
807
			'[#from_name#]',
808
			'[#subject#]',
809
			'[#comments#]',
810
			'[#login_url#]',
811
			'[#login_details#]',
812
			'[#client_name#]',
813 9
			'[#posted_date#]',
814
			'[#from_email#]',
815 9
			'[#user_login#]',
816
			'[#username#]',
817 9
			'[#post_author_id#]',
818
			'[#post_author_name#]',
819
			'[#current_date#]'
820
		);
821
		$replace_array = array(
822
			$listingLink,
823
			$siteurl_link,
824
			$post_id,
825
			$sitefromEmailName,
826
			$toEmailName,
827
			$fromEmailName,
828
			$to_subject,
829
			$to_message,
830
			$loginurl_link,
831
			$login_details,
832 9
			$toEmailName,
833 9
			$posted_date,
834
			$fromEmail,
835 9
			$user_login,
836 9
			$user_login,
837 1
			$post_author_id,
838 1
			$post_author_name,
839
			$current_date
840 1
		);
841 1
		$message       = str_replace( $search_array, $replace_array, $message );
0 ignored issues
show
Bug introduced by
The variable $message 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...
842 1
843
		$search_array  = array(
844 1
			'[#listing_link#]',
845 1
			'[#site_name_url#]',
846 1
			'[#post_id#]',
847
			'[#site_name#]',
848 1
			'[#to_name#]',
849 1
			'[#from_name#]',
850
			'[#subject#]',
851 1
			'[#client_name#]',
852 8
			'[#posted_date#]',
853 2
			'[#from_email#]',
854 2
			'[#user_login#]',
855 2
			'[#username#]',
856 6
			'[#post_author_id#]',
857 2
			'[#post_author_name#]',
858 2
			'[#current_date#]'
859 2
		);
860 4
		$replace_array = array(
861 2
			$listingLink,
862 2
			$siteurl_link,
863 2
			$post_id,
864 2
			$sitefromEmailName,
865 1
			$toEmailName,
866 1
			$fromEmailName,
867 1
			$to_subject,
868
			$toEmailName,
869 9
			$posted_date,
870 8
			$fromEmail,
871
			$user_login,
872 8
			$user_login,
873
			$post_author_id,
874
			$post_author_name,
875
			$current_date
876
		);
877
		$subject       = str_replace( $search_array, $replace_array, $subject );
0 ignored issues
show
Bug introduced by
The variable $subject 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...
878
879
		$headers = 'MIME-Version: 1.0' . "\r\n";
880
		$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
881
		$headers .= "Reply-To: " . $fromEmail . "\r\n";
882
		$headers .= 'From: ' . $sitefromEmailName . ' <' . $sitefromEmail . '>' . "\r\n";
883
884
		$to = $toEmail;
885 8
886
		/**
887 9
		 * Filter the client email to address.
888
		 *
889
		 * @since   1.6.1
890
		 * @package GeoDirectory
891
		 *
892
		 * @param string $to            The email address the email is being sent to.
893
		 * @param string $fromEmail     Sender email address.
894
		 * @param string $fromEmailName Sender name.
895
		 * @param string $toEmail       Receiver email address.
896
		 * @param string $toEmailName   Receiver name.
897
		 * @param string $to_subject    Email subject.
898
		 * @param string $to_message    Email content.
899
		 * @param string $extra         Not being used.
900
		 * @param string $message_type  The message type. Can be send_friend, send_enquiry, forgot_password, registration, post_submit, listing_published.
901
		 * @param string $post_id       The post ID.
902
		 * @param string $user_id       The user ID.
903
		 */
904
		$to = apply_filters( 'geodir_sendEmail_to', $to, $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra, $message_type, $post_id, $user_id );
905
		/**
906
		 * Filter the client email subject.
907
		 *
908
		 * @since   1.6.1
909
		 * @package GeoDirectory_Payment_Manager
910
		 *
911
		 * @param string $subject       The email subject.
912
		 * @param string $fromEmail     Sender email address.
913
		 * @param string $fromEmailName Sender name.
914
		 * @param string $toEmail       Receiver email address.
915
		 * @param string $toEmailName   Receiver name.
916
		 * @param string $to_subject    Email subject.
917
		 * @param string $to_message    Email content.
918
		 * @param string $extra         Not being used.
919
		 * @param string $message_type  The message type. Can be send_friend, send_enquiry, forgot_password, registration, post_submit, listing_published.
920
		 * @param string $post_id       The post ID.
921
		 * @param string $user_id       The user ID.
922
		 */
923
		$subject = apply_filters( 'geodir_sendEmail_subject', $subject, $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra, $message_type, $post_id, $user_id );
924
		/**
925
		 * Filter the client email message.
926
		 *
927
		 * @since   1.6.1
928
		 * @package GeoDirectory_Payment_Manager
929
		 *
930
		 * @param string $message       The email message text.
931
		 * @param string $fromEmail     Sender email address.
932
		 * @param string $fromEmailName Sender name.
933
		 * @param string $toEmail       Receiver email address.
934
		 * @param string $toEmailName   Receiver name.
935
		 * @param string $to_subject    Email subject.
936 5
		 * @param string $to_message    Email content.
937
		 * @param string $extra         Not being used.
938
		 * @param string $message_type  The message type. Can be send_friend, send_enquiry, forgot_password, registration, post_submit, listing_published.
939
		 * @param string $post_id       The post ID.
940
		 * @param string $user_id       The user ID.
941
		 */
942
		$message = apply_filters( 'geodir_sendEmail_message', $message, $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra, $message_type, $post_id, $user_id );
943 5
		/**
944
		 * Filter the client email headers.
945 5
		 *
946 5
		 * @since   1.6.1
947 5
		 * @package GeoDirectory_Payment_Manager
948 5
		 *
949
		 * @param string $headers       The email headers.
950
		 * @param string $fromEmail     Sender email address.
951
		 * @param string $fromEmailName Sender name.
952
		 * @param string $toEmail       Receiver email address.
953
		 * @param string $toEmailName   Receiver name.
954 5
		 * @param string $to_subject    Email subject.
955
		 * @param string $to_message    Email content.
956 5
		 * @param string $extra         Not being used.
957 5
		 * @param string $message_type  The message type. Can be send_friend, send_enquiry, forgot_password, registration, post_submit, listing_published.
958
		 * @param string $post_id       The post ID.
959 5
		 * @param string $user_id       The user ID.
960
		 */
961 5
		$headers = apply_filters( 'geodir_sendEmail_headers', $headers, $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra, $message_type, $post_id, $user_id );
962
963 5
		$sent = wp_mail( $to, $subject, $message, $headers );
964 5
965 5
		if ( ! $sent ) {
966
			if ( is_array( $to ) ) {
967 5
				$to = implode( ',', $to );
968 5
			}
969
			$log_message = sprintf(
970 5
				__( "Email from GeoDirectory failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\n\n", 'geodirectory' ),
971 5
				$message_type,
972
				date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ),
973 5
				$to,
974 2
				$subject
975 2
			);
976 2
			geodir_error_log( $log_message );
977
		}
978 2
979
		///////// ADMIN BCC EMIALS
980
		$adminEmail = get_bloginfo( 'admin_email' );
981
		$to         = $adminEmail;
982
983
		$admin_bcc = false;
984
		if ( $message_type == 'post_submit' ) {
985
			$subject = __( stripslashes_deep( get_option( 'geodir_post_submited_success_email_subject_admin' ) ), 'geodirectory' );
986
			$message = __( stripslashes_deep( get_option( 'geodir_post_submited_success_email_content_admin' ) ), 'geodirectory' );
987
988
			$search_array  = array(
989
				'[#listing_link#]',
990 2
				'[#site_name_url#]',
991
				'[#post_id#]',
992 2
				'[#site_name#]',
993 2
				'[#to_name#]',
994
				'[#from_name#]',
995
				'[#subject#]',
996
				'[#comments#]',
997
				'[#login_url#]',
998
				'[#login_details#]',
999
				'[#client_name#]',
1000
				'[#posted_date#]',
1001
				'[#user_login#]',
1002
				'[#username#]'
1003
			);
1004
			$replace_array = array(
1005
				$listingLink,
1006 2
				$siteurl_link,
1007 2
				$post_id,
1008
				$sitefromEmailName,
1009
				$toEmailName,
1010
				$fromEmailName,
1011
				$to_subject,
1012
				$to_message,
1013 2
				$loginurl_link,
1014
				$login_details,
1015
				$toEmailName,
1016
				$posted_date,
1017
				$user_login,
1018
				$user_login
1019 2
			);
1020
			$message       = str_replace( $search_array, $replace_array, $message );
1021
1022
			$search_array  = array(
1023
				'[#listing_link#]',
1024
				'[#site_name_url#]',
1025
				'[#post_id#]',
1026
				'[#site_name#]',
1027
				'[#to_name#]',
1028
				'[#from_name#]',
1029
				'[#subject#]',
1030 2
				'[#client_name#]',
1031 2
				'[#posted_date#]',
1032 2
				'[#user_login#]',
1033 2
				'[#username#]'
1034 2
			);
1035 2
			$replace_array = array(
1036
				$listingLink,
1037
				$siteurl_link,
1038 2
				$post_id,
1039 2
				$sitefromEmailName,
1040 2
				$toEmailName,
1041
				$fromEmailName,
1042 1
				$to_subject,
1043
				$toEmailName,
1044 2
				$posted_date,
1045 2
				$user_login,
1046
				$user_login
1047 2
			);
1048
			$subject       = str_replace( $search_array, $replace_array, $subject );
1049 2
1050
			$subject .= ' - ADMIN BCC COPY';
1051
			$admin_bcc = true;
1052
1053
		} elseif ( $message_type == 'registration' && get_option( 'geodir_bcc_new_user' ) ) {
1054
			$subject .= ' - ADMIN BCC COPY';
1055
			$admin_bcc = true;
1056
		} elseif ( $message_type == 'send_friend' && get_option( 'geodir_bcc_friend' ) ) {
1057
			$subject .= ' - ADMIN BCC COPY';
1058
			$admin_bcc = true;
1059
		} elseif ( $message_type == 'send_enquiry' && get_option( 'geodir_bcc_enquiry' ) ) {
1060
			$subject .= ' - ADMIN BCC COPY';
1061
			$admin_bcc = true;
1062
		} elseif ( $message_type == 'listing_published' && get_option( 'geodir_bcc_listing_published' ) ) {
1063
			$subject .= ' - ADMIN BCC COPY';
1064
			$admin_bcc = true;
1065
		}
1066
1067 View Code Duplication
		if ( $admin_bcc === true ) {
1068
			$sent = wp_mail( $to, $subject, $message, $headers );
1069
1070
			if ( ! $sent ) {
1071
				if ( is_array( $to ) ) {
1072
					$to = implode( ',', $to );
1073
				}
1074
				$log_message = sprintf(
1075
					__( "Email from GeoDirectory failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\n\n", 'geodirectory' ),
1076
					$message_type,
1077
					date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ),
1078
					$to,
1079
					$subject
1080
				);
1081
				geodir_error_log( $log_message );
1082
			}
1083
		}
1084
1085
	}
1086
}
1087
1088
1089
/**
1090
 * Generates breadcrumb for taxonomy (category, tags etc.) pages.
1091
 *
1092
 * @since   1.0.0
1093
 * @package GeoDirectory
1094
 */
1095
function geodir_taxonomy_breadcrumb() {
1096
1097
	$term   = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
1098
	$parent = $term->parent;
1099
1100
	while ( $parent ):
1101
		$parents[]  = $parent;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$parents was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parents = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1102
		$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ) );
1103
		$parent     = $new_parent->parent;
1104
	endwhile;
1105
1106
	if ( ! empty( $parents ) ):
1107
		$parents = array_reverse( $parents );
1108
1109
		foreach ( $parents as $parent ):
1110
			$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ) );
1111
			$url  = get_term_link( $item, get_query_var( 'taxonomy' ) );
1112
			echo '<li> > <a href="' . $url . '">' . $item->name . '</a></li>';
1113
		endforeach;
1114
1115
	endif;
1116
1117
	echo '<li> > ' . $term->name . '</li>';
1118
}
1119
1120
1121
/**
1122 2
 * Main function that generates breadcrumb for all pages.
1123
 *
1124
 * @since   1.0.0
1125
 * @since   1.5.7 Changes for the neighbourhood system improvement.
1126
 * @package GeoDirectory
1127
 * @global object $wp_query   WordPress Query object.
1128
 * @global object $post       The current post object.
1129
 * @global object $gd_session GeoDirectory Session object.
1130
 */
1131
function geodir_breadcrumb() {
1132
	global $wp_query, $geodir_add_location_url;
1133
1134
	/**
1135
	 * Filter breadcrumb separator.
1136
	 *
1137
	 * @since 1.0.0
1138
	 */
1139
	$separator = apply_filters( 'geodir_breadcrumb_separator', ' > ' );
1140
1141
	if ( ! geodir_is_page( 'home' ) ) {
1142
		$breadcrumb    = '';
1143
		$url_categoris = '';
0 ignored issues
show
Unused Code introduced by
$url_categoris 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...
1144
		$breadcrumb .= '<div class="geodir-breadcrumb clearfix"><ul id="breadcrumbs">';
1145
		/**
1146
		 * Filter breadcrumb's first link.
1147
		 *
1148
		 * @since 1.0.0
1149
		 */
1150
		$breadcrumb .= '<li>' . apply_filters( 'geodir_breadcrumb_first_link', '<a href="' . home_url() . '">' . __( 'Home', 'geodirectory' ) . '</a>' ) . '</li>';
1151
1152
		$gd_post_type   = geodir_get_current_posttype();
1153
		$post_type_info = get_post_type_object( $gd_post_type );
1154
1155
		remove_filter( 'post_type_archive_link', 'geodir_get_posttype_link' );
1156
1157
		$listing_link = get_post_type_archive_link( $gd_post_type );
1158
1159
		add_filter( 'post_type_archive_link', 'geodir_get_posttype_link', 10, 2 );
1160
		$listing_link = rtrim( $listing_link, '/' );
1161
		$listing_link .= '/';
1162
1163
		$post_type_for_location_link = $listing_link;
1164
		$location_terms              = geodir_get_current_location_terms( 'query_vars', $gd_post_type );
1165
1166
		global $wp, $gd_session;
1167
		$location_link = $post_type_for_location_link;
1168
1169
		if ( geodir_is_page( 'detail' ) || geodir_is_page( 'listing' ) ) {
1170 2
			global $post;
1171 2
			$location_manager     = defined( 'POST_LOCATION_TABLE' ) ? true : false;
1172
			$neighbourhood_active = $location_manager && get_option( 'location_neighbourhoods' ) ? true : false;
1173 2
1174 View Code Duplication
			if ( geodir_is_page( 'detail' ) && isset( $post->country_slug ) ) {
1175
				$location_terms = array(
1176 5
					'gd_country' => $post->country_slug,
1177 1
					'gd_region'  => $post->region_slug,
1178 1
					'gd_city'    => $post->city_slug
1179 1
				);
1180
1181
				if ( $neighbourhood_active && ! empty( $location_terms['gd_city'] ) && $gd_ses_neighbourhood = $gd_session->get( 'gd_neighbourhood' ) ) {
1182
					$location_terms['gd_neighbourhood'] = $gd_ses_neighbourhood;
1183
				}
1184
			}
1185
1186
			$geodir_show_location_url = get_option( 'geodir_show_location_url' );
1187
1188 1
			$hide_url_part = array();
1189
			if ( $location_manager ) {
1190 1
				$hide_country_part = get_option( 'geodir_location_hide_country_part' );
1191 1
				$hide_region_part  = get_option( 'geodir_location_hide_region_part' );
1192
1193 1
				if ( $hide_region_part && $hide_country_part ) {
1194
					$hide_url_part = array( 'gd_country', 'gd_region' );
1195
				} else if ( $hide_region_part && ! $hide_country_part ) {
1196
					$hide_url_part = array( 'gd_region' );
1197
				} else if ( ! $hide_region_part && $hide_country_part ) {
1198
					$hide_url_part = array( 'gd_country' );
1199
				}
1200
			}
1201
1202
			$hide_text_part = array();
0 ignored issues
show
Unused Code introduced by
$hide_text_part 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...
1203
			if ( $geodir_show_location_url == 'country_city' ) {
1204
				$hide_text_part = array( 'gd_region' );
0 ignored issues
show
Unused Code introduced by
$hide_text_part 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...
1205
1206
				if ( isset( $location_terms['gd_region'] ) && ! $location_manager ) {
1207
					unset( $location_terms['gd_region'] );
1208
				}
1209 1
			} else if ( $geodir_show_location_url == 'region_city' ) {
1210
				$hide_text_part = array( 'gd_country' );
0 ignored issues
show
Unused Code introduced by
$hide_text_part 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...
1211 1
1212 4
				if ( isset( $location_terms['gd_country'] ) && ! $location_manager ) {
1213
					unset( $location_terms['gd_country'] );
1214
				}
1215
			} else if ( $geodir_show_location_url == 'city' ) {
1216
				$hide_text_part = array( 'gd_country', 'gd_region' );
0 ignored issues
show
Unused Code introduced by
$hide_text_part 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...
1217
1218
				if ( isset( $location_terms['gd_country'] ) && ! $location_manager ) {
1219
					unset( $location_terms['gd_country'] );
1220
				}
1221
				if ( isset( $location_terms['gd_region'] ) && ! $location_manager ) {
1222 3
					unset( $location_terms['gd_region'] );
1223 2
				}
1224
			}
1225 2
1226 1
			$is_location_last = '';
0 ignored issues
show
Unused Code introduced by
$is_location_last 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...
1227 1
			$is_taxonomy_last = '';
0 ignored issues
show
Unused Code introduced by
$is_taxonomy_last 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...
1228 1
			$breadcrumb .= '<li>';
1229 1
			if ( get_query_var( $gd_post_type . 'category' ) ) {
1230 1
				$gd_taxonomy = $gd_post_type . 'category';
1231 1
			} elseif ( get_query_var( $gd_post_type . '_tags' ) ) {
1232
				$gd_taxonomy = $gd_post_type . '_tags';
1233 2
			}
1234 2
1235 2
			$breadcrumb .= $separator . '<a href="' . $listing_link . '">' . __( ucfirst( $post_type_info->label ), 'geodirectory' ) . '</a>';
1236 3
			if ( ! empty( $gd_taxonomy ) || geodir_is_page( 'detail' ) ) {
1237
				$is_location_last = false;
1238 1
			} else {
1239
				$is_location_last = true;
1240
			}
1241
1242 1
			if ( ! empty( $gd_taxonomy ) && geodir_is_page( 'listing' ) ) {
1243
				$is_taxonomy_last = true;
1244
			} else {
1245
				$is_taxonomy_last = false;
1246 1
			}
1247
1248
			if ( ! empty( $location_terms ) ) {
1249
				$geodir_get_locations = function_exists( 'get_actual_location_name' ) ? true : false;
1250 1
1251
				foreach ( $location_terms as $key => $location_term ) {
1252
					if ( $location_term != '' ) {
1253 1
						if ( ! empty( $hide_url_part ) && in_array( $key, $hide_url_part ) ) { // Hide location part from url & breadcrumb.
1254
							continue;
1255
						}
1256 1
1257 1
						$gd_location_link_text = preg_replace( '/-(\d+)$/', '', $location_term );
1258 1
						$gd_location_link_text = preg_replace( '/[_-]/', ' ', $gd_location_link_text );
1259 1
						$gd_location_link_text = ucfirst( $gd_location_link_text );
1260 5
1261
						$location_term_actual_country = '';
1262
						$location_term_actual_region  = '';
1263
						$location_term_actual_city    = '';
1264
						if ( $geodir_get_locations ) {
1265
							if ( $key == 'gd_country' ) {
1266
								$location_term_actual_country = get_actual_location_name( 'country', $location_term, true );
1267
							} else if ( $key == 'gd_region' ) {
1268
								$location_term_actual_region = get_actual_location_name( 'region', $location_term, true );
1269 5
							} else if ( $key == 'gd_city' ) {
1270 5
								$location_term_actual_city = get_actual_location_name( 'city', $location_term, true );
1271 5
							}
1272
						} else {
1273
							$location_info = geodir_get_location();
1274
1275
							if ( ! empty( $location_info ) && isset( $location_info->location_id ) ) {
1276
								if ( $key == 'gd_country' ) {
1277
									$location_term_actual_country = __( $location_info->country, 'geodirectory' );
1278
								} else if ( $key == 'gd_region' ) {
1279
									$location_term_actual_region = __( $location_info->region, 'geodirectory' );
1280
								} else if ( $key == 'gd_city' ) {
1281
									$location_term_actual_city = __( $location_info->city, 'geodirectory' );
1282
								}
1283
							}
1284
						}
1285
1286
						if ( $is_location_last && $key == 'gd_country' && ! ( isset( $location_terms['gd_region'] ) && $location_terms['gd_region'] != '' ) && ! ( isset( $location_terms['gd_city'] ) && $location_terms['gd_city'] != '' ) ) {
1287 1
							$breadcrumb .= $location_term_actual_country != '' ? $separator . $location_term_actual_country : $separator . $gd_location_link_text;
1288 1
						} else if ( $is_location_last && $key == 'gd_region' && ! ( isset( $location_terms['gd_city'] ) && $location_terms['gd_city'] != '' ) ) {
1289 1
							$breadcrumb .= $location_term_actual_region != '' ? $separator . $location_term_actual_region : $separator . $gd_location_link_text;
1290
						} else if ( $is_location_last && $key == 'gd_city' && empty( $location_terms['gd_neighbourhood'] ) ) {
1291
							$breadcrumb .= $location_term_actual_city != '' ? $separator . $location_term_actual_city : $separator . $gd_location_link_text;
1292
						} else if ( $is_location_last && $key == 'gd_neighbourhood' ) {
1293
							$breadcrumb .= $separator . $gd_location_link_text;
1294
						} else {
1295
							if ( get_option( 'permalink_structure' ) != '' ) {
1296
								$location_link .= $location_term . '/';
1297
							} else {
1298 1
								$location_link .= "&$key=" . $location_term;
1299
							}
1300
1301
							if ( $key == 'gd_country' && $location_term_actual_country != '' ) {
1302
								$gd_location_link_text = $location_term_actual_country;
1303
							} else if ( $key == 'gd_region' && $location_term_actual_region != '' ) {
1304
								$gd_location_link_text = $location_term_actual_region;
1305
							} else if ( $key == 'gd_city' && $location_term_actual_city != '' ) {
1306
								$gd_location_link_text = $location_term_actual_city;
1307
							}
1308
1309
							/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
1310
                            if (geodir_is_page('detail') && !empty($hide_text_part) && in_array($key, $hide_text_part)) {
1311
                                continue;
1312
                            }
1313 1
                            */
1314 1
1315 1
							$breadcrumb .= $separator . '<a href="' . $location_link . '">' . $gd_location_link_text . '</a>';
1316
						}
1317
					}
1318 1
				}
1319 1
			}
1320 1
1321 1
			if ( ! empty( $gd_taxonomy ) ) {
1322 1
				$term_index = 1;
1323 1
1324 1
				//if(get_option('geodir_add_categories_url'))
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
1325 1
				{
1326 1
					if ( get_query_var( $gd_post_type . '_tags' ) ) {
1327 1
						$cat_link = $listing_link . 'tags/';
1328
					} else {
1329 1
						$cat_link = $listing_link;
1330
					}
1331 1
1332
					foreach ( $location_terms as $key => $location_term ) {
1333
						if ( $location_manager && in_array( $key, $hide_url_part ) ) {
1334 1
							continue;
1335
						}
1336
1337
						if ( $location_term != '' ) {
1338 1
							if ( get_option( 'permalink_structure' ) != '' ) {
1339
								$cat_link .= $location_term . '/';
1340 1
							}
1341 1
						}
1342 1
					}
1343
1344
					$term_array = explode( "/", trim( $wp_query->query[ $gd_taxonomy ], "/" ) );
1345 1
					foreach ( $term_array as $term ) {
1346
						$term_link_text = preg_replace( '/-(\d+)$/', '', $term );
1347
						$term_link_text = preg_replace( '/[_-]/', ' ', $term_link_text );
0 ignored issues
show
Unused Code introduced by
$term_link_text 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...
1348 1
1349
						// get term actual name
1350 1
						$term_info = get_term_by( 'slug', $term, $gd_taxonomy, 'ARRAY_A' );
1351 1
						if ( ! empty( $term_info ) && isset( $term_info['name'] ) && $term_info['name'] != '' ) {
1352
							$term_link_text = urldecode( $term_info['name'] );
1353
						} else {
1354
							continue;
1355
							//$term_link_text = wp_strip_all_tags(geodir_ucwords(urldecode($term_link_text)));
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
1356 1
						}
1357
1358 1
						if ( $term_index == count( $term_array ) && $is_taxonomy_last ) {
1359
							$breadcrumb .= $separator . $term_link_text;
1360
						} else {
1361
							$cat_link .= $term . '/';
1362 1
							$breadcrumb .= $separator . '<a href="' . $cat_link . '">' . $term_link_text . '</a>';
1363
						}
1364
						$term_index ++;
1365 1
					}
1366
				}
1367
1368 1
1369
			}
1370
1371
			if ( geodir_is_page( 'detail' ) ) {
1372 1
				$breadcrumb .= $separator . get_the_title();
1373
			}
1374
1375
			$breadcrumb .= '</li>';
1376
1377
1378 1
		} elseif ( geodir_is_page( 'author' ) ) {
1379 1
			$user_id             = get_current_user_id();
1380 1
			$author_link         = get_author_posts_url( $user_id );
1381 1
			$default_author_link = geodir_getlink( $author_link, array(
1382
				'geodir_dashbord' => 'true',
1383
				'stype'           => 'gd_place'
1384
			), false );
1385
1386 1
			/**
1387 1
			 * Filter author page link.
1388
			 *
1389 1
			 * @since 1.0.0
1390
			 *
1391
			 * @param string $default_author_link Default author link.
1392
			 * @param int $user_id                Author ID.
1393
			 */
1394
			$default_author_link = apply_filters( 'geodir_dashboard_author_link', $default_author_link, $user_id );
1395
1396
			$breadcrumb .= '<li>';
1397
			$breadcrumb .= $separator . '<a href="' . $default_author_link . '">' . __( 'My Dashboard', 'geodirectory' ) . '</a>';
1398
1399
			if ( isset( $_REQUEST['list'] ) ) {
1400
				$author_link = geodir_getlink( $author_link, array(
1401 1
					'geodir_dashbord' => 'true',
1402
					'stype'           => $_REQUEST['stype']
1403 1
				), false );
1404
1405
				/**
1406 1
				 * Filter author page link.
1407
				 *
1408
				 * @since 1.0.0
1409
				 *
1410
				 * @param string $author_link Author page link.
1411
				 * @param int $user_id        Author ID.
1412
				 * @param string $_REQUEST    ['stype'] Post type.
1413
				 */
1414
				$author_link = apply_filters( 'geodir_dashboard_author_link', $author_link, $user_id, $_REQUEST['stype'] );
1415 1
1416
				$breadcrumb .= $separator . '<a href="' . $author_link . '">' . __( ucfirst( $post_type_info->label ), 'geodirectory' ) . '</a>';
1417
				$breadcrumb .= $separator . ucfirst( __( 'My', 'geodirectory' ) . ' ' . $_REQUEST['list'] );
1418
			} else {
1419
				$breadcrumb .= $separator . __( ucfirst( $post_type_info->label ), 'geodirectory' );
1420
			}
1421
1422
			$breadcrumb .= '</li>';
1423
		} elseif ( is_category() || is_single() ) {
1424
			$category = get_the_category();
1425
			if ( is_category() ) {
1426
				$breadcrumb .= '<li>' . $separator . $category[0]->cat_name . '</li>';
1427
			}
1428
			if ( is_single() ) {
1429 1
				$breadcrumb .= '<li>' . $separator . '<a href="' . get_category_link( $category[0]->term_id ) . '">' . $category[0]->cat_name . '</a></li>';
1430 1
				$breadcrumb .= '<li>' . $separator . get_the_title() . '</li>';
1431 1
			}
1432
			/* End of my version ##################################################### */
1433
		} else if ( is_page() ) {
1434
			$page_title = get_the_title();
1435
1436
			if ( geodir_is_page( 'location' ) ) {
1437
				$location_page_id = geodir_location_page_id();
1438
				$loc_post         = get_post( $location_page_id );
1439
				$post_name        = $loc_post->post_name;
1440
				$slug             = ucwords( str_replace( '-', ' ', $post_name ) );
1441
				$page_title       = ! empty( $slug ) ? $slug : __( 'Location', 'geodirectory' );
1442
			}
1443
1444
			$breadcrumb .= '<li>' . $separator;
1445
			$breadcrumb .= stripslashes_deep( $page_title );
1446
			$breadcrumb .= '</li>';
1447
		} else if ( is_tag() ) {
1448
			$breadcrumb .= "<li> " . $separator . single_tag_title( '', false ) . '</li>';
1449
		} else if ( is_day() ) {
1450
			$breadcrumb .= "<li> " . $separator . __( " Archive for", 'geodirectory' ) . " ";
1451
			the_time( 'F jS, Y' );
1452
			$breadcrumb .= '</li>';
1453
		} else if ( is_month() ) {
1454
			$breadcrumb .= "<li> " . $separator . __( " Archive for", 'geodirectory' ) . " ";
1455
			the_time( 'F, Y' );
1456
			$breadcrumb .= '</li>';
1457
		} else if ( is_year() ) {
1458
			$breadcrumb .= "<li> " . $separator . __( " Archive for", 'geodirectory' ) . " ";
1459
			the_time( 'Y' );
1460
			$breadcrumb .= '</li>';
1461
		} else if ( is_author() ) {
1462
			$breadcrumb .= "<li> " . $separator . __( " Author Archive", 'geodirectory' );
1463
			$breadcrumb .= '</li>';
1464
		} else if ( isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) ) {
1465
			$breadcrumb .= "<li>" . $separator . __( "Blog Archives", 'geodirectory' );
1466
			$breadcrumb .= '</li>';
1467
		} else if ( is_search() ) {
1468
			$breadcrumb .= "<li> " . $separator . __( " Search Results", 'geodirectory' );
1469
			$breadcrumb .= '</li>';
1470
		}
1471
		$breadcrumb .= '</ul></div>';
1472
1473
		/**
1474
		 * Filter breadcrumb html output.
1475
		 *
1476
		 * @since 1.0.0
1477
		 *
1478
		 * @param string $breadcrumb Breadcrumb HTML.
1479
		 * @param string $separator  Breadcrumb separator.
1480
		 */
1481
		echo $breadcrumb = apply_filters( 'geodir_breadcrumb', $breadcrumb, $separator );
1482
	}
1483
}
1484
1485
1486
add_action( "admin_init", "geodir_allow_wpadmin" ); // check user is admin
1487
if ( ! function_exists( 'geodir_allow_wpadmin' ) ) {
1488
	/**
1489
	 * Allow only admins to access wp-admin.
1490
	 *
1491
	 * Normal users will be redirected to home page.
1492
	 *
1493
	 * @since   1.0.0
1494
	 * @package GeoDirectory
1495
	 * @global object $wpdb WordPress Database object.
1496
	 */
1497
	function geodir_allow_wpadmin() {
1498
		global $wpdb;
1499
		if ( get_option( 'geodir_allow_wpadmin' ) == '0' && is_user_logged_in() && ( ! defined( 'DOING_AJAX' ) ) ) // checking action in request to allow ajax request go through
1500
		{
1501
			if ( current_user_can( 'administrator' ) ) {
1502
			} else {
1503
1504
				wp_redirect( home_url() );
1505
				exit;
1506
			}
1507
1508
		}
1509
	}
1510
}
1511
1512
1513
/**
1514
 * Move Images from a remote url to upload directory.
1515
 *
1516
 * @since   1.0.0
1517
 * @package GeoDirectory
1518
 *
1519
 * @param string $url The remote image url.
1520
 *
1521
 * @return array|WP_Error The uploaded data as array. When failure returns error.
1522
 */
1523
function fetch_remote_file( $url ) {
1524
	// extract the file name and extension from the url
1525
	require_once( ABSPATH . 'wp-includes/pluggable.php' );
1526
	$file_name = basename( $url );
1527
	if ( strpos( $file_name, '?' ) !== false ) {
1528
		list( $file_name ) = explode( '?', $file_name );
1529
	}
1530
	$dummy        = false;
1531
	$add_to_cache = false;
1532
	$key          = null;
1533
	if ( strpos( $url, '/dummy/' ) !== false ) {
1534
		$dummy = true;
1535
		$key   = "dummy_" . str_replace( '.', '_', $file_name );
1536
		$value = get_transient( 'cached_dummy_images' );
1537
		if ( $value ) {
1538
			if ( isset( $value[ $key ] ) ) {
1539
				return $value[ $key ];
1540
			} else {
1541
				$add_to_cache = true;
1542
			}
1543
		} else {
1544
			$add_to_cache = true;
1545
		}
1546
	}
1547
1548
	// get placeholder file in the upload dir with a unique, sanitized filename
1549
1550
	$post_upload_date = isset( $post['upload_date'] ) ? $post['upload_date'] : '';
0 ignored issues
show
Bug introduced by
The variable $post seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
1551
1552
	$upload = wp_upload_bits( $file_name, 0, '', $post_upload_date );
1553
	if ( $upload['error'] ) {
1554
		return new WP_Error( 'upload_dir_error', $upload['error'] );
1555
	}
1556
1557
1558
	sleep( 0.3 );// if multiple remote file this can cause the remote server to timeout so we add a slight delay
1559
1560
	// fetch the remote url and write it to the placeholder file
1561
	$headers = wp_remote_get( $url, array( 'stream' => true, 'filename' => $upload['file'] ) );
1562
1563
	$log_message = '';
1564
	if ( is_wp_error( $headers ) ) {
1565
		echo 'file: ' . $url;
1566
1567
		return new WP_Error( 'import_file_error', $headers->get_error_message() );
1568
	}
1569
1570
	$filesize = filesize( $upload['file'] );
1571
	// request failed
1572
	if ( ! $headers ) {
1573
		$log_message = __( 'Remote server did not respond', 'geodirectory' );
1574
	} // make sure the fetch was successful
1575
	elseif ( $headers['response']['code'] != '200' ) {
1576
		$log_message = sprintf( __( 'Remote server returned error response %1$d %2$s', 'geodirectory' ), esc_html( $headers['response'] ), get_status_header_desc( $headers['response'] ) );
1577
	} elseif ( isset( $headers['headers']['content-length'] ) && $filesize != $headers['headers']['content-length'] ) {
1578
		$log_message = __( 'Remote file is incorrect size', 'geodirectory' );
1579
	} elseif ( 0 == $filesize ) {
1580
		$log_message = __( 'Zero size file downloaded', 'geodirectory' );
1581
	}
1582
1583
	if ( $log_message ) {
1584
		$del = unlink( $upload['file'] );
1585
		if ( ! $del ) {
1586
			geodir_error_log( __( 'GeoDirectory: fetch_remote_file() failed to delete temp file.', 'geodirectory' ) );
1587
		}
1588
1589
		return new WP_Error( 'import_file_error', $log_message );
1590
	}
1591
1592
	if ( $dummy && $add_to_cache && is_array( $upload ) ) {
1593
		$images = get_transient( 'cached_dummy_images' );
1594
		if ( is_array( $images ) ) {
1595
			$images[ $key ] = $upload;
1596
		} else {
1597
			$images = array( $key => $upload );
1598
		}
1599
1600
		//setting the cache using the WP Transient API
1601
		set_transient( 'cached_dummy_images', $images, 60 * 10 ); //10 minutes cache
1602
	}
1603
1604
	return $upload;
1605
}
1606
1607
/**
1608
 * Get maximum file upload size.
1609
 *
1610
 * @since   1.0.0
1611
 * @package GeoDirectory
1612
 * @return string|void Max upload size.
1613
 */
1614 View Code Duplication
function geodir_max_upload_size() {
0 ignored issues
show
Best Practice introduced by
The function geodir_max_upload_size() has been defined more than once; this definition is ignored, only the first definition in geodirectory-functions/custom_fields_functions.php (L2622-2633) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1615
	$max_filesize = (float) get_option( 'geodir_upload_max_filesize', 2 );
1616
1617
	if ( $max_filesize > 0 && $max_filesize < 1 ) {
1618
		$max_filesize = (int) ( $max_filesize * 1024 ) . 'kb';
1619
	} else {
1620
		$max_filesize = $max_filesize > 0 ? $max_filesize . 'mb' : '2mb';
1621
	}
1622
1623
	/**
1624
	 * Filter default image upload size limit.
1625
	 *
1626
	 * @since 1.0.0
1627
	 *
1628
	 * @param string $max_filesize Max file upload size. Ex. 10mb, 512kb.
1629
	 */
1630
	return apply_filters( 'geodir_default_image_upload_size_limit', $max_filesize );
1631
}
1632
1633
/**
1634
 * Check if dummy folder exists or not.
1635
 *
1636
 * Check if dummy folder exists or not , if not then fetch from live url.
1637
 *
1638
 * @since   1.0.0
1639
 * @package GeoDirectory
1640
 * @return bool If dummy folder exists returns true, else false.
1641
 */
1642
function geodir_dummy_folder_exists() {
1643
	$path = geodir_plugin_path() . '/geodirectory-admin/dummy/';
1644
	if ( ! is_dir( $path ) ) {
1645
		return false;
1646
	} else {
1647
		return true;
1648
	}
1649
1650
}
1651
1652
/**
1653
 * Get the author info.
1654
 *
1655
 * @since   1.0.0
1656
 * @package GeoDirectory
1657
 * @global object $wpdb WordPress Database object.
1658
 *
1659
 * @param int $aid      The author ID.
1660
 *
1661
 * @return object Author info.
1662
 */
1663
function geodir_get_author_info( $aid ) {
1664
	global $wpdb;
1665
	/*$infosql = "select * from $wpdb->users where ID=$aid";*/
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...
1666
	$infosql = $wpdb->prepare( "select * from $wpdb->users where ID=%d", array( $aid ) );
1667
	$info    = $wpdb->get_results( $infosql );
1668
	if ( $info ) {
1669
		return $info[0];
1670
	}
1671
}
1672
1673
if ( ! function_exists( 'adminEmail' ) ) {
1674
	/**
1675
	 * Send emails to client on post submission, renew etc.
1676
	 *
1677
	 * @since   1.0.0
1678
	 * @package GeoDirectory
1679
	 * @global object $wpdb        WordPress Database object.
1680
	 *
1681
	 * @param int|string $page_id  Page ID.
1682
	 * @param int|string $user_id  User ID.
1683
	 * @param string $message_type Can be 'expiration','post_submited','renew','upgrade','claim_approved','claim_rejected','claim_requested','auto_claim','payment_success','payment_fail'.
1684
	 * @param string $custom_1     Custom data to be sent.
1685
	 */
1686
	function adminEmail( $page_id, $user_id, $message_type, $custom_1 = '' ) {
1687
		global $wpdb;
1688
		if ( $message_type == 'expiration' ) {
1689
			$subject        = stripslashes( __( get_option( 'renew_email_subject' ), 'geodirectory' ) );
1690
			$client_message = stripslashes( __( get_option( 'renew_email_content' ), 'geodirectory' ) );
1691
		} elseif ( $message_type == 'post_submited' ) {
1692
			$subject        = __( get_option( 'post_submited_success_email_subject_admin' ), 'geodirectory' );
1693
			$client_message = __( get_option( 'post_submited_success_email_content_admin' ), 'geodirectory' );
1694
		} elseif ( $message_type == 'renew' ) {
1695
			$subject        = __( get_option( 'post_renew_success_email_subject_admin' ), 'geodirectory' );
1696
			$client_message = __( get_option( 'post_renew_success_email_content_admin' ), 'geodirectory' );
1697
		} elseif ( $message_type == 'upgrade' ) {
1698
			$subject        = __( get_option( 'post_upgrade_success_email_subject_admin' ), 'geodirectory' );
1699
			$client_message = __( get_option( 'post_upgrade_success_email_content_admin' ), 'geodirectory' );
1700
		} elseif ( $message_type == 'claim_approved' ) {
1701
			$subject        = __( get_option( 'claim_approved_email_subject' ), 'geodirectory' );
1702
			$client_message = __( get_option( 'claim_approved_email_content' ), 'geodirectory' );
1703
		} elseif ( $message_type == 'claim_rejected' ) {
1704
			$subject        = __( get_option( 'claim_rejected_email_subject' ), 'geodirectory' );
1705
			$client_message = __( get_option( 'claim_rejected_email_content' ), 'geodirectory' );
1706
		} elseif ( $message_type == 'claim_requested' ) {
1707
			$subject        = __( get_option( 'claim_email_subject_admin' ), 'geodirectory' );
1708
			$client_message = __( get_option( 'claim_email_content_admin' ), 'geodirectory' );
1709
		} elseif ( $message_type == 'auto_claim' ) {
1710
			$subject        = __( get_option( 'auto_claim_email_subject' ), 'geodirectory' );
1711
			$client_message = __( get_option( 'auto_claim_email_content' ), 'geodirectory' );
1712
		} elseif ( $message_type == 'payment_success' ) {
1713
			$subject        = __( get_option( 'post_payment_success_admin_email_subject' ), 'geodirectory' );
1714
			$client_message = __( get_option( 'post_payment_success_admin_email_content' ), 'geodirectory' );
1715
		} elseif ( $message_type == 'payment_fail' ) {
1716
			$subject        = __( get_option( 'post_payment_fail_admin_email_subject' ), 'geodirectory' );
1717
			$client_message = __( get_option( 'post_payment_fail_admin_email_content' ), 'geodirectory' );
1718
		}
1719
		$transaction_details = $custom_1;
1720
		$fromEmail           = get_option( 'site_email' );
1721
		$fromEmailName       = get_site_emailName();
1722
//$alivedays = get_post_meta($page_id,'alive_days',true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
1723
		$pkg_limit            = get_property_price_info_listing( $page_id );
1724
		$alivedays            = $pkg_limit['days'];
1725
		$productlink          = get_permalink( $page_id );
1726
		$post_info            = get_post( $page_id );
1727
		$post_date            = date( 'dS F,Y', strtotime( $post_info->post_date ) );
1728
		$listingLink          = '<a href="' . $productlink . '"><b>' . $post_info->post_title . '</b></a>';
1729
		$loginurl             = geodir_login_url();
1730
		$loginurl_link        = '<a href="' . $loginurl . '">login</a>';
1731
		$siteurl              = home_url();
1732
		$siteurl_link         = '<a href="' . $siteurl . '">' . $fromEmailName . '</a>';
1733
		$user_info            = get_userdata( $user_id );
1734
		$user_email           = $user_info->user_email;
1735
		$display_name         = geodir_get_client_name( $user_id );
1736
		$user_login           = $user_info->user_login;
1737
		$number_of_grace_days = get_option( 'ptthemes_listing_preexpiry_notice_days' );
1738
		if ( $number_of_grace_days == '' ) {
1739
			$number_of_grace_days = 1;
1740
		}
1741
		if ( $post_info->post_type == 'event' ) {
1742
			$post_type = 'event';
1743
		} else {
1744
			$post_type = 'listing';
1745
		}
1746
		$renew_link     = '<a href="' . $siteurl . '?ptype=post_' . $post_type . '&renew=1&pid=' . $page_id . '">' . RENEW_LINK . '</a>';
1747
		$search_array   = array(
1748
			'[#client_name#]',
1749
			'[#listing_link#]',
1750
			'[#posted_date#]',
1751
			'[#number_of_days#]',
1752
			'[#number_of_grace_days#]',
1753
			'[#login_url#]',
1754
			'[#username#]',
1755
			'[#user_email#]',
1756
			'[#site_name_url#]',
1757
			'[#renew_link#]',
1758
			'[#post_id#]',
1759
			'[#site_name#]',
1760
			'[#transaction_details#]'
1761
		);
1762
		$replace_array  = array(
1763
			$display_name,
1764
			$listingLink,
1765
			$post_date,
1766
			$alivedays,
1767
			$number_of_grace_days,
1768
			$loginurl_link,
1769
			$user_login,
1770
			$user_email,
1771
			$siteurl_link,
1772
			$renew_link,
1773 7
			$page_id,
1774 7
			$fromEmailName,
1775 7
			$transaction_details
1776
		);
1777
		$client_message = str_replace( $search_array, $replace_array, $client_message );
0 ignored issues
show
Bug introduced by
The variable $client_message 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...
1778
		$subject        = str_replace( $search_array, $replace_array, $subject );
0 ignored issues
show
Bug introduced by
The variable $subject 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...
1779
		$headers        = 'MIME-Version: 1.0' . "\r\n";
1780 7
		$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
1781
		$headers .= 'From: ' . $fromEmailName . ' <' . $fromEmail . '>' . "\r\n";
1782
1783
		$to      = $fromEmail;
1784
		$message = $client_message;
1785
1786
1787
		/**
1788
		 * Filter the admin email to address.
1789
		 *
1790
		 * @since   1.6.1
1791
		 * @package GeoDirectory
1792
		 *
1793 7
		 * @param string $to           The email address the email is being sent to.
1794
		 * @param int|string $page_id  Page ID.
1795
		 * @param int|string $user_id  User ID.
1796
		 * @param string $message_type Can be 'expiration','post_submited','renew','upgrade','claim_approved','claim_rejected','claim_requested','auto_claim','payment_success','payment_fail'.
1797 7
		 * @param string $custom_1     Custom data to be sent.
1798
		 */
1799
		$to = apply_filters( 'geodir_adminEmail_to', $to, $page_id, $user_id, $message_type, $custom_1 );
1800
		/**
1801
		 * Filter the admin email subject.
1802
		 *
1803
		 * @since   1.6.1
1804
		 * @package GeoDirectory_Payment_Manager
1805
		 *
1806
		 * @param string $subject      The email subject.
1807
		 * @param int|string $page_id  Page ID.
1808
		 * @param int|string $user_id  User ID.
1809
		 * @param string $message_type Can be 'expiration','post_submited','renew','upgrade','claim_approved','claim_rejected','claim_requested','auto_claim','payment_success','payment_fail'.
1810
		 * @param string $custom_1     Custom data to be sent.
1811
		 */
1812
		$subject = apply_filters( 'geodir_adminEmail_subject', $subject, $page_id, $user_id, $message_type, $custom_1 );
1813
		/**
1814
		 * Filter the admin email message.
1815
		 *
1816
		 * @since   1.6.1
1817 2
		 * @package GeoDirectory_Payment_Manager
1818
		 *
1819
		 * @param string $message      The email message text.
1820
		 * @param int|string $page_id  Page ID.
1821
		 * @param int|string $user_id  User ID.
1822
		 * @param string $message_type Can be 'expiration','post_submited','renew','upgrade','claim_approved','claim_rejected','claim_requested','auto_claim','payment_success','payment_fail'.
1823
		 * @param string $custom_1     Custom data to be sent.
1824
		 */
1825
		$message = apply_filters( 'geodir_adminEmail_message', $message, $page_id, $user_id, $message_type, $custom_1 );
1826
		/**
1827
		 * Filter the admin email headers.
1828
		 *
1829
		 * @since   1.6.1
1830
		 * @package GeoDirectory_Payment_Manager
1831
		 *
1832
		 * @param string $headers      The email headers.
1833
		 * @param int|string $page_id  Page ID.
1834
		 * @param int|string $user_id  User ID.
1835
		 * @param string $message_type Can be 'expiration','post_submited','renew','upgrade','claim_approved','claim_rejected','claim_requested','auto_claim','payment_success','payment_fail'.
1836
		 * @param string $custom_1     Custom data to be sent.
1837
		 */
1838
		$headers = apply_filters( 'geodir_adminEmail_headers', $headers, $page_id, $user_id, $message_type, $custom_1 );
1839
1840
1841
		$sent = wp_mail( $to, $subject, $message, $headers );
1842
		if ( ! $sent ) {
1843
			if ( is_array( $to ) ) {
1844
				$to = implode( ',', $to );
1845
			}
1846
			$log_message = sprintf(
1847
				__( "Email from GeoDirectory failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\n\n", 'geodirectory' ),
1848
				$message_type,
1849 18
				date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ),
1850
				$to,
1851
				$subject
1852
			);
1853 18
			geodir_error_log( $log_message );
1854
		}
1855
	}
1856
}
1857
1858
if ( ! function_exists( 'sendEmail' ) ) {
1859
	/**
1860
	 * @todo    could be a duplicate of geodir_sendEmail.
1861
	 *
1862
	 * @since   1.0.0
1863
	 * @package GeoDirectory
1864
	 *
1865 18
	 * @param string $fromEmail     Sender email address.
1866
	 * @param string $fromEmailName Sender name.
1867
	 * @param string $toEmail       Receiver email address.
1868 18
	 * @param string $toEmailName   Receiver name.
1869
	 * @param string $to_subject    Email subject.
1870
	 * @param string $to_message    Email content.
1871
	 * @param string $extra         Not being used.
1872
	 * @param string $message_type  The message type. Can be send_friend, send_enquiry, forgot_password, registration.
1873
	 * @param string $post_id       The post ID.
1874
	 * @param string $user_id       The user ID.
1875
	 */
1876
	function sendEmail( $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra = '', $message_type, $post_id = '', $user_id = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $extra 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...
Unused Code introduced by
The parameter $user_id 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...
1877
		$login_details = '';
1878
		if ( $message_type == 'send_friend' ) {
1879
			$subject = stripslashes( __( get_option( 'email_friend_subject' ), 'geodirectory' ) );
1880
			$message = stripslashes( __( get_option( 'email_friend_content' ), 'geodirectory' ) );
1881
		} elseif ( $message_type == 'send_enquiry' ) {
1882
			$subject = __( get_option( 'email_enquiry_subject' ), 'geodirectory' );
1883
			$message = __( get_option( 'email_enquiry_content' ), 'geodirectory' );
1884
		} elseif ( $message_type == 'forgot_password' ) {
1885
			$subject       = __( get_option( 'forgot_password_subject' ), 'geodirectory' );
1886
			$message       = __( get_option( 'forgot_password_content' ), 'geodirectory' );
1887
			$login_details = $to_message;
1888
		} elseif ( $message_type == 'registration' ) {
1889
			$subject       = __( get_option( 'registration_success_email_subject' ), 'geodirectory' );
1890
			$message       = __( get_option( 'registration_success_email_content' ), 'geodirectory' );
1891
			$login_details = $to_message;
1892
		}
1893
		$to_message        = nl2br( $to_message );
1894
		$sitefromEmail     = get_option( 'site_email' );
1895
		$sitefromEmailName = get_site_emailName();
1896
		$productlink       = get_permalink( $post_id );
1897
		$post_info         = get_post( $post_id );
1898
		$listingLink       = '<a href="' . $productlink . '"><b>' . $post_info->post_title . '</b></a>';
1899
		$siteurl           = home_url();
1900
		$siteurl_link      = '<a href="' . $siteurl . '">' . $siteurl . '</a>';
1901
		$loginurl          = geodir_login_url();
1902
		$loginurl_link     = '<a href="' . $loginurl . '">login</a>';
1903
		if ( $fromEmail == '' ) {
1904
			$fromEmail = get_option( 'site_email' );
1905
		}
1906
		if ( $fromEmailName == '' ) {
1907
			$fromEmailName = get_option( 'site_email_name' );
1908
		}
1909
		$search_array  = array(
1910
			'[#listing_link#]',
1911
			'[#site_name_url#]',
1912
			'[#post_id#]',
1913
			'[#site_name#]',
1914
			'[#to_name#]',
1915
			'[#from_name#]',
1916
			'[#subject#]',
1917
			'[#comments#]',
1918
			'[#login_url#]',
1919
			'[#login_details#]',
1920
			'[#client_name#]'
1921
		);
1922
		$replace_array = array(
1923
			$listingLink,
1924
			$siteurl_link,
1925
			$post_id,
1926
			$sitefromEmailName,
1927
			$toEmailName,
1928
			$fromEmailName,
1929
			$to_subject,
1930
			$to_message,
1931
			$loginurl_link,
1932
			$login_details,
1933
			$toEmailName
1934
		);
1935
		$message       = str_replace( $search_array, $replace_array, $message );
0 ignored issues
show
Bug introduced by
The variable $message 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...
1936
1937 8
		$search_array  = array(
1938
			'[#listing_link#]',
1939 8
			'[#site_name_url#]',
1940 8
			'[#post_id#]',
1941
			'[#site_name#]',
1942
			'[#to_name#]',
1943
			'[#from_name#]',
1944 8
			'[#subject#]',
1945 8
			'[#client_name#]'
1946
		);
1947 8
		$replace_array = array(
1948
			$listingLink,
1949
			$siteurl_link,
1950 8
			$post_id,
1951 8
			$sitefromEmailName,
1952 4
			$toEmailName,
1953 4
			$fromEmailName,
1954 4
			$to_subject,
1955
			$toEmailName
1956
		);
1957 4
		$subject       = str_replace( $search_array, $replace_array, $subject );
0 ignored issues
show
Bug introduced by
The variable $subject 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...
1958
		$headers       = 'MIME-Version: 1.0' . "\r\n";
1959
		$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
1960 4
		$headers .= "Reply-To: " . $fromEmail . "\r\n";
1961 3
		$headers .= 'From: ' . $sitefromEmailName . ' <' . $sitefromEmail . '>' . "\r\n";
1962 3
1963 1
		$to = $toEmail;
1964
1965
		$sent = wp_mail( $to, $subject, $message, $headers );
1966 1
		if ( ! $sent ) {
1967
			if ( is_array( $to ) ) {
1968
				$to = implode( ',', $to );
1969 1
			}
1970 1
			$log_message = sprintf(
1971 1
				__( "Email from GeoDirectory failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\n\n", 'geodirectory' ),
1972 1
				$message_type,
1973
				date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ),
1974 8
				$to,
1975
				$subject
1976
			);
1977
			geodir_error_log( $log_message );
1978
		}
1979
1980
		///////// ADMIN BCC EMIALS
1981
		$admin_bcc = false;
1982
		if ( $message_type == 'registration' ) {
1983
			$message_raw  = explode( __( "Password:", 'geodirectory' ), $message );
1984
			$message_raw2 = explode( "</p>", $message_raw[1], 2 );
1985
			$message      = $message_raw[0] . __( 'Password:', 'geodirectory' ) . ' **********</p>' . $message_raw2[1];
1986
		}
1987
		$adminEmail = get_bloginfo( 'admin_email' );
1988
		$to         = $adminEmail;
1989
1990
		if ( $message_type == 'registration' && get_option( 'bcc_new_user' ) ) {
1991
			$subject .= ' - ADMIN BCC COPY';
1992 8
			$admin_bcc = true;
1993 8
		} elseif ( $message_type == 'send_friend' && get_option( 'bcc_friend' ) ) {
1994 8
			$subject .= ' - ADMIN BCC COPY';
1995
			$admin_bcc = true;
1996 8
		} elseif ( $message_type == 'send_enquiry' && get_option( 'bcc_enquiry' ) ) {
1997 8
			$subject .= ' - ADMIN BCC COPY';
1998
			$admin_bcc = true;
1999 8
		}
2000
2001 View Code Duplication
		if ( $admin_bcc === true ) {
2002
			$sent = wp_mail( $to, $subject, $message, $headers );
2003
			if ( ! $sent ) {
2004
				if ( is_array( $to ) ) {
2005
					$to = implode( ',', $to );
2006
				}
2007
				$log_message = sprintf(
2008 8
					__( "Email from GeoDirectory failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\n\n", 'geodirectory' ),
2009
					$message_type,
2010 8
					date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ),
2011
					$to,
2012
					$subject
2013
				);
2014 8
				geodir_error_log( $log_message );
2015
			}
2016
		}
2017
2018
	}
2019
}
2020
2021
/*
2022
Language translation helper functions
2023
*/
2024
2025
/**
2026
 * Function to get the translated category id's.
2027
 *
2028
 * @since   1.0.0
2029
 * @package GeoDirectory
2030
 *
2031 8
 * @param array $ids_array Category IDs.
2032
 * @param string $type     Category taxonomy.
2033 8
 *
2034
 * @return array Category IDs.
2035 8
 */
2036
function gd_lang_object_ids( $ids_array, $type ) {
2037
	if ( function_exists( 'icl_object_id' ) ) {
2038 8
		$res = array();
2039
		foreach ( $ids_array as $id ) {
2040
			$xlat = icl_object_id( $id, $type, false );
2041
			if ( ! is_null( $xlat ) ) {
2042
				$res[] = $xlat;
2043
			}
2044
		}
2045
2046
		return $res;
2047
	} else {
2048
		return $ids_array;
2049
	}
2050
}
2051 8
2052 8
2053
/**
2054 8
 * function to add class to body when multi post type is active.
2055
 *
2056
 * @since   1.0.0
2057
 * @since   1.5.6 Add geodir-page class to body for all gd pages.
2058
 * @package GeoDirectory
2059
 * @global object $wpdb  WordPress Database object.
2060
 *
2061
 * @param array $classes Body CSS classes.
2062 8
 *
2063
 * @return array Modified Body CSS classes.
2064 8
 */
2065 1
function geodir_custom_posts_body_class( $classes ) {
2066 1
	global $wpdb, $wp;
2067 1
	$post_types = geodir_get_posttypes( 'object' );
2068 1
	if ( ! empty( $post_types ) && count( (array) $post_types ) > 1 ) {
2069 1
		$classes[] = 'geodir_custom_posts';
2070 8
	}
2071
2072
	// fix body class for signup page
2073
	if ( geodir_is_page( 'login' ) ) {
2074
		$new_classes   = array();
2075
		$new_classes[] = 'signup page-geodir-signup';
2076
		if ( ! empty( $classes ) ) {
2077
			foreach ( $classes as $class ) {
2078
				if ( $class && $class != 'home' && $class != 'blog' ) {
2079 8
					$new_classes[] = $class;
2080 8
				}
2081 8
			}
2082
		}
2083 8
		$classes = $new_classes;
2084
	}
2085
2086
	if ( geodir_is_geodir_page() ) {
2087
		$classes[] = 'geodir-page';
2088
	}
2089
2090
	return $classes;
2091 8
}
2092
2093 8
add_filter( 'body_class', 'geodir_custom_posts_body_class' ); // let's add a class to the body so we can style the new addition to the search
2094 8
2095 8
2096
/**
2097 8
 * Returns available map zoom levels.
2098
 *
2099 8
 * @since   1.0.0
2100 8
 * @package GeoDirectory
2101 8
 * @return array Available map zoom levels.
2102 8
 */
2103 8
function geodir_map_zoom_level() {
2104 8
	/**
2105 8
	 * Filter GD map zoom level.
2106
	 *
2107
	 * @since 1.0.0
2108 8
	 */
2109 8
	return apply_filters( 'geodir_map_zoom_level', array(
2110
		1,
2111 8
		2,
2112
		3,
2113
		4,
2114
		5,
2115
		6,
2116
		7,
2117
		8,
2118
		9,
2119
		10,
2120
		11,
2121
		12,
2122
		13,
2123
		14,
2124
		15,
2125
		16,
2126 8
		17,
2127
		18,
2128 8
		19
2129 8
	) );
2130
2131
}
2132
2133 8
2134
/**
2135
 * This function takes backup of an option so they can be restored later.
2136
 *
2137
 * @since   1.0.0
2138
 * @package GeoDirectory
2139
 *
2140
 * @param string $geodir_option_name Option key.
2141
 */
2142
function geodir_option_version_backup( $geodir_option_name ) {
2143
	$version_date  = time();
2144
	$geodir_option = get_option( $geodir_option_name );
2145
2146
	if ( ! empty( $geodir_option ) ) {
2147
		add_option( $geodir_option_name . '_' . $version_date, $geodir_option );
2148 8
	}
2149
}
2150 8
2151 8
/**
2152
 * display add listing page for wpml.
2153
 *
2154
 * @since   1.0.0
2155 8
 * @package GeoDirectory
2156 8
 *
2157
 * @param int $page_id The page ID.
2158 8
 *
2159
 * @return int Page ID.
2160
 */
2161
function get_page_id_geodir_add_listing_page( $page_id ) {
2162 8
	if ( geodir_wpml_multilingual_status() ) {
2163 4
		$post_type = 'post_page';
2164 4
		$page_id   = geodir_get_wpml_element_id( $page_id, $post_type );
2165 2
	}
2166 2
2167 4
	return $page_id;
2168
}
2169 8
2170
/**
2171
 * Returns wpml multilingual status.
2172
 *
2173
 * @since   1.0.0
2174
 * @package GeoDirectory
2175
 * @return bool Returns true when sitepress multilingual CMS active. else returns false.
2176
 */
2177
function geodir_wpml_multilingual_status() {
2178
	if ( function_exists( 'icl_object_id' ) ) {
2179
		return true;
2180
	}
2181
2182
	return false;
2183
}
2184 8
2185
/**
2186 8
 * Returns WPML element ID.
2187 8
 *
2188
 * @since   1.0.0
2189
 * @package GeoDirectory
2190 8
 *
2191 8
 * @param int $page_id      The page ID.
2192
 * @param string $post_type The post type.
2193 8
 *
2194 8
 * @return int Element ID when exists. Else the page id.
2195
 */
2196
function geodir_get_wpml_element_id( $page_id, $post_type ) {
2197
	global $sitepress;
2198 8
	if ( geodir_wpml_multilingual_status() && ! empty( $sitepress ) && isset( $sitepress->queries ) ) {
2199
		$trid = $sitepress->get_element_trid( $page_id, $post_type );
2200
2201
		if ( $trid > 0 ) {
2202 8
			$translations = $sitepress->get_element_translations( $trid, $post_type );
2203 2
2204 2
			$lang = $sitepress->get_current_language();
2205
			$lang = $lang ? $lang : $sitepress->get_default_language();
2206 8
2207
			if ( ! empty( $translations ) && ! empty( $lang ) && isset( $translations[ $lang ] ) && isset( $translations[ $lang ]->element_id ) && ! empty( $translations[ $lang ]->element_id ) ) {
2208
				$page_id = $translations[ $lang ]->element_id;
2209
			}
2210 8
		}
2211
	}
2212
2213
	return $page_id;
2214 8
}
2215 2
2216 2
/**
2217
 * WPML check element ID.
2218 8
 *
2219
 * @since      1.0.0
2220
 * @package    GeoDirectory
2221
 * @deprecated 1.4.6 No longer needed as we handle translating GD pages as normal now.
2222 8
 */
2223 4
function geodir_wpml_check_element_id() {
2224
	global $sitepress;
2225 4
	if ( geodir_wpml_multilingual_status() && ! empty( $sitepress ) && isset( $sitepress->queries ) ) {
2226 2
		$el_type      = 'post_page';
2227 2
		$el_id        = get_option( 'geodir_add_listing_page' );
2228 4
		$default_lang = $sitepress->get_default_language();
2229 8
		$el_details   = $sitepress->get_element_language_details( $el_id, $el_type );
2230
2231 8
		if ( ! ( $el_id > 0 && $default_lang && ! empty( $el_details ) && isset( $el_details->language_code ) && $el_details->language_code == $default_lang ) ) {
2232
			if ( ! $el_details->source_language_code ) {
2233
				$sitepress->set_element_language_details( $el_id, $el_type, '', $default_lang );
2234
				$sitepress->icl_translations_cache->clear();
2235
			}
2236
		}
2237
	}
2238
}
2239
2240
/**
2241
 * Returns orderby SQL using the given query args.
2242
 *
2243
 * @since   1.0.0
2244
 * @package GeoDirectory
2245
 * @global object $wpdb          WordPress Database object.
2246 8
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2247
 *
2248 8
 * @param array $query_args      The query array.
2249 8
 *
2250
 * @return string Orderby SQL.
2251
 */
2252
function geodir_widget_listings_get_order( $query_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $query_args 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...
2253 8
	global $wpdb, $plugin_prefix, $gd_query_args_widgets;
2254
2255
	$query_args = $gd_query_args_widgets;
2256
	if ( empty( $query_args ) || empty( $query_args['is_geodir_loop'] ) ) {
2257
		return $wpdb->posts . ".post_date DESC, ";
2258
	}
2259
2260
	$post_type = empty( $query_args['post_type'] ) ? 'gd_place' : $query_args['post_type'];
2261
	$table     = $plugin_prefix . $post_type . '_detail';
2262
2263
	$sort_by = ! empty( $query_args['order_by'] ) ? $query_args['order_by'] : '';
2264
2265
	switch ( $sort_by ) {
2266
		case 'latest':
2267
		case 'newest':
2268 8
			$orderby = $wpdb->posts . ".post_date DESC, ";
2269
			break;
2270 8
		case 'featured':
2271 8
			$orderby = $table . ".is_featured ASC, ";
2272
			break;
2273
		case 'az':
2274
			$orderby = $wpdb->posts . ".post_title ASC, ";
2275 8
			break;
2276 8
		case 'high_review':
2277 8
			$orderby = $table . ".rating_count DESC, " . $table . ".overall_rating DESC, ";
2278
			break;
2279 8
		case 'high_rating':
2280
			$orderby = "( " . $table . ".overall_rating  ) DESC, ";
2281
			break;
2282
		case 'random':
2283
			$orderby = "RAND(), ";
2284
			break;
2285
		default:
2286
			$orderby = $wpdb->posts . ".post_title ASC, ";
2287
			break;
2288
	}
2289
2290
	return $orderby;
2291
}
2292
2293 2
/**
2294 2
 * Retrieve listings/count using requested filter parameters.
2295 2
 *
2296
 * @since   1.0.0
2297 2
 * @package GeoDirectory
2298 2
 * @since   1.4.2 New parameter $count_only added
2299 2
 * @global object $wpdb          WordPress Database object.
2300
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2301
 * @global string $table_prefix  WordPress Database Table prefix.
2302
 *
2303
 * @param array $query_args      The query array.
2304
 * @param  int|bool $count_only  If true returns listings count only, otherwise returns array
2305
 *
2306
 * @return mixed Result object.
2307
 */
2308
function geodir_get_widget_listings( $query_args = array(), $count_only = false ) {
2309 2
	global $wpdb, $plugin_prefix, $table_prefix;
2310 2
	$GLOBALS['gd_query_args_widgets'] = $query_args;
2311
	$gd_query_args_widgets            = $query_args;
2312
2313
	$post_type = empty( $query_args['post_type'] ) ? 'gd_place' : $query_args['post_type'];
2314
	$table     = $plugin_prefix . $post_type . '_detail';
2315
2316
	$fields = $wpdb->posts . ".*, " . $table . ".*";
2317
	/**
2318
	 * Filter widget listing fields string part that is being used for query.
2319
	 *
2320
	 * @since 1.0.0
2321
	 *
2322
	 * @param string $fields    Fields string.
2323
	 * @param string $table     Table name.
2324 2
	 * @param string $post_type Post type.
2325 2
	 */
2326 2
	$fields = apply_filters( 'geodir_filter_widget_listings_fields', $fields, $table, $post_type );
2327
2328 2
	$join = "INNER JOIN " . $table . " ON (" . $table . ".post_id = " . $wpdb->posts . ".ID)";
2329 2
2330 2
	########### WPML ###########
2331
2332
	if ( function_exists( 'icl_object_id' ) ) {
2333
		global $sitepress;
2334
		$lang_code = ICL_LANGUAGE_CODE;
2335
		if ( $lang_code ) {
2336
			$join .= " JOIN " . $table_prefix . "icl_translations icl_t ON icl_t.element_id = " . $table_prefix . "posts.ID";
2337
		}
2338
	}
2339
2340 2
	########### WPML ###########
2341
2342 2
	/**
2343
	 * Filter widget listing join clause string part that is being used for query.
2344
	 *
2345
	 * @since 1.0.0
2346
	 *
2347
	 * @param string $join      Join clause string.
2348
	 * @param string $post_type Post type.
2349
	 */
2350
	$join = apply_filters( 'geodir_filter_widget_listings_join', $join, $post_type );
2351
2352
	$post_status = is_super_admin() ? " OR " . $wpdb->posts . ".post_status = 'private'" : '';
2353
2354
	$where = " AND ( " . $wpdb->posts . ".post_status = 'publish' " . $post_status . " ) AND " . $wpdb->posts . ".post_type = '" . $post_type . "'";
2355
2356
	########### WPML ###########
2357
	if ( function_exists( 'icl_object_id' ) ) {
2358
		if ( $lang_code ) {
2359
			$where .= " AND icl_t.language_code = '$lang_code' AND icl_t.element_type = 'post_$post_type' ";
0 ignored issues
show
Bug introduced by
The variable $lang_code 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...
2360
		}
2361
	}
2362
	########### WPML ###########
2363
	/**
2364
	 * Filter widget listing where clause string part that is being used for query.
2365
	 *
2366
	 * @since 1.0.0
2367
	 *
2368
	 * @param string $where     Where clause string.
2369
	 * @param string $post_type Post type.
2370
	 */
2371
	$where = apply_filters( 'geodir_filter_widget_listings_where', $where, $post_type );
2372
	$where = $where != '' ? " WHERE 1=1 " . $where : '';
2373
2374
	$groupby = " GROUP BY $wpdb->posts.ID ";
2375
	/**
2376
	 * Filter widget listing groupby clause string part that is being used for query.
2377
	 *
2378
	 * @since 1.0.0
2379
	 *
2380
	 * @param string $groupby   Group by clause string.
2381
	 * @param string $post_type Post type.
2382
	 */
2383
	$groupby = apply_filters( 'geodir_filter_widget_listings_groupby', $groupby, $post_type );
2384
2385
	if ( $count_only ) {
2386
		$sql  = "SELECT COUNT(" . $wpdb->posts . ".ID) AS total FROM " . $wpdb->posts . "
2387
			" . $join . "
2388
			" . $where;
2389 9
		$rows = (int) $wpdb->get_var( $sql );
2390
	} else {
2391 9
		$orderby = geodir_widget_listings_get_order( $query_args );
2392 9
		/**
2393 9
		 * Filter widget listing orderby clause string part that is being used for query.
2394 4
		 *
2395
		 * @since 1.0.0
2396 9
		 *
2397 9
		 * @param string $orderby   Order by clause string.
2398
		 * @param string $table     Table name.
2399
		 * @param string $post_type Post type.
2400
		 */
2401
		$orderby = apply_filters( 'geodir_filter_widget_listings_orderby', $orderby, $table, $post_type );
2402
		$orderby .= $wpdb->posts . ".post_title ASC";
2403
		$orderby = $orderby != '' ? " ORDER BY " . $orderby : '';
2404
2405
		$limit = ! empty( $query_args['posts_per_page'] ) ? $query_args['posts_per_page'] : 5;
2406
		/**
2407
		 * Filter widget listing limit that is being used for query.
2408
		 *
2409 8
		 * @since 1.0.0
2410 8
		 *
2411 8
		 * @param int $limit        Query results limit.
2412
		 * @param string $post_type Post type.
2413
		 */
2414 8
		$limit = apply_filters( 'geodir_filter_widget_listings_limit', $limit, $post_type );
2415 8
2416
		$page = ! empty( $query_args['pageno'] ) ? absint( $query_args['pageno'] ) : 1;
2417
		if ( ! $page ) {
2418 8
			$page = 1;
2419 8
		}
2420
2421 8
		$limit = (int) $limit > 0 ? " LIMIT " . absint( ( $page - 1 ) * (int) $limit ) . ", " . (int) $limit : "";
2422
2423
		$sql  = "SELECT SQL_CALC_FOUND_ROWS " . $fields . " FROM " . $wpdb->posts . "
2424 8
			" . $join . "
2425
			" . $where . "
2426
			" . $groupby . "
2427
			" . $orderby . "
2428
			" . $limit;
2429
		$rows = $wpdb->get_results( $sql );
2430
	}
2431
2432
	unset( $GLOBALS['gd_query_args_widgets'] );
2433
	unset( $gd_query_args_widgets );
2434
2435
	return $rows;
2436
}
2437
2438
/**
2439
 * Listing query fields SQL part for widgets.
2440
 *
2441
 * @since   1.0.0
2442
 * @package GeoDirectory
2443
 * @global object $wpdb          WordPress Database object.
2444
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2445
 *
2446
 * @param string $fields         Fields SQL.
2447
 *
2448
 * @return string Modified fields SQL.
2449
 */
2450
function geodir_function_widget_listings_fields( $fields ) {
2451
	global $wpdb, $plugin_prefix, $gd_query_args_widgets;
2452
2453
	$query_args = $gd_query_args_widgets;
2454
	if ( empty( $query_args ) || empty( $query_args['is_geodir_loop'] ) ) {
2455
		return $fields;
2456
	}
2457
2458
	return $fields;
2459
}
2460
2461
/**
2462
 * Listing query join clause SQL part for widgets.
2463
 *
2464 7
 * @since   1.0.0
2465 7
 * @package GeoDirectory
2466 7
 * @global object $wpdb          WordPress Database object.
2467
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2468
 *
2469 7
 * @param string $join           Join clause SQL.
2470 1
 *
2471 1
 * @return string Modified join clause SQL.
2472
 */
2473 7
function geodir_function_widget_listings_join( $join ) {
2474
	global $wpdb, $plugin_prefix, $gd_query_args_widgets;
2475
2476
	$query_args = $gd_query_args_widgets;
2477
	if ( empty( $query_args ) || empty( $query_args['is_geodir_loop'] ) ) {
2478
		return $join;
2479
	}
2480
2481
	$post_type = empty( $query_args['post_type'] ) ? 'gd_place' : $query_args['post_type'];
2482
	$table     = $plugin_prefix . $post_type . '_detail';
2483
2484 View Code Duplication
	if ( ! empty( $query_args['with_pics_only'] ) ) {
2485
		$join .= " LEFT JOIN " . GEODIR_ATTACHMENT_TABLE . " ON ( " . GEODIR_ATTACHMENT_TABLE . ".post_id=" . $table . ".post_id AND " . GEODIR_ATTACHMENT_TABLE . ".mime_type LIKE '%image%' )";
2486
	}
2487
2488 View Code Duplication
	if ( ! empty( $query_args['tax_query'] ) ) {
2489
		$tax_queries = get_tax_sql( $query_args['tax_query'], $wpdb->posts, 'ID' );
2490
		if ( ! empty( $tax_queries['join'] ) && ! empty( $tax_queries['where'] ) ) {
2491
			$join .= $tax_queries['join'];
2492
		}
2493
	}
2494 3
2495 3
	return $join;
2496
}
2497 3
2498
/**
2499
 * Listing query where clause SQL part for widgets.
2500 3
 *
2501
 * @since   1.0.0
2502 3
 * @package GeoDirectory
2503
 * @global object $wpdb          WordPress Database object.
2504 3
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2505 3
 *
2506
 * @param string $where          Where clause SQL.
2507 3
 *
2508
 * @return string Modified where clause SQL.
2509
 */
2510 3
function geodir_function_widget_listings_where( $where ) {
2511 3
	global $wpdb, $plugin_prefix, $gd_query_args_widgets;
2512
2513
	$query_args = $gd_query_args_widgets;
2514 3
	if ( empty( $query_args ) || empty( $query_args['is_geodir_loop'] ) ) {
2515 3
		return $where;
2516
	}
2517
	$post_type = empty( $query_args['post_type'] ) ? 'gd_place' : $query_args['post_type'];
2518 3
	$table     = $plugin_prefix . $post_type . '_detail';
2519
2520
	if ( ! empty( $query_args ) ) {
2521 3
		if ( ! empty( $query_args['gd_location'] ) && function_exists( 'geodir_default_location_where' ) ) {
2522 3
			$where = geodir_default_location_where( $where, $table );
2523 3
		}
2524
2525 3
		if ( ! empty( $query_args['post_author'] ) ) {
2526 3
			$where .= " AND " . $wpdb->posts . ".post_author = " . (int) $query_args['post_author'];
2527 3
		}
2528 3
2529 3
		if ( ! empty( $query_args['show_featured_only'] ) ) {
2530
			$where .= " AND " . $table . ".is_featured = '1'";
2531 3
		}
2532 3
2533 3
		if ( ! empty( $query_args['show_special_only'] ) ) {
2534 3
			$where .= " AND ( " . $table . ".geodir_special_offers != '' AND " . $table . ".geodir_special_offers IS NOT NULL )";
2535
		}
2536 3
2537
		if ( ! empty( $query_args['with_pics_only'] ) ) {
2538 3
			$where .= " AND " . GEODIR_ATTACHMENT_TABLE . ".ID IS NOT NULL ";
2539 3
		}
2540
2541
		if ( ! empty( $query_args['featured_image_only'] ) ) {
2542
			$where .= " AND " . $table . ".featured_image IS NOT NULL AND " . $table . ".featured_image!='' ";
2543
		}
2544
2545
		if ( ! empty( $query_args['with_videos_only'] ) ) {
2546
			$where .= " AND ( " . $table . ".geodir_video != '' AND " . $table . ".geodir_video IS NOT NULL )";
2547
		}
2548
2549 3 View Code Duplication
		if ( ! empty( $query_args['tax_query'] ) ) {
2550 3
			$tax_queries = get_tax_sql( $query_args['tax_query'], $wpdb->posts, 'ID' );
2551 3
2552 3
			if ( ! empty( $tax_queries['join'] ) && ! empty( $tax_queries['where'] ) ) {
2553
				$where .= $tax_queries['where'];
2554
			}
2555
		}
2556
	}
2557 3
2558
	return $where;
2559 3
}
2560
2561 3
/**
2562
 * Listing query orderby clause SQL part for widgets.
2563 3
 *
2564
 * @since   1.0.0
2565 3
 * @package GeoDirectory
2566
 * @global object $wpdb          WordPress Database object.
2567
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2568
 *
2569 3
 * @param string $orderby        Orderby clause SQL.
2570 3
 *
2571 3
 * @return string Modified orderby clause SQL.
2572 3
 */
2573 3
function geodir_function_widget_listings_orderby( $orderby ) {
2574 3
	global $wpdb, $plugin_prefix, $gd_query_args_widgets;
2575 3
2576 3
	$query_args = $gd_query_args_widgets;
2577
	if ( empty( $query_args ) || empty( $query_args['is_geodir_loop'] ) ) {
2578 3
		return $orderby;
2579
	}
2580
2581
	return $orderby;
2582 3
}
2583 3
2584 3
/**
2585 3
 * Listing query limit for widgets.
2586
 *
2587
 * @since   1.0.0
2588
 * @package GeoDirectory
2589
 * @global object $wpdb          WordPress Database object.
2590
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2591
 *
2592
 * @param int $limit             Query limit.
2593
 *
2594
 * @return int Query limit.
2595
 */
2596
function geodir_function_widget_listings_limit( $limit ) {
2597
	global $wpdb, $plugin_prefix, $gd_query_args_widgets;
2598 3
2599 3
	$query_args = $gd_query_args_widgets;
2600
	if ( empty( $query_args ) || empty( $query_args['is_geodir_loop'] ) ) {
2601 3
		return $limit;
2602
	}
2603
2604 3
	if ( ! empty( $query_args ) && ! empty( $query_args['posts_per_page'] ) ) {
2605 3
		$limit = (int) $query_args['posts_per_page'];
2606 3
	}
2607
2608 3
	return $limit;
2609
}
2610 3
2611
/**
2612 3
 * WP media large width.
2613 3
 *
2614
 * @since   1.0.0
2615 3
 * @package GeoDirectory
2616
 *
2617
 * @param int $default         Default width.
2618
 * @param string|array $params Image parameters.
2619
 *
2620
 * @return int Large size width.
2621
 */
2622 View Code Duplication
function geodir_media_image_large_width( $default = 800, $params = '' ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2623
	$large_size_w = get_option( 'large_size_w' );
2624 3
	$large_size_w = $large_size_w > 0 ? $large_size_w : $default;
2625
	$large_size_w = absint( $large_size_w );
2626 3
2627 3
	if ( ! get_option( 'geodir_use_wp_media_large_size' ) ) {
2628 3
		$large_size_w = 800;
2629 3
	}
2630 3
2631
	/**
2632
	 * Filter large image width.
2633
	 *
2634
	 * @since 1.0.0
2635
	 *
2636
	 * @param int $large_size_w    Large image width.
2637
	 * @param int $default         Default width.
2638
	 * @param string|array $params Image parameters.
2639
	 */
2640
	$large_size_w = apply_filters( 'geodir_filter_media_image_large_width', $large_size_w, $default, $params );
2641
2642
	return $large_size_w;
2643
}
2644 2
2645
/**
2646 2
 * WP media large height.
2647
 *
2648
 * @since   1.0.0
2649 2
 * @package GeoDirectory
2650
 *
2651
 * @param int $default   Default height.
2652
 * @param string $params Image parameters.
2653
 *
2654
 * @return int Large size height.
2655
 */
2656 2 View Code Duplication
function geodir_media_image_large_height( $default = 800, $params = '' ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2657
	$large_size_h = get_option( 'large_size_h' );
2658
	$large_size_h = $large_size_h > 0 ? $large_size_h : $default;
2659
	$large_size_h = absint( $large_size_h );
2660
2661
	if ( ! get_option( 'geodir_use_wp_media_large_size' ) ) {
2662
		$large_size_h = 800;
2663 2
	}
2664
2665
	/**
2666
	 * Filter large image height.
2667
	 *
2668
	 * @since 1.0.0
2669
	 *
2670 2
	 * @param int $large_size_h    Large image height.
2671
	 * @param int $default         Default height.
2672
	 * @param string|array $params Image parameters.
2673
	 */
2674
	$large_size_h = apply_filters( 'geodir_filter_media_image_large_height', $large_size_h, $default, $params );
2675
2676
	return $large_size_h;
2677 2
}
2678
2679
/**
2680
 * Sanitize location name.
2681
 *
2682
 * @since   1.0.0
2683
 * @package GeoDirectory
2684 2
 *
2685
 * @param string $type    Location type. Can be gd_country, gd_region, gd_city.
2686
 * @param string $name    Location name.
2687
 * @param bool $translate Do you want to translate the name? Default: true.
2688
 *
2689
 * @return string Sanitized name.
2690
 */
2691 2
function geodir_sanitize_location_name( $type, $name, $translate = true ) {
2692
	if ( $name == '' ) {
2693
		return null;
2694
	}
2695
2696
	$type = $type == 'gd_country' ? 'country' : $type;
2697
	$type = $type == 'gd_region' ? 'region' : $type;
2698 2
	$type = $type == 'gd_city' ? 'city' : $type;
2699
2700
	$return = $name;
2701
	if ( function_exists( 'get_actual_location_name' ) ) {
2702
		$return = get_actual_location_name( $type, $name, $translate );
2703
	} else {
2704
		$return = preg_replace( '/-(\d+)$/', '', $return );
2705 2
		$return = preg_replace( '/[_-]/', ' ', $return );
2706
		$return = geodir_ucwords( $return );
2707
		$return = $translate ? __( $return, 'geodirectory' ) : $return;
2708
	}
2709
2710
	return $return;
2711
}
2712 2
2713
2714
/**
2715
 * Pluralize comment number.
2716
 *
2717
 * @since   1.0.0
2718
 * @package GeoDirectory
2719 2
 *
2720
 * @param int $number Comments number.
2721
 */
2722
function geodir_comments_number( $number ) {
2723
2724
	if ( $number > 1 ) {
2725
		$output = str_replace( '%', number_format_i18n( $number ), __( '% Reviews', 'geodirectory' ) );
2726 2
	} elseif ( $number == 0 || $number == '' ) {
2727
		$output = __( 'No Reviews', 'geodirectory' );
2728
	} else { // must be one
2729
		$output = __( '1 Review', 'geodirectory' );
2730
	}
2731
	echo $output;
2732
}
2733 2
2734
/**
2735
 * Checks whether the current page is geodirectory home page or not.
2736
 *
2737
 * @since   1.0.0
2738
 * @package GeoDirectory
2739
 * @global object $wpdb WordPress Database object.
2740 2
 * @return bool If current page is GD home page returns true, else false.
2741 2
 */
2742
function is_page_geodir_home() {
2743 2
	global $wpdb;
2744
	$cur_url = str_replace( array( "https://", "http://", "www." ), array( '', '', '' ), geodir_curPageURL() );
2745
	if ( function_exists( 'geodir_location_geo_home_link' ) ) {
2746
		remove_filter( 'home_url', 'geodir_location_geo_home_link', 100000 );
2747
	}
2748
	$home_url = home_url( '', 'http' );
2749
	if ( function_exists( 'geodir_location_geo_home_link' ) ) {
2750
		add_filter( 'home_url', 'geodir_location_geo_home_link', 100000, 2 );
2751
	}
2752
	$home_url = str_replace( "www.", "", $home_url );
2753
	if ( ( strpos( $home_url, $cur_url ) !== false || strpos( $home_url . '/', $cur_url ) !== false ) && ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && get_option( 'page_on_front' ) == get_option( 'geodir_home_page' ) ) ) {
2754
		return true;
2755
	} elseif ( get_query_var( 'page_id' ) == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && get_option( 'page_on_front' ) == get_option( 'geodir_home_page' ) ) {
2756
		return true;
2757
	} else {
2758
		return false;
2759
	}
2760
2761
}
2762
2763
2764
/**
2765
 * Returns homepage canonical url for SEO plugins.
2766
 *
2767
 * @since   1.0.0
2768
 * @package GeoDirectory
2769
 * @global object $post The current post object.
2770
 *
2771
 * @param string $url   The old url.
2772
 *
2773
 * @return string The canonical URL.
2774
 */
2775
function geodir_wpseo_homepage_canonical( $url ) {
2776
	global $post;
2777
2778
	if ( is_page_geodir_home() ) {
2779
		return home_url();
2780
	}
2781
2782
	return $url;
2783
}
2784
2785
add_filter( 'wpseo_canonical', 'geodir_wpseo_homepage_canonical', 10 );
2786 2
add_filter( 'aioseop_canonical_url', 'geodir_wpseo_homepage_canonical', 10 );
2787 2
2788 2
/**
2789
 * Add extra fields to google maps script call.
2790 2
 *
2791 2
 * @since   1.0.0
2792 1
 * @package GeoDirectory
2793 1
 * @global object $post The current post object.
2794
 *
2795 2
 * @param string $extra Old extra string.
2796 2
 *
2797
 * @return string Modified extra string.
2798 2
 */
2799 2
function geodir_googlemap_script_extra_details_page( $extra ) {
2800
	global $post;
2801 2
	$add_google_places_api = false;
2802
	if ( isset( $post->post_content ) && has_shortcode( $post->post_content, 'gd_add_listing' ) ) {
2803 2
		$add_google_places_api = true;
2804 2
	}
2805
	if ( ! str_replace( 'libraries=places', '', $extra ) && ( geodir_is_page( 'detail' ) || $add_google_places_api ) ) {
2806
		$extra .= "&amp;libraries=places";
2807 2
	}
2808
2809 2
	return $extra;
2810
}
2811
2812
add_filter( 'geodir_googlemap_script_extra', 'geodir_googlemap_script_extra_details_page', 101, 1 );
2813 2
2814 2
2815 1
/**
2816
 * Generates popular post category HTML.
2817
 *
2818
 * @since   1.0.0
2819 1
 * @since   1.5.1 Added option to set default post type.
2820
 * @package GeoDirectory
2821 1
 * @global object $wpdb                     WordPress Database object.
2822
 * @global string $plugin_prefix            Geodirectory plugin table prefix.
2823 1
 * @global string $geodir_post_category_str The geodirectory post category.
2824 1
 *
2825 1
 * @param array|string $args                Display arguments including before_title, after_title, before_widget, and
2826
 *                                          after_widget.
2827 1
 * @param array|string $instance            The settings for the particular instance of the widget.
2828 1
 */
2829 1
function geodir_popular_post_category_output( $args = '', $instance = '' ) {
2830 1
	// prints the widget
2831
	global $wpdb, $plugin_prefix, $geodir_post_category_str;
2832 1
	extract( $args, EXTR_SKIP );
2833 1
2834 1
	echo $before_widget;
2835 1
2836 1
	/** This filter is documented in geodirectory_widgets.php */
2837
	$title = empty( $instance['title'] ) ? __( 'Popular Categories', 'geodirectory' ) : apply_filters( 'widget_title', __( $instance['title'], 'geodirectory' ) );
2838
2839 1
	$gd_post_type = geodir_get_current_posttype();
2840
2841 1
	$category_limit = isset( $instance['category_limit'] ) && $instance['category_limit'] > 0 ? (int) $instance['category_limit'] : 15;
2842 1
	if ( ! empty( $gd_post_type ) ) {
2843
		$default_post_type = $gd_post_type;
2844
	} elseif ( isset( $instance['default_post_type'] ) && gdsc_is_post_type_valid( $instance['default_post_type'] ) ) {
2845
		$default_post_type = $instance['default_post_type'];
2846
	} else {
2847
		$all_gd_post_type  = geodir_get_posttypes();
2848
		$default_post_type = ( isset( $all_gd_post_type[0] ) ) ? $all_gd_post_type[0] : '';
2849
	}
2850
2851
	$taxonomy = array();
2852
	if ( ! empty( $gd_post_type ) ) {
2853
		$taxonomy[] = $gd_post_type . "category";
2854
	} else {
2855
		$taxonomy = geodir_get_taxonomies( $gd_post_type );
2856
	}
2857
2858
	$terms   = get_terms( $taxonomy );
2859 1
	$a_terms = array();
2860 1
	$b_terms = array();
2861 1
2862 1
	foreach ( $terms as $term ) {
2863 1
		if ( $term->count > 0 ) {
2864
			$a_terms[ $term->taxonomy ][] = $term;
2865
		}
2866
	}
2867
2868
	if ( ! empty( $a_terms ) ) {
2869
		foreach ( $a_terms as $b_key => $b_val ) {
2870
			$b_terms[ $b_key ] = geodir_sort_terms( $b_val, 'count' );
2871
		}
2872
2873
		$default_taxonomy = $default_post_type != '' && isset( $b_terms[ $default_post_type . 'category' ] ) ? $default_post_type . 'category' : '';
2874
2875
		$tax_change_output = '';
2876
		if ( count( $b_terms ) > 1 ) {
2877 1
			$tax_change_output .= "<select data-limit='$category_limit' class='geodir-cat-list-tax'  onchange='geodir_get_post_term(this);'>";
2878 1
			foreach ( $b_terms as $key => $val ) {
2879 1
				$ptype    = get_post_type_object( str_replace( "category", "", $key ) );
2880 2
				$cpt_name = __( $ptype->labels->singular_name, 'geodirectory' );
2881 2
				$tax_change_output .= "<option value='$key' " . selected( $key, $default_taxonomy, false ) . ">" . sprintf( __( '%s Categories', 'geodirectory' ), $cpt_name ) . "</option>";
2882
			}
2883
			$tax_change_output .= "</select>";
2884
		}
2885
2886
		if ( ! empty( $b_terms ) ) {
2887
			$terms = $default_taxonomy != '' && isset( $b_terms[ $default_taxonomy ] ) ? $b_terms[ $default_taxonomy ] : reset( $b_terms );// get the first array
2888
			global $cat_count;//make global so we can change via function
2889
			$cat_count = 0;
2890
			?>
2891
			<div class="geodir-category-list-in clearfix">
2892
				<div class="geodir-cat-list clearfix">
2893
					<?php
2894
					echo $before_title . __( $title ) . $after_title;
2895
2896
					echo $tax_change_output;
2897
2898 2
					echo '<ul class="geodir-popular-cat-list">';
2899
2900
					geodir_helper_cat_list_output( $terms, $category_limit );
2901 2
2902
					echo '</ul>';
2903 2
					?>
2904 2
				</div>
2905
				<?php
2906 2
				$hide = '';
2907 2
				if ( $cat_count < $category_limit ) {
2908
					$hide = 'style="display:none;"';
2909 2
				}
2910 2
				echo "<div class='geodir-cat-list-more' $hide >";
2911
				echo '<a href="javascript:void(0)" class="geodir-morecat geodir-showcat">' . __( 'More Categories', 'geodirectory' ) . '</a>';
2912 2
				echo '<a href="javascript:void(0)" class="geodir-morecat geodir-hidecat geodir-hide">' . __( 'Less Categories', 'geodirectory' ) . '</a>';
2913 2
				echo "</div>";
2914
				/* add scripts */
2915
				add_action( 'wp_footer', 'geodir_popular_category_add_scripts', 100 );
2916
				?>
2917
			</div>
2918 2
			<?php
2919 2
		}
2920 2
	}
2921
	echo $after_widget;
2922 2
}
2923 2
2924 2
/**
2925
 * Generates category list HTML.
2926 2
 *
2927
 * @since   1.0.0
2928 2
 * @package GeoDirectory
2929
 * @global string $geodir_post_category_str The geodirectory post category.
2930 2
 *
2931
 * @param array $terms                      An array of term objects.
2932 2
 * @param int $category_limit               Number of categories to display by default.
2933 2
 */
2934 2
function geodir_helper_cat_list_output( $terms, $category_limit ) {
2935
	global $geodir_post_category_str, $cat_count;
2936
	$term_icons = geodir_get_term_icon();
2937
2938
	$geodir_post_category_str = array();
2939
2940
2941
	foreach ( $terms as $cat ) {
2942
		$post_type     = str_replace( "category", "", $cat->taxonomy );
2943
		$term_icon_url = ! empty( $term_icons ) && isset( $term_icons[ $cat->term_id ] ) ? $term_icons[ $cat->term_id ] : '';
2944 2
2945
		$cat_count ++;
2946 2
2947
		$geodir_post_category_str[] = array( 'posttype' => $post_type, 'termid' => $cat->term_id );
2948 2
2949 2
		$class_row  = $cat_count > $category_limit ? 'geodir-pcat-hide geodir-hide' : 'geodir-pcat-show';
2950
		$total_post = $cat->count;
2951 2
2952
		$term_link = get_term_link( $cat, $cat->taxonomy );
2953
		/**
2954
		 * Filer the category term link.
2955
		 *
2956
		 * @since 1.4.5
2957
		 *
2958
		 * @param string $term_link The term permalink.
2959
		 * @param int $cat          ->term_id The term id.
2960
		 * @param string $post_type Wordpress post type.
2961
		 */
2962 2
		$term_link = apply_filters( 'geodir_category_term_link', $term_link, $cat->term_id, $post_type );
2963
2964 2
		echo '<li class="' . $class_row . '"><a href="' . $term_link . '">';
2965
		echo '<img alt="' . esc_attr( $cat->name ) . ' icon" style="height:20px;vertical-align:middle;" src="' . $term_icon_url . '"/> <span class="cat-link">';
2966 2
		echo $cat->name . '</span> <span class="geodir_term_class geodir_link_span geodir_category_class_' . $post_type . '_' . $cat->term_id . '">(' . $total_post . ')</span> ';
2967 2
		echo '</a></li>';
2968
	}
2969 2
}
2970
2971
/**
2972
 * Generates listing slider HTML.
2973
 *
2974
 * @since   1.0.0
2975
 * @package GeoDirectory
2976
 * @global object $post          The current post object.
2977
 *
2978
 * @param array|string $args     Display arguments including before_title, after_title, before_widget, and after_widget.
2979
 * @param array|string $instance The settings for the particular instance of the widget.
2980
 */
2981
function geodir_listing_slider_widget_output( $args = '', $instance = '' ) {
2982
	// prints the widget
2983
	extract( $args, EXTR_SKIP );
2984
2985
	echo $before_widget;
2986
2987
	/** This filter is documented in geodirectory_widgets.php */
2988
	$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', __( $instance['title'], 'geodirectory' ) );
2989
	/**
2990
	 * Filter the widget post type.
2991
	 *
2992
	 * @since 1.0.0
2993
	 *
2994
	 * @param string $instance ['post_type'] Post type of listing.
2995
	 */
2996
	$post_type = empty( $instance['post_type'] ) ? 'gd_place' : apply_filters( 'widget_post_type', $instance['post_type'] );
2997
	/**
2998
	 * Filter the widget's term.
2999
	 *
3000
	 * @since 1.0.0
3001
	 *
3002
	 * @param string $instance ['category'] Filter by term. Can be any valid term.
3003
	 */
3004
	$category = empty( $instance['category'] ) ? '0' : apply_filters( 'widget_category', $instance['category'] );
3005
	/**
3006
	 * Filter the widget listings limit.
3007
	 *
3008
	 * @since 1.0.0
3009
	 *
3010
	 * @param string $instance ['post_number'] Number of listings to display.
3011 2
	 */
3012 2
	$post_number = empty( $instance['post_number'] ) ? '5' : apply_filters( 'widget_post_number', $instance['post_number'] );
3013
	/**
3014 2
	 * Filter the widget listings limit shown at one time.
3015 2
	 *
3016
	 * @since 1.5.0
3017 2
	 *
3018 2
	 * @param string $instance ['max_show'] Number of listings to display on screen.
3019 2
	 */
3020 2
	$max_show = empty( $instance['max_show'] ) ? '1' : apply_filters( 'widget_max_show', $instance['max_show'] );
3021
	/**
3022 2
	 * Filter the widget slide width.
3023 2
	 *
3024 1
	 * @since 1.5.0
3025 1
	 *
3026
	 * @param string $instance ['slide_width'] Width of the slides shown.
3027
	 */
3028
	$slide_width = empty( $instance['slide_width'] ) ? '' : apply_filters( 'widget_slide_width', $instance['slide_width'] );
3029
	/**
3030
	 * Filter widget's "show title" value.
3031
	 *
3032
	 * @since 1.0.0
3033
	 *
3034
	 * @param string|bool $instance ['show_title'] Do you want to display title? Can be 1 or 0.
3035 2
	 */
3036
	$show_title = empty( $instance['show_title'] ) ? '' : apply_filters( 'widget_show_title', $instance['show_title'] );
3037 2
	/**
3038 2
	 * Filter widget's "slideshow" value.
3039 2
	 *
3040
	 * @since 1.0.0
3041 2
	 *
3042
	 * @param int $instance ['slideshow'] Setup a slideshow for the slider to animate automatically.
3043
	 */
3044
	$slideshow = empty( $instance['slideshow'] ) ? 0 : apply_filters( 'widget_slideshow', $instance['slideshow'] );
3045
	/**
3046
	 * Filter widget's "animationLoop" value.
3047
	 *
3048
	 * @since 1.0.0
3049
	 *
3050
	 * @param int $instance ['animationLoop'] Gives the slider a seamless infinite loop.
3051
	 */
3052 2
	$animationLoop = empty( $instance['animationLoop'] ) ? 0 : apply_filters( 'widget_animationLoop', $instance['animationLoop'] );
3053 2
	/**
3054
	 * Filter widget's "directionNav" value.
3055 2
	 *
3056
	 * @since 1.0.0
3057
	 *
3058
	 * @param int $instance ['directionNav'] Enable previous/next arrow navigation?. Can be 1 or 0.
3059
	 */
3060
	$directionNav = empty( $instance['directionNav'] ) ? 0 : apply_filters( 'widget_directionNav', $instance['directionNav'] );
3061
	/**
3062 2
	 * Filter widget's "slideshowSpeed" value.
3063 2
	 *
3064 2
	 * @since 1.0.0
3065
	 *
3066
	 * @param int $instance ['slideshowSpeed'] Set the speed of the slideshow cycling, in milliseconds.
3067
	 */
3068
	$slideshowSpeed = empty( $instance['slideshowSpeed'] ) ? 5000 : apply_filters( 'widget_slideshowSpeed', $instance['slideshowSpeed'] );
3069
	/**
3070
	 * Filter widget's "animationSpeed" value.
3071
	 *
3072
	 * @since 1.0.0
3073
	 *
3074
	 * @param int $instance ['animationSpeed'] Set the speed of animations, in milliseconds.
3075
	 */
3076
	$animationSpeed = empty( $instance['animationSpeed'] ) ? 600 : apply_filters( 'widget_animationSpeed', $instance['animationSpeed'] );
3077
	/**
3078
	 * Filter widget's "animation" value.
3079
	 *
3080
	 * @since 1.0.0
3081
	 *
3082
	 * @param string $instance ['animation'] Controls the animation type, "fade" or "slide".
3083
	 */
3084
	$animation = empty( $instance['animation'] ) ? 'slide' : apply_filters( 'widget_animation', $instance['animation'] );
3085
	/**
3086
	 * Filter widget's "list_sort" type.
3087
	 *
3088
	 * @since 1.0.0
3089
	 *
3090
	 * @param string $instance ['list_sort'] Listing sort by type.
3091
	 */
3092
	$list_sort          = empty( $instance['list_sort'] ) ? 'latest' : apply_filters( 'widget_list_sort', $instance['list_sort'] );
3093
	$show_featured_only = ! empty( $instance['show_featured_only'] ) ? 1 : null;
3094
3095
	wp_enqueue_script( 'geodirectory-jquery-flexslider-js' );
3096
	?>
3097
	<script type="text/javascript">
3098
		jQuery(window).load(function () {
3099
			jQuery('#geodir_widget_carousel').flexslider({
3100
				animation: "slide",
3101
				selector: ".geodir-slides > li",
3102
				namespace: "geodir-",
3103
				controlNav: false,
3104
				directionNav: false,
3105
				animationLoop: false,
3106
				slideshow: false,
3107
				itemWidth: 75,
3108
				itemMargin: 5,
3109
				asNavFor: '#geodir_widget_slider',
3110
				rtl: <?php echo( is_rtl() ? 'true' : 'false' ); /* fix rtl issue */ ?>
3111
			});
3112
3113 2
			jQuery('#geodir_widget_slider').flexslider({
3114 2
				animation: "<?php echo $animation;?>",
3115
				selector: ".geodir-slides > li",
3116
				namespace: "geodir-",
3117
				controlNav: true,
3118
				animationLoop: <?php echo $animationLoop;?>,
3119
				slideshow: <?php echo $slideshow;?>,
3120
				slideshowSpeed: <?php echo $slideshowSpeed;?>,
3121
				animationSpeed: <?php echo $animationSpeed;?>,
3122
				directionNav: <?php echo $directionNav;?>,
3123
				maxItems: <?php echo $max_show;?>,
3124
				move: 1,
3125
				<?php if ( $slide_width ) {
3126
				echo "itemWidth: " . $slide_width . ",";
3127
			}?>
3128
				sync: "#geodir_widget_carousel",
3129
				start: function (slider) {
3130
					jQuery('.geodir-listing-flex-loader').hide();
3131
					jQuery('#geodir_widget_slider').css({'visibility': 'visible'});
3132 2
					jQuery('#geodir_widget_carousel').css({'visibility': 'visible'});
3133
				},
3134
				rtl: <?php echo( is_rtl() ? 'true' : 'false' ); /* fix rtl issue */ ?>
3135 2
			});
3136
		});
3137 2
	</script>
3138
	<?php
3139
	$query_args = array(
3140 2
		'posts_per_page' => $post_number,
3141
		'is_geodir_loop' => true,
3142
		'post_type'      => $post_type,
3143
		'order_by'       => $list_sort
3144
	);
3145
	if ( $show_featured_only ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $show_featured_only of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
3146
		$query_args['show_featured_only'] = 1;
3147 2
	}
3148
3149
	if ( $category != 0 || $category != '' ) {
3150
		$category_taxonomy = geodir_get_taxonomies( $post_type );
3151
		$tax_query         = array(
3152
			'taxonomy' => $category_taxonomy[0],
3153
			'field'    => 'id',
3154 2
			'terms'    => $category
3155
		);
3156
3157
		$query_args['tax_query'] = array( $tax_query );
3158
	}
3159
3160
	// we want listings with featured image only
3161 2
	$query_args['featured_image_only'] = 1;
3162
3163
	if ( $post_type == 'gd_event' ) {
3164
		$query_args['gedir_event_listing_filter'] = 'upcoming';
3165
	}// show only upcoming events
3166
3167
	$widget_listings = geodir_get_widget_listings( $query_args );
3168 2
	if ( ! empty( $widget_listings ) || ( isset( $with_no_results ) && $with_no_results ) ) {
3169
		if ( $title ) {
3170
			echo $before_title . $title . $after_title;
3171
		}
3172
3173
		global $post;
3174
3175 2
		$current_post = $post;// keep current post info
3176
3177
		$widget_main_slides = '';
3178
		$nav_slides         = '';
3179
		$widget_slides      = 0;
3180
3181
		foreach ( $widget_listings as $widget_listing ) {
3182 2
			global $gd_widget_listing_type;
3183
			$post         = $widget_listing;
3184
			$widget_image = geodir_get_featured_image( $post->ID, 'thumbnail', get_option( 'geodir_listing_no_img' ) );
3185
3186
			if ( ! empty( $widget_image ) ) {
3187
				if ( $widget_image->height >= 200 ) {
3188
					$widget_spacer_height = 0;
3189 2
				} else {
3190 2
					$widget_spacer_height = ( ( 200 - $widget_image->height ) / 2 );
3191
				}
3192
3193 2
				$widget_main_slides .= '<li class="geodir-listing-slider-widget"><img class="geodir-listing-slider-spacer" src="' . geodir_plugin_url() . "/geodirectory-assets/images/spacer.gif" . '" alt="' . $widget_image->title . '" title="' . $widget_image->title . '" style="max-height:' . $widget_spacer_height . 'px !important;margin:0 auto;" width="100" />';
3194 1
3195 1
				$title = '';
3196
				if ( $show_title ) {
3197
					$title_html     = '<div class="geodir-slider-title"><a href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a></div>';
3198
					$post_id        = $post->ID;
3199 1
					$post_permalink = get_permalink( $post->ID );
3200
					$post_title     = get_the_title( $post->ID );
3201 2
					/**
3202 2
					 * Filter the listing slider widget title.
3203
					 *
3204 2
					 * @since 1.6.1
3205 2
					 *
3206
					 * @param string $title_html     The html output of the title.
3207 2
					 * @param int $post_id           The post id.
3208
					 * @param string $post_permalink The post permalink url.
3209
					 * @param string $post_title     The post title text.
3210
					 */
3211
					$title = apply_filters( 'geodir_listing_slider_title', $title_html, $post_id, $post_permalink, $post_title );
3212
				}
3213
3214 1
				$widget_main_slides .= $title . '<img src="' . $widget_image->src . '" alt="' . $widget_image->title . '" title="' . $widget_image->title . '" style="max-height:200px;margin:0 auto;" /></li>';
3215 1
				$nav_slides .= '<li><img src="' . $widget_image->src . '" alt="' . $widget_image->title . '" title="' . $widget_image->title . '" style="max-height:48px;margin:0 auto;" /></li>';
3216 1
				$widget_slides ++;
3217
			}
3218
		}
3219 2
		?>
3220 2
		<div class="flex-container" style="min-height:200px;">
3221 2
			<div class="geodir-listing-flex-loader"><i class="fa fa-refresh fa-spin"></i></div>
3222
			<div id="geodir_widget_slider" class="geodir_flexslider">
3223 2
				<ul class="geodir-slides clearfix"><?php echo $widget_main_slides; ?></ul>
3224 2
			</div>
3225 2
			<?php if ( $widget_slides > 1 ) { ?>
3226
				<div id="geodir_widget_carousel" class="geodir_flexslider">
3227
					<ul class="geodir-slides clearfix"><?php echo $nav_slides; ?></ul>
3228
				</div>
3229
			<?php } ?>
3230
		</div>
3231
		<?php
3232
		$GLOBALS['post'] = $current_post;
3233
		setup_postdata( $current_post );
3234
	}
3235
	echo $after_widget;
3236
}
3237
3238
3239
/**
3240
 * Generates login box HTML.
3241
 *
3242
 * @since   1.0.0
3243
 * @package GeoDirectory
3244
 * @global object $current_user  Current user object.
3245
 *
3246
 * @param array|string $args     Display arguments including before_title, after_title, before_widget, and after_widget.
3247
 * @param array|string $instance The settings for the particular instance of the widget.
3248
 */
3249
function geodir_loginwidget_output( $args = '', $instance = '' ) {
3250
	//print_r($args);
3251
	//print_r($instance);
3252 2
	// prints the widget
3253 2
	extract( $args, EXTR_SKIP );
3254 2
3255
	/** This filter is documented in geodirectory_widgets.php */
3256
	$title = empty( $instance['title'] ) ? __( 'My Dashboard', 'geodirectory' ) : apply_filters( 'widget_title', __( $instance['title'], 'geodirectory' ) );
3257
3258
	echo $before_widget;
3259 2
	echo $before_title . $title . $after_title;
3260
3261
	if ( is_user_logged_in() ) {
3262 2
		global $current_user;
3263
3264
		$author_link = get_author_posts_url( $current_user->data->ID );
3265 2
		$author_link = geodir_getlink( $author_link, array( 'geodir_dashbord' => 'true' ), false );
3266
3267
		echo '<ul class="geodir-loginbox-list">';
3268
		ob_start();
3269
		?>
3270
		<li><a class="signin"
3271
		       href="<?php echo wp_logout_url( home_url() ); ?>"><?php _e( 'Logout', 'geodirectory' ); ?></a></li>
3272
		<?php
3273
		$post_types                           = geodir_get_posttypes( 'object' );
3274
		$show_add_listing_post_types_main_nav = get_option( 'geodir_add_listing_link_user_dashboard' );
3275
		$geodir_allow_posttype_frontend       = get_option( 'geodir_allow_posttype_frontend' );
0 ignored issues
show
Unused Code introduced by
$geodir_allow_posttype_frontend 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...
3276
3277
		if ( ! empty( $show_add_listing_post_types_main_nav ) ) {
3278 2
			$addlisting_links = '';
3279
			foreach ( $post_types as $key => $postobj ) {
0 ignored issues
show
Bug introduced by
The expression $post_types of type array|object|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...
3280
3281
				if ( in_array( $key, $show_add_listing_post_types_main_nav ) ) {
3282 2
3283
					if ( $add_link = geodir_get_addlisting_link( $key ) ) {
3284
3285 2
						$name = $postobj->labels->name;
3286 2
3287 2
						$selected = '';
3288 2
						if ( geodir_get_current_posttype() == $key && geodir_is_page( 'add-listing' ) ) {
3289
							$selected = 'selected="selected"';
3290 2
						}
3291
3292 2
						/**
3293 1
						 * Filter add listing link.
3294 1
						 *
3295
						 * @since 1.0.0
3296 2
						 *
3297 1
						 * @param string $add_link  Add listing link.
3298 1
						 * @param string $key       Add listing array key.
3299
						 * @param int $current_user ->ID Current user ID.
3300 2
						 */
3301
						$add_link = apply_filters( 'geodir_dashboard_link_add_listing', $add_link, $key, $current_user->ID );
3302
3303
						$addlisting_links .= '<option ' . $selected . ' value="' . $add_link . '">' . __( ucfirst( $name ), 'geodirectory' ) . '</option>';
3304 2
3305
					}
3306
				}
3307
3308
			}
3309 2
3310 View Code Duplication
			if ( $addlisting_links != '' ) { ?>
3311
3312 2
				<li><select id="geodir_add_listing" class="chosen_select" onchange="window.location.href=this.value"
3313
				            option-autoredirect="1" name="geodir_add_listing" option-ajaxchosen="false"
3314 2
				            data-placeholder="<?php echo esc_attr( __( 'Add Listing', 'geodirectory' ) ); ?>">
3315
						<option value="" disabled="disabled" selected="selected"
3316
						        style='display:none;'><?php echo esc_attr( __( 'Add Listing', 'geodirectory' ) ); ?></option>
3317
						<?php echo $addlisting_links; ?>
3318
					</select></li> <?php
3319
3320
			}
3321
3322
		}
3323
		// My Favourites in Dashboard
3324
		$show_favorite_link_user_dashboard = get_option( 'geodir_favorite_link_user_dashboard' );
3325
		$user_favourite                    = geodir_user_favourite_listing_count();
3326
3327
		if ( ! empty( $show_favorite_link_user_dashboard ) && ! empty( $user_favourite ) ) {
3328
			$favourite_links = '';
3329
3330
			foreach ( $post_types as $key => $postobj ) {
0 ignored issues
show
Bug introduced by
The expression $post_types of type array|object|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...
3331
				if ( in_array( $key, $show_favorite_link_user_dashboard ) && array_key_exists( $key, $user_favourite ) ) {
3332 2
					$name           = $postobj->labels->name;
3333
					$post_type_link = geodir_getlink( $author_link, array(
3334 2
						'stype' => $key,
3335
						'list'  => 'favourite'
3336 2
					), false );
3337
3338
					$selected = '';
3339
3340 View Code Duplication
					if ( isset( $_REQUEST['list'] ) && $_REQUEST['list'] == 'favourite' && isset( $_REQUEST['stype'] ) && $_REQUEST['stype'] == $key && isset( $_REQUEST['geodir_dashbord'] ) ) {
3341
						$selected = 'selected="selected"';
3342
					}
3343
					/**
3344
					 * Filter favorite listing link.
3345
					 *
3346
					 * @since 1.0.0
3347
					 *
3348
					 * @param string $post_type_link Favorite listing link.
3349
					 * @param string $key            Favorite listing array key.
3350
					 * @param int $current_user      ->ID Current user ID.
3351
					 */
3352
					$post_type_link = apply_filters( 'geodir_dashboard_link_favorite_listing', $post_type_link, $key, $current_user->ID );
3353
3354
					$favourite_links .= '<option ' . $selected . ' value="' . $post_type_link . '">' . __( ucfirst( $name ), 'geodirectory' ) . '</option>';
3355
				}
3356
			}
3357
3358 View Code Duplication
			if ( $favourite_links != '' ) {
3359
				?>
3360 2
				<li>
3361 2
					<select id="geodir_my_favourites" class="chosen_select" onchange="window.location.href=this.value"
3362 2
					        option-autoredirect="1" name="geodir_my_favourites" option-ajaxchosen="false"
3363 2
					        data-placeholder="<?php echo esc_attr( __( 'My Favorites', 'geodirectory' ) ); ?>">
3364 2
						<option value="" disabled="disabled" selected="selected"
3365
						        style='display:none;'><?php echo esc_attr( __( 'My Favorites', 'geodirectory' ) ); ?></option>
3366
						<?php echo $favourite_links; ?>
3367
					</select>
3368
				</li>
3369
				<?php
3370
			}
3371
		}
3372
3373 2
3374 2
		$show_listing_link_user_dashboard = get_option( 'geodir_listing_link_user_dashboard' );
3375
		$user_listing                     = geodir_user_post_listing_count();
3376
3377
		if ( ! empty( $show_listing_link_user_dashboard ) && ! empty( $user_listing ) ) {
3378
			$listing_links = '';
3379
3380
			foreach ( $post_types as $key => $postobj ) {
0 ignored issues
show
Bug introduced by
The expression $post_types of type array|object|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...
3381
				if ( in_array( $key, $show_listing_link_user_dashboard ) && array_key_exists( $key, $user_listing ) ) {
3382
					$name         = $postobj->labels->name;
3383
					$listing_link = geodir_getlink( $author_link, array( 'stype' => $key ), false );
3384 2
3385
					$selected = '';
3386 2 View Code Duplication
					if ( ! isset( $_REQUEST['list'] ) && isset( $_REQUEST['geodir_dashbord'] ) && isset( $_REQUEST['stype'] ) && $_REQUEST['stype'] == $key ) {
3387 2
						$selected = 'selected="selected"';
3388 2
					}
3389 2
3390
					/**
3391
					 * Filter my listing link.
3392
					 *
3393
					 * @since 1.0.0
3394
					 *
3395
					 * @param string $listing_link My listing link.
3396 2
					 * @param string $key          My listing array key.
3397
					 * @param int $current_user    ->ID Current user ID.
3398 2
					 */
3399
					$listing_link = apply_filters( 'geodir_dashboard_link_my_listing', $listing_link, $key, $current_user->ID );
3400 2
3401 2
					$listing_links .= '<option ' . $selected . ' value="' . $listing_link . '">' . __( ucfirst( $name ), 'geodirectory' ) . '</option>';
3402 2
				}
3403 2
			}
3404 2
3405 View Code Duplication
			if ( $listing_links != '' ) {
3406
				?>
3407
				<li>
3408 2
					<select id="geodir_my_listings" class="chosen_select" onchange="window.location.href=this.value"
3409 2
					        option-autoredirect="1" name="geodir_my_listings" option-ajaxchosen="false"
3410
					        data-placeholder="<?php echo esc_attr( __( 'My Listings', 'geodirectory' ) ); ?>">
3411 2
						<option value="" disabled="disabled" selected="selected"
3412
						        style='display:none;'><?php echo esc_attr( __( 'My Listings', 'geodirectory' ) ); ?></option>
3413
						<?php echo $listing_links; ?>
3414
					</select>
3415
				</li>
3416
				<?php
3417
			}
3418
		}
3419
3420
		$dashboard_link = ob_get_clean();
3421
		/**
3422
		 * Filter dashboard links HTML.
3423
		 *
3424
		 * @since 1.0.0
3425
		 *
3426
		 * @param string $dashboard_link Dashboard links HTML.
3427
		 */
3428
		echo apply_filters( 'geodir_dashboard_links', $dashboard_link );
3429
		echo '</ul>';
3430
3431
		/**
3432 2
		 * Called after the loginwidget form for logged in users.
3433
		 *
3434 2
		 * @since 1.6.6
3435
		 */
3436 2
		do_action( 'geodir_after_loginwidget_form_logged_in' );
3437
3438
3439
	} else {
3440
		?>
3441
		<?php
3442
		/**
3443
		 * Filter signup form action link.
3444
		 *
3445
		 * @since 1.0.0
3446
		 */
3447 2
		?>
3448
		<form name="loginform" class="loginform1"
3449 2
		      action="<?php echo geodir_login_url(); ?>"
3450
		      method="post">
3451 2
			<div class="geodir_form_row"><input placeholder="<?php _e( 'Email', 'geodirectory' ); ?>" name="log"
3452
			                                    type="text" class="textfield user_login1"/> <span
3453
					class="user_loginInfo"></span></div>
3454
			<div class="geodir_form_row"><input placeholder="<?php _e( 'Password', 'geodirectory' ); ?>"
3455
			                                    name="pwd" type="password"
3456
			                                    class="textfield user_pass1 input-text"/><span
3457
					class="user_passInfo"></span></div>
3458
3459
			<input type="hidden" name="redirect_to" value="<?php echo htmlspecialchars( geodir_curPageURL() ); ?>"/>
3460
			<input type="hidden" name="testcookie" value="1"/>
3461
3462
				<?php do_action( 'login_form' ); ?>
3463
3464
			<div class="geodir_form_row clearfix"><input type="submit" name="submit"
3465
			                                             value="<?php echo SIGN_IN_BUTTON; ?>" class="b_signin"/>
3466
3467
				<p class="geodir-new-forgot-link">
3468
					<?php
3469
					/**
3470
					 * Filter signup page register form link.
3471
					 *
3472
					 * @since 1.0.0
3473
					 */
3474
					?>
3475 8
					<a href="<?php echo geodir_login_url( array( 'signup' => true ) ); ?>"
3476 8
					   class="goedir-newuser-link"><?php echo NEW_USER_TEXT; ?></a>
3477
3478
					<?php
3479
					/**
3480 8
					 * Filter signup page forgot password form link.
3481
					 *
3482 8
					 * @since 1.0.0
3483 7
					 */
3484 7
					?>
3485 7
					<a href="<?php echo geodir_login_url( array( 'forgot' => true ) ); ?>"
3486 7
					   class="goedir-forgot-link"><?php echo FORGOT_PW_TEXT; ?></a></p></div>
3487 7
		</form>
3488 7
		<?php
3489
		/**
3490 7
		 * Called after the loginwidget form for logged out users.
3491 2
		 *
3492 2
		 * @since 1.6.6
3493 2
		 */
3494 2
		do_action( 'geodir_after_loginwidget_form_logged_out' );
3495 2
	}
3496 2
3497
	echo $after_widget;
3498 7
}
3499
3500 7
3501 7
/**
3502
 * Generates popular postview HTML.
3503 7
 *
3504
 * @since   1.0.0
3505
 * @since   1.5.1 View all link fixed for location filter disabled.
3506
 * @package GeoDirectory
3507 7
 * @global object $post                    The current post object.
3508
 * @global string $gridview_columns_widget The girdview style of the listings for widget.
3509
 * @global bool $geodir_is_widget_listing  Is this a widget listing?. Default: false.
3510
 * @global object $gd_session              GeoDirectory Session object.
3511
 *
3512
 * @param array|string $args               Display arguments including before_title, after_title, before_widget, and
3513
 *                                         after_widget.
3514
 * @param array|string $instance           The settings for the particular instance of the widget.
3515
 */
3516 7
function geodir_popular_postview_output( $args = '', $instance = '' ) {
3517
	global $gd_session;
3518
3519
	// prints the widget
3520
	extract( $args, EXTR_SKIP );
3521
3522
	echo $before_widget;
3523
3524
	/** This filter is documented in geodirectory_widgets.php */
3525
	$title = empty( $instance['title'] ) ? geodir_ucwords( $instance['category_title'] ) : apply_filters( 'widget_title', __( $instance['title'], 'geodirectory' ) );
3526
	/**
3527
	 * Filter the widget post type.
3528
	 *
3529
	 * @since 1.0.0
3530
	 *
3531
	 * @param string $instance ['post_type'] Post type of listing.
3532
	 */
3533
	$post_type = empty( $instance['post_type'] ) ? 'gd_place' : apply_filters( 'widget_post_type', $instance['post_type'] );
3534
	/**
3535
	 * Filter the widget's term.
3536
	 *
3537
	 * @since 1.0.0
3538
	 *
3539
	 * @param string $instance ['category'] Filter by term. Can be any valid term.
3540
	 */
3541
	$category = empty( $instance['category'] ) ? '0' : apply_filters( 'widget_category', $instance['category'] );
3542
	/**
3543
	 * Filter the widget listings limit.
3544 7
	 *
3545
	 * @since 1.0.0
3546 7
	 *
3547 7
	 * @param string $instance ['post_number'] Number of listings to display.
3548
	 */
3549 1
	$post_number = empty( $instance['post_number'] ) ? '5' : apply_filters( 'widget_post_number', $instance['post_number'] );
3550
	/**
3551
	 * Filter widget's "layout" type.
3552
	 *
3553
	 * @since 1.0.0
3554
	 *
3555
	 * @param string $instance ['layout'] Widget layout type.
3556
	 */
3557
	$layout = empty( $instance['layout'] ) ? 'gridview_onehalf' : apply_filters( 'widget_layout', $instance['layout'] );
3558
	/**
3559
	 * Filter widget's "add_location_filter" value.
3560
	 *
3561
	 * @since 1.0.0
3562 8
	 *
3563
	 * @param string|bool $instance ['add_location_filter'] Do you want to add location filter? Can be 1 or 0.
3564
	 */
3565
	$add_location_filter = empty( $instance['add_location_filter'] ) ? '0' : apply_filters( 'widget_add_location_filter', $instance['add_location_filter'] );
3566 8
	/**
3567
	 * Filter widget's listing width.
3568
	 *
3569
	 * @since 1.0.0
3570 8
	 *
3571 8
	 * @param string $instance ['listing_width'] Listing width.
3572 8
	 */
3573 2
	$listing_width = empty( $instance['listing_width'] ) ? '' : apply_filters( 'widget_listing_width', $instance['listing_width'] );
3574
	/**
3575
	 * Filter widget's "list_sort" type.
3576 6
	 *
3577
	 * @since 1.0.0
3578
	 *
3579
	 * @param string $instance ['list_sort'] Listing sort by type.
3580 6
	 */
3581 6
	$list_sort             = empty( $instance['list_sort'] ) ? 'latest' : apply_filters( 'widget_list_sort', $instance['list_sort'] );
3582 6
	$use_viewing_post_type = ! empty( $instance['use_viewing_post_type'] ) ? true : false;
3583 6
3584
	// set post type to current viewing post type
3585 6 View Code Duplication
	if ( $use_viewing_post_type ) {
3586 5
		$current_post_type = geodir_get_current_posttype();
3587 5
		if ( $current_post_type != '' && $current_post_type != $post_type ) {
3588
			$post_type = $current_post_type;
3589 6
			$category  = array(); // old post type category will not work for current changed post type
3590
		}
3591
	}
3592
	// replace widget title dynamically
3593 4
	$posttype_plural_label   = __( get_post_type_plural_label( $post_type ), 'geodirectory' );
3594 4
	$posttype_singular_label = __( get_post_type_singular_label( $post_type ), 'geodirectory' );
3595
3596
	$title = str_replace( "%posttype_plural_label%", $posttype_plural_label, $title );
3597
	$title = str_replace( "%posttype_singular_label%", $posttype_singular_label, $title );
3598
3599 View Code Duplication
	if ( isset( $instance['character_count'] ) ) {
3600
		/**
3601
		 * Filter the widget's excerpt character count.
3602
		 *
3603
		 * @since 1.0.0
3604
		 *
3605
		 * @param int $instance ['character_count'] Excerpt character count.
3606
		 */
3607
		$character_count = apply_filters( 'widget_list_character_count', $instance['character_count'] );
3608
	} else {
3609
		$character_count = '';
3610
	}
3611
3612
	if ( empty( $title ) || $title == 'All' ) {
3613
		$title .= ' ' . __( get_post_type_plural_label( $post_type ), 'geodirectory' );
3614
	}
3615
3616
	$location_url = array();
3617
	$city         = get_query_var( 'gd_city' );
3618
	if ( ! empty( $city ) ) {
3619
		$country = get_query_var( 'gd_country' );
3620
		$region  = get_query_var( 'gd_region' );
3621
3622
		$geodir_show_location_url = get_option( 'geodir_show_location_url' );
3623
3624
		if ( $geodir_show_location_url == 'all' ) {
3625
			if ( $country != '' ) {
3626
				$location_url[] = $country;
3627
			}
3628
3629
			if ( $region != '' ) {
3630
				$location_url[] = $region;
3631
			}
3632 4
		} else if ( $geodir_show_location_url == 'country_city' ) {
3633 4
			if ( $country != '' ) {
3634
				$location_url[] = $country;
3635
			}
3636
		} else if ( $geodir_show_location_url == 'region_city' ) {
3637
			if ( $region != '' ) {
3638
				$location_url[] = $region;
3639
			}
3640
		}
3641
3642
		$location_url[] = $city;
3643
	}
3644
3645
	$location_url  = implode( '/', $location_url );
3646 1
	$skip_location = false;
3647 1
	if ( ! $add_location_filter && $gd_session->get( 'gd_multi_location' ) ) {
3648
		$skip_location = true;
3649
		$gd_session->un_set( 'gd_multi_location' );
3650
	}
3651
3652
	if ( get_option( 'permalink_structure' ) ) {
3653
		$viewall_url = get_post_type_archive_link( $post_type );
3654
	} else {
3655
		$viewall_url = get_post_type_archive_link( $post_type );
3656
	}
3657
3658
	if ( ! empty( $category ) && $category[0] != '0' ) {
3659
		global $geodir_add_location_url;
3660
3661 5
		$geodir_add_location_url = '0';
3662 4
3663
		if ( $add_location_filter != '0' ) {
3664 1
			$geodir_add_location_url = '1';
3665 1
		}
3666
3667
		$viewall_url = get_term_link( (int) $category[0], $post_type . 'category' );
3668
3669
		$geodir_add_location_url = null;
3670
	}
3671
	if ( $skip_location ) {
3672
		$gd_session->set( 'gd_multi_location', 1 );
3673
	}
3674
3675
	if ( is_wp_error( $viewall_url ) ) {
3676
		$viewall_url = '';
3677
	}
3678
3679
	$query_args = array(
3680
		'posts_per_page' => $post_number,
3681
		'is_geodir_loop' => true,
3682
		'gd_location'    => $add_location_filter ? true : false,
3683
		'post_type'      => $post_type,
3684
		'order_by'       => $list_sort
3685
	);
3686
3687
	if ( $character_count ) {
3688
		$query_args['excerpt_length'] = $character_count;
3689
	}
3690
3691
	if ( ! empty( $instance['show_featured_only'] ) ) {
3692
		$query_args['show_featured_only'] = 1;
3693
	}
3694
3695
	if ( ! empty( $instance['show_special_only'] ) ) {
3696
		$query_args['show_special_only'] = 1;
3697 4
	}
3698
3699 View Code Duplication
	if ( ! empty( $instance['with_pics_only'] ) ) {
3700
		$query_args['with_pics_only']      = 0;
3701
		$query_args['featured_image_only'] = 1;
3702
	}
3703
3704
	if ( ! empty( $instance['with_videos_only'] ) ) {
3705
		$query_args['with_videos_only'] = 1;
3706
	}
3707
	$with_no_results = ! empty( $instance['without_no_results'] ) ? false : true;
3708
3709 View Code Duplication
	if ( ! empty( $category ) && $category[0] != '0' ) {
3710
		$category_taxonomy = geodir_get_taxonomies( $post_type );
3711 1
3712
		######### WPML #########
3713
		if ( function_exists( 'icl_object_id' ) ) {
3714
			$category = gd_lang_object_ids( $category, $category_taxonomy[0] );
3715
		}
3716
		######### WPML #########
3717
3718
		$tax_query = array(
3719
			'taxonomy' => $category_taxonomy[0],
3720
			'field'    => 'id',
3721
			'terms'    => $category
3722
		);
3723
3724
		$query_args['tax_query'] = array( $tax_query );
3725
	}
3726
3727
	global $gridview_columns_widget, $geodir_is_widget_listing;
3728
3729
	$widget_listings = geodir_get_widget_listings( $query_args );
3730
3731
	if ( ! empty( $widget_listings ) || $with_no_results ) {
3732
		?>
3733
		<div class="geodir_locations geodir_location_listing">
3734
3735
			<?php
3736
			/**
3737
			 * Called before the div containing the title and view all link in popular post view widget.
3738
			 *
3739
			 * @since 1.0.0
3740
			 */
3741
			do_action( 'geodir_before_view_all_link_in_widget' ); ?>
3742
			<div class="geodir_list_heading clearfix">
3743
				<?php echo $before_title . $title . $after_title; ?>
3744
				<a href="<?php echo $viewall_url; ?>"
3745
				   class="geodir-viewall"><?php _e( 'View all', 'geodirectory' ); ?></a>
3746
			</div>
3747
			<?php
3748
			/**
3749
			 * Called after the div containing the title and view all link in popular post view widget.
3750
			 *
3751
			 * @since 1.0.0
3752
			 */
3753
			do_action( 'geodir_after_view_all_link_in_widget' ); ?>
3754
			<?php
3755 View Code Duplication
			if ( strstr( $layout, 'gridview' ) ) {
3756
				$listing_view_exp        = explode( '_', $layout );
3757
				$gridview_columns_widget = $layout;
3758
				$layout                  = $listing_view_exp[0];
3759
			} else {
3760
				$gridview_columns_widget = '';
3761
			}
3762
3763
			/**
3764
			 * Filter the widget listing listview template path.
3765
			 *
3766
			 * @since 1.0.0
3767
			 */
3768
			$template = apply_filters( "geodir_template_part-widget-listing-listview", geodir_locate_template( 'widget-listing-listview' ) );
3769
			if ( ! isset( $character_count ) ) {
3770
				/**
3771 1
				 * Filter the widget's excerpt character count.
3772 1
				 *
3773 1
				 * @since 1.0.0
3774 1
				 *
3775 1
				 * @param int $instance ['character_count'] Excerpt character count.
3776 1
				 */
3777
				$character_count = $character_count == '' ? 50 : apply_filters( 'widget_character_count', $character_count );
3778 1
			}
3779
3780 1
			global $post, $map_jason, $map_canvas_arr;
3781 1
3782
			$current_post             = $post;
3783 1
			$current_map_jason        = $map_jason;
3784 1
			$current_map_canvas_arr   = $map_canvas_arr;
3785
			$geodir_is_widget_listing = true;
3786 1
3787
			/**
3788
			 * Includes related listing listview template.
3789
			 *
3790
			 * @since 1.0.0
3791
			 */
3792
			include( $template );
3793
3794
			$geodir_is_widget_listing = false;
3795 1
3796
			$GLOBALS['post'] = $current_post;
3797 1
			if ( ! empty( $current_post ) ) {
3798
				setup_postdata( $current_post );
3799 1
			}
3800 1
			$map_jason      = $current_map_jason;
3801 1
			$map_canvas_arr = $current_map_canvas_arr;
3802 1
			?>
3803 1
		</div>
3804 1
		<?php
3805 1
	}
3806 1
	echo $after_widget;
3807 1
3808 1
}
3809
3810 1
3811 1
/*-----------------------------------------------------------------------------------*/
3812 1
/*  Review count functions
3813
/*-----------------------------------------------------------------------------------*/
3814 1
/**
3815
 * Count reviews by term ID.
3816 1
 *
3817 1
 * @since   1.0.0
3818 1
 * @since   1.5.1 Added filter to change SQL.
3819 1
 * @package GeoDirectory
3820 1
 * @global object $wpdb          WordPress Database object.
3821 1
 * @global string $plugin_prefix Geodirectory plugin table prefix.
3822 1
 *
3823 1
 * @param int $term_id           The term ID.
3824
 * @param int $taxonomy          The taxonomy Id.
3825 1
 * @param string $post_type      The post type.
3826
 *
3827 1
 * @return int Reviews count.
3828 1
 */
3829
function geodir_count_reviews_by_term_id( $term_id, $taxonomy, $post_type ) {
3830
	global $wpdb, $plugin_prefix;
3831
3832
	$detail_table = $plugin_prefix . $post_type . '_detail';
3833
3834
	$sql = "SELECT COALESCE(SUM(rating_count),0) FROM " . $detail_table . " WHERE post_status = 'publish' AND rating_count > 0 AND FIND_IN_SET(" . $term_id . ", " . $taxonomy . ")";
3835
3836
	/**
3837
	 * Filter count review sql query.
3838
	 *
3839
	 * @since 1.5.1
3840
	 *
3841
	 * @param string $sql       Database sql query..
3842
	 * @param int $term_id      The term ID.
3843
	 * @param int $taxonomy     The taxonomy Id.
3844
	 * @param string $post_type The post type.
3845
	 */
3846 1
	$sql = apply_filters( 'geodir_count_reviews_by_term_sql', $sql, $term_id, $taxonomy, $post_type );
3847
3848
	$count = $wpdb->get_var( $sql );
3849 1
3850 1
	return $count;
3851
}
3852 1
3853 1
/**
3854 1
 * Count reviews by terms.
3855 1
 *
3856
 * @since   1.0.0
3857 1
 * @since   1.6.1 Fixed add listing page load time.
3858 1
 * @package GeoDirectory
3859
 *
3860 1
 * @global object $gd_session GeoDirectory Session object.
3861 1
 *
3862
 * @param bool $force_update  Force update option value?. Default.false.
3863 1
 *
3864 1
 * @return array Term array data.
3865
 */
3866 1
function geodir_count_reviews_by_terms( $force_update = false, $post_ID = 0 ) {
3867 1
	/**
3868
	 * Filter review count option data.
3869 1
	 *
3870 1
	 * @since 1.0.0
3871
	 * @since 1.6.1 Added $post_ID param.
3872 1
	 *
3873 1
	 * @param bool $force_update Force update option value?. Default.false.
3874
	 * @param int $post_ID       The post id to update if any.
3875 1
	 */
3876 1
	$option_data = apply_filters( 'geodir_count_reviews_by_terms_before', '', $force_update, $post_ID );
3877 1
	if ( ! empty( $option_data ) ) {
3878 1
		return $option_data;
3879 1
	}
3880 1
3881 1
	$option_data = get_option( 'geodir_global_review_count' );
3882 1
3883 1
	if ( ! $option_data || $force_update ) {
3884 1
		if ( (int) $post_ID > 0 ) { // Update reviews count for specific post categories only.
3885
			global $gd_session;
3886
			$term_array = (array) $option_data;
3887 1
			$post_type  = get_post_type( $post_ID );
3888 1
			$taxonomy   = $post_type . 'category';
3889
			$terms      = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) );
3890 1
3891 View Code Duplication
			if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
3892
				foreach ( $terms as $term_id ) {
3893
					$count                  = geodir_count_reviews_by_term_id( $term_id, $taxonomy, $post_type );
3894
					$children               = get_term_children( $term_id, $taxonomy );
0 ignored issues
show
Unused Code introduced by
$children 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...
3895
					$term_array[ $term_id ] = $count;
3896
				}
3897
			}
3898
3899
			$session_listing = $gd_session->get( 'listing' );
3900
3901
			$terms = array();
3902
			if ( isset( $_POST['post_category'][ $taxonomy ] ) ) {
3903
				$terms = (array) $_POST['post_category'][ $taxonomy ];
3904 1
			} else if ( ! empty( $session_listing ) && isset( $session_listing['post_category'][ $taxonomy ] ) ) {
3905
				$terms = (array) $session_listing['post_category'][ $taxonomy ];
3906
			}
3907
3908 View Code Duplication
			if ( ! empty( $terms ) ) {
3909
				foreach ( $terms as $term_id ) {
3910
					if ( $term_id > 0 ) {
3911
						$count                  = geodir_count_reviews_by_term_id( $term_id, $taxonomy, $post_type );
3912
						$children               = get_term_children( $term_id, $taxonomy );
0 ignored issues
show
Unused Code introduced by
$children 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...
3913
						$term_array[ $term_id ] = $count;
3914
					}
3915
				}
3916
			}
3917
		} else { // Update reviews count for all post categories.
3918
			$term_array = array();
3919
			$post_types = geodir_get_posttypes();
3920
			foreach ( $post_types as $post_type ) {
0 ignored issues
show
Bug introduced by
The expression $post_types of type array|object|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...
3921
3922 1
				$taxonomy = geodir_get_taxonomies( $post_type );
3923
				$taxonomy = $taxonomy[0];
3924 1
3925
				$args = array(
3926
					'hide_empty' => false
3927
				);
3928
3929
				$terms = get_terms( $taxonomy, $args );
3930
3931
				foreach ( $terms as $term ) {
3932
					$count    = geodir_count_reviews_by_term_id( $term->term_id, $taxonomy, $post_type );
3933
					$children = get_term_children( $term->term_id, $taxonomy );
0 ignored issues
show
Unused Code introduced by
$children 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...
3934
					/*if ( is_array( $children ) ) {
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...
3935
                        foreach ( $children as $child_id ) {
3936
                            $child_count = geodir_count_reviews_by_term_id($child_id, $taxonomy, $post_type);
3937
                            $count = $count + $child_count;
3938
                        }
3939
                    }*/
3940
					$term_array[ $term->term_id ] = $count;
3941
				}
3942
			}
3943
		}
3944
3945
		update_option( 'geodir_global_review_count', $term_array );
3946
		//clear cache
3947
		wp_cache_delete( 'geodir_global_review_count' );
3948
3949
		return $term_array;
3950
	} else {
3951
		return $option_data;
3952
	}
3953
}
3954
3955
/**
3956
 * Force update review count.
3957
 *
3958
 * @since   1.0.0
3959
 * @since   1.6.1 Fixed add listing page load time.
3960
 * @package GeoDirectory
3961
 * @return bool
3962
 */
3963
function geodir_term_review_count_force_update( $new_status, $old_status = '', $post = '' ) {
3964
	if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'geodir_import_export' ) {
3965
		return; // do not run if importing listings
3966
	}
3967
3968
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
3969
		return;
3970
	}
3971
3972
	$post_ID = 0;
3973
	if ( ! empty( $post ) ) {
3974
		if ( isset( $post->post_type ) && strpos( $post->post_type, 'gd_' ) !== 0 ) {
3975
			return;
3976
		}
3977
3978
		if ( $new_status == 'auto-draft' && $old_status == 'new' ) {
3979
			return;
3980
		}
3981
3982
		if ( ! empty( $post->ID ) ) {
3983
			$post_ID = $post->ID;
3984
		}
3985
	}
3986
3987
	if ( $new_status != $old_status ) {
3988
		geodir_count_reviews_by_terms( true, $post_ID );
3989
	}
3990
3991
	return true;
3992
}
3993
3994
function geodir_term_review_count_force_update_single_post( $post_id ) {
3995
	geodir_count_reviews_by_terms( true, $post_id );
3996
}
3997
3998
/*-----------------------------------------------------------------------------------*/
3999
/*  Term count functions
4000
/*-----------------------------------------------------------------------------------*/
4001
/**
4002
 * Count posts by term.
4003
 *
4004
 * @since   1.0.0
4005
 * @package GeoDirectory
4006
 *
4007
 * @param array $data  Count data array.
4008
 * @param object $term The term object.
4009
 *
4010
 * @return int Post count.
4011
 */
4012 9
function geodir_count_posts_by_term( $data, $term ) {
4013
4014 9
	if ( $data ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data 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...
4015
		if ( isset( $data[ $term->term_id ] ) ) {
4016 9
			return $data[ $term->term_id ];
4017 2
		} else {
4018 2
			return 0;
4019 2
		}
4020
	} else {
4021
		return $term->count;
4022
	}
4023
}
4024 2
4025
/**
4026 9
 * Sort terms object by post count.
4027
 *
4028
 * @since   1.0.0
4029
 * @package GeoDirectory
4030
 * param array $terms An array of term objects.
4031
 * @return array Sorted terms array.
4032
 */
4033
function geodir_sort_terms_by_count( $terms ) {
4034
	usort( $terms, "geodir_sort_by_count_obj" );
4035
4036
	return $terms;
4037
}
4038
4039
/**
4040
 * Sort terms object by review count.
4041
 *
4042
 * @since   1.0.0
4043
 * @package GeoDirectory
4044
 *
4045
 * @param array $terms An array of term objects.
4046
 *
4047
 * @return array Sorted terms array.
4048
 */
4049
function geodir_sort_terms_by_review_count( $terms ) {
4050
	usort( $terms, "geodir_sort_by_review_count_obj" );
4051
4052
	return $terms;
4053
}
4054
4055
/**
4056
 * Sort terms either by post count or review count.
4057
 *
4058
 * @since   1.0.0
4059
 * @package GeoDirectory
4060
 *
4061
 * @param array $terms An array of term objects.
4062
 * @param string $sort The sort type. Can be count (Post Count) or review_count. Default. count.
4063
 *
4064
 * @return array Sorted terms array.
4065
 */
4066
function geodir_sort_terms( $terms, $sort = 'count' ) {
4067
	if ( $sort == 'count' ) {
4068
		return geodir_sort_terms_by_count( $terms );
4069
	}
4070
	if ( $sort == 'review_count' ) {
4071
		return geodir_sort_terms_by_review_count( $terms );
4072
	}
4073
}
4074
4075
/*-----------------------------------------------------------------------------------*/
4076
/*  Utils
4077
/*-----------------------------------------------------------------------------------*/
4078
/**
4079
 * Compares post count from array for sorting.
4080
 *
4081
 * @since   1.0.0
4082
 * @package GeoDirectory
4083
 *
4084
 * @param array $a The left side array to compare.
4085
 * @param array $b The right side array to compare.
4086
 *
4087
 * @return bool
4088
 */
4089
function geodir_sort_by_count( $a, $b ) {
4090
	return $a['count'] < $b['count'];
4091
}
4092
4093
/**
4094
 * Compares post count from object for sorting.
4095
 *
4096
 * @since   1.0.0
4097
 * @package GeoDirectory
4098
 *
4099
 * @param object $a The left side object to compare.
4100
 * @param object $b The right side object to compare.
4101
 *
4102
 * @return bool
4103
 */
4104
function geodir_sort_by_count_obj( $a, $b ) {
4105
	return $a->count < $b->count;
4106
}
4107
4108
/**
4109
 * Compares review count from object for sorting.
4110
 *
4111
 * @since   1.0.0
4112
 * @package GeoDirectory
4113
 *
4114
 * @param object $a The left side object to compare.
4115
 * @param object $b The right side object to compare.
4116
 *
4117
 * @return bool
4118
 */
4119
function geodir_sort_by_review_count_obj( $a, $b ) {
4120
	return $a->review_count < $b->review_count;
4121
}
4122
4123
/**
4124
 * Load geodirectory plugin textdomain.
4125
 *
4126
 * @since   1.4.2
4127
 * @package GeoDirectory
4128
 */
4129
function geodir_load_textdomain() {
4130
	/**
4131
	 * Filter the plugin locale.
4132
	 *
4133
	 * @since   1.4.2
4134
	 * @package GeoDirectory
4135
	 */
4136
	$locale = apply_filters( 'plugin_locale', get_locale(), 'geodirectory' );
4137
4138
	load_textdomain( 'geodirectory', WP_LANG_DIR . '/' . 'geodirectory' . '/' . 'geodirectory' . '-' . $locale . '.mo' );
4139
	load_plugin_textdomain( 'geodirectory', false, plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/geodirectory-languages' );
4140
4141
	/**
4142
	 * Define language constants.
4143
	 *
4144
	 * @since 1.0.0
4145
	 */
4146
	require_once( geodir_plugin_path() . '/language.php' );
4147
4148
	$language_file = geodir_plugin_path() . '/db-language.php';
4149
4150
	// Load language string file if not created yet
4151
	if ( ! file_exists( $language_file ) ) {
4152
		geodirectory_load_db_language();
4153
	}
4154
4155
	if ( file_exists( $language_file ) ) {
4156
		/**
4157
		 * Language strings from database.
4158
		 *
4159
		 * @since 1.4.2
4160
		 */
4161
		try {
4162
			require_once( $language_file );
4163
		} catch ( Exception $e ) {
4164
			error_log( 'Language Error: ' . $e->getMessage() );
4165
		}
4166
	}
4167
}
4168
4169
/**
4170
 * Load language strings in to file to translate via po editor
4171
 *
4172
 * @since   1.4.2
4173
 * @package GeoDirectory
4174
 *
4175
 * @global null|object $wp_filesystem WP_Filesystem object.
4176
 *
4177
 * @return bool True if file created otherwise false
4178
 */
4179
function geodirectory_load_db_language() {
4180
	global $wp_filesystem;
4181
	if ( empty( $wp_filesystem ) ) {
4182
		require_once( ABSPATH . '/wp-admin/includes/file.php' );
4183
		WP_Filesystem();
4184
		global $wp_filesystem;
4185
	}
4186
4187
	$language_file = geodir_plugin_path() . '/db-language.php';
4188
4189
	if ( is_file( $language_file ) && ! is_writable( $language_file ) ) {
4190
		return false;
4191
	} // Not possible to create.
4192
4193
	if ( ! is_file( $language_file ) && ! is_writable( dirname( $language_file ) ) ) {
4194
		return false;
4195
	} // Not possible to create.
4196
4197
	$contents_strings = array();
4198
4199
	/**
4200
	 * Filter the language string from database to translate via po editor
4201
	 *
4202
	 * @since 1.4.2
4203
	 *
4204
	 * @param array $contents_strings Array of strings.
4205
	 */
4206
	$contents_strings = apply_filters( 'geodir_load_db_language', $contents_strings );
4207 7
4208
	$contents_strings = array_unique( $contents_strings );
4209 7
4210 3
	$contents_head   = array();
4211
	$contents_head[] = "<?php";
4212
	$contents_head[] = "/**";
4213 5
	$contents_head[] = " * Translate language string stored in database. Ex: Custom Fields";
4214
	$contents_head[] = " *";
4215
	$contents_head[] = " * @package GeoDirectory";
4216
	$contents_head[] = " * @since 1.4.2";
4217
	$contents_head[] = " */";
4218
	$contents_head[] = "";
4219
	$contents_head[] = "// Language keys";
4220
4221 2
	$contents_foot   = array();
4222 2
	$contents_foot[] = "";
4223
	$contents_foot[] = "";
4224 5
4225 3
	$contents = implode( PHP_EOL, $contents_head );
4226 3
4227
	if ( ! empty( $contents_strings ) ) {
4228 5
		foreach ( $contents_strings as $string ) {
4229 5
			if ( is_scalar( $string ) && $string != '' ) {
4230 5
				$string = str_replace( "'", "\'", $string );
4231
				$contents .= PHP_EOL . "__('" . $string . "', 'geodirectory');";
4232 5
			}
4233
		}
4234
	}
4235
4236 5
	$contents .= implode( PHP_EOL, $contents_foot );
4237
4238
	if ( $wp_filesystem->put_contents( $language_file, $contents, FS_CHMOD_FILE ) ) {
4239
		return false;
4240 5
	} // Failure; could not write file.
4241 2
4242 5
	return true;
4243 1
}
4244 1
4245 4
/**
4246 1
 * Get the custom fields texts for translation
4247 1
 *
4248 2
 * @since   1.4.2
4249
 * @since   1.5.7 Option values are translatable via db translation.
4250
 * @package GeoDirectory
4251 5
 *
4252 1
 * @global object $wpdb             WordPress database abstraction object.
4253 1
 *
4254 1
 * @param  array $translation_texts Array of text strings.
4255 1
 *
4256
 * @return array Translation texts.
4257 1
 */
4258 1
function geodir_load_custom_field_translation( $translation_texts = array() ) {
4259
	global $wpdb;
4260 5
4261 2
	// Custom fields table
4262 2
	$sql  = "SELECT admin_title, admin_desc, site_title, clabels, required_msg, default_value, option_values FROM " . GEODIR_CUSTOM_FIELDS_TABLE;
4263 2
	$rows = $wpdb->get_results( $sql );
4264 2
4265
	if ( ! empty( $rows ) ) {
4266 2
		foreach ( $rows as $row ) {
4267 2
			if ( ! empty( $row->admin_title ) ) {
4268
				$translation_texts[] = stripslashes_deep( $row->admin_title );
4269 5
			}
4270
4271
			if ( ! empty( $row->admin_desc ) ) {
4272
				$translation_texts[] = stripslashes_deep( $row->admin_desc );
4273
			}
4274
4275
			if ( ! empty( $row->site_title ) ) {
4276
				$translation_texts[] = stripslashes_deep( $row->site_title );
4277
			}
4278
4279
			if ( ! empty( $row->clabels ) ) {
4280
				$translation_texts[] = stripslashes_deep( $row->clabels );
4281
			}
4282
4283
			if ( ! empty( $row->required_msg ) ) {
4284
				$translation_texts[] = stripslashes_deep( $row->required_msg );
4285
			}
4286 5
4287
			if ( ! empty( $row->default_value ) ) {
4288
				$translation_texts[] = stripslashes_deep( $row->default_value );
4289
			}
4290
4291
			if ( ! empty( $row->option_values ) ) {
4292
				$option_values = geodir_string_values_to_options( stripslashes_deep( $row->option_values ) );
4293
4294
				if ( ! empty( $option_values ) ) {
4295
					foreach ( $option_values as $option_value ) {
4296
						if ( ! empty( $option_value['label'] ) ) {
4297
							$translation_texts[] = $option_value['label'];
4298
						}
4299
					}
4300
				}
4301
			}
4302
		}
4303 5
	}
4304
4305
	// Custom sorting fields table
4306
	$sql  = "SELECT site_title, asc_title, desc_title FROM " . GEODIR_CUSTOM_SORT_FIELDS_TABLE;
4307
	$rows = $wpdb->get_results( $sql );
4308 5
4309 5 View Code Duplication
	if ( ! empty( $rows ) ) {
4310 5
		foreach ( $rows as $row ) {
4311
			if ( ! empty( $row->site_title ) ) {
4312
				$translation_texts[] = stripslashes_deep( $row->site_title );
4313 5
			}
4314 5
4315
			if ( ! empty( $row->asc_title ) ) {
4316
				$translation_texts[] = stripslashes_deep( $row->asc_title );
4317
			}
4318
4319
			if ( ! empty( $row->desc_title ) ) {
4320
				$translation_texts[] = stripslashes_deep( $row->desc_title );
4321
			}
4322
		}
4323
	}
4324
4325 5
	// Advance search filter fields table
4326 5
	if ( defined( 'GEODIR_ADVANCE_SEARCH_TABLE' ) ) {
4327 5
		$sql  = "SELECT field_site_name, front_search_title, field_desc FROM " . GEODIR_ADVANCE_SEARCH_TABLE;
4328
		$rows = $wpdb->get_results( $sql );
4329
4330 View Code Duplication
		if ( ! empty( $rows ) ) {
4331
			foreach ( $rows as $row ) {
4332 5
				if ( ! empty( $row->field_site_name ) ) {
4333 5
					$translation_texts[] = stripslashes_deep( $row->field_site_name );
4334 5
				}
4335 5
4336
				if ( ! empty( $row->front_search_title ) ) {
4337 5
					$translation_texts[] = stripslashes_deep( $row->front_search_title );
4338
				}
4339 5
4340
				if ( ! empty( $row->field_desc ) ) {
4341
					$translation_texts[] = stripslashes_deep( $row->field_desc );
4342
				}
4343
			}
4344
		}
4345 5
	}
4346
4347
	$translation_texts = ! empty( $translation_texts ) ? array_unique( $translation_texts ) : $translation_texts;
4348
4349
	return $translation_texts;
4350
}
4351
4352
/**
4353
 * Retrieve list of mime types and file extensions allowed for file upload.
4354
 *
4355 5
 * @since   1.4.7
4356
 * @package GeoDirectory
4357
 *
4358
 * @return array Array of mime types.
4359
 */
4360
function geodir_allowed_mime_types() {
4361
	/**
4362
	 * Filter the list of mime types and file extensions allowed for file upload.
4363
	 *
4364
	 * @since   1.4.7
4365 5
	 * @package GeoDirectory
4366
	 *
4367
	 * @param array $geodir_allowed_mime_types and file extensions.
4368
	 */
4369
	return apply_filters( 'geodir_allowed_mime_types', array(
4370
			'Image'       => array( // Image formats.
4371
				'jpg'  => 'image/jpeg',
4372
				'jpe'  => 'image/jpeg',
4373
				'jpeg' => 'image/jpeg',
4374
				'gif'  => 'image/gif',
4375
				'png'  => 'image/png',
4376
				'bmp'  => 'image/bmp',
4377 5
				'ico'  => 'image/x-icon',
4378
			),
4379
			'Video'       => array( // Video formats.
4380
				'asf'  => 'video/x-ms-asf',
4381
				'avi'  => 'video/avi',
4382
				'flv'  => 'video/x-flv',
4383
				'mkv'  => 'video/x-matroska',
4384
				'mp4'  => 'video/mp4',
4385
				'mpeg' => 'video/mpeg',
4386
				'mpg'  => 'video/mpeg',
4387
				'wmv'  => 'video/x-ms-wmv',
4388
				'3gp'  => 'video/3gpp',
4389
			),
4390
			'Audio'       => array( // Audio formats.
4391
				'ogg' => 'audio/ogg',
4392
				'mp3' => 'audio/mpeg',
4393
				'wav' => 'audio/wav',
4394
				'wma' => 'audio/x-ms-wma',
4395
			),
4396
			'Text'        => array( // Text formats.
4397
				'css'  => 'text/css',
4398
				'csv'  => 'text/csv',
4399
				'htm'  => 'text/html',
4400
				'html' => 'text/html',
4401
				'txt'  => 'text/plain',
4402 5
				'rtx'  => 'text/richtext',
4403 1
				'vtt'  => 'text/vtt',
4404 1
			),
4405
			'Application' => array( // Application formats.
4406
				'doc'  => 'application/msword',
4407 1
				'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
4408 1
				'exe'  => 'application/x-msdownload',
4409
				'js'   => 'application/javascript',
4410 5
				'odt'  => 'application/vnd.oasis.opendocument.text',
4411 1
				'pdf'  => 'application/pdf',
4412 1
				'pot'  => 'application/vnd.ms-powerpoint',
4413
				'ppt'  => 'application/vnd.ms-powerpoint',
4414
				'pptx' => 'application/vnd.ms-powerpoint',
4415 1
				'psd'  => 'application/octet-stream',
4416 1
				'rar'  => 'application/rar',
4417
				'rtf'  => 'application/rtf',
4418 5
				'swf'  => 'application/x-shockwave-flash',
4419 1
				'tar'  => 'application/x-tar',
4420
				'xls'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
4421
				'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
4422 1
				'zip'  => 'application/zip',
4423 1
			)
4424
		)
4425 5
	);
4426
}
4427
4428
/**
4429
 * Retrieve list of user display name for user id.
4430 5
 *
4431 1
 * @since 1.5.0
4432 1
 *
4433
 * @param  string $user_id The WP user id.
4434
 *
4435 1
 * @return string User display name.
4436 1
 */
4437
function geodir_get_client_name( $user_id ) {
4438 5
	$client_name = '';
4439 1
4440 1
	$user_data = get_userdata( $user_id );
4441
4442
	if ( ! empty( $user_data ) ) {
4443 1
		if ( isset( $user_data->display_name ) && trim( $user_data->display_name ) != '' ) {
4444 1
			$client_name = trim( $user_data->display_name );
4445
		} else if ( isset( $user_data->user_nicename ) && trim( $user_data->user_nicename ) != '' ) {
4446 5
			$client_name = trim( $user_data->user_nicename );
4447 1
		} else {
4448 1
			$client_name = trim( $user_data->user_login );
4449 1
		}
4450
	}
4451 1
4452 1
	return $client_name;
4453 1
}
4454 1
4455 1
4456 1
add_filter( 'wpseo_replacements', 'geodir_wpseo_replacements', 10, 1 );
4457
/*
4458 5
 * Add location variables to wpseo replacements.
4459
 *
4460
 * @since 1.5.4
4461
 */
4462 5
function geodir_wpseo_replacements( $vars ) {
4463
4464
	global $wp;
4465
	$title = '';
0 ignored issues
show
Unused Code introduced by
$title 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...
4466 5
	// location variables
4467
	$gd_post_type   = geodir_get_current_posttype();
4468
	$location_array = geodir_get_current_location_terms( 'query_vars', $gd_post_type );
4469
	/**
4470
	 * Filter the title variables location variables array
4471 5
	 *
4472 5
	 * @since   1.5.5
4473 5
	 * @package GeoDirectory
4474
	 *
4475
	 * @param array $location_array The array of location variables.
4476
	 * @param array $vars           The page title variables.
4477
	 */
4478
	$location_array  = apply_filters( 'geodir_filter_title_variables_location_arr_seo', $location_array, $vars );
4479
	$location_titles = array();
4480 View Code Duplication
	if ( get_query_var( 'gd_country_full' ) ) {
4481
		if ( get_query_var( 'gd_country_full' ) ) {
4482
			$location_array['gd_country'] = get_query_var( 'gd_country_full' );
4483
		}
4484
		if ( get_query_var( 'gd_region_full' ) ) {
4485
			$location_array['gd_region'] = get_query_var( 'gd_region_full' );
4486 5
		}
4487
		if ( get_query_var( 'gd_city_full' ) ) {
4488
			$location_array['gd_city'] = get_query_var( 'gd_city_full' );
4489
		}
4490
	}
4491
	$location_single = '';
4492
	$gd_country      = ( isset( $wp->query_vars['gd_country'] ) && $wp->query_vars['gd_country'] != '' ) ? $wp->query_vars['gd_country'] : '';
4493
	$gd_region       = ( isset( $wp->query_vars['gd_region'] ) && $wp->query_vars['gd_region'] != '' ) ? $wp->query_vars['gd_region'] : '';
4494
	$gd_city         = ( isset( $wp->query_vars['gd_city'] ) && $wp->query_vars['gd_city'] != '' ) ? $wp->query_vars['gd_city'] : '';
4495
4496
	$gd_country_actual = $gd_region_actual = $gd_city_actual = '';
4497
4498 View Code Duplication
	if ( function_exists( 'get_actual_location_name' ) ) {
4499 1
		$gd_country_actual = $gd_country != '' ? get_actual_location_name( 'country', $gd_country, true ) : $gd_country;
4500
		$gd_region_actual  = $gd_region != '' ? get_actual_location_name( 'region', $gd_region ) : $gd_region;
4501 1
		$gd_city_actual    = $gd_city != '' ? get_actual_location_name( 'city', $gd_city ) : $gd_city;
4502 1
	}
4503 1
4504 1 View Code Duplication
	if ( $gd_city != '' ) {
4505 1
		if ( $gd_city_actual != '' ) {
4506
			$gd_city = $gd_city_actual;
4507 1
		} else {
4508 1
			$gd_city = preg_replace( '/-(\d+)$/', '', $gd_city );
4509 1
			$gd_city = preg_replace( '/[_-]/', ' ', $gd_city );
4510 1
			$gd_city = __( geodir_ucwords( $gd_city ), 'geodirectory' );
4511 1
		}
4512 1
		$location_single = $gd_city;
4513 1
4514 1
	} else if ( $gd_region != '' ) {
4515 1
		if ( $gd_region_actual != '' ) {
4516 1
			$gd_region = $gd_region_actual;
4517 1
		} else {
4518 1
			$gd_region = preg_replace( '/-(\d+)$/', '', $gd_region );
4519 1
			$gd_region = preg_replace( '/[_-]/', ' ', $gd_region );
4520 1
			$gd_region = __( geodir_ucwords( $gd_region ), 'geodirectory' );
4521 1
		}
4522 1
4523 1
		$location_single = $gd_region;
4524 1
	} else if ( $gd_country != '' ) {
4525 1
		if ( $gd_country_actual != '' ) {
4526 1
			$gd_country = $gd_country_actual;
4527 1
		} else {
4528 1
			$gd_country = preg_replace( '/-(\d+)$/', '', $gd_country );
4529 1
			$gd_country = preg_replace( '/[_-]/', ' ', $gd_country );
4530 1
			$gd_country = __( geodir_ucwords( $gd_country ), 'geodirectory' );
4531 1
		}
4532 1
4533 1
		$location_single = $gd_country;
4534 1
	}
4535 1
4536 1 View Code Duplication
	if ( ! empty( $location_array ) ) {
4537 1
4538 1
		$actual_location_name = function_exists( 'get_actual_location_name' ) ? true : false;
4539 1
		$location_array       = array_reverse( $location_array );
4540 1
4541
		foreach ( $location_array as $location_type => $location ) {
4542 1
			$gd_location_link_text = preg_replace( '/-(\d+)$/', '', $location );
4543 1
			$gd_location_link_text = preg_replace( '/[_-]/', ' ', $gd_location_link_text );
4544 1
4545
			$location_name = geodir_ucwords( $gd_location_link_text );
4546 1
			$location_name = __( $location_name, 'geodirectory' );
4547
4548
			if ( $actual_location_name ) {
4549
				$location_type = strpos( $location_type, 'gd_' ) === 0 ? substr( $location_type, 3 ) : $location_type;
4550
				$location_name = get_actual_location_name( $location_type, $location, true );
4551
			}
4552
4553 1
			$location_titles[] = $location_name;
4554 1
		}
4555 1
		if ( ! empty( $location_titles ) ) {
4556
			$location_titles = array_unique( $location_titles );
4557 1
		}
4558
	}
4559
4560
4561
	if ( ! empty( $location_titles ) ) {
4562
		$vars['%%location%%'] = implode( ", ", $location_titles );
4563
	}
4564
4565
4566
	if ( ! empty( $location_titles ) ) {
4567
		$vars['%%in_location%%'] = __( 'in ', 'geodirectory' ) . implode( ", ", $location_titles );
4568
	}
4569
4570
4571
	if ( $location_single ) {
4572
		$vars['%%in_location_single%%'] = __( 'in', 'geodirectory' ) . ' ' . $location_single;
4573
	}
4574
4575
4576
	if ( $location_single ) {
4577
		$vars['%%location_single%%'] = $location_single;
4578
	}
4579
4580
	/**
4581
	 * Filter the title variables after standard ones have been filtered for wpseo.
4582
	 *
4583
	 * @since   1.5.7
4584
	 * @package GeoDirectory
4585
	 *
4586
	 * @param string $vars          The title with variables.
4587
	 * @param array $location_array The array of location variables.
4588
	 */
4589
	return apply_filters( 'geodir_wpseo_replacements_vars', $vars, $location_array );
4590
}
4591
4592
4593
add_filter( 'geodir_seo_meta_title', 'geodir_filter_title_variables', 10, 3 );
4594
add_filter( 'geodir_seo_page_title', 'geodir_filter_title_variables', 10, 2 );
4595
add_filter( 'geodir_seo_meta_description_pre', 'geodir_filter_title_variables', 10, 3 );
4596
4597
/**
4598
 * Filter the title variables.
4599
 *
4600
 * %%date%%                        Replaced with the date of the post/page
4601
 * %%title%%                    Replaced with the title of the post/page
4602
 * %%sitename%%                    The site's name
4603
 * %%sitedesc%%                    The site's tagline / description
4604 8
 * %%excerpt%%                    Replaced with the post/page excerpt (or auto-generated if it does not exist)
4605 7
 * %%tag%%                        Replaced with the current tag/tags
4606
 * %%category%%                    Replaced with the post categories (comma separated)
4607
 * %%category_description%%        Replaced with the category description
4608 1
 * %%tag_description%%            Replaced with the tag description
4609 1
 * %%term_description%%            Replaced with the term description
4610 1
 * %%term_title%%                Replaced with the term name
4611
 * %%searchphrase%%                Replaced with the current search phrase
4612 1
 * %%sep%%                        The separator defined in your theme's wp_title() tag.
4613
 *
4614
 * ADVANCED
4615
 * %%pt_single%%                Replaced with the post type single label
4616 1
 * %%pt_plural%%                Replaced with the post type plural label
4617
 * %%modified%%                    Replaced with the post/page modified time
4618
 * %%id%%                        Replaced with the post/page ID
4619
 * %%name%%                        Replaced with the post/page author's 'nicename'
4620 1
 * %%userid%%                    Replaced with the post/page author's userid
4621
 * %%page%%                        Replaced with the current page number (i.e. page 2 of 4)
4622 1
 * %%pagetotal%%                Replaced with the current page total
4623
 * %%pagenumber%%                Replaced with the current page number
4624
 *
4625
 * @since   1.5.7
4626
 * @package GeoDirectory
4627
 *
4628
 * @global object $wp     WordPress object.
4629
 * @global object $post   The current post object.
4630
 *
4631
 * @param string $title   The title with variables.
4632
 * @param string $gd_page The page being filtered.
4633
 * @param string $sep     The separator, default: `|`.
4634
 *
4635
 * @return string Title after filtered variables.
4636
 */
4637
function geodir_filter_title_variables( $title, $gd_page, $sep = '' ) {
4638
	global $wp, $post;
4639
4640
	if ( ! $gd_page || ! $title ) {
4641
		return $title; // if no a GD page then bail.
4642
	}
4643
4644
	if ( $sep == '' ) {
4645 1
		/**
4646
		 * Filter the page title separator.
4647
		 *
4648
		 * @since   1.0.0
4649
		 * @package GeoDirectory
4650
		 *
4651
		 * @param string $sep The separator, default: `|`.
4652
		 */
4653
		$sep = apply_filters( 'geodir_page_title_separator', '|' );
4654
	}
4655
4656
	if ( strpos( $title, '%%title%%' ) !== false ) {
4657
		$title = str_replace( "%%title%%", $post->post_title, $title );
4658
	}
4659
4660 View Code Duplication
	if ( strpos( $title, '%%sitename%%' ) !== false ) {
4661
		$title = str_replace( "%%sitename%%", get_bloginfo( 'name' ), $title );
4662
	}
4663
4664 View Code Duplication
	if ( strpos( $title, '%%sitedesc%%' ) !== false ) {
4665
		$title = str_replace( "%%sitedesc%%", get_bloginfo( 'description' ), $title );
4666
	}
4667
4668
	if ( strpos( $title, '%%excerpt%%' ) !== false ) {
4669
		$title = str_replace( "%%excerpt%%", strip_tags( get_the_excerpt() ), $title );
4670
	}
4671
4672
	if ( $gd_page == 'search' || $gd_page == 'author' ) {
4673
		$post_type = isset( $_REQUEST['stype'] ) ? sanitize_text_field( $_REQUEST['stype'] ) : '';
4674
	} else if ( $gd_page == 'add-listing' ) {
4675
		$post_type = ( isset( $_REQUEST['listing_type'] ) ) ? sanitize_text_field( $_REQUEST['listing_type'] ) : '';
4676
		$post_type = ! $post_type && ! empty( $_REQUEST['pid'] ) ? get_post_type( (int) $_REQUEST['pid'] ) : $post_type;
4677
	} else if ( isset( $post->post_type ) && $post->post_type && in_array( $post->post_type, geodir_get_posttypes() ) ) {
4678
		$post_type = $post->post_type;
4679
	} else {
4680
		$post_type = get_query_var( 'post_type' );
4681
	}
4682
4683 View Code Duplication
	if ( strpos( $title, '%%pt_single%%' ) !== false ) {
4684
		$singular_name = '';
4685
		if ( $post_type && $singular_name = get_post_type_singular_label( $post_type ) ) {
4686
			$singular_name = __( $singular_name, 'geodirectory' );
4687
		}
4688
4689
		$title = str_replace( "%%pt_single%%", $singular_name, $title );
4690
	}
4691
4692 View Code Duplication
	if ( strpos( $title, '%%pt_plural%%' ) !== false ) {
4693
		$plural_name = '';
4694
		if ( $post_type && $plural_name = get_post_type_plural_label( $post_type ) ) {
4695
			$plural_name = __( $plural_name, 'geodirectory' );
4696
		}
4697
4698
		$title = str_replace( "%%pt_plural%%", $plural_name, $title );
4699
	}
4700
4701 View Code Duplication
	if ( strpos( $title, '%%category%%' ) !== false ) {
4702
		$cat_name = '';
4703
4704
		if ( $gd_page == 'detail' ) {
4705
			if ( $post->default_category ) {
4706
				$cat      = get_term( $post->default_category, $post->post_type . 'category' );
4707
				$cat_name = ( isset( $cat->name ) ) ? $cat->name : '';
4708
			}
4709
		} else if ( $gd_page == 'listing' ) {
4710
			$queried_object = get_queried_object();
4711
			if ( isset( $queried_object->name ) ) {
4712
				$cat_name = $queried_object->name;
4713
			}
4714
		}
4715
		$title = str_replace( "%%category%%", $cat_name, $title );
4716
	}
4717
4718 View Code Duplication
	if ( strpos( $title, '%%tag%%' ) !== false ) {
4719
		$cat_name = '';
4720
4721
		if ( $gd_page == 'detail' ) {
4722
			if ( $post->default_category ) {
4723
				$cat      = get_term( $post->default_category, $post->post_type . 'category' );
4724
				$cat_name = ( isset( $cat->name ) ) ? $cat->name : '';
4725
			}
4726
		} else if ( $gd_page == 'listing' ) {
4727
			$queried_object = get_queried_object();
4728
			if ( isset( $queried_object->name ) ) {
4729
				$cat_name = $queried_object->name;
4730
			}
4731
		}
4732
		$title = str_replace( "%%tag%%", $cat_name, $title );
4733
	}
4734
4735
	if ( strpos( $title, '%%id%%' ) !== false ) {
4736
		$ID    = ( isset( $post->ID ) ) ? $post->ID : '';
4737
		$title = str_replace( "%%id%%", $ID, $title );
4738
	}
4739
4740
	if ( strpos( $title, '%%sep%%' ) !== false ) {
4741
		$title = str_replace( "%%sep%%", $sep, $title );
4742
	}
4743
4744
	// location variables
4745
	$gd_post_type   = geodir_get_current_posttype();
4746
	$location_array = geodir_get_current_location_terms( 'query_vars', $gd_post_type );
4747
	/**
4748
	 * Filter the title variables location variables array
4749
	 *
4750
	 * @since   1.5.5
4751
	 * @package GeoDirectory
4752
	 *
4753
	 * @param array $location_array The array of location variables.
4754
	 * @param string $title         The title with variables..
4755
	 * @param string $gd_page       The page being filtered.
4756
	 * @param string $sep           The separator, default: `|`.
4757
	 */
4758
	$location_array  = apply_filters( 'geodir_filter_title_variables_location_arr', $location_array, $title, $gd_page, $sep );
4759
	$location_titles = array();
4760 View Code Duplication
	if ( $gd_page == 'location' && get_query_var( 'gd_country_full' ) ) {
4761
		if ( get_query_var( 'gd_country_full' ) ) {
4762
			$location_array['gd_country'] = get_query_var( 'gd_country_full' );
4763
		}
4764
		if ( get_query_var( 'gd_region_full' ) ) {
4765
			$location_array['gd_region'] = get_query_var( 'gd_region_full' );
4766
		}
4767
		if ( get_query_var( 'gd_city_full' ) ) {
4768
			$location_array['gd_city'] = get_query_var( 'gd_city_full' );
4769
		}
4770
	}
4771 3
	$location_single = '';
4772 3
	$gd_country      = ( isset( $wp->query_vars['gd_country'] ) && $wp->query_vars['gd_country'] != '' ) ? $wp->query_vars['gd_country'] : '';
4773
	$gd_region       = ( isset( $wp->query_vars['gd_region'] ) && $wp->query_vars['gd_region'] != '' ) ? $wp->query_vars['gd_region'] : '';
4774
	$gd_city         = ( isset( $wp->query_vars['gd_city'] ) && $wp->query_vars['gd_city'] != '' ) ? $wp->query_vars['gd_city'] : '';
4775 3
4776 3
	$gd_country_actual = $gd_region_actual = $gd_city_actual = '';
4777 3
4778 3 View Code Duplication
	if ( function_exists( 'get_actual_location_name' ) ) {
4779 3
		$gd_country_actual = $gd_country != '' ? get_actual_location_name( 'country', $gd_country, true ) : $gd_country;
4780 3
		$gd_region_actual  = $gd_region != '' ? get_actual_location_name( 'region', $gd_region ) : $gd_region;
4781 3
		$gd_city_actual    = $gd_city != '' ? get_actual_location_name( 'city', $gd_city ) : $gd_city;
4782
	}
4783
4784 View Code Duplication
	if ( $gd_city != '' ) {
4785
		if ( $gd_city_actual != '' ) {
4786
			$gd_city = $gd_city_actual;
4787
		} else {
4788
			$gd_city = preg_replace( '/-(\d+)$/', '', $gd_city );
4789
			$gd_city = preg_replace( '/[_-]/', ' ', $gd_city );
4790
			$gd_city = __( geodir_ucwords( $gd_city ), 'geodirectory' );
4791
		}
4792
		$location_single = $gd_city;
4793
4794 2
	} else if ( $gd_region != '' ) {
4795 1
		if ( $gd_region_actual != '' ) {
4796 1
			$gd_region = $gd_region_actual;
4797 2
		} else {
4798
			$gd_region = preg_replace( '/-(\d+)$/', '', $gd_region );
4799
			$gd_region = preg_replace( '/[_-]/', ' ', $gd_region );
4800
			$gd_region = __( geodir_ucwords( $gd_region ), 'geodirectory' );
4801
		}
4802
4803
		$location_single = $gd_region;
4804
	} else if ( $gd_country != '' ) {
4805
		if ( $gd_country_actual != '' ) {
4806
			$gd_country = $gd_country_actual;
4807
		} else {
4808
			$gd_country = preg_replace( '/-(\d+)$/', '', $gd_country );
4809
			$gd_country = preg_replace( '/[_-]/', ' ', $gd_country );
4810
			$gd_country = __( geodir_ucwords( $gd_country ), 'geodirectory' );
4811
		}
4812
4813
		$location_single = $gd_country;
4814
	}
4815
4816 View Code Duplication
	if ( ! empty( $location_array ) ) {
4817
4818
		$actual_location_name = function_exists( 'get_actual_location_name' ) ? true : false;
4819
		$location_array       = array_reverse( $location_array );
4820
4821
		foreach ( $location_array as $location_type => $location ) {
4822
			$gd_location_link_text = preg_replace( '/-(\d+)$/', '', $location );
4823
			$gd_location_link_text = preg_replace( '/[_-]/', ' ', $gd_location_link_text );
4824
4825
			$location_name = geodir_ucwords( $gd_location_link_text );
4826
			$location_name = __( $location_name, 'geodirectory' );
4827
4828
			if ( $actual_location_name ) {
4829
				$location_type = strpos( $location_type, 'gd_' ) === 0 ? substr( $location_type, 3 ) : $location_type;
4830
				$location_name = get_actual_location_name( $location_type, $location, true );
4831
			}
4832
4833
			$location_titles[] = $location_name;
4834
		}
4835
		if ( ! empty( $location_titles ) ) {
4836
			$location_titles = array_unique( $location_titles );
4837
		}
4838
	}
4839
4840
4841
	if ( strpos( $title, '%%location%%' ) !== false ) {
4842
		$location = '';
4843
		if ( $location_titles ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $location_titles 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...
4844
			$location = implode( ", ", $location_titles );
4845
		}
4846
		$title = str_replace( "%%location%%", $location, $title );
4847
	}
4848
4849 View Code Duplication
	if ( strpos( $title, '%%in_location%%' ) !== false ) {
4850
		$location = '';
4851
		if ( $location_titles ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $location_titles 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...
4852
			$location = __( 'in ', 'geodirectory' ) . implode( ", ", $location_titles );
4853
		}
4854
		$title = str_replace( "%%in_location%%", $location, $title );
4855
	}
4856
4857 View Code Duplication
	if ( strpos( $title, '%%in_location_single%%' ) !== false ) {
4858
		if ( $location_single ) {
4859
			$location_single = __( 'in', 'geodirectory' ) . ' ' . $location_single;
4860
		}
4861
		$title = str_replace( "%%in_location_single%%", $location_single, $title );
4862
	}
4863
4864
	if ( strpos( $title, '%%location_single%%' ) !== false ) {
4865
		$title = str_replace( "%%location_single%%", $location_single, $title );
4866
	}
4867
4868
4869 View Code Duplication
	if ( strpos( $title, '%%search_term%%' ) !== false ) {
4870
		$search_term = '';
4871
		if ( isset( $_REQUEST['s'] ) ) {
4872
			$search_term = esc_attr( $_REQUEST['s'] );
4873
		}
4874
		$title = str_replace( "%%search_term%%", $search_term, $title );
4875
	}
4876
4877 View Code Duplication
	if ( strpos( $title, '%%search_near%%' ) !== false ) {
4878
		$search_term = '';
4879
		if ( isset( $_REQUEST['snear'] ) ) {
4880
			$search_term = esc_attr( $_REQUEST['snear'] );
4881
		}
4882
		$title = str_replace( "%%search_near%%", $search_term, $title );
4883
	}
4884
4885
	if ( strpos( $title, '%%name%%' ) !== false ) {
4886
		if ( is_author() ) {
4887
			$curauth     = ( get_query_var( 'author_name' ) ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
4888
			$author_name = $curauth->display_name;
4889
		} else {
4890
			$author_name = get_the_author();
4891
		}
4892
		if ( ! $author_name || $author_name === '' ) {
4893
			$queried_object = get_queried_object();
4894
4895
			if ( isset( $queried_object->data->user_nicename ) ) {
4896
				$author_name = $queried_object->data->display_name;
4897
			}
4898
		}
4899
		$title = str_replace( "%%name%%", $author_name, $title );
4900
	}
4901
4902
	if ( strpos( $title, '%%page%%' ) !== false ) {
4903
		$page  = geodir_title_meta_page( $sep );
4904
		$title = str_replace( "%%page%%", $page, $title );
4905
	}
4906
	if ( strpos( $title, '%%pagenumber%%' ) !== false ) {
4907
		$pagenumber = geodir_title_meta_pagenumber();
4908
		$title      = str_replace( "%%pagenumber%%", $pagenumber, $title );
4909
	}
4910
	if ( strpos( $title, '%%pagetotal%%' ) !== false ) {
4911
		$pagetotal = geodir_title_meta_pagetotal();
4912
		$title     = str_replace( "%%pagetotal%%", $pagetotal, $title );
4913
	}
4914
4915
	$title = wptexturize( $title );
4916
	$title = convert_chars( $title );
4917
	$title = esc_html( $title );
4918
4919
	/**
4920
	 * Filter the title variables after standard ones have been filtered.
4921
	 *
4922
	 * @since   1.5.7
4923
	 * @package GeoDirectory
4924
	 *
4925
	 * @param string $title         The title with variables.
4926
	 * @param array $location_array The array of location variables.
4927
	 * @param string $gd_page       The page being filtered.
4928
	 * @param string $sep           The separator, default: `|`.
4929
	 */
4930
4931
	return apply_filters( 'geodir_filter_title_variables_vars', $title, $location_array, $gd_page, $sep );
4932
}
4933
4934
/**
4935
 * Get the cpt texts for translation.
4936
 *
4937
 * @since   1.5.5
4938
 * @package GeoDirectory
4939
 *
4940
 * @param  array $translation_texts Array of text strings.
4941
 *
4942
 * @return array Translation texts.
4943
 */
4944
function geodir_load_cpt_text_translation( $translation_texts = array() ) {
4945
	$gd_post_types = geodir_get_posttypes( 'array' );
4946
4947
	if ( ! empty( $gd_post_types ) ) {
4948
		foreach ( $gd_post_types as $post_type => $cpt_info ) {
0 ignored issues
show
Bug introduced by
The expression $gd_post_types of type array|object|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...
4949
			$labels      = isset( $cpt_info['labels'] ) ? $cpt_info['labels'] : '';
4950
			$description = isset( $cpt_info['description'] ) ? $cpt_info['description'] : '';
4951
			$seo         = isset( $cpt_info['seo'] ) ? $cpt_info['seo'] : '';
4952
4953
			if ( ! empty( $labels ) ) {
4954 View Code Duplication
				if ( $labels['name'] != '' && ! in_array( $labels['name'], $translation_texts ) ) {
4955
					$translation_texts[] = $labels['name'];
4956
				}
4957 View Code Duplication
				if ( $labels['singular_name'] != '' && ! in_array( $labels['singular_name'], $translation_texts ) ) {
4958
					$translation_texts[] = $labels['singular_name'];
4959
				}
4960 View Code Duplication
				if ( $labels['add_new'] != '' && ! in_array( $labels['add_new'], $translation_texts ) ) {
4961
					$translation_texts[] = $labels['add_new'];
4962
				}
4963 View Code Duplication
				if ( $labels['add_new_item'] != '' && ! in_array( $labels['add_new_item'], $translation_texts ) ) {
4964
					$translation_texts[] = $labels['add_new_item'];
4965
				}
4966 View Code Duplication
				if ( $labels['edit_item'] != '' && ! in_array( $labels['edit_item'], $translation_texts ) ) {
4967
					$translation_texts[] = $labels['edit_item'];
4968
				}
4969 View Code Duplication
				if ( $labels['new_item'] != '' && ! in_array( $labels['new_item'], $translation_texts ) ) {
4970
					$translation_texts[] = $labels['new_item'];
4971
				}
4972 View Code Duplication
				if ( $labels['view_item'] != '' && ! in_array( $labels['view_item'], $translation_texts ) ) {
4973
					$translation_texts[] = $labels['view_item'];
4974
				}
4975 View Code Duplication
				if ( $labels['search_items'] != '' && ! in_array( $labels['search_items'], $translation_texts ) ) {
4976
					$translation_texts[] = $labels['search_items'];
4977
				}
4978 View Code Duplication
				if ( $labels['not_found'] != '' && ! in_array( $labels['not_found'], $translation_texts ) ) {
4979
					$translation_texts[] = $labels['not_found'];
4980
				}
4981 View Code Duplication
				if ( $labels['not_found_in_trash'] != '' && ! in_array( $labels['not_found_in_trash'], $translation_texts ) ) {
4982
					$translation_texts[] = $labels['not_found_in_trash'];
4983
				}
4984 View Code Duplication
				if ( isset( $labels['label_post_profile'] ) && $labels['label_post_profile'] != '' && ! in_array( $labels['label_post_profile'], $translation_texts ) ) {
4985
					$translation_texts[] = $labels['label_post_profile'];
4986
				}
4987 View Code Duplication
				if ( isset( $labels['label_post_info'] ) && $labels['label_post_info'] != '' && ! in_array( $labels['label_post_info'], $translation_texts ) ) {
4988
					$translation_texts[] = $labels['label_post_info'];
4989
				}
4990 View Code Duplication
				if ( isset( $labels['label_post_images'] ) && $labels['label_post_images'] != '' && ! in_array( $labels['label_post_images'], $translation_texts ) ) {
4991
					$translation_texts[] = $labels['label_post_images'];
4992
				}
4993 View Code Duplication
				if ( isset( $labels['label_post_map'] ) && $labels['label_post_map'] != '' && ! in_array( $labels['label_post_map'], $translation_texts ) ) {
4994
					$translation_texts[] = $labels['label_post_map'];
4995
				}
4996 View Code Duplication
				if ( isset( $labels['label_reviews'] ) && $labels['label_reviews'] != '' && ! in_array( $labels['label_reviews'], $translation_texts ) ) {
4997
					$translation_texts[] = $labels['label_reviews'];
4998
				}
4999 View Code Duplication
				if ( isset( $labels['label_related_listing'] ) && $labels['label_related_listing'] != '' && ! in_array( $labels['label_related_listing'], $translation_texts ) ) {
5000
					$translation_texts[] = $labels['label_related_listing'];
5001
				}
5002
			}
5003
5004
			if ( $description != '' && ! in_array( $description, $translation_texts ) ) {
5005
				$translation_texts[] = normalize_whitespace( $description );
5006
			}
5007
5008
			if ( ! empty( $seo ) ) {
5009 View Code Duplication
				if ( isset( $seo['meta_keyword'] ) && $seo['meta_keyword'] != '' && ! in_array( $seo['meta_keyword'], $translation_texts ) ) {
5010
					$translation_texts[] = normalize_whitespace( $seo['meta_keyword'] );
5011
				}
5012
5013 View Code Duplication
				if ( isset( $seo['meta_description'] ) && $seo['meta_description'] != '' && ! in_array( $seo['meta_description'], $translation_texts ) ) {
5014
					$translation_texts[] = normalize_whitespace( $seo['meta_description'] );
5015
				}
5016
			}
5017
		}
5018
	}
5019
	$translation_texts = ! empty( $translation_texts ) ? array_unique( $translation_texts ) : $translation_texts;
5020
5021
	return $translation_texts;
5022
}
5023
5024
/**
5025
 * Remove the location terms to hide term from location url.
5026
 *
5027
 * @since   1.5.5
5028
 * @package GeoDirectory
5029
 *
5030
 * @param  array $location_terms Array of location terms.
5031
 *
5032
 * @return array Location terms.
5033
 */
5034
function geodir_remove_location_terms( $location_terms = array() ) {
5035
	$location_manager = defined( 'POST_LOCATION_TABLE' ) ? true : false;
5036
5037
	if ( ! empty( $location_terms ) && $location_manager ) {
5038
		$hide_country_part = get_option( 'geodir_location_hide_country_part' );
5039
		$hide_region_part  = get_option( 'geodir_location_hide_region_part' );
5040
5041
		if ( $hide_region_part && $hide_country_part ) {
5042
			if ( isset( $location_terms['gd_country'] ) ) {
5043
				unset( $location_terms['gd_country'] );
5044
			}
5045
			if ( isset( $location_terms['gd_region'] ) ) {
5046
				unset( $location_terms['gd_region'] );
5047
			}
5048
		} else if ( $hide_region_part && ! $hide_country_part ) {
5049
			if ( isset( $location_terms['gd_region'] ) ) {
5050
				unset( $location_terms['gd_region'] );
5051
			}
5052
		} else if ( ! $hide_region_part && $hide_country_part ) {
5053
			if ( isset( $location_terms['gd_country'] ) ) {
5054
				unset( $location_terms['gd_country'] );
5055
			}
5056
		}
5057
	}
5058
5059
	return $location_terms;
5060
}
5061
5062
/**
5063
 * Send notification when a listing has been edited by it's author.
5064
 *
5065
 * @since   1.5.9
5066
 * @package GeoDirectory
5067
 *
5068
 * @param int $post_ID  Post ID.
5069
 * @param WP_Post $post Post object.
5070
 * @param bool $update  Whether this is an existing listing being updated or not.
5071
 */
5072
function geodir_on_wp_insert_post( $post_ID, $post, $update ) {
5073
	if ( ! $update ) {
5074
		return;
5075
	}
5076
5077
	$action      = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : '';
5078
	$is_admin    = is_admin() && ( ! defined( 'DOING_AJAX' ) || ( defined( 'DOING_AJAX' ) && ! DOING_AJAX ) ) ? true : false;
5079
	$inline_save = $action == 'inline-save' ? true : false;
5080
5081
	if ( empty( $post->post_type ) || $is_admin || $inline_save || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
5082
		return;
5083
	}
5084
5085
	if ( $action != '' && in_array( $action, array( 'geodir_import_export' ) ) ) {
5086
		return;
5087
	}
5088
5089
	$user_id = (int) get_current_user_id();
5090
5091
	if ( $user_id > 0 && get_option( 'geodir_notify_post_edited' ) && ! wp_is_post_revision( $post_ID ) && in_array( $post->post_type, geodir_get_posttypes() ) ) {
5092
		$author_id = ! empty( $post->post_author ) ? $post->post_author : 0;
5093
5094
		if ( $user_id == $author_id && ! is_super_admin() ) {
5095
			$from_email   = get_option( 'site_email' );
5096
			$from_name    = get_site_emailName();
5097
			$to_email     = get_option( 'admin_email' );
5098
			$to_name      = get_option( 'name' );
5099
			$message_type = 'listing_edited';
5100
5101
			$notify_edited = true;
5102
			/**
5103
			 * Send notification when listing edited by author?
5104
			 *
5105
			 * @since 1.6.0
5106
			 *
5107
			 * @param bool $notify_edited Notify on listing edited by author?
5108
			 * @param object $post        The current post object.
5109
			 */
5110
			$notify_edited = apply_filters( 'geodir_notify_on_listing_edited', $notify_edited, $post );
0 ignored issues
show
Unused Code introduced by
$notify_edited 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...
5111
5112
			geodir_sendEmail( $from_email, $from_name, $to_email, $to_name, '', '', '', $message_type, $post_ID );
5113
		}
5114
	}
5115
}
5116
5117
/**
5118
 * Retrieve the current page start & end numbering with context (i.e. 'page 2 of 4') for use as replacement string.
5119
 *
5120
 * @since   1.6.0
5121
 * @package GeoDirectory
5122
 *
5123
 * @param string $sep The separator tag.
5124
 *
5125
 * @return string|null The current page start & end numbering.
5126
 */
5127
function geodir_title_meta_page( $sep ) {
5128
	$replacement = null;
5129
5130
	$max = geodir_title_meta_pagenumbering( 'max' );
5131
	$nr  = geodir_title_meta_pagenumbering( 'nr' );
5132
5133
	if ( $max > 1 && $nr > 1 ) {
5134
		$replacement = sprintf( $sep . ' ' . __( 'Page %1$d of %2$d', 'geodirectory' ), $nr, $max );
5135
	}
5136
5137
	return $replacement;
5138
}
5139
5140
/**
5141
 * Retrieve the current page number for use as replacement string.
5142
 *
5143
 * @since   1.6.0
5144
 * @package GeoDirectory
5145
 *
5146
 * @return string|null The current page number.
5147
 */
5148 View Code Duplication
function geodir_title_meta_pagenumber() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
5149
	$replacement = null;
5150
5151
	$nr = geodir_title_meta_pagenumbering( 'nr' );
5152
	if ( isset( $nr ) && $nr > 0 ) {
5153
		$replacement = (string) $nr;
5154
	}
5155
5156
	return $replacement;
5157
}
5158
5159
/**
5160
 * Retrieve the current page total for use as replacement string.
5161
 *
5162
 * @since   1.6.0
5163
 * @package GeoDirectory
5164
 *
5165
 * @return string|null The current page total.
5166
 */
5167 View Code Duplication
function geodir_title_meta_pagetotal() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
5168
	$replacement = null;
5169
5170
	$max = geodir_title_meta_pagenumbering( 'max' );
5171
	if ( isset( $max ) && $max > 0 ) {
5172
		$replacement = (string) $max;
5173
	}
5174
5175
	return $replacement;
5176
}
5177
5178
/**
5179
 * Determine the page numbering of the current post/page/cpt.
5180
 *
5181
 * @param string $request   'nr'|'max' - whether to return the page number or the max number of pages.
5182
 *
5183
 * @since   1.6.0
5184
 * @package GeoDirectory
5185
 *
5186
 * @global object $wp_query WordPress Query object.
5187
 * @global object $post     The current post object.
5188
 *
5189
 * @return int|null The current page numbering.
5190
 */
5191
function geodir_title_meta_pagenumbering( $request = 'nr' ) {
5192
	global $wp_query, $post;
5193
	$max_num_pages = null;
0 ignored issues
show
Unused Code introduced by
$max_num_pages 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...
5194
	$page_number   = null;
0 ignored issues
show
Unused Code introduced by
$page_number 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...
5195
5196
	$max_num_pages = 1;
5197
5198
	if ( ! is_singular() ) {
5199
		$page_number = get_query_var( 'paged' );
5200
		if ( $page_number === 0 || $page_number === '' ) {
5201
			$page_number = 1;
5202
		}
5203
5204
		if ( isset( $wp_query->max_num_pages ) && ( $wp_query->max_num_pages != '' && $wp_query->max_num_pages != 0 ) ) {
5205
			$max_num_pages = $wp_query->max_num_pages;
5206
		}
5207
	} else {
5208
		$page_number = get_query_var( 'page' );
5209
		if ( $page_number === 0 || $page_number === '' ) {
5210
			$page_number = 1;
5211
		}
5212
5213
		if ( isset( $post->post_content ) ) {
5214
			$max_num_pages = ( substr_count( $post->post_content, '<!--nextpage-->' ) + 1 );
5215
		}
5216
	}
5217
5218
	$return = null;
5219
5220
	switch ( $request ) {
5221
		case 'nr':
5222
			$return = $page_number;
5223
			break;
5224
		case 'max':
5225
			$return = $max_num_pages;
5226
			break;
5227
	}
5228
5229
	return $return;
5230
}
5231
5232
/**
5233
 * Filter the terms with count empty.
5234
 *
5235
 * @since 1.5.4
5236
 *
5237
 * @param array $terms Terms array.
5238
 *
5239
 * @return array Terms.
5240
 */
5241 View Code Duplication
function geodir_filter_empty_terms( $terms ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
5242
	if ( empty( $terms ) ) {
5243
		return $terms;
5244
	}
5245
5246
	$return = array();
5247
	foreach ( $terms as $term ) {
5248
		if ( isset( $term->count ) && $term->count > 0 ) {
5249
			$return[] = $term;
5250
		} else {
5251
			/**
5252
			 * Allow to filter terms with no count.
5253
			 *
5254
			 * @since 1.6.6
5255
			 *
5256
			 * @param array $return The array of terms to return.
5257
			 * @param object $term  The term object.
5258
			 */
5259
			$return = apply_filters( 'geodir_filter_empty_terms_filter', $return, $term );
5260
		}
5261
	}
5262
5263
	return $return;
5264
}
5265
5266
5267
/**
5268
 * Remove the hentry class structured data from details pages.
5269
 *
5270
 * @since 1.6.5
5271
 *
5272
 * @param $class
5273
 *
5274
 * @return array
5275
 */
5276
function geodir_remove_hentry( $class ) {
5277
	if ( geodir_is_page( 'detail' ) ) {
5278
		$class = array_diff( $class, array( 'hentry' ) );
5279
	}
5280
5281
	return $class;
5282
}
5283
5284
add_filter( 'post_class', 'geodir_remove_hentry' );