Test Failed
Push — master ( a5e425...6adb05 )
by Stiofan
23:07
created

custom_functions.php ➔ geodir_send_inquiry()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 125
Code Lines 36

Duplication

Lines 5
Ratio 4 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 36
nc 32
nop 1
dl 5
loc 125
ccs 0
cts 38
cp 0
crap 56
rs 6.4589
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Custom functions
4
 *
5
 * @since   1.0.0
6
 * @package GeoDirectory
7
 */
8
9
/**
10
 * Front end listing view template selection.
11
 *
12
 * This function adds a drop down in front end listing page for selecting view template. Ex: list view, 2 column grid
13
 * view, etc.
14
 *
15
 * @since   1.0.0
16
 * @package GeoDirectory
17
 *
18
 * @global object $gd_session GeoDirectory Session object.
19
 */
20
function geodir_list_view_select() {
21
	global $gd_session;
22
	?>
23
	<script type="text/javascript">
24
		function geodir_list_view_select(list) {
25
			//alert(listval);
26
			val = list.value;
27
			if (!val) {
28
				return;
29
			}
30
31
			var listSel = jQuery(list).parent().parent().next('.geodir_category_list_view');
32
			if (val != 1) {
33
				jQuery(listSel).children('li').addClass('geodir-gridview');
34
				jQuery(listSel).children('li').removeClass('geodir-listview');
35
			} else {
36
				jQuery(listSel).children('li').addClass('geodir-listview');
37
			}
38
39
			if (val == 1) {
40
				jQuery(listSel).children('li').removeClass('geodir-gridview gridview_onehalf gridview_onethird gridview_onefourth gridview_onefifth');
41
			}
42
			else if (val == 2) {
43
				jQuery(listSel).children('li').switchClass('gridview_onethird gridview_onefourth gridview_onefifth', 'gridview_onehalf', 600);
44
			}
45
			else if (val == 3) {
46
				jQuery(listSel).children('li').switchClass('gridview_onehalf gridview_onefourth gridview_onefifth', 'gridview_onethird', 600);
47
			}
48
			else if (val == 4) {
49
				jQuery(listSel).children('li').switchClass('gridview_onehalf gridview_onethird gridview_onefifth', 'gridview_onefourth', 600);
50
			}
51
			else if (val == 5) {
52
				jQuery(listSel).children('li').switchClass('gridview_onehalf gridview_onethird gridview_onefourth', 'gridview_onefifth', 600);
53
			}
54
55
			jQuery.post("<?php echo geodir_get_ajax_url();?>&gd_listing_view=" + val, function (data) {
56
				//alert(data );
57
			});
58
		}
59
	</script>
60
	<div class="geodir-list-view-select">
61
		<select name="gd_list_view" id="gd_list_view" onchange="geodir_list_view_select(this);">
62
			<?php $listing_view = (int) $gd_session->get( 'gd_listing_view' ); ?>
63
			<option value=""><?php _e( 'View:', 'geodirectory' ); ?></option>
64
			<option
65
				value="1" <?php selected( 1, $listing_view ); ?>><?php _e( 'View: List', 'geodirectory' ); ?></option>
66
			<option
67
				value="2" <?php selected( 2, $listing_view ); ?>><?php _e( 'View: Grid 2', 'geodirectory' ); ?></option>
68
			<option
69
				value="3" <?php selected( 3, $listing_view ); ?>><?php _e( 'View: Grid 3', 'geodirectory' ); ?></option>
70
			<option
71
				value="4" <?php selected( 4, $listing_view ); ?>><?php _e( 'View: Grid 4', 'geodirectory' ); ?></option>
72
			<option
73
				value="5" <?php selected( 5, $listing_view ); ?>><?php _e( 'View: Grid 5', 'geodirectory' ); ?></option>
74
		</select>
75
	</div>
76
	<?php
77
}
78
79
add_action( 'geodir_before_listing', 'geodir_list_view_select', 100 );
80
81
/**
82
 * Limit the listing excerpt.
83
 *
84
 * This function limits excerpt characters and display "read more" link.
85
 *
86
 * @since   1.0.0
87
 * @package GeoDirectory
88
 *
89
 * @param string|int $charlength The character length.
90
 *
91
 * @global object $post          The current post object.
92
 * @return string The modified excerpt.
93
 */
94
function geodir_max_excerpt( $charlength ) {
95
	global $post;
96
	if ( $charlength == '0' ) {
97
		return;
98
	}
99
	$out = '';
100
101
	$temp_post = $post;
102
	$excerpt   = get_the_excerpt();
103
104
	$charlength ++;
105
	$excerpt_more = function_exists( 'geodirf_excerpt_more' ) ? geodirf_excerpt_more( '' ) : geodir_excerpt_more( '' );
106
	if ( geodir_utf8_strlen( $excerpt ) > $charlength ) {
107
		if ( geodir_utf8_strlen( $excerpt_more ) > 0 && geodir_utf8_strpos( $excerpt, $excerpt_more ) !== false ) {
108
			$excut = - ( geodir_utf8_strlen( $excerpt_more ) );
109
			$subex = geodir_utf8_substr( $excerpt, 0, $excut );
110
			if ( $charlength > 0 && geodir_utf8_strlen( $subex ) > $charlength ) {
111
				$subex = geodir_utf8_substr( $subex, 0, $charlength );
112
			}
113
			$out .= $subex;
114
		} else {
115
			$subex   = geodir_utf8_substr( $excerpt, 0, $charlength - 5 );
116
			$exwords = explode( ' ', $subex );
117
			$excut   = - ( geodir_utf8_strlen( $exwords[ count( $exwords ) - 1 ] ) );
118
			if ( $excut < 0 ) {
119
				$out .= geodir_utf8_substr( $subex, 0, $excut );
120
			} else {
121
				$out .= $subex;
122
			}
123
		}
124
		$out .= ' <a class="excerpt-read-more" href="' . get_permalink() . '" title="' . get_the_title() . '">';
125
		/**
126
		 * Filter excerpt read more text.
127
		 *
128
		 * @since 1.0.0
129
		 */
130
		$out .= apply_filters( 'geodir_max_excerpt_end', __( 'Read more [...]', 'geodirectory' ) );
131
		$out .= '</a>';
132
133
	} else {
134
		if ( geodir_utf8_strlen( $excerpt_more ) > 0 && geodir_utf8_strpos( $excerpt, $excerpt_more ) !== false ) {
135
			$excut = - ( geodir_utf8_strlen( $excerpt_more ) );
136
			$out .= geodir_utf8_substr( $excerpt, 0, $excut );
137
			$out .= ' <a class="excerpt-read-more" href="' . get_permalink() . '" title="' . get_the_title() . '">';
138
			/**
139
			 * Filter excerpt read more text.
140
			 *
141
			 * @since 1.0.0
142
			 */
143
			$out .= apply_filters( 'geodir_max_excerpt_end', __( 'Read more [...]', 'geodirectory' ) );
144
			$out .= '</a>';
145
		} else {
146
			$out .= $excerpt;
147
		}
148
	}
149
	$post = $temp_post;
150
151
	return $out;
152
}
153
154
/**
155
 * Returns package information as an objects.
156
 *
157
 * @since   1.0.0
158
 * @package GeoDirectory
159
 *
160
 * @param array $package_info Package info array.
161
 * @param object|string $post The post object.
162
 * @param string $post_type   The post type.
163
 *
164
 * @return object Returns filtered package info as an object.
165
 */
166
function geodir_post_package_info( $package_info, $post = '', $post_type = '' ) {
167 6
	$package_info['pid']              = 0;
168 6
	$package_info['days']             = 0;
169 6
	$package_info['amount']           = 0;
170 6
	$package_info['is_featured']      = 0;
171 6
	$package_info['image_limit']      = '';
172 6
	$package_info['google_analytics'] = 1;
173 6
	$package_info['sendtofriend']     = 1;
174
175
	/**
176
	 * Filter listing package info.
177
	 *
178
	 * @since 1.0.0
179
	 *
180
	 * @param array $package_info  {
181
	 *                             Attributes of the package_info.
182
	 *
183
	 * @type int $pid              Package ID. Default 0.
184
	 * @type int $days             Package validity in Days. Default 0.
185
	 * @type int $amount           Package amount. Default 0.
186
	 * @type int $is_featured      Is this featured package? Default 0.
187
	 * @type string $image_limit   Image limit for this package. Default "".
188
	 * @type int $google_analytics Add analytics to this package. Default 1.
189
	 * @type int $sendtofriend     Send to friend. Default 1.
190
	 *
191
	 * }
192
	 * @param object|string $post  The post object.
193
	 * @param string $post_type    The post type.
194
	 */
195 6
	return (object) apply_filters( 'geodir_post_package_info', $package_info, $post, $post_type );
196
197
}
198
199
200
/**
201
 * Send enquiry to listing author
202
 *
203
 * This function let the user to send Enquiry to listing author. If listing author email not available, then admin
204
 * email will be used. Email content will be used WP Admin -> Geodirectory -> Notifications -> Other Emails -> Email
205
 * enquiry
206
 *
207
 * @since   1.0.0
208
 * @package GeoDirectory
209
 * @global object $wpdb    WordPress Database object.
210
 *
211
 * @param array $request   {
212
 *                         The submitted form fields as an array.
213
 *
214
 * @type string $sendact   Enquiry type. Default "send_inqury".
215
 * @type string $pid       Post ID.
216
 * @type string $inq_name  Sender name.
217
 * @type string $inq_email Sender mail.
218
 * @type string $inq_phone Sender phone.
219
 * @type string $inq_msg   Email message.
220
 *
221
 * }
222
 */
223
function geodir_send_inquiry( $request ) {
224
	global $wpdb;
225
226
	// strip slashes from text
227
	$request = ! empty( $request ) ? stripslashes_deep( $request ) : $request;
228
229
	$yourname      = sanitize_text_field($request['inq_name']);
230
	$youremail     = sanitize_email($request['inq_email']);
231
	$inq_phone     = sanitize_text_field($request['inq_phone']);
232
	$frnd_comments = sanitize_text_field($request['inq_msg']);
233
	$pid           = absint($request['pid']);
234
235
	$author_id  = '';
236
237
	if ( $pid && 'publish' == get_post_status ( $pid  ) ) {
238
239
		check_ajax_referer( 'send_inquiry_'.$pid );
240
241
		$p_post = get_post($pid);
242
243
		$author_id  = $p_post->post_author;
244
245
	}else{
246
		gd_die();
247
	}
248
249
	$to_email  = geodir_get_post_meta( $pid, 'geodir_email', true );
250
	$to_name   = geodir_get_client_name( $author_id );
251
252
	if ( $to_email == '' ) {
253
		$to_email = get_option( 'admin_email' );
254
	}
255
256
	/**
257
	 * Called after the send enquiry var have been set but before the email has been sent.
258
	 *
259
	 * @since 1.0.0
260
	 *
261
	 * @param array $request   {
262
	 *                         The submitted form fields as an array.
263
	 *
264
	 * @type string $sendact   Enquiry type. Default "send_inqury".
265
	 * @type string $pid       Post ID.
266
	 * @type string $inq_name  Sender name.
267
	 * @type string $inq_email Sender mail.
268
	 * @type string $inq_phone Sender phone.
269
	 * @type string $inq_msg   Email message.
270
	 *
271
	 * }
272
	 * @param string $type     The form type, default: `Enquiry`.
273
	 */
274
	do_action( 'geodir_after_send_enquiry', $request, 'Enquiry' );
275
276
	$client_message = $frnd_comments;
277
	$client_message .= '<br>' . __( 'From :', 'geodirectory' ) . ' ' . $yourname . '<br>' . __( 'Phone :', 'geodirectory' ) . ' ' . $inq_phone . '<br>' . __( 'Email :', 'geodirectory' ) . ' ' . $youremail . '<br><br>' . __( 'Sent from', 'geodirectory' ) . ' - <b><a href="' . trailingslashit( home_url() ) . '">' . get_option( 'blogname' ) . '</a></b>.';
278
	/**
279
	 * Filter client message text.
280
	 *
281
	 * @since 1.0.0
282
	 *
283
	 * @param string $client_message Client message text.
284
	 */
285
	$client_message = apply_filters( 'geodir_inquiry_email_msg', $client_message );
286
287
	/**
288
	 * Called before the send enquiry email is sent.
289
	 *
290
	 * @since 1.0.0
291
	 *
292
	 * @param array $request   {
293
	 *                         The submitted form fields as an array.
294
	 *
295
	 * @type string $sendact   Enquiry type. Default "send_inqury".
296
	 * @type string $pid       Post ID.
297
	 * @type string $inq_name  Sender name.
298
	 * @type string $inq_email Sender mail.
299
	 * @type string $inq_phone Sender phone.
300
	 * @type string $inq_msg   Email message.
301
	 *
302
	 * }
303
	 */
304
	do_action( 'geodir_before_send_enquiry_email', $request );
305
	if ( $to_email ) {
306
		// strip slashes message
307
		$client_message = stripslashes_deep( $client_message );
308
309
		geodir_sendEmail( $youremail, $yourname, $to_email, $to_name, '', $client_message, $extra = '', 'send_enquiry', $request['pid'] );//To client email
310
	}
311
312
	/**
313
	 * Called after the send enquiry email is sent.
314
	 *
315
	 * @since 1.0.0
316
	 *
317
	 * @param array $request   {
318
	 *                         The submitted form fields as an array.
319
	 *
320
	 * @type string $sendact   Enquiry type. Default "send_inqury".
321
	 * @type string $pid       Post ID.
322
	 * @type string $inq_name  Sender name.
323
	 * @type string $inq_email Sender mail.
324
	 * @type string $inq_phone Sender phone.
325
	 * @type string $inq_msg   Email message.
326
	 *
327
	 * }
328
	 */
329
	do_action( 'geodir_after_send_enquiry_email', $request );
330
	$url = get_permalink( $pid );
331 View Code Duplication
	if ( strstr( $url, '?' ) ) {
332
		$url = $url . "&send_inquiry=success";
333
	} else {
334
		$url = $url . "?send_inquiry=success";
335
	}
336
	/**
337
	 * Filter redirect url after the send enquiry email is sent.
338
	 *
339
	 * @since 1.0.0
340
	 *
341
	 * @param string $url Redirect url.
342
	 */
343
	$url = apply_filters( 'geodir_send_enquiry_after_submit_redirect', $url );
344
	wp_redirect( $url );
345
	gd_die();
346
347
}
348
349
/**
350
 * Send Email to a friend.
351
 *
352
 * This function let the user to send Email to a friend.
353
 * Email content will be used WP Admin -> Geodirectory -> Notifications -> Other Emails -> Send to friend
354
 *
355
 * @since   1.0.0
356
 * @package GeoDirectory
357
 *
358
 * @param array $request       {
359
 *                             The submitted form fields as an array.
360
 *
361
 * @type string $sendact       Enquiry type. Default "email_frnd".
362
 * @type string $pid           Post ID.
363
 * @type string $to_name       Friend name.
364
 * @type string $to_email      Friend email.
365
 * @type string $yourname      Sender name.
366
 * @type string $youremail     Sender email.
367
 * @type string $frnd_subject  Email subject.
368
 * @type string $frnd_comments Email Message.
369
 *
370
 * }
371
 * @global object $wpdb        WordPress Database object.
372
 */
373
function geodir_send_friend( $request ) {
374
	global $wpdb;
375
376
	// strip slashes from text
377
	$request = ! empty( $request ) ? stripslashes_deep( $request ) : $request;
378
379
	$yourname      = sanitize_text_field($request['yourname']);
380
	$youremail     = sanitize_email($request['youremail']);
381
	$frnd_subject  = sanitize_text_field($request['frnd_subject']);
382
	$frnd_comments = sanitize_text_field($request['frnd_comments']);
383
	$pid           = absint($request['pid']);
384
	$to_email      = sanitize_email($request['to_email']);
385
	$to_name       = sanitize_text_field($request['to_name']);
386
	if ( $pid && 'publish' == get_post_status ( $pid  ) ) {
387
388
		check_ajax_referer( 'send_to_frnd_'.$pid );
389
390
	}else{
391
		gd_die();
392
	}
393
394
	/**
395
	 * Called before the send to friend email is sent.
396
	 *
397
	 * @since 1.0.0
398
	 *
399
	 * @param array $request       {
400
	 *                             The submitted form fields as an array.
401
	 *
402
	 * @type string $sendact       Enquiry type. Default "email_frnd".
403
	 * @type string $pid           Post ID.
404
	 * @type string $to_name       Friend name.
405
	 * @type string $to_email      Friend email.
406
	 * @type string $yourname      Sender name.
407
	 * @type string $youremail     Sender email.
408
	 * @type string $frnd_subject  Email subject.
409
	 * @type string $frnd_comments Email Message.
410
	 *
411
	 * }
412
	 */
413
	do_action( 'geodir_before_send_to_friend_email', $request );
414
	geodir_sendEmail( $youremail, $yourname, $to_email, $to_name, $frnd_subject, $frnd_comments, $extra = '', 'send_friend', $request['pid'] );//To client email
415
416
	/**
417
	 * Called after the send to friend email is sent.
418
	 *
419
	 * @since 1.0.0
420
	 *
421
	 * @param array $request       {
422
	 *                             The submitted form fields as an array.
423
	 *
424
	 * @type string $sendact       Enquiry type. Default "email_frnd".
425
	 * @type string $pid           Post ID.
426
	 * @type string $to_name       Friend name.
427
	 * @type string $to_email      Friend email.
428
	 * @type string $yourname      Sender name.
429
	 * @type string $youremail     Sender email.
430
	 * @type string $frnd_subject  Email subject.
431
	 * @type string $frnd_comments Email Message.
432
	 *
433
	 * }
434
	 */
435
	do_action( 'geodir_after_send_to_friend_email', $request );
436
437
	$url = get_permalink( $pid );
438 View Code Duplication
	if ( strstr( $url, '?' ) ) {
439
		$url = $url . "&sendtofrnd=success";
440
	} else {
441
		$url = $url . "?sendtofrnd=success";
442
	}
443
	/**
444
	 * Filter redirect url after the send to friend email is sent.
445
	 *
446
	 * @since 1.0.0
447
	 *
448
	 * @param string $url Redirect url.
449
	 */
450
	$url = apply_filters( 'geodir_send_to_friend_after_submit_redirect', $url );
451
	wp_redirect( $url );
452
	gd_die();
453
}
454
455
/**
456
 * Adds open div before the tab content.
457
 *
458
 * This function adds open div before the tab content like post information, post images, reviews etc.
459
 *
460
 * @since   1.0.0
461
 * @package GeoDirectory
462
 *
463
 * @param string $hash_key
464
 */
465
function geodir_before_tab_content( $hash_key ) {
466
	switch ( $hash_key ) {
467
		case 'post_info' :
468
			echo '<div class="geodir-company_info field-group">';
469
			break;
470
		case 'post_images' :
471
			/**
472
			 * Filter post gallery HTML id.
473
			 *
474
			 * @since 1.0.0
475
			 */
476
			echo ' <div id="' . apply_filters( 'geodir_post_gallery_id', 'geodir-post-gallery' ) . '" class="clearfix" >';
477
			break;
478
		case 'reviews' :
479
			echo '<div id="reviews-wrap" class="clearfix"> ';
480
			break;
481
		case 'post_video':
482
			echo ' <div id="post_video-wrap" class="clearfix">';
483
			break;
484
		case 'special_offers':
485
			echo '<div id="special_offers-wrap" class="clearfix">';
486
			break;
487
	}
488
}
489
490
/**
491
 * Adds closing div after the tab content.
492
 *
493
 * This function adds closing div after the tab content like post information, post images, reviews etc.
494
 *
495
 * @since   1.0.0
496
 * @package GeoDirectory
497
 *
498
 * @param string $hash_key
499
 */
500
function geodir_after_tab_content( $hash_key ) {
501
	switch ( $hash_key ) {
502
		case 'post_info' :
503
			echo '</div>';
504
			break;
505
		case 'post_images' :
506
			echo '</div>';
507
			break;
508
		case 'reviews' :
509
			echo '</div>';
510
			break;
511
		case 'post_video':
512
			echo '</div>';
513
			break;
514
		case 'special_offers':
515
			echo '</div>';
516
			break;
517
	}
518
}
519
520
521
/**
522
 * Returns default sorting order of a post type.
523
 *
524
 * @since   1.0.0
525
 * @package GeoDirectory
526
 *
527
 * @param string $post_type The post type.
528
 *
529
 * @global object $wpdb     WordPress Database object.
530
 * @return bool|null|string Returns default sort results, when the post type is valid. Otherwise returns false.
531
 */
532 View Code Duplication
function geodir_get_posts_default_sort( $post_type ) {
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...
533
534
	global $wpdb;
535
536
	if ( $post_type != '' ) {
537
538
		$all_postypes = geodir_get_posttypes();
539
540
		if ( ! in_array( $post_type, $all_postypes ) ) {
541
			return false;
542
		}
543
544
		$sort_field_info = $wpdb->get_var( $wpdb->prepare( "select default_order from " . GEODIR_CUSTOM_SORT_FIELDS_TABLE . " where	post_type= %s and is_active=%d and is_default=%d", array(
545
			$post_type,
546
			1,
547
			1
548
		) ) );
549
550
		if ( ! empty( $sort_field_info ) ) {
551
			return $sort_field_info;
552
		}
553
554
	}
555
556
}
557
558
559
/**
560
 * Returns sort options of a post type.
561
 *
562
 * @since   1.0.0
563
 * @package GeoDirectory
564
 *
565
 * @param string $post_type The post type.
566
 *
567
 * @global object $wpdb     WordPress Database object.
568
 * @return bool|mixed|void Returns sort results, when the post type is valid. Otherwise returns false.
569
 */
570 View Code Duplication
function geodir_get_sort_options( $post_type ) {
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...
571
	global $wpdb;
572
573
	if ( $post_type != '' ) {
574
		$all_postypes = geodir_get_posttypes();
575
576
		if ( ! in_array( $post_type, $all_postypes ) ) {
577
			return false;
578
		}
579
580
		$sort_field_info = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . GEODIR_CUSTOM_SORT_FIELDS_TABLE . " WHERE post_type=%s AND is_active=%d AND (sort_asc=1 || sort_desc=1 || field_type='random') AND field_type != 'address' ORDER BY sort_order ASC", array(
581
			$post_type,
582
			1
583
		) ) );
584
585
		/**
586
		 * Filter post sort options.
587
		 *
588
		 * @since 1.0.0
589
		 *
590
		 * @param array $sort_field_info Unfiltered sort field array.
591
		 * @param string $post_type      Post type.
592
		 */
593
		return apply_filters( 'geodir_get_sort_options', $sort_field_info, $post_type );
594
	}
595
596
}
597
598
599
/**
600
 * Display list of sort options available in front end using dropdown.
601
 *
602
 * @since   1.0.0
603
 * @package GeoDirectory
604
 * @global object $wp_query WordPress Query object.
605
 */
606
function geodir_display_sort_options() {
607
	global $wp_query;
608
609
	/**
610
	 * On search pages there should be no sort options, sorting is done by search criteria.
611
	 *
612
	 * @since 1.4.4
613
	 */
614
	if ( is_search() ) {
615
		return;
616
	}
617
618
	$sort_by = '';
619
620
	if ( isset( $_REQUEST['sort_by'] ) ) {
621
		$sort_by = $_REQUEST['sort_by'];
622
	}
623
624
	$gd_post_type = geodir_get_current_posttype();
625
626
	$sort_options = geodir_get_sort_options( $gd_post_type );
627
628
629
	$sort_field_options = '';
630
631
	if ( ! empty( $sort_options ) ) {
632
		foreach ( $sort_options as $sort ) {
633
			$sort = stripslashes_deep( $sort ); // strip slashes
634
635
			$label = __( $sort->site_title, 'geodirectory' );
636
637
			if ( $sort->field_type == 'random' ) {
638
				$key = $sort->field_type;
639
				( $sort_by == $key || ( $sort->is_default == '1' && ! isset( $_REQUEST['sort_by'] ) ) ) ? $selected = 'selected="selected"' : $selected = '';
640
				$sort_field_options .= '<option ' . $selected . ' value="' . esc_url( add_query_arg( 'sort_by', $key ) ) . '">' . __( $label, 'geodirectory' ) . '</option>';
641
			}
642
643
			if ( $sort->htmlvar_name == 'comment_count' ) {
644
				$sort->htmlvar_name = 'rating_count';
645
			}
646
647 View Code Duplication
			if ( $sort->sort_asc ) {
648
				$key   = $sort->htmlvar_name . '_asc';
649
				$label = $sort->site_title;
650
				if ( $sort->asc_title ) {
651
					$label = $sort->asc_title;
652
				}
653
				( $sort_by == $key || ( $sort->is_default == '1' && $sort->default_order == $key && ! isset( $_REQUEST['sort_by'] ) ) ) ? $selected = 'selected="selected"' : $selected = '';
654
				$sort_field_options .= '<option ' . $selected . ' value="' . esc_url( add_query_arg( 'sort_by', $key ) ) . '">' . __( $label, 'geodirectory' ) . '</option>';
655
			}
656
657 View Code Duplication
			if ( $sort->sort_desc ) {
658
				$key   = $sort->htmlvar_name . '_desc';
659
				$label = $sort->site_title;
660
				if ( $sort->desc_title ) {
661
					$label = $sort->desc_title;
662
				}
663
				( $sort_by == $key || ( $sort->is_default == '1' && $sort->default_order == $key && ! isset( $_REQUEST['sort_by'] ) ) ) ? $selected = 'selected="selected"' : $selected = '';
664
				$sort_field_options .= '<option ' . $selected . ' value="' . esc_url( add_query_arg( 'sort_by', $key ) ) . '">' . __( $label, 'geodirectory' ) . '</option>';
665
			}
666
667
		}
668
	}
669
670
	if ( $sort_field_options != '' ) {
671
672
		?>
673
674
		<div class="geodir-tax-sort">
675
676
			<select name="sort_by" id="sort_by" onchange="javascript:window.location=this.value;">
677
678
				<option
679
					value="<?php echo esc_url( add_query_arg( 'sort_by', '' ) ); ?>" <?php if ( $sort_by == '' ) {
680
					echo 'selected="selected"';
681
				} ?>><?php _e( 'Sort By', 'geodirectory' ); ?></option><?php
682
683
				echo $sort_field_options; ?>
684
685
			</select>
686
687
		</div>
688
		<?php
689
690
	}
691
692
}
693
694
695
/**
696
 * Removes the section title
697
 *
698
 * Removes the section title "Posts sort options", if the custom field type is multiselect or textarea or taxonomy.
699
 * Can be found here. WP Admin -> Geodirectory -> Place settings -> Custom fields
700
 *
701
 * @since   1.0.0
702
 * @package GeoDirectory
703
 *
704
 * @param string $title      The section title.
705
 * @param string $field_type The field type.
706
 *
707
 * @return string Returns the section title.
708
 */
709
function geodir_advance_customfields_heading( $title, $field_type ) {
710
711 1
	if ( in_array( $field_type, array( 'multiselect', 'textarea', 'taxonomy' ) ) ) {
712
		$title = '';
713
	}
714
715 1
	return $title;
716
}
717
718
719
/**
720
 * Returns related listings of a listing.
721
 *
722
 * @since   1.0.0
723
 * @package GeoDirectory
724
 *
725
 * @param array $request            Related posts request array.
726
 *
727
 * @global object $wpdb             WordPress Database object.
728
 * @global object $post             The current post object.
729
 * @global string $gridview_columns The girdview style of the listings.
730
 * @global object $gd_session       GeoDirectory Session object.
731
 * @return string Returns related posts html.
732
 */
733
function geodir_related_posts_display( $request ) {
734
	if ( ! empty( $request ) ) {
735
		$before_title = ( isset( $request['before_title'] ) && ! empty( $request['before_title'] ) ) ? $request['before_title'] : '';
736
		$after_title  = ( isset( $request['after_title'] ) && ! empty( $request['after_title'] ) ) ? $request['after_title'] : '';
737
738
		$title               = ( isset( $request['title'] ) && ! empty( $request['title'] ) ) ? $request['title'] : __( 'Related Listings', 'geodirectory' );
739
		$post_number         = ( isset( $request['post_number'] ) && ! empty( $request['post_number'] ) ) ? $request['post_number'] : '5';
740
		$relate_to           = ( isset( $request['relate_to'] ) && ! empty( $request['relate_to'] ) ) ? $request['relate_to'] : 'category';
741
		$layout              = ( isset( $request['layout'] ) && ! empty( $request['layout'] ) ) ? $request['layout'] : 'gridview_onehalf';
742
		$add_location_filter = ( isset( $request['add_location_filter'] ) && ! empty( $request['add_location_filter'] ) ) ? $request['add_location_filter'] : '0';
743
		$listing_width       = ( isset( $request['listing_width'] ) && ! empty( $request['listing_width'] ) ) ? $request['listing_width'] : '';
744
		$list_sort           = ( isset( $request['list_sort'] ) && ! empty( $request['list_sort'] ) ) ? $request['list_sort'] : 'latest';
745
		$character_count     = ( isset( $request['character_count'] ) && ! empty( $request['character_count'] ) ) ? $request['character_count'] : '';
746
747
		global $wpdb, $post, $gd_session, $related_nearest, $related_parent_lat, $related_parent_lon;
748
		$related_parent_lat   = !empty($post->post_latitude) ? $post->post_latitude : '';
749
		$related_parent_lon   = !empty($post->post_longitude) ? $post->post_longitude : '';
750
		$arr_detail_page_tabs = geodir_detail_page_tabs_list();
751
752
		$related_listing_array = array();
753
		if ( get_option( 'geodir_add_related_listing_posttypes' ) ) {
754
			$related_listing_array = get_option( 'geodir_add_related_listing_posttypes' );
755
		}
756
		if ( isset($post->post_type) && in_array( $post->post_type, $related_listing_array ) ) {
757
			$arr_detail_page_tabs['related_listing']['is_display'] = true;
758
		}
759
760
		$is_display        = $arr_detail_page_tabs['related_listing']['is_display'];
761
		$origi_post        = $post;
762
		$post_type         = '';
763
		$post_id           = '';
764
		$category_taxonomy = '';
765
		$tax_field         = 'id';
766
		$category          = array();
767
768
		if ( isset( $_REQUEST['backandedit'] ) ) {
769
			$post      = (object) $gd_session->get( 'listing' );
770
			$post_type = $post->listing_type;
771
			if ( isset( $_REQUEST['pid'] ) && $_REQUEST['pid'] != '' ) {
772
				$post_id = $_REQUEST['pid'];
773
			}
774 View Code Duplication
		} elseif ( isset( $_REQUEST['pid'] ) && $_REQUEST['pid'] != '' ) {
775
			$post      = geodir_get_post_info( $_REQUEST['pid'] );
776
			$post_type = $post->post_type;
777
			$post_id   = $_REQUEST['pid'];
778
		} elseif ( isset( $post->post_type ) && $post->post_type != '' ) {
779
			$post_type = $post->post_type;
780
			$post_id   = $post->ID;
781
		}
782
783
		if ( $relate_to == 'category' ) {
784
785
			$category_taxonomy = $post_type . $relate_to;
786
			if ( isset( $post->{$category_taxonomy} ) && $post->{$category_taxonomy} != '' ) {
787
				$category = explode( ',', trim( $post->{$category_taxonomy}, ',' ) );
788
			}
789
790
		} elseif ( $relate_to == 'tags' ) {
791
792
			$category_taxonomy = $post_type . '_' . $relate_to;
793
			if ( $post->post_tags != '' ) {
794
				$category = explode( ',', trim( $post->post_tags, ',' ) );
795
			}
796
			$tax_field = 'name';
797
		}
798
799
		/* --- return false in invalid request --- */
800
		if ( empty( $category ) ) {
801
			return false;
802
		}
803
804
		$all_postypes = geodir_get_posttypes();
805
806
		if ( ! in_array( $post_type, $all_postypes ) ) {
807
			return false;
808
		}
809
810
		/* --- return false in invalid request --- */
811
812
		$location_url = '';
813
		if ( $add_location_filter != '0' ) {
814
			$location_url             = array();
815
			$geodir_show_location_url = get_option( 'geodir_show_location_url' );
816
817
			$gd_city = get_query_var( 'gd_city' );
818
819
			if ( $gd_city ) {
820
				$gd_country = get_query_var( 'gd_country' );
821
				$gd_region  = get_query_var( 'gd_region' );
822
			} else {
823
				$location = geodir_get_default_location();
824
825
				$gd_country = isset( $location->country_slug ) ? $location->country_slug : '';
826
				$gd_region  = isset( $location->region_slug ) ? $location->region_slug : '';
827
				$gd_city    = isset( $location->city_slug ) ? $location->city_slug : '';
828
			}
829
830
			if ( $geodir_show_location_url == 'all' ) {
831
				$location_url[] = $gd_country;
832
				$location_url[] = $gd_region;
833
			} else if ( $geodir_show_location_url == 'country_city' ) {
834
				$location_url[] = $gd_country;
835
			} else if ( $geodir_show_location_url == 'region_city' ) {
836
				$location_url[] = $gd_region;
837
			}
838
839
			$location_url[] = $gd_city;
840
841
			$location_url = implode( '/', $location_url );
842
		}
843
844
845
		if ( ! empty( $category ) ) {
846
			global $geodir_add_location_url;
847
			$geodir_add_location_url = '0';
848
			if ( $add_location_filter != '0' ) {
849
				$geodir_add_location_url = '1';
850
			}
851
			$viewall_url             = get_term_link( (int) $category[0], $post_type . $category_taxonomy );
852
			$geodir_add_location_url = null;
853
		}
854
		ob_start();
855
		?>
856
857
858
		<div class="geodir_locations geodir_location_listing">
859
860
			<?php
861
			if ( isset( $request['is_widget'] ) && $request['is_widget'] == '1' ) {
862
				/** geodir_before_title filter Documented in geodirectory_widgets.php */
863
				$before_title = isset( $before_title ) ? $before_title : apply_filters( 'geodir_before_title', '<h3 class="widget-title">' );
864
				/** geodir_after_title filter Documented in geodirectory_widgets.php */
865
				$after_title = isset( $after_title ) ? $after_title : apply_filters( 'geodir_after_title', '</h3>' );
866
				?>
867
				<div class="location_list_heading clearfix">
868
					<?php echo $before_title . $title . $after_title; ?>
869
				</div>
870
				<?php
871
			}
872
			$query_args = array(
873
				'posts_per_page'   => $post_number,
874
				'is_geodir_loop'   => true,
875
				'gd_location'      => ( $add_location_filter ) ? true : false,
876
				'post_type'        => $post_type,
877
				'order_by'         => $list_sort,
878
				'post__not_in'     => array( $post_id ),
879
				'excerpt_length'   => $character_count,
880
				'related_listings' => $is_display
881
			);
882
883
			$tax_query = array(
884
				'taxonomy' => $category_taxonomy,
885
				'field'    => $tax_field,
886
				'terms'    => $category
887
			);
888
889
			$query_args['tax_query'] = array( $tax_query );
890
891
			global $gridview_columns, $post;
892
893
			/**
894
			 * Filters related listing query args.
895
			 *
896
			 * @since 1.6.11
897
			 *
898
			 * @param array $query_args The query array.
899
			 * @param array $request Related posts request array.
900
			 */
901
			$query_args = apply_filters( 'geodir_related_posts_widget_query_args', $query_args, $request );
902
903
			query_posts( $query_args );
904
905 View Code Duplication
			if ( strstr( $layout, 'gridview' ) ) {
906
				$listing_view_exp = explode( '_', $layout );
907
				$gridview_columns = $layout;
908
				$layout           = $listing_view_exp[0];
909
			} else if ( $layout == 'list' ) {
910
				$gridview_columns = '';
911
			}
912
			$related_posts = true;
913
914
			$related_nearest = false;
915
			if ( $list_sort == 'nearest' ) {
916
				$related_nearest = true;
917
			}
918
919
920
			/**
921
			 * Filters related listing listview template.
922
			 *
923
			 * @since 1.0.0
924
			 */
925
			$template = apply_filters( "geodir_template_part-related-listing-listview", geodir_locate_template( 'listing-listview' ) );
926
927
			/**
928
			 * Includes related listing listview template.
929
			 *
930
			 * @since 1.0.0
931
			 */
932
			include( $template );
933
934
			wp_reset_query();
935
			$post            = $origi_post;
936
			$related_nearest = false;
937
			?>
938
939
		</div>
940
		<?php
941
		return $html = ob_get_clean();
0 ignored issues
show
Unused Code introduced by
$html 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...
942
943
	}
944
945
}
946
947
948
//add_action('wp_footer', 'geodir_category_count_script', 10);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
949
/**
950
 * Adds the category post count javascript code
951
 *
952
 * @since       1.0.0
953
 * @package     GeoDirectory
954
 * @global string $geodir_post_category_str The geodirectory post category.
955
 * @depreciated No longer needed.
956
 */
957
function geodir_category_count_script() {
958
	global $geodir_post_category_str;
959
960
	if ( ! empty( $geodir_post_category_str ) ) {
961
		$geodir_post_category_str = serialize( $geodir_post_category_str );
962
	}
963
964
	$all_var['post_category_array'] = html_entity_decode( (string) $geodir_post_category_str, ENT_QUOTES, 'UTF-8' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_var was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_var = 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...
965
	$script                         = "var post_category_array = " . json_encode( $all_var ) . ';';
966
	echo '<script>';
967
	echo $script;
968
	echo '</script>';
969
970
}
971
972
/**
973
 * Returns the default language of the map.
974
 *
975
 * @since   1.0.0
976
 * @package GeoDirectory
977
 * @return string Returns the default language.
978
 */
979
function geodir_get_map_default_language() {
980
	$geodir_default_map_language = get_option( 'geodir_default_map_language' );
981
	if ( empty( $geodir_default_map_language ) ) {
982
		$geodir_default_map_language = 'en';
983
	}
984
985
	/**
986
	 * Filter default map language.
987
	 *
988
	 * @since 1.0.0
989
	 *
990
	 * @param string $geodir_default_map_language Default map language.
991
	 */
992
	return apply_filters( 'geodir_default_map_language', $geodir_default_map_language );
993
}
994
995
/**
996
 * Returns the Google maps api key.
997
 *
998
 * @since   1.6.4
999
 * @package GeoDirectory
1000
 * @return string Returns the api key.
1001
 */
1002
function geodir_get_map_api_key() {
1003 1
	$key = get_option( 'geodir_google_api_key' );
1004
1005
	/**
1006
	 * Filter Google maps api key.
1007
	 *
1008
	 * @since 1.6.4
1009
	 *
1010
	 * @param string $key Google maps api key.
1011
	 */
1012 1
	return apply_filters( 'geodir_google_api_key', $key );
1013
}
1014
1015
1016
/**
1017
 * Adds meta keywords and description for SEO.
1018
 *
1019
 * @since   1.0.0
1020
 * @since   1.5.4 Modified to replace %location% from meta when Yoast SEO plugin active.
1021
 * @since   1.6.18 Option added to disable overwrite by Yoast SEO titles & metas on GD pages.
1022
 * @package GeoDirectory
1023
 * @global object $wpdb             WordPress Database object.
1024
 * @global object $post             The current post object.
1025
 * @global object $wp_query         WordPress Query object.
1026
 * @global array $geodir_addon_list List of active GeoDirectory extensions.
1027
 */
1028
function geodir_add_meta_keywords() {
1029
	global $wp, $post, $wp_query, $wpdb, $geodir_addon_list;
1030
1031
	$is_geodir_page = geodir_is_geodir_page();
1032
	if ( ! $is_geodir_page ) {
1033
		return;
1034
	}// if non GD page, bail
1035
1036
	$use_gd_meta = true;
1037
	if ( ( class_exists( 'WPSEO_Frontend' ) || class_exists( 'All_in_One_SEO_Pack' ) ) && !geodir_disable_yoast_seo_metas() ) {
1038
		$use_gd_meta = false;
1039
1040
		if ( geodir_is_page( 'search' ) ) {
1041
			$use_gd_meta = true;
1042
		}
1043
	}
1044
1045
	if ( ! $use_gd_meta ) {
1046
		return;
1047
	}// bail if Yoast Wordpress SEO or All_in_One_SEO_Pack active.
1048
1049
	$current_term = $wp_query->get_queried_object();
1050
1051
	$all_postypes = geodir_get_posttypes();
1052
1053
	$geodir_taxonomies = geodir_get_taxonomies( '', true );
1054
1055
	$meta_desc = '';
1056
	$meta_key  = '';
1057
	if ( isset( $current_term->ID ) && $current_term->ID == geodir_location_page_id() ) {
1058
		/**
1059
		 * Filter SEO meta location description.
1060
		 *
1061
		 * @since 1.0.0
1062
		 */
1063
		$meta_desc = apply_filters( 'geodir_seo_meta_location_description', '' );
1064
		$meta_desc .= '';
1065
	}
1066
	if ( have_posts() && is_single() OR is_page() ) {
1067
		while ( have_posts() ) {
1068
			the_post();
1069
1070
			if ( has_excerpt() ) {
1071
				$out_excerpt = strip_tags( strip_shortcodes( get_the_excerpt() ) );
1072
				if ( empty( $out_excerpt ) ) {
1073
					$out_excerpt = strip_tags( do_shortcode( get_the_excerpt() ) );
1074
				}
1075
				$out_excerpt = str_replace( array( "\r\n", "\r", "\n" ), "", $out_excerpt );
1076
			} else {
1077
				$out_excerpt = str_replace( array( "\r\n", "\r", "\n" ), "", $post->post_content );
1078
				$out_excerpt = strip_tags( strip_shortcodes( $out_excerpt ) );
1079
				if ( empty( $out_excerpt ) ) {
1080
					$out_excerpt = strip_tags( do_shortcode( $out_excerpt ) ); // parse short code from content
1081
				}
1082
				$out_excerpt = trim( wp_trim_words( $out_excerpt, 35, '' ), '.!?,;:-' );
1083
			}
1084
1085
			$meta_desc .= $out_excerpt;
1086
		}
1087
	} elseif ( ( is_category() || is_tag() ) && isset( $current_term->taxonomy ) && in_array( $current_term->taxonomy, $geodir_taxonomies ) ) {
1088
		if ( is_category() ) {
1089
			$meta_desc .= __( "Posts related to Category:", 'geodirectory' ) . " " . geodir_utf8_ucfirst( single_cat_title( "", false ) );
1090
		} elseif ( is_tag() ) {
1091
			$meta_desc .= __( "Posts related to Tag:", 'geodirectory' ) . " " . geodir_utf8_ucfirst( single_tag_title( "", false ) );
1092
		}
1093
	} elseif ( isset( $current_term->taxonomy ) && in_array( $current_term->taxonomy, $geodir_taxonomies ) ) {
1094
		$meta_desc .= isset( $current_term->description ) ? $current_term->description : '';
1095
	}
1096
1097
1098
	$geodir_post_type       = geodir_get_current_posttype();
1099
	$geodir_post_type_info  = get_post_type_object( $geodir_post_type );
1100
	$geodir_is_page_listing = geodir_is_page( 'listing' ) ? true : false;
1101
1102
	$category_taxonomy = geodir_get_taxonomies( $geodir_post_type );
1103
	$tag_taxonomy      = geodir_get_taxonomies( $geodir_post_type, true );
1104
1105
	$geodir_is_category = isset( $category_taxonomy[0] ) && get_query_var( $category_taxonomy[0] ) ? get_query_var( $category_taxonomy[0] ) : false;
1106
	$geodir_is_tag      = isset( $tag_taxonomy[0] ) && get_query_var( $tag_taxonomy[0] ) ? true : false;
1107
1108
	$geodir_is_search        = geodir_is_page( 'search' ) ? true : false;
1109
	$geodir_is_location      = geodir_is_page( 'location' ) ? true : false;
0 ignored issues
show
Unused Code introduced by
$geodir_is_location 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...
1110
	$geodir_location_manager = isset( $geodir_addon_list['geodir_location_manager'] ) && $geodir_addon_list['geodir_location_manager'] = 'yes' ? true : false;
1111
	$godir_location_terms    = geodir_get_current_location_terms( 'query_vars' );
1112
	$gd_city                 = $geodir_location_manager && isset( $godir_location_terms['gd_city'] ) ? $godir_location_terms['gd_city'] : null;
1113
	$gd_region               = $geodir_location_manager && isset( $godir_location_terms['gd_region'] ) ? $godir_location_terms['gd_region'] : null;
0 ignored issues
show
Unused Code introduced by
$gd_region 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...
1114
	$gd_country              = $geodir_location_manager && isset( $godir_location_terms['gd_country'] ) ? $godir_location_terms['gd_country'] : null;
0 ignored issues
show
Unused Code introduced by
$gd_country 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...
1115
	/**
1116
	 * Filter the Everywhere text in location description.
1117
	 *
1118
	 * @since 1.6.22
1119
	 *
1120
	 * @param string $replace_location Everywhere text.
1121
	 */
1122
	$replace_location        = apply_filters( 'geodir_location_description_everywhere_text', __( 'Everywhere', 'geodirectory' ) );
1123
	$location_id             = null;
1124
	if ( $geodir_location_manager ) {
1125
		$sql           = $wpdb->prepare( "SELECT location_id FROM " . POST_LOCATION_TABLE . " WHERE city_slug=%s ORDER BY location_id ASC LIMIT 1", array( $gd_city ) );
1126
		$location_id   = (int) $wpdb->get_var( $sql );
1127
		$location_type = geodir_what_is_current_location();
1128
		if ( $location_type == 'city' ) {
1129
			$replace_location = geodir_get_current_location( array( 'what' => 'city', 'echo' => false ) );
1130
		} elseif ( $location_type == 'region' ) {
1131
			$replace_location = geodir_get_current_location( array( 'what' => 'region', 'echo' => false ) );
1132
		} elseif ( $location_type == 'country' ) {
1133
			$replace_location = geodir_get_current_location( array( 'what' => 'country', 'echo' => false ) );
1134
			$replace_location = __( $replace_location, 'geodirectory' );
1135
		}
1136
		$country          = get_query_var( 'gd_country' );
1137
		$region           = get_query_var( 'gd_region' );
1138
		$city             = get_query_var( 'gd_city' );
1139
		$current_location = '';
1140
		if ( $country != '' ) {
1141
			$current_location = get_actual_location_name( 'country', $country, true );
1142
		}
1143
		if ( $region != '' ) {
1144
			$current_location = get_actual_location_name( 'region', $region );
1145
		}
1146
		if ( $city != '' ) {
1147
			$current_location = get_actual_location_name( 'city', $city );
1148
		}
1149
		$replace_location = $current_location != '' ? $current_location : $replace_location;
1150
	}
1151
1152
	$geodir_meta_keys = '';
1153
	$geodir_meta_desc = '';
1154
	if ( $is_geodir_page && ! empty( $geodir_post_type_info ) ) {
1155
		if ( $geodir_is_page_listing || $geodir_is_search || geodir_is_page( 'add-listing' ) ) {
1156
			$geodir_meta_keys = isset( $geodir_post_type_info->seo['meta_keyword'] ) && $geodir_post_type_info->seo['meta_keyword'] != '' ? $geodir_post_type_info->seo['meta_keyword'] : $geodir_meta_keys;
1157
1158
			$geodir_meta_desc = isset( $geodir_post_type_info->description ) ? $geodir_post_type_info->description : $geodir_meta_desc;
1159
			$geodir_meta_desc = isset( $geodir_post_type_info->seo['meta_description'] ) && $geodir_post_type_info->seo['meta_description'] != '' ? $geodir_post_type_info->seo['meta_description'] : $geodir_meta_desc;
1160
1161
			if ( $geodir_is_category ) {
1162
				$category = $geodir_is_category ? get_term_by( 'slug', $geodir_is_category, $category_taxonomy[0] ) : null;
1163
				if ( isset( $category->term_id ) && ! empty( $category->term_id ) ) {
1164
					$category_id   = $category->term_id;
1165
					$category_desc = trim( $category->description ) != '' ? trim( $category->description ) : geodir_get_tax_meta( $category_id, 'ct_cat_top_desc', false, $geodir_post_type );
1166
					if ( $location_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $location_id 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...
1167
						$option_name    = 'geodir_cat_loc_' . $geodir_post_type . '_' . $category_id;
1168
						$cat_loc_option = get_option( $option_name );
1169
1170
						$gd_cat_loc_default = ! empty( $cat_loc_option ) && isset( $cat_loc_option['gd_cat_loc_default'] ) && $cat_loc_option['gd_cat_loc_default'] > 0 ? true : false;
1171
						if ( ! $gd_cat_loc_default ) {
1172
							$option_name   = 'geodir_cat_loc_' . $geodir_post_type . '_' . $category_id . '_' . $location_id;
1173
							$option        = get_option( $option_name );
1174
							$category_desc = isset( $option['gd_cat_loc_desc'] ) && trim( $option['gd_cat_loc_desc'] ) != '' ? trim( $option['gd_cat_loc_desc'] ) : $category_desc;
1175
						}
1176
					}
1177
					$geodir_meta_desc = __( "Posts related to Category:", 'geodirectory' ) . " " . geodir_utf8_ucfirst( single_cat_title( "", false ) ) . '. ' . $category_desc;
0 ignored issues
show
Unused Code introduced by
$geodir_meta_desc 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...
1178
				}
1179
			} else if ( $geodir_is_tag ) {
1180
				$geodir_meta_desc = __( "Posts related to Tag:", 'geodirectory' ) . " " . geodir_utf8_ucfirst( single_tag_title( "", false ) ) . '. ' . $geodir_meta_desc;
0 ignored issues
show
Unused Code introduced by
$geodir_meta_desc 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...
1181
			}
1182
		}
1183
	}
1184
1185
1186
	$gd_page = '';
1187 View Code Duplication
	if ( geodir_is_page( 'home' ) ) {
1188
		$gd_page   = 'home';
1189
		$meta_desc = ( get_option( 'geodir_meta_desc_homepage' ) ) ? get_option( 'geodir_meta_desc_homepage' ) : $meta_desc;
1190
	} elseif ( geodir_is_page( 'detail' ) ) {
1191
		$gd_page   = 'detail';
1192
		$meta_desc = ( get_option( 'geodir_meta_desc_detail' ) ) ? get_option( 'geodir_meta_desc_detail' ) : $meta_desc;
1193
	} elseif ( geodir_is_page( 'pt' ) ) {
1194
		$gd_page   = 'pt';
1195
		$meta_desc = ( get_option( 'geodir_meta_desc_pt' ) ) ? get_option( 'geodir_meta_desc_pt' ) : $meta_desc;
1196
	} elseif ( geodir_is_page( 'listing' ) ) {
1197
		$gd_page   = 'listing';
1198
		$meta_desc = ( get_option( 'geodir_meta_desc_listing' ) ) ? get_option( 'geodir_meta_desc_listing' ) : $meta_desc;
1199
	} elseif ( geodir_is_page( 'location' ) ) {
1200
		$gd_page   = 'location';
1201
		$meta_desc = ( get_option( 'geodir_meta_desc_location' ) ) ? get_option( 'geodir_meta_desc_location' ) : $meta_desc;
1202
		$meta_desc = apply_filters( 'geodir_seo_meta_location_description', $meta_desc );
1203
1204
	} elseif ( geodir_is_page( 'search' ) ) {
1205
		$gd_page   = 'search';
1206
		$meta_desc = ( get_option( 'geodir_meta_desc_search' ) ) ? get_option( 'geodir_meta_desc_search' ) : $meta_desc;
1207
	} elseif ( geodir_is_page( 'add-listing' ) ) {
1208
		$gd_page   = 'add-listing';
1209
		$meta_desc = ( get_option( 'geodir_meta_desc_add-listing' ) ) ? get_option( 'geodir_meta_desc_add-listing' ) : $meta_desc;
1210
	} elseif ( geodir_is_page( 'author' ) ) {
1211
		$gd_page   = 'author';
1212
		$meta_desc = ( get_option( 'geodir_meta_desc_author' ) ) ? get_option( 'geodir_meta_desc_author' ) : $meta_desc;
1213
	} elseif ( geodir_is_page( 'login' ) ) {
1214
		$gd_page   = 'login';
1215
		$meta_desc = ( get_option( 'geodir_meta_desc_login' ) ) ? get_option( 'geodir_meta_desc_login' ) : $meta_desc;
1216
	} elseif ( geodir_is_page( 'listing-success' ) ) {
1217
		$gd_page   = 'listing-success';
1218
		$meta_desc = ( get_option( 'geodir_meta_desc_listing-success' ) ) ? get_option( 'geodir_meta_desc_listing-success' ) : $meta_desc;
1219
	}
1220
1221
1222
	if ( $meta_desc ) {
1223
		$meta_desc = stripslashes_deep( $meta_desc );
1224
		/**
1225
		 * Filter page description to replace variables.
1226
		 *
1227
		 * @since 1.5.4
1228
		 *
1229
		 * @param string $title   The page description including variables.
1230
		 * @param string $gd_page The GeoDirectory page type if any.
1231
		 */
1232
		$meta_desc = apply_filters( 'geodir_seo_meta_description_pre', __( $meta_desc, 'geodirectory' ), $gd_page, '' );
1233
1234
		/**
1235
		 * Filter SEO meta description.
1236
		 *
1237
		 * @since 1.0.0
1238
		 *
1239
		 * @param string $meta_desc Meta description content.
1240
		 */
1241
		echo apply_filters( 'geodir_seo_meta_description', '<meta name="description" content="' . $meta_desc . '" />', $meta_desc );
1242
	}
1243
1244
	// meta keywords
1245
	if ( isset( $post->post_type ) && in_array( $post->post_type, $all_postypes ) ) {
1246
		$place_tags = wp_get_post_terms( $post->ID, $post->post_type . '_tags', array( "fields" => "names" ) );
1247
		$place_cats = wp_get_post_terms( $post->ID, $post->post_type . 'category', array( "fields" => "names" ) );
1248
1249
		$meta_key .= implode( ", ", array_merge( (array) $place_cats, (array) $place_tags ) );
1250
	} else {
1251
		$posttags = get_the_tags();
1252
		if ( $posttags ) {
1253
			foreach ( $posttags as $tag ) {
1254
				$meta_key .= $tag->name . ' ';
1255
			}
1256
		} else {
1257
			$tags = get_tags( array( 'orderby' => 'count', 'order' => 'DESC' ) );
1258
			$xt   = 1;
1259
1260
			foreach ( $tags as $tag ) {
1261
				if ( $xt <= 20 ) {
1262
					$meta_key .= $tag->name . ", ";
1263
				}
1264
1265
				$xt ++;
1266
			}
1267
		}
1268
	}
1269
1270
	$meta_key         = $meta_key != '' ? rtrim( trim( $meta_key ), "," ) : $meta_key;
1271
	$geodir_meta_keys = $geodir_meta_keys != '' ? ( $meta_key != '' ? $meta_key . ', ' . $geodir_meta_keys : $geodir_meta_keys ) : $meta_key;
1272
	if ( $geodir_meta_keys != '' ) {
1273
		$geodir_meta_keys = strip_tags( $geodir_meta_keys );
1274
		$geodir_meta_keys = esc_html( $geodir_meta_keys );
1275
		$geodir_meta_keys = geodir_strtolower( $geodir_meta_keys );
1276
		$geodir_meta_keys = wp_html_excerpt( $geodir_meta_keys, 1000, '' );
1277
		$geodir_meta_keys = str_replace( '%location%', $replace_location, $geodir_meta_keys );
1278
1279
		$meta_key = rtrim( trim( $geodir_meta_keys ), "," );
1280
	}
1281
1282
	if ( $meta_key ) {
1283
		$meta_key = stripslashes_deep( $meta_key );
1284
		/**
1285
		 * Filter SEO meta keywords.
1286
		 *
1287
		 * @since 1.0.0
1288
		 *
1289
		 * @param string $meta_desc Meta keywords.
1290
		 */
1291
		echo apply_filters( 'geodir_seo_meta_keywords', '<meta name="keywords" content="' . $meta_key . '" />', $meta_key );
1292
	}
1293
1294
}
1295
1296
/**
1297
 * Returns list of options available for "Detail page tab settings"
1298
 *
1299
 * Options will be populated here. WP Admin -> Geodirectory -> Design -> Detail -> Detail page tab settings -> Exclude
1300
 * selected tabs from detail page
1301
 *
1302
 * @since   1.0.0
1303
 * @package GeoDirectory
1304
 * @return array returns list of options available as an array.
1305
 */
1306
function geodir_detail_page_tabs_key_value_array() {
1307
	$geodir_detail_page_tabs_key_value_array = array();
1308
1309
	$geodir_detail_page_tabs_array = geodir_detail_page_tabs_array();
1310
1311
	foreach ( $geodir_detail_page_tabs_array as $key => $tabs_obj ) {
1312
		$geodir_detail_page_tabs_key_value_array[ $key ] = $tabs_obj['heading_text'];
1313
	}
1314
1315
	return $geodir_detail_page_tabs_key_value_array;
1316
}
1317
1318
/**
1319
 * Build and return the list of available tabs as an array.
1320
 *
1321
 * @since   1.0.0
1322
 * @package GeoDirectory
1323
 * @return mixed|array Tabs array.
1324
 */
1325
function geodir_detail_page_tabs_array() {
1326
1327
	$arr_tabs = array();
1328
	/**
1329
	 * Filter detail page tab display.
1330
	 *
1331
	 * @since 1.0.0
1332
	 */
1333
	$arr_tabs['post_profile'] = array(
1334
		'heading_text'  => __( 'Profile', 'geodirectory' ),
1335
		'is_active_tab' => true,
1336
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'post_profile' ),
1337
		'tab_content'   => ''
1338
	);
1339
	$arr_tabs['post_info']    = array(
1340
		'heading_text'  => __( 'More Info', 'geodirectory' ),
1341
		'is_active_tab' => false,
1342
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'post_info' ),
1343
		'tab_content'   => ''
1344
	);
1345
1346
	$arr_tabs['post_images'] = array(
1347
		'heading_text'  => __( 'Photos', 'geodirectory' ),
1348
		'is_active_tab' => false,
1349
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'post_images' ),
1350
		'tab_content'   => ''
1351
	);
1352
1353
	$arr_tabs['post_video'] = array(
1354
		'heading_text'  => __( 'Video', 'geodirectory' ),
1355
		'is_active_tab' => false,
1356
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'post_video' ),
1357
		'tab_content'   => ''
1358
	);
1359
1360
	$arr_tabs['special_offers'] = array(
1361
		'heading_text'  => __( 'Special Offers', 'geodirectory' ),
1362
		'is_active_tab' => false,
1363
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'special_offers' ),
1364
		'tab_content'   => ''
1365
	);
1366
1367
	$arr_tabs['post_map'] = array(
1368
		'heading_text'  => __( 'Map', 'geodirectory' ),
1369
		'is_active_tab' => false,
1370
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'post_map' ),
1371
		'tab_content'   => ''
1372
	);
1373
1374
	$arr_tabs['reviews'] = array(
1375
		'heading_text'  => __( 'Reviews', 'geodirectory' ),
1376
		'is_active_tab' => false,
1377
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'reviews' ),
1378
		'tab_content'   => 'review display'
1379
	);
1380
1381
	$arr_tabs['related_listing'] = array(
1382
		'heading_text'  => __( 'Related Listing', 'geodirectory' ),
1383
		'is_active_tab' => false,
1384
		'is_display'    => apply_filters( 'geodir_detail_page_tab_is_display', true, 'related_listing' ),
1385
		'tab_content'   => ''
1386
	);
1387
1388
	/**
1389
	 * Filter the tabs array.
1390
	 *
1391
	 * @since 1.0.0
1392
	 */
1393
	return apply_filters( 'geodir_detail_page_tab_list_extend', $arr_tabs );
1394
1395
1396
}
1397
1398
1399
/**
1400
 * Returns the list of tabs available as an array.
1401
 *
1402
 * @since   1.0.0
1403
 * @package GeoDirectory
1404
 * @return mixed|array Tabs array.
1405
 */
1406
function geodir_detail_page_tabs_list() {
1407
	$tabs_excluded = get_option( 'geodir_detail_page_tabs_excluded' );
1408
	$tabs_array    = geodir_detail_page_tabs_array();
1409
1410
	if ( ! empty( $tabs_excluded ) ) {
1411
		foreach ( $tabs_excluded as $tab ) {
1412
			if ( array_key_exists( $tab, $tabs_array ) ) {
1413
				unset( $tabs_array[ $tab ] );
1414
			}
1415
		}
1416
	}
1417
1418
	return $tabs_array;
1419
}
1420
1421
1422
/**
1423
 * The main function responsible for displaying tabs in frontend detail page.
1424
 *
1425
 * @since   1.0.0
1426
 * @package GeoDirectory
1427
 * @global object $post                      The current post object.
1428
 * @global array $post_images                List of images attached to the post.
1429
 * @global string $video                     The video embed content.
1430
 * @global string $special_offers            Special offers content.
1431
 * @global string $related_listing           Related listing html.
1432
 * @global string $geodir_post_detail_fields Detail field html.
1433
 */
1434
function geodir_show_detail_page_tabs() {
1435
	global $post, $post_images, $video, $special_offers, $related_listing, $geodir_post_detail_fields, $preview;
1436
1437
	$post_id            = ! empty( $post ) && isset( $post->ID ) ? (int) $post->ID : 0;
1438
	$request_post_id    = ! empty( $_REQUEST['p'] ) ? (int) $_REQUEST['p'] : 0;
1439
	$is_backend_preview = ( is_single() && ! empty( $_REQUEST['post_type'] ) && ! empty( $_REQUEST['preview'] ) && ! empty( $_REQUEST['p'] ) ) && is_super_admin() ? true : false; // skip if preview from backend
1440
1441
	if ( $is_backend_preview && ! $post_id > 0 && $request_post_id > 0 ) {
1442
		$post = geodir_get_post_info( $request_post_id );
1443
		setup_postdata( $post );
1444
	}
1445
1446
	$geodir_post_detail_fields = geodir_show_listing_info( 'moreinfo' );
1447
1448
	$package_info = geodir_post_package_info(array(), $post, (!empty($post->post_type) ? $post->post_type : ''));
0 ignored issues
show
Bug introduced by
It seems like $post defined by geodir_get_post_info($request_post_id) on line 1442 can also be of type boolean; however, geodir_post_package_info() does only seem to accept string|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1449
	$image_limit = '';
1450 View Code Duplication
	if (defined('GEODIRPAYMENT_VERSION') && !empty($package_info) && isset($package_info->image_limit) && $package_info->image_limit !== '') {
1451
		$image_limit = (int)$package_info->image_limit;
1452
	}
1453
1454
	if ( geodir_is_page( 'detail' ) ) {
1455
		$video                 = geodir_get_video( $post->ID );
1456
		$special_offers        = geodir_get_special_offers( $post->ID );
1457
		$related_listing_array = array();
1458
		if ( get_option( 'geodir_add_related_listing_posttypes' ) ) {
1459
			$related_listing_array = get_option( 'geodir_add_related_listing_posttypes' );
1460
		}
1461
1462
		$excluded_tabs = get_option( 'geodir_detail_page_tabs_excluded' );
1463
		if ( ! $excluded_tabs ) {
1464
			$excluded_tabs = array();
1465
		}
1466
1467
		$related_listing = '';
1468
		if ( in_array( $post->post_type, $related_listing_array ) && ! in_array( 'related_listing', $excluded_tabs ) ) {
1469
			$request = array(
1470
				'post_number'         => get_option( 'geodir_related_post_count' ),
1471
				'relate_to'           => get_option( 'geodir_related_post_relate_to' ),
1472
				'layout'              => get_option( 'geodir_related_post_listing_view' ),
1473
				'add_location_filter' => get_option( 'geodir_related_post_location_filter' ),
1474
				'list_sort'           => get_option( 'geodir_related_post_sortby' ),
1475
				'character_count'     => get_option( 'geodir_related_post_excerpt' )
1476
			);
1477
1478
			if ( $post->post_type == 'gd_event' && defined( 'GDEVENTS_VERSION' ) ) {
1479
				$related_listing = geodir_get_detail_page_related_events( $request );
1480
			} else {
1481
				$related_listing = geodir_related_posts_display( $request );
1482
			}
1483
1484
		}
1485
1486
		$post_images = geodir_get_images( $post->ID, 'thumbnail' );
1487
		$thumb_image = '';
1488
		if ( ! empty( $post_images ) ) {
1489
			$count = 1;
1490
			foreach ( $post_images as $image ) {
0 ignored issues
show
Bug introduced by
The expression $post_images 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...
1491
				if ($image_limit !== '' && $count > $image_limit) {
1492
					break;
1493
				}
1494
				$caption = ( ! empty( $image->caption ) ) ? $image->caption : '';
1495
				$thumb_image .= '<a href="' . $image->src . '" title="' . $caption . '">';
1496
				$thumb_image .= geodir_show_image( $image, 'thumbnail', true, false );
1497
				$thumb_image .= '</a>';
1498
				$count++;
1499
			}
1500
		}
1501
1502
		$map_args                    = array();
1503
		$map_args['map_canvas_name'] = 'detail_page_map_canvas';
1504
		$map_args['width']           = '600';
1505
		$map_args['height']          = '300';
1506
		if ( $post->post_mapzoom ) {
1507
			$map_args['zoom'] = '' . $post->post_mapzoom . '';
1508
		}
1509
		$map_args['autozoom']                 = false;
1510
		$map_args['scrollwheel']              = ( get_option( 'geodir_add_listing_mouse_scroll' ) ) ? 0 : 1;
1511
		$map_args['child_collapse']           = '0';
1512
		$map_args['enable_cat_filters']       = false;
1513
		$map_args['enable_text_search']       = false;
1514
		$map_args['enable_post_type_filters'] = false;
1515
		$map_args['enable_location_filters']  = false;
1516
		$map_args['enable_jason_on_load']     = true;
1517
		$map_args['enable_map_direction']     = true;
1518
		$map_args['map_class_name']           = 'geodir-map-detail-page';
1519
		$map_args['maptype']                  = ( ! empty( $post->post_mapview ) ) ? $post->post_mapview : 'ROADMAP';
1520
	} else if ( geodir_is_page( 'preview' ) ) {
1521
		$video          = isset( $post->geodir_video ) ? $post->geodir_video : '';
1522
		$special_offers = isset( $post->geodir_special_offers ) ? $post->geodir_special_offers : '';
1523
1524
		if ( isset( $post->post_images ) ) {
1525
			$post->post_images = trim( $post->post_images, "," );
1526
		}
1527
1528
		if ( isset( $post->post_images ) && ! empty( $post->post_images ) ) {
1529
			$post_images = explode( ",", $post->post_images );
1530
		}
1531
1532
		$thumb_image = '';
1533
		if ( ! empty( $post_images ) ) {
1534
			$count = 1;
1535
			foreach ( $post_images as $image ) {
1536
				if ( $image != '' ) {
1537
					if ($image_limit !== '' && $count > $image_limit) {
1538
						break;
1539
					}
1540
					$thumb_image .= '<a href="' . $image . '">';
1541
					$thumb_image .= geodir_show_image( array( 'src' => $image ), 'thumbnail', true, false );
1542
					$thumb_image .= '</a>';
1543
					$count++;
1544
				}
1545
			}
1546
		}
1547
1548
		global $map_jason;
1549
		$marker_json      = $post->marker_json != '' ? json_decode( $post->marker_json, true ) : array();
1550
		$marker_icon      = ( ! empty( $marker_json ) && ! empty( $marker_json['i'] ) ) ? $marker_json['i'] : '';
1551
		$icon_size        = geodir_get_marker_size( $marker_icon );
1552
		$marker_json['w'] = $icon_size['w'];
1553
		$marker_json['h'] = $icon_size['h'];
1554
		$map_jason[]      = json_encode( $marker_json );
1555
1556
		$address_latitude  = isset( $post->post_latitude ) ? $post->post_latitude : '';
1557
		$address_longitude = isset( $post->post_longitude ) ? $post->post_longitude : '';
1558
		$mapview           = isset( $post->post_mapview ) ? $post->post_mapview : '';
1559
		$mapzoom           = isset( $post->post_mapzoom ) ? $post->post_mapzoom : '';
1560
		if ( ! $mapzoom ) {
1561
			$mapzoom = 12;
1562
		}
1563
1564
		$map_args                             = array();
1565
		$map_args['map_canvas_name']          = 'preview_map_canvas';
1566
		$map_args['width']                    = '950';
1567
		$map_args['height']                   = '300';
1568
		$map_args['child_collapse']           = '0';
1569
		$map_args['maptype']                  = $mapview;
1570
		$map_args['autozoom']                 = false;
1571
		$map_args['zoom']                     = "$mapzoom";
1572
		$map_args['latitude']                 = $address_latitude;
1573
		$map_args['longitude']                = $address_longitude;
1574
		$map_args['enable_cat_filters']       = false;
1575
		$map_args['enable_text_search']       = false;
1576
		$map_args['enable_post_type_filters'] = false;
1577
		$map_args['enable_location_filters']  = false;
1578
		$map_args['enable_jason_on_load']     = true;
1579
		$map_args['enable_map_direction']     = true;
1580
		$map_args['map_class_name']           = 'geodir-map-preview-page';
1581
	}
1582
1583
	$arr_detail_page_tabs = geodir_detail_page_tabs_list();// get this sooner so we can get the active tab for the user
1584
1585
	$active_tab       = '';
1586
	$active_tab_name  = '';
1587
	$default_tab      = '';
1588
	$default_tab_name = '';
1589
	foreach ( $arr_detail_page_tabs as $tab_index => $tabs ) {
1590
		if ( isset( $tabs['is_active_tab'] ) && $tabs['is_active_tab'] && ! empty( $tabs['is_display'] ) && isset( $tabs['heading_text'] ) && $tabs['heading_text'] ) {
1591
			$active_tab      = $tab_index;
1592
			$active_tab_name = __( $tabs['heading_text'], 'geodirectory' );
1593
		}
1594
1595
		if ( $default_tab === '' && ! empty( $tabs['is_display'] ) && ! empty( $tabs['heading_text'] ) ) {
1596
			$default_tab      = $tab_index;
1597
			$default_tab_name = __( $tabs['heading_text'], 'geodirectory' );
1598
		}
1599
	}
1600
1601
	if ( $active_tab === '' && $default_tab !== '' ) { // Make first tab as a active tab if not any tab is active.
1602
		if ( isset( $arr_detail_page_tabs[ $active_tab ] ) && isset( $arr_detail_page_tabs[ $active_tab ]['is_active_tab'] ) ) {
1603
			$arr_detail_page_tabs[ $active_tab ]['is_active_tab'] = false;
1604
		}
1605
1606
		$arr_detail_page_tabs[ $default_tab ]['is_active_tab'] = true;
1607
		$active_tab                                            = $default_tab;
0 ignored issues
show
Unused Code introduced by
$active_tab 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...
1608
		$active_tab_name                                       = $default_tab_name;
1609
	}
1610
	$tab_list = ( get_option( 'geodir_disable_tabs', false ) ) ? true : false;
1611
	?>
1612
	<div class="geodir-tabs" id="gd-tabs" style="position:relative;">
1613
		<?php if ( ! $tab_list ){ ?>
1614
		<div id="geodir-tab-mobile-menu">
1615
			<i class="fa fa-bars"></i>
1616
			<span class="geodir-mobile-active-tab"><?php echo $active_tab_name; ?></span>
1617
			<i class="fa fa-sort-desc"></i>
1618
		</div>
1619
		<dl class="geodir-tab-head">
1620
			<?php
1621
			}
1622
			/**
1623
			 * Called before the details page tab list headings, inside the `dl` tag.
1624
			 *
1625
			 * @since 1.0.0
1626
			 * @see   'geodir_after_tab_list'
1627
			 */
1628
			do_action( 'geodir_before_tab_list' ); ?>
1629
			<?php
1630
1631
			foreach ( $arr_detail_page_tabs as $tab_index => $detail_page_tab ) {
1632
				if ( $detail_page_tab['is_display'] ) {
1633
1634
					if ( ! $tab_list ) {
1635
						?>
1636
						<dt></dt> <!-- added to comply with validation -->
1637
						<dd <?php if ( $detail_page_tab['is_active_tab'] ){ ?>class="geodir-tab-active"<?php } ?> ><a
1638
								data-tab="#<?php echo $tab_index; ?>"
1639
								data-status="enable"><?php _e( $detail_page_tab['heading_text'], 'geodirectory' ); ?></a>
1640
						</dd>
1641
						<?php
1642
					}
1643
					ob_start() // start tab content buffering
1644
					?>
1645
					<li id="<?php echo $tab_index; ?>Tab">
1646
						<?php if ( $tab_list ) {
1647
							$tab_title = '<span class="gd-tab-list-title" ><a href="#' . $tab_index . '">' . __( $detail_page_tab['heading_text'], 'geodirectory' ) . '</a></span><hr />';
1648
							/**
1649
							 * Filter the tab list title html.
1650
							 *
1651
							 * @since 1.6.1
1652
							 *
1653
							 * @param string $tab_title      The html for the tab title.
1654
							 * @param string $tab_index      The tab index type.
1655
							 * @param array $detail_page_tab The array of values including title text.
1656
							 */
1657
							echo apply_filters( 'geodir_tab_list_title', $tab_title, $tab_index, $detail_page_tab );
1658
						} ?>
1659
						<div id="<?php echo $tab_index; ?>" class="hash-offset"></div>
1660
						<?php
1661
						/**
1662
						 * Called before the details tab content is output per tab.
1663
						 *
1664
						 * @since 1.0.0
1665
						 *
1666
						 * @param string $tab_index The tab name ID.
1667
						 */
1668
						do_action( 'geodir_before_tab_content', $tab_index );
1669
1670
						/**
1671
						 * Called before the details tab content is output per tab.
1672
						 *
1673
						 * Uses dynamic hook name: geodir_before_$tab_index_tab_content
1674
						 *
1675
						 * @since 1.0.0
1676
						 * @todo  do we need this if we have the hook above? 'geodir_before_tab_content'
1677
						 */
1678
						do_action( 'geodir_before_' . $tab_index . '_tab_content' );
1679
						/// write a code to generate content of each tab
1680
						switch ( $tab_index ) {
1681
							case 'post_profile':
1682
								/**
1683
								 * Called before the listing description content on the details page tab.
1684
								 *
1685
								 * @since 1.0.0
1686
								 */
1687
								do_action( 'geodir_before_description_on_listing_detail' );
1688
								if ( geodir_is_page( 'detail' ) ) {
1689
									the_content();
1690
								} else {
1691
									/** This action is documented in geodirectory_template_actions.php */
1692
									echo apply_filters( 'the_content', stripslashes( $post->post_desc ) );
1693
								}
1694
1695
								/**
1696
								 * Called after the listing description content on the details page tab.
1697
								 *
1698
								 * @since 1.0.0
1699
								 */
1700
								do_action( 'geodir_after_description_on_listing_detail' );
1701
								break;
1702
							case 'post_info':
1703
								echo $geodir_post_detail_fields;
1704
								break;
1705
							case 'post_images':
1706
								echo $thumb_image;
0 ignored issues
show
Bug introduced by
The variable $thumb_image 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...
1707
								break;
1708
							case 'post_video':
1709
								// some browsers hide $_POST data if used for embeds so we replace with a placeholder
1710
								if ( $preview ) {
1711
									if ( $video ) {
1712
										echo "<span class='gd-video-embed-preview' ><p class='gd-video-preview-text'><i class=\"fa fa-video-camera\" aria-hidden=\"true\"></i><br />" . __( 'Video Preview Placeholder', 'geodirectory' ) . "</p></span>";
1713
									}
1714
								} else {
1715
1716
									// stop payment manager filtering content length
1717
									$filter_priority = has_filter( 'the_content', 'geodir_payments_the_content' );
1718
									if ( false !== $filter_priority ) {
1719
										remove_filter( 'the_content', 'geodir_payments_the_content', $filter_priority );
1720
									}
1721
1722
									/** This action is documented in geodirectory_template_actions.php */
1723
									echo apply_filters( 'the_content', stripslashes( $video ) );// we apply the_content filter so oembed works also;
1724
1725
									if ( false !== $filter_priority ) {
1726
										add_filter( 'the_content', 'geodir_payments_the_content', $filter_priority );
1727
									}
1728
								}
1729
								break;
1730
							case 'special_offers':
1731
								echo apply_filters( 'gd_special_offers_content', wpautop( stripslashes( $special_offers ) ) );
1732
1733
								break;
1734
							case 'post_map':
1735
								geodir_draw_map( $map_args );
0 ignored issues
show
Bug introduced by
The variable $map_args 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...
1736
								break;
1737
							case 'reviews':
1738
								comments_template();
1739
								break;
1740
							case 'related_listing':
1741
								echo $related_listing;
1742
								break;
1743
							default: {
1744
								if ( ( isset( $post->{$tab_index} ) || ( ! isset( $post->{$tab_index} ) && ( strpos( $tab_index, 'gd_tab_' ) !== false || $tab_index == 'link_business' ) ) ) && ! empty( $detail_page_tab['tab_content'] ) ) {
1745
									echo $detail_page_tab['tab_content'];
1746
								}
1747
							}
1748
								break;
1749
						}
1750
1751
						/**
1752
						 * Called after the details tab content is output per tab.
1753
						 *
1754
						 * @since 1.0.0
1755
						 */
1756
						do_action( 'geodir_after_tab_content', $tab_index );
1757
1758
						/**
1759
						 * Called after the details tab content is output per tab.
1760
						 *
1761
						 * Uses dynamic hook name: geodir_after_$tab_index_tab_content
1762
						 *
1763
						 * @since 1.0.0
1764
						 * @todo  do we need this if we have the hook above? 'geodir_after_tab_content'
1765
						 */
1766
						do_action( 'geodir_after_' . $tab_index . '_tab_content' );
1767
						?> </li>
1768
					<?php
1769
					/**
1770
					 * Filter the current tab content.
1771
					 *
1772
					 * @since 1.0.0
1773
					 */
1774
					$arr_detail_page_tabs[ $tab_index ]['tab_content'] = apply_filters( "geodir_modify_" . $detail_page_tab['tab_content'] . "_tab_content", ob_get_clean() );
1775
				} // end of if for is_display
1776
			}// end of foreach
1777
1778
			/**
1779
			 * Called after the details page tab list headings, inside the `dl` tag.
1780
			 *
1781
			 * @since 1.0.0
1782
			 * @see   'geodir_before_tab_list'
1783
			 */
1784
			do_action( 'geodir_after_tab_list' );
1785
			?>
1786
			<?php if ( ! $tab_list ){ ?></dl><?php } ?>
1787
		<ul class="geodir-tabs-content entry-content <?php if ( $tab_list ) { ?>geodir-tabs-list<?php } ?>"
1788
		    style="position:relative;">
1789
			<?php
1790
			foreach ( $arr_detail_page_tabs as $detail_page_tab ) {
1791
				if ( $detail_page_tab['is_display'] && ! empty( $detail_page_tab['tab_content'] ) ) {
1792
					echo $detail_page_tab['tab_content'];
1793
				}// end of if
1794
			}// end of foreach
1795
1796
			/**
1797
			 * Called after all the tab content is output in `li` tags, called before the closing `ul` tag.
1798
			 *
1799
			 * @since 1.0.0
1800
			 */
1801
			do_action( 'geodir_add_tab_content' ); ?>
1802
		</ul>
1803
		<!--gd-tabs-content ul end-->
1804
	</div>
1805
	<?php if ( ! $tab_list ) { ?>
1806
		<script>
1807
			if (window.location.hash && window.location.hash.indexOf('&') === -1 && jQuery(window.location.hash + 'Tab').length) {
1808
				hashVal = window.location.hash;
1809
			} else {
1810
				hashVal = jQuery('dl.geodir-tab-head dd.geodir-tab-active').find('a').attr('data-tab');
1811
			}
1812
			jQuery('dl.geodir-tab-head dd').each(function () {
1813
				//Get all tabs
1814
				var tabs = jQuery(this).children('dd');
1815
				var tab = '';
1816
				tab = jQuery(this).find('a').attr('data-tab');
1817
				if (hashVal != tab) {
1818
					jQuery(tab + 'Tab').hide();
1819
				}
1820
1821
			});
1822
		</script>
1823
		<?php
1824
	}
1825
}
1826
1827
/**
1828
 * Fixes image orientation.
1829
 *
1830
 * @since   1.0.0
1831
 * @package GeoDirectory
1832
 *
1833
 * @param array $file The image file.
1834
 *
1835
 * @return mixed Image file.
1836
 */
1837
function geodir_exif( $file ) {
1838
	if ( empty( $file ) || ! is_array( $file ) ) {
1839
		return $file;
1840
	}
1841
1842
	$file_path = ! empty( $file['tmp_name'] ) ? sanitize_text_field( $file['tmp_name'] ) : '';
1843
	if ( ! ( $file_path && file_exists( $file_path ) ) ) {
1844
		return $file;
1845
	}
1846
	$file['file'] = $file_path;
1847
1848
	if ( ! file_is_valid_image( $file_path ) ) {
1849
		return $file; // Bail if file is not an image.
1850
	}
1851
1852
	if ( ! function_exists( 'wp_get_image_editor' ) ) {
1853
		return $file;
1854
	}
1855
1856
	$mime_type = $file['type'];
1857
	$exif      = array();
1858
	if ( $mime_type == 'image/jpeg' && function_exists( 'exif_read_data' ) ) {
1859
		try {
1860
			$exif = exif_read_data( $file_path );
1861
		} catch ( Exception $e ) {
1862
			$exif = array();
1863
		}
1864
	}
1865
1866
	$rotate      = false;
1867
	$flip        = false;
1868
	$modify      = false;
1869
	$orientation = 0;
1870
	if ( ! empty( $exif ) && isset( $exif['Orientation'] ) ) {
1871
		switch ( (int) $exif['Orientation'] ) {
1872
			case 1:
1873
				// do nothing
1874
				break;
1875 View Code Duplication
			case 2:
1876
				$flip   = array( false, true );
1877
				$modify = true;
1878
				break;
1879
			case 3:
1880
				$orientation = - 180;
1881
				$rotate      = true;
1882
				$modify      = true;
1883
				break;
1884 View Code Duplication
			case 4:
1885
				$flip   = array( true, false );
1886
				$modify = true;
1887
				break;
1888 View Code Duplication
			case 5:
1889
				$orientation = - 90;
1890
				$rotate      = true;
1891
				$flip        = array( false, true );
1892
				$modify      = true;
1893
				break;
1894
			case 6:
1895
				$orientation = - 90;
1896
				$rotate      = true;
1897
				$modify      = true;
1898
				break;
1899 View Code Duplication
			case 7:
1900
				$orientation = - 270;
1901
				$rotate      = true;
1902
				$flip        = array( false, true );
1903
				$modify      = true;
1904
				break;
1905
			case 8:
1906
			case 9:
1907
				$orientation = - 270;
1908
				$rotate      = true;
1909
				$modify      = true;
1910
				break;
1911
			default:
1912
				$orientation = 0;
1913
				$rotate      = true;
1914
				$modify      = true;
1915
				break;
1916
		}
1917
	}
1918
1919
	$quality = null;
1920
	/**
1921
	 * Filter the image quality.
1922
	 *
1923
	 * @since 1.5.7
1924
	 *
1925
	 * @param int|null $quality Image Compression quality between 1-100% scale. Default null.
1926
	 * @param string $quality   Image mime type.
1927
	 */
1928
	$quality = apply_filters( 'geodir_image_upload_set_quality', $quality, $mime_type );
1929
	if ( $quality !== null ) {
1930
		$modify = true;
1931
	}
1932
1933
	if ( ! $modify ) {
1934
		return $file; // no change
1935
	}
1936
1937
	$image = wp_get_image_editor( $file_path );
1938
	if ( ! is_wp_error( $image ) ) {
1939
		if ( $rotate ) {
1940
			$image->rotate( $orientation );
1941
		}
1942
1943
		if ( ! empty( $flip ) ) {
1944
			$image->flip( $flip[0], $flip[1] );
1945
		}
1946
1947
		if ( $quality !== null ) {
1948
			$image->set_quality( (int) $quality );
1949
		}
1950
1951
		$result = $image->save( $file_path );
1952
		if ( ! is_wp_error( $result ) ) {
1953
			$file['file']     = $result['path'];
1954
			$file['tmp_name'] = $result['path'];
1955
		}
1956
	}
1957
1958
	// The image orientation is fixed, pass it back for further processing
1959
	return $file;
1960
}
1961
1962
/**
1963
 * Returns the recent reviews.
1964
 *
1965
 * @since   1.0.0
1966
 * @since   1.6.21 Recent reviews doesn't working well with WPML.
1967
 * @package GeoDirectory
1968
 *
1969
 * @global object $wpdb        WordPress Database object.
1970
 * @global object $gd_session  GeoDirectory Session object.
1971
 *
1972
 * @param int $g_size          Optional. Avatar size in pixels. Default 60.
1973
 * @param int $no_comments     Optional. Number of reviews you want to display. Default: 10.
1974
 * @param int $comment_lenth   Optional. Maximum number of characters you want to display. After that read more link
1975
 *                             will appear.
1976
 * @param bool $show_pass_post Optional. Not yet implemented.
1977
 *
1978
 * @return string Returns the recent reviews html.
1979
 */
1980
function geodir_get_recent_reviews( $g_size = 60, $no_comments = 5, $comment_lenth = 60, $show_pass_post = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $show_pass_post 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...
1981
	global $wpdb, $tablecomments, $tableposts, $rating_table_name, $gd_session, $table_prefix;
1982
	$tablecomments = $wpdb->comments;
1983
	$tableposts    = $wpdb->posts;
1984
1985
	$comments_echo  = '';
1986
	$city_filter    = '';
0 ignored issues
show
Unused Code introduced by
$city_filter 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...
1987
	$region_filter  = '';
0 ignored issues
show
Unused Code introduced by
$region_filter 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...
1988
	$country_filter = '';
0 ignored issues
show
Unused Code introduced by
$country_filter 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...
1989
1990
	if ( $gd_session->get( 'gd_multi_location' ) ) {
1991
		if ( $gd_ses_country = $gd_session->get( 'gd_country' ) ) {
1992
			$country_filter = $wpdb->prepare( " AND r.post_country=%s ", str_replace( "-", " ", $gd_ses_country ) );
0 ignored issues
show
Unused Code introduced by
$country_filter 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...
1993
		}
1994
1995
		if ( $gd_ses_region = $gd_session->get( 'gd_region' ) ) {
1996
			$region_filter = $wpdb->prepare( " AND r.post_region=%s ", str_replace( "-", " ", $gd_ses_region ) );
0 ignored issues
show
Unused Code introduced by
$region_filter 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...
1997
		}
1998
1999
		if ( $gd_ses_city = $gd_session->get( 'gd_city' ) ) {
2000
			$city_filter = $wpdb->prepare( " AND r.post_city=%s ", str_replace( "-", " ", $gd_ses_city ) );
0 ignored issues
show
Unused Code introduced by
$city_filter 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...
2001
		}
2002
	}
2003
2004
	$join = '';
2005
	$where = '';
2006
2007
	if (geodir_is_wpml()) {
2008
		$lang_code = ICL_LANGUAGE_CODE;
2009
2010
		if ($lang_code) {
2011
			$join .= " JOIN " . $table_prefix . "icl_translations AS icltr2 ON icltr2.element_id = c.comment_post_ID AND p.ID = icltr2.element_id AND CONCAT('post_', p.post_type) = icltr2.element_type LEFT JOIN " . $table_prefix . "icl_translations AS icltr_comment ON icltr_comment.element_id = c.comment_ID AND icltr_comment.element_type = 'comment'";
2012
			$where .= " AND icltr2.language_code = '" . $lang_code . "' AND (icltr_comment.language_code IS NULL OR icltr_comment.language_code = icltr2.language_code)";
2013
		}
2014
	}
2015
2016
	$request = "SELECT r.id AS ID, r.post_type, r.comment_id AS comment_ID, r.post_date AS comment_date, r.overall_rating, r.user_id, r.post_id FROM " . GEODIR_REVIEW_TABLE . " AS r JOIN " . $wpdb->comments . " AS c ON c.comment_ID = r.comment_id JOIN " . $wpdb->posts . " AS p ON p.ID = c.comment_post_ID " . $join . " WHERE c.comment_parent = 0 AND c.comment_approved = 1 AND r.status = 1 AND r.overall_rating >= 1 AND p.post_status = 'publish' " . $where . " ORDER BY r.post_date DESC, r.id DESC LIMIT ". $no_comments;
2017
2018
	$comments = $wpdb->get_results( $request );
2019
2020
	foreach ( $comments as $comment ) {
2021
		// Set the extra comment info needed.
2022
		$comment_extra = $wpdb->get_row( "SELECT * FROM $wpdb->comments WHERE comment_ID =$comment->comment_ID" );
2023
		$comment->comment_content      = $comment_extra->comment_content;
2024
		$comment->comment_author       = $comment_extra->comment_author;
2025
		$comment->comment_author_email = $comment_extra->comment_author_email;
2026
2027
		$comment_id      = '';
0 ignored issues
show
Unused Code introduced by
$comment_id 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...
2028
		$comment_id      = $comment->comment_ID;
2029
		$comment_content = strip_tags( $comment->comment_content );
2030
2031
		$comment_content = preg_replace( '#(\\[img\\]).+(\\[\\/img\\])#', '', $comment_content );
2032
2033
		$permalink            = get_permalink( $comment->ID ) . "#comment-" . $comment->comment_ID;
0 ignored issues
show
Unused Code introduced by
$permalink 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...
2034
		$comment_author_email = $comment->comment_author_email;
0 ignored issues
show
Unused Code introduced by
$comment_author_email 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...
2035
		$comment_post_ID      = $comment->post_id;
2036
2037
		$post_title        = get_the_title( $comment_post_ID );
2038
		$permalink         = get_permalink( $comment_post_ID );
2039
		$comment_permalink = $permalink . "#comment-" . $comment->comment_ID;
2040
		$read_more         = '<a class="comment_excerpt" href="' . $comment_permalink . '">' . __( 'Read more', 'geodirectory' ) . '</a>';
2041
2042
		$comment_content_length = strlen( $comment_content );
2043
		if ( $comment_content_length > $comment_lenth ) {
2044
			$comment_excerpt = geodir_utf8_substr( $comment_content, 0, $comment_lenth ) . '... ' . $read_more;
2045
		} else {
2046
			$comment_excerpt = $comment_content;
2047
		}
2048
2049
		if ( $comment->user_id ) {
2050
			$user_profile_url = get_author_posts_url( $comment->user_id );
2051
		} else {
2052
			$user_profile_url = '';
2053
		}
2054
2055
		if ( $comment_id ) {
2056
			$comments_echo .= '<li class="clearfix">';
2057
			$comments_echo .= "<span class=\"li" . $comment_id . " geodir_reviewer_image\">";
2058
			if ( function_exists( 'get_avatar' ) ) {
2059
				if ( ! isset( $comment->comment_type ) ) {
2060
					if ( $user_profile_url ) {
2061
						$comments_echo .= '<a href="' . $user_profile_url . '">';
2062
					}
2063
					$comments_echo .= get_avatar( $comment->comment_author_email, $g_size, geodir_plugin_url() . '/geodirectory-assets/images/gravatar2.png' );
2064
					if ( $user_profile_url ) {
2065
						$comments_echo .= '</a>';
2066
					}
2067
				} elseif ( ( isset( $comment->comment_type ) && $comment->comment_type == 'trackback' ) || ( isset( $comment->comment_type ) && $comment->comment_type == 'pingback' ) ) {
2068
					if ( $user_profile_url ) {
2069
						$comments_echo .= '<a href="' . $user_profile_url . '">';
2070
					}
2071
					$comments_echo .= get_avatar( $comment->comment_author_url, $g_size, geodir_plugin_url() . '/geodirectory-assets/images/gravatar2.png' );
2072
				}
2073
			} elseif ( function_exists( 'gravatar' ) ) {
2074
				if ( $user_profile_url ) {
2075
					$comments_echo .= '<a href="' . $user_profile_url . '">';
2076
				}
2077
				$comments_echo .= "<img src=\"";
2078
				if ( '' == $comment->comment_type ) {
2079
					$comments_echo .= gravatar( $comment->comment_author_email, $g_size, geodir_plugin_url() . '/geodirectory-assets/images/gravatar2.png' );
2080
					if ( $user_profile_url ) {
2081
						$comments_echo .= '</a>';
2082
					}
2083
				} elseif ( ( 'trackback' == $comment->comment_type ) || ( 'pingback' == $comment->comment_type ) ) {
2084
					if ( $user_profile_url ) {
2085
						$comments_echo .= '<a href="' . $user_profile_url . '">';
2086
					}
2087
					$comments_echo .= gravatar( $comment->comment_author_url, $g_size, geodir_plugin_url() . '/geodirectory-assets/images/gravatar2.png' );
2088
					if ( $user_profile_url ) {
2089
						$comments_echo .= '</a>';
2090
					}
2091
				}
2092
				$comments_echo .= "\" alt=\"\" class=\"avatar\" />";
2093
			}
2094
2095
			$comments_echo .= "</span>\n";
2096
2097
			$comments_echo .= '<span class="geodir_reviewer_content">';
2098
			$author_link = apply_filters('geodir_reviewer_content_author_link', true);
2099
			if ( $comment->user_id && $author_link ) {
2100
				$comments_echo .= '<a href="' . get_author_posts_url( $comment->user_id ) . '">';
2101
			}
2102
			$comments_echo .= '<span class="geodir_reviewer_author">' . $comment->comment_author . '</span> ';
2103
			if ( $comment->user_id && $author_link ) {
2104
				$comments_echo .= '</a>';
2105
			}
2106
			$comments_echo .= '<span class="geodir_reviewer_reviewed">' . __( 'reviewed', 'geodirectory' ) . '</span> ';
2107
			$comments_echo .= '<a href="' . $permalink . '" class="geodir_reviewer_title">' . $post_title . '</a>';
2108
			$comments_echo .= geodir_get_rating_stars( $comment->overall_rating, $comment_post_ID );
2109
			$comments_echo .= '<p class="geodir_reviewer_text">' . $comment_excerpt . '';
2110
			//echo preg_replace('#(\\[img\\]).+(\\[\\/img\\])#', '', $comment_excerpt);
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...
2111
			$comments_echo .= '</p>';
2112
2113
			$comments_echo .= "</span>\n";
2114
			$comments_echo .= '</li>';
2115
		}
2116
	}
2117
2118
	return $comments_echo;
2119
}
2120
2121
/**
2122
 * Returns All post categories from all GD post types.
2123
 *
2124
 * @since   1.0.0
2125
 * @package GeoDirectory
2126
 * @return array Returns post categories as an array.
2127
 */
2128
function geodir_home_map_cats_key_value_array() {
2129
	$post_types = geodir_get_posttypes( 'object' );
2130
2131
	$return = array();
2132
	if ( ! empty( $post_types ) ) {
2133
		foreach ( $post_types as $key => $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...
2134
			$cpt_name       = __( $post_type->labels->singular_name, 'geodirectory' );
2135
			$post_type_name = sprintf( __( '%s Categories', 'geodirectory' ), $cpt_name );
2136
			$taxonomies     = geodir_get_taxonomies( $key );
2137
			$cat_taxonomy   = ! empty( $taxonomies[0] ) ? $taxonomies[0] : null;
2138
			$cat_terms      = $cat_taxonomy ? get_terms( $cat_taxonomy ) : null;
2139
2140
			if ( ! empty( $cat_terms ) ) {
2141
				$return[ 'optgroup_start-' . $key ] = $post_type_name;
2142
2143
				foreach ( $cat_terms as $cat_term ) {
2144
					$return[ $key . '_' . $cat_term->term_id ] = $cat_term->name;
2145
				}
2146
2147
				$return[ 'optgroup_end-' . $key ] = $post_type_name;
2148
			}
2149
		}
2150
	}
2151
2152
	return $return;
2153
}
2154
2155
/**
2156
 * "Twitter tweet" button code.
2157
 *
2158
 * To display "Twitter tweet" button, you can call this function.
2159
 * @since   1.0.0
2160
 * @package GeoDirectory
2161
 */
2162
function geodir_twitter_tweet_button() {
2163
	if ( isset( $_GET['gde'] ) ) {
2164
		$link = '?url=' . urlencode( geodir_curPageURL() );
2165
	} else {
2166
		$link = '';
2167
	}
2168
	?>
2169
	<a href="http://twitter.com/share<?php echo $link; ?>"
2170
	   class="twitter-share-button"><?php _e( 'Tweet', 'geodirectory' ); ?></a>
2171
	<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
2172
	<?php
2173
}
2174
2175
/**
2176
 * "Facebook like" button code.
2177
 *
2178
 * To display "Facebook like" button, you can call this function.
2179
 *
2180
 * @since   1.0.0
2181
 * @package GeoDirectory
2182
 * @global object $post The current post object.
2183
 */
2184
function geodir_fb_like_button() {
2185
	global $post;
2186
	?>
2187 View Code Duplication
	<iframe <?php if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false ) ) {
2188
		echo 'allowtransparency="true"';
2189
	} ?> class="facebook"
2190
	     src="//www.facebook.com/plugins/like.php?href=<?php echo urlencode( get_permalink( $post->ID ) ); ?>&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light"
2191
	     style="border:none; overflow:hidden; width:100px; height:20px"></iframe>
2192
	<?php
2193
}
2194
2195
/**
2196
 * "Google Plus" share button code.
2197
 *
2198
 * To display "Google Plus" share button, you can call this function.
2199
 *
2200
 * @since   1.0.0
2201
 * @package GeoDirectory
2202
 */
2203
function geodir_google_plus_button() {
2204
	?>
2205
	<div id="plusone-div" class="g-plusone" data-size="medium"></div>
2206
	<script type="text/javascript">
2207
		(function () {
2208
			var po = document.createElement('script');
2209
			po.type = 'text/javascript';
2210
			po.async = true;
2211
			po.src = 'https://apis.google.com/js/platform.js';
2212
			var s = document.getElementsByTagName('script')[0];
2213
			s.parentNode.insertBefore(po, s);
2214
		})();
2215
	</script>
2216
	<?php
2217
}
2218
2219
2220
function geodir_listing_bounce_map_pin_on_hover() {
2221
	if ( get_option( 'geodir_listing_hover_bounce_map_pin', true ) ) {
2222
		?>
2223
		<script>
2224
			jQuery(function ($) {
2225
				if (typeof(animate_marker) == 'function') {
2226
					var groupTab = $("ul.geodir_category_list_view").children("li");
2227
					groupTab.hover(function () {
2228
						animate_marker('listing_map_canvas', String($(this).data("post-id")));
2229
					}, function () {
2230
						stop_marker_animation('listing_map_canvas', String($(this).data("post-id")));
2231
					});
2232
				} else {
2233
					window.animate_marker = function () {
2234
					};
2235
					window.stop_marker_animation = function () {
2236
					};
2237
				}
2238
			});
2239
		</script>
2240
		<?php
2241
	}
2242
}
2243
2244
add_action( 'geodir_after_listing_listview', 'geodir_listing_bounce_map_pin_on_hover', 10 );
2245
2246
add_action( 'geodir_after_favorite_html', 'geodir_output_favourite_html_listings', 1, 1 );
2247
function geodir_output_favourite_html_listings( $post_id ) {
2248
	geodir_favourite_html( '', $post_id );
2249
}
2250
2251
add_action( 'geodir_listing_after_pinpoint', 'geodir_output_pinpoint_html_listings', 1, 2 );
2252
function geodir_output_pinpoint_html_listings( $post_id, $post = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_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...
2253
	global $wp_query;
2254
2255
	$show_pin_point = $wp_query->is_main_query();
2256
2257
	if ( ! empty( $post ) && ! empty( $show_pin_point ) && is_active_widget( false, "", "geodir_map_v3_listing_map" ) ) {
2258
		$term_icon_url = geodir_get_tax_meta( $post->default_category, 'ct_cat_icon', false, $post->post_type );
2259
		$marker_icon   = isset( $term_icon_url['src'] ) ? $term_icon_url['src'] : get_option( 'geodir_default_marker_icon' );
2260
		?>
2261
		<span class="geodir-pinpoint"
2262
		      style="background:url('<?php echo $marker_icon; ?>') no-repeat scroll left top transparent;background-size:auto 100%; -webkit-background-size:auto 100%;-moz-background-size:auto 100%;height:9px;width:14px;"><?php echo apply_filters( 'geodir_listing_listview_pinpoint_inner_content', '', 'listing' ); ?></span>
2263
		<a class="geodir-pinpoint-link" href="javascript:void(0)"
2264
		   onclick="if(typeof openMarker=='function'){openMarker('listing_map_canvas' ,'<?php echo $post->ID; ?>')}"
2265
		   onmouseover="if(typeof animate_marker=='function'){animate_marker('listing_map_canvas' ,'<?php echo $post->ID; ?>')}"
2266
		   onmouseout="if(typeof stop_marker_animation=='function'){stop_marker_animation('listing_map_canvas' ,'<?php echo $post->ID; ?>')}"><?php _e( 'Pinpoint', 'geodirectory' ); ?></a>
2267
		<?php
2268
	}
2269
}
2270
2271
function geodir_search_form_submit_button() {
2272
2273
	$new_style = get_option( 'geodir_show_search_old_search_from' ) ? false : true;
2274
2275
	if ( $new_style ) {
2276
		$default_search_button_label = '<i class="fa fa-search" aria-hidden="true"></i>';
2277
	}else{
2278
		$default_search_button_label = 'Search';
2279
	}
2280
	if ( get_option( 'geodir_search_button_label' ) && get_option( 'geodir_search_button_label' ) != 'Search' ) {
2281
		$default_search_button_label = __( get_option( 'geodir_search_button_label' ), 'geodirectory' );
2282
	}
2283
2284
	/**
2285
	 * Filter the default search button text value for the search form.
2286
	 *
2287
	 * This text can be changed via an option in settings, this is a last resort.
2288
	 *
2289
	 * @since 1.5.5
2290
	 *
2291
	 * @param string $default_search_button_label The current search button text.
2292
	 */
2293
	$default_search_button_label = apply_filters( 'geodir_search_default_search_button_text', $default_search_button_label );
2294
2295
	$fa_class = '';
2296
	if ( strpos( $default_search_button_label, '&#' ) !== false ) {
2297
		$fa_class = 'fa';
2298
	}
2299
2300
2301
	if ( $new_style ) {
2302
		?>
2303
		<button class="geodir_submit_search <?php echo $fa_class; ?>"><?php _e( $default_search_button_label ,'geodirectory'); ?></button>
2304
	<?php }else{?>
2305
		<input type="button" value="<?php esc_attr_e( $default_search_button_label ); ?>"
2306
		       class="geodir_submit_search <?php echo $fa_class; ?>"/>
2307
	<?php }
2308
}
2309
2310
add_action( 'geodir_before_search_button', 'geodir_search_form_submit_button', 5000 );
2311
2312
function geodir_search_form_post_type_input() {
2313
	global $geodir_search_post_type;
2314
	$post_types     = apply_filters( 'geodir_search_form_post_types', geodir_get_posttypes( 'object' ) );
2315
	$curr_post_type = $geodir_search_post_type;
2316
2317
	if ( ! empty( $post_types ) && count( (array) $post_types ) > 1 ) {
2318
2319 View Code Duplication
		foreach ( $post_types as $post_type => $info ){
2320
			global $wpdb;
2321
			$has_posts = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_status='publish' LIMIT 1", $post_type ) );
2322
			if ( ! $has_posts ) {
2323
				unset($post_types->{$post_type});
2324
			}
2325
		}
2326
2327
		if ( ! empty( $post_types ) && count( (array) $post_types ) > 1 ) {
2328
2329
			$new_style = get_option( 'geodir_show_search_old_search_from' ) ? false : true;
2330
			if ( $new_style ) {
2331
				echo "<div class='gd-search-input-wrapper gd-search-field-cpt'>";
2332
			}
2333
			?>
2334
			<select name="stype" class="search_by_post">
2335
				<?php foreach ( $post_types as $post_type => $info ):
2336
					global $wpdb;
2337
					?>
2338
2339
					<option data-label="<?php echo get_post_type_archive_link( $post_type ); ?>"
2340
					        value="<?php echo $post_type; ?>" <?php if ( isset( $_REQUEST['stype'] ) ) {
2341
						if ( $post_type == $_REQUEST['stype'] ) {
2342
							echo 'selected="selected"';
2343
						}
2344
					} elseif ( $curr_post_type == $post_type ) {
2345
						echo 'selected="selected"';
2346
					} ?>><?php _e( geodir_utf8_ucfirst( $info->labels->name ), 'geodirectory' ); ?></option>
2347
2348
				<?php endforeach; ?>
2349
			</select>
2350
			<?php
2351
			if ( $new_style ) {
2352
				echo "</div>";
2353
			}
2354
		}else{
2355
			if(! empty( $post_types )){
2356
				$pt_arr = (array)$post_types;
2357
				echo '<input type="hidden" name="stype" value="' . key( $pt_arr  ) . '"  />';
2358
			}else{
2359
				echo '<input type="hidden" name="stype" value="gd_place"  />';
2360
			}
2361
2362
		}
2363
2364
	}elseif ( ! empty( $post_types ) ) {
2365
		echo '<input type="hidden" name="stype" value="gd_place"  />';
2366
	}
2367
}
2368
2369
function geodir_search_form_search_input() {
2370
2371
	$default_search_for_text = SEARCH_FOR_TEXT;
2372
	if ( get_option( 'geodir_search_field_default_text' ) ) {
2373
		$default_search_for_text = __( get_option( 'geodir_search_field_default_text' ), 'geodirectory' );
2374
	}
2375
2376
	$new_style = get_option('geodir_show_search_old_search_from') ? false : true;
2377
	if($new_style){
2378
		echo "<div class='gd-search-input-wrapper gd-search-field-search'>";
2379
	}
2380
	?>
2381
	<input class="search_text" name="s"
2382
	       value="<?php if ( isset( $_REQUEST['s'] ) && trim( $_REQUEST['s'] ) != '' ) {
2383
		       echo esc_attr( stripslashes_deep( $_REQUEST['s'] ) );
2384
	       } else {
2385
		       echo $default_search_for_text;
2386
	       } ?>" type="text"
2387
	       onblur="if (this.value.trim() == '') {this.value = '<?php echo esc_sql( $default_search_for_text ); ?>';}"
2388
	       onfocus="if (this.value == '<?php echo esc_sql( $default_search_for_text ); ?>') {this.value = '';}"
2389
	       onkeydown="javascript: if(event.keyCode == 13) geodir_click_search(this);">
2390
	<?php
2391
	if($new_style){
2392
		echo "</div>";
2393
	}
2394
}
2395
2396
function geodir_search_form_near_input() {
2397
2398
	$default_near_text = NEAR_TEXT;
2399
	if ( get_option( 'geodir_near_field_default_text' ) ) {
2400
		$default_near_text = __( get_option( 'geodir_near_field_default_text' ), 'geodirectory' );
2401
	}
2402
2403
	if ( isset( $_REQUEST['snear'] ) && $_REQUEST['snear'] != '' ) {
2404
		$near = esc_attr( stripslashes_deep( $_REQUEST['snear'] ) );
2405
	} else {
2406
		$near = $default_near_text;
2407
	}
2408
2409
2410
	global $geodir_search_post_type;
2411
	$curr_post_type = $geodir_search_post_type;
2412
	/**
2413
	 * Used to hide the near field and other things.
2414
	 *
2415
	 * @since 1.6.9
2416
	 * @param string $curr_post_type The current post type.
2417
	 */
2418
	$near_input_extra = apply_filters('geodir_near_input_extra','',$curr_post_type);
2419
2420
2421
	/**
2422
	 * Filter the "Near" text value for the search form.
2423
	 *
2424
	 * This is the input "value" attribute and can change depending on what the user searches and is not always the default value.
2425
	 *
2426
	 * @since 1.0.0
2427
	 *
2428
	 * @param string $near              The current near value.
2429
	 * @param string $default_near_text The default near value.
2430
	 */
2431
	$near = apply_filters( 'geodir_search_near_text', $near, $default_near_text );
2432
	/**
2433
	 * Filter the default "Near" text value for the search form.
2434
	 *
2435
	 * This is the default value if nothing has been searched.
2436
	 *
2437
	 * @since 1.0.0
2438
	 *
2439
	 * @param string $near              The current near value.
2440
	 * @param string $default_near_text The default near value.
2441
	 */
2442
	$default_near_text = apply_filters( 'geodir_search_default_near_text', $default_near_text, $near );
2443
	/**
2444
	 * Filter the class for the near search input.
2445
	 *
2446
	 * @since 1.0.0
2447
	 *
2448
	 * @param string $class The class for the HTML near input, default is blank.
2449
	 */
2450
	$near_class = apply_filters( 'geodir_search_near_class', '' );
2451
2452
	$new_style = get_option('geodir_show_search_old_search_from') ? false : true;
2453
	if($new_style){
2454
		echo "<div class='gd-search-input-wrapper gd-search-field-near' $near_input_extra>";
2455
2456
		do_action('geodir_before_near_input');
2457
	}
2458
2459
	?>
2460
	<input name="snear" class="snear <?php echo $near_class; ?>" type="text" value="<?php echo $near; ?>"
2461
	       onblur="if (this.value.trim() == '') {this.value = ('<?php echo esc_sql( $near ); ?>' != '' ? '<?php echo esc_sql( $near ); ?>' : '<?php echo $default_near_text; ?>');}"
2462
	       onfocus="if (this.value == '<?php echo $default_near_text; ?>' || this.value =='<?php echo esc_sql( $near ); ?>') {this.value = '';}"
2463
	       onkeydown="javascript: if(event.keyCode == 13) geodir_click_search(this);" <?php echo $near_input_extra;?>/>
2464
	<?php
2465
	if($new_style){
2466
		do_action('geodir_after_near_input');
2467
2468
		echo "</div>";
2469
	}
2470
}
2471
2472
add_action( 'geodir_search_form_inputs', 'geodir_search_form_post_type_input', 10 );
2473
add_action( 'geodir_search_form_inputs', 'geodir_search_form_search_input', 20 );
2474
add_action( 'geodir_search_form_inputs', 'geodir_search_form_near_input', 30 );
2475
2476
function geodir_get_search_post_type($pt=''){
2477
	global $geodir_search_post_type;
2478
2479
	if($pt!=''){return $geodir_search_post_type = $pt;}
2480
	if(!empty($geodir_search_post_type)){ return $geodir_search_post_type;}
2481
2482
	$geodir_search_post_type = geodir_get_current_posttype();
2483
2484
	if(!$geodir_search_post_type) {
2485
		$geodir_search_post_type = geodir_get_default_posttype();
2486
	}
2487
2488
2489
	return $geodir_search_post_type;
2490
}
2491
2492
function geodir_search_form(){
2493
2494
	geodir_get_search_post_type();
2495
2496
	geodir_get_template_part('listing', 'filter-form');
2497
2498
	// Always die in functions echoing ajax content
2499
	die();
2500
}
2501
2502
add_action( 'wp_ajax_geodir_search_form', 'geodir_search_form' );
2503
add_action( 'wp_ajax_nopriv_geodir_search_form', 'geodir_search_form' );
2504
2505
/**
2506
 * Check wpml active or not.
2507
 *
2508
 * @since 1.5.0
2509
 *
2510
 * @return True if WPML is active else False.
2511
 */
2512
function geodir_is_wpml() {
2513 10
	if (class_exists('SitePress') && function_exists('icl_object_id')) {
2514
		return true;
2515
	}
2516
2517 10
	return false;
2518
}
2519
2520
/**
2521
 * Get WPML language code for current term.
2522
 *
2523
 * @since 1.5.0
2524
 *
2525
 * @global object $sitepress Sitepress WPML object.
2526
 *
2527
 * @param int $element_id Post ID or Term id.
2528
 * @param string $element_type Element type. Ex: post_gd_place or tax_gd_placecategory.
2529
 * @return Language code.
2530
 */
2531
function geodir_get_language_for_element($element_id, $element_type) {
2532
	global $sitepress;
2533
2534
	return $sitepress->get_language_for_element($element_id, $element_type);
2535
}
2536
2537
/**
2538
 * Duplicate post details for WPML translation post.
2539
 *
2540
 * @since 1.5.0
2541
 * @since 1.6.16 Sync reviews if sync comments allowed.
2542
 *
2543
 * @param int $master_post_id Original Post ID.
2544
 * @param string $lang Language code for translating post.
2545
 * @param array $postarr Array of post data.
2546
 * @param int $tr_post_id Translation Post ID.
2547
 * @param bool $after_save If true it will force duplicate.
2548
 *                         Added to fix duplicate translation for front end.
2549
 */
2550
function geodir_icl_make_duplicate($master_post_id, $lang, $postarr, $tr_post_id, $after_save = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $postarr 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...
2551
	global $sitepress;
2552
2553
	$post_type = get_post_type($master_post_id);
2554
	$icl_ajx_action = !empty($_REQUEST['icl_ajx_action']) && $_REQUEST['icl_ajx_action'] == 'make_duplicates' ? true : false;
2555 View Code Duplication
	if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'wpml_duplicate_dashboard' && !empty($_REQUEST['duplicate_post_ids'])) {
2556
		$icl_ajx_action = true;
2557
	}
2558
2559
	if (in_array($post_type, geodir_get_posttypes())) {
2560
		if ($icl_ajx_action || $after_save) {
2561
			// Duplicate post details
2562
			geodir_icl_duplicate_post_details($master_post_id, $tr_post_id, $lang);
2563
2564
			// Duplicate taxonomies
2565
			geodir_icl_duplicate_taxonomies($master_post_id, $tr_post_id, $lang);
2566
2567
			// Duplicate post images
2568
			geodir_icl_duplicate_post_images($master_post_id, $tr_post_id, $lang);
2569
		}
2570
2571
		// Sync post reviews
2572
		if ($sitepress->get_setting('sync_comments_on_duplicates')) {
2573
			geodir_wpml_duplicate_post_reviews($master_post_id, $tr_post_id, $lang);
2574
		}
2575
	}
2576
}
2577
add_filter( 'icl_make_duplicate', 'geodir_icl_make_duplicate', 11, 4 );
2578
2579
/**
2580
 * Duplicate post listing manually after listing saved.
2581
 *
2582
 * @since 1.6.16 Sync reviews if sync comments allowed.
2583
 *
2584
 * @param int $post_id The Post ID.
2585
 * @param string $lang Language code for translating post.
0 ignored issues
show
Bug introduced by
There is no parameter named $lang. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
2586
 * @param array $request_info The post details in an array.
2587
 */
2588
function geodir_wpml_duplicate_listing($post_id, $request_info) {
2589
	global $sitepress;
2590
2591
	$icl_ajx_action = !empty($_REQUEST['icl_ajx_action']) && $_REQUEST['icl_ajx_action'] == 'make_duplicates' ? true : false;
2592 View Code Duplication
	if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'wpml_duplicate_dashboard' && !empty($_REQUEST['duplicate_post_ids'])) {
2593
		$icl_ajx_action = true;
2594
	}
2595
2596
	if (!$icl_ajx_action && in_array(get_post_type($post_id), geodir_get_posttypes()) && $post_duplicates = $sitepress->get_duplicates($post_id)) {
2597
		foreach ($post_duplicates as $lang => $dup_post_id) {
2598
			geodir_icl_make_duplicate($post_id, $lang, $request_info, $dup_post_id, true);
2599
		}
2600
	}
2601
}
2602
2603
/**
2604
 * Duplicate post reviews for WPML translation post.
2605
 *
2606
 * @since 1.6.16
2607
 *
2608
 * @global object $wpdb WordPress Database object.
2609
 *
2610
 * @param int $master_post_id Original Post ID.
2611
 * @param int $tr_post_id Translation Post ID.
2612
 * @param string $lang Language code for translating post.
2613
 * @return bool True for success, False for fail.
2614
 */
2615
function geodir_wpml_duplicate_post_reviews($master_post_id, $tr_post_id, $lang) {
2616
	global $wpdb;
2617
2618
	$reviews = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM " . GEODIR_REVIEW_TABLE . " WHERE post_id=%d ORDER BY id ASC", $master_post_id), ARRAY_A);
2619
2620
	if (!empty($reviews)) {
2621
		foreach ($reviews as $review) {
2622
			geodir_wpml_duplicate_post_review($review['comment_id'], $master_post_id, $tr_post_id, $lang);
2623
		}
2624
	}
2625
2626
	return false;
2627
}
2628
2629
/**
2630
 * Duplicate post general details for WPML translation post.
2631
 *
2632
 * @since 1.5.0
2633
 *
2634
 * @global object $wpdb WordPress Database object.
2635
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2636
 *
2637
 * @param int $master_post_id Original Post ID.
2638
 * @param int $tr_post_id Translation Post ID.
2639
 * @param string $lang Language code for translating post.
2640
 * @return bool True for success, False for fail.
2641
 */
2642
function geodir_icl_duplicate_post_details($master_post_id, $tr_post_id, $lang) {
0 ignored issues
show
Unused Code introduced by
The parameter $lang 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...
2643
	global $wpdb, $plugin_prefix;
2644
2645
	$post_type = get_post_type($master_post_id);
2646
	$post_table = $plugin_prefix . $post_type . '_detail';
2647
2648
	$query = $wpdb->prepare("SELECT * FROM " . $post_table . " WHERE post_id = %d", array($master_post_id));
2649
	$data = (array)$wpdb->get_row($query);
2650
2651
	if ( !empty( $data ) ) {
2652
		$data['post_id'] = $tr_post_id;
2653
		unset($data['default_category'], $data['marker_json'], $data['featured_image'], $data[$post_type . 'category']);
2654
		$wpdb->update($post_table, $data, array('post_id' => $tr_post_id));
2655
		return true;
2656
	}
2657
2658
	return false;
2659
}
2660
2661
/**
2662
 * Duplicate post taxonomies for WPML translation post.
2663
 *
2664
 * @since 1.5.0
2665
 *
2666
 * @global object $sitepress Sitepress WPML object.
2667
 * @global object $wpdb WordPress Database object.
2668
 *
2669
 * @param int $master_post_id Original Post ID.
2670
 * @param int $tr_post_id Translation Post ID.
2671
 * @param string $lang Language code for translating post.
2672
 * @return bool True for success, False for fail.
2673
 */
2674
function geodir_icl_duplicate_taxonomies($master_post_id, $tr_post_id, $lang) {
2675
	global $sitepress, $wpdb;
2676
	$post_type = get_post_type($master_post_id);
2677
2678
	remove_filter('get_term', array($sitepress,'get_term_adjust_id')); // AVOID filtering to current language
2679
2680
	$taxonomies = get_object_taxonomies($post_type);
2681
	foreach ($taxonomies as $taxonomy) {
2682
		$terms = get_the_terms($master_post_id, $taxonomy);
2683
		$terms_array = array();
2684
2685
		if ($terms) {
2686
			foreach ($terms as $term) {
2687
				$tr_id = apply_filters( 'translate_object_id',$term->term_id, $taxonomy, false, $lang);
2688
2689
				if (!is_null($tr_id)){
2690
					// not using get_term - unfiltered get_term
2691
					$translated_term = $wpdb->get_row($wpdb->prepare("
2692
                        SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id = t.term_id WHERE t.term_id = %d AND x.taxonomy = %s", $tr_id, $taxonomy));
2693
2694
					$terms_array[] = $translated_term->term_id;
2695
				}
2696
			}
2697
2698
			if (!is_taxonomy_hierarchical($taxonomy)){
2699
				$terms_array = array_unique( array_map( 'intval', $terms_array ) );
2700
			}
2701
2702
			wp_set_post_terms($tr_post_id, $terms_array, $taxonomy);
2703
2704
			if ($taxonomy == $post_type . 'category') {
2705
				geodir_set_postcat_structure($tr_post_id, $post_type . 'category');
2706
			}
2707
		}
2708
	}
2709
}
2710
2711
/**
2712
 * Duplicate post images for WPML translation post.
2713
 *
2714
 * @since 1.5.0
2715
 *
2716
 * @global object $wpdb WordPress Database object.
2717
 *
2718
 * @param int $master_post_id Original Post ID.
2719
 * @param int $tr_post_id Translation Post ID.
2720
 * @param string $lang Language code for translating post.
2721
 * @return bool True for success, False for fail.
2722
 */
2723
function geodir_icl_duplicate_post_images($master_post_id, $tr_post_id, $lang) {
0 ignored issues
show
Unused Code introduced by
The parameter $lang 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...
2724
	global $wpdb;
2725
2726
	$query = $wpdb->prepare("DELETE FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE mime_type like %s AND post_id = %d", array('%image%', $tr_post_id));
2727
	$wpdb->query($query);
2728
2729
	$query = $wpdb->prepare("SELECT * FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE mime_type like %s AND post_id = %d ORDER BY menu_order ASC", array('%image%', $master_post_id));
2730
	$post_images = $wpdb->get_results($query);
2731
2732
	if ( !empty( $post_images ) ) {
2733
		foreach ( $post_images as $post_image) {
2734
			$image_data = (array)$post_image;
2735
			unset($image_data['ID']);
2736
			$image_data['post_id'] = $tr_post_id;
2737
2738
			$wpdb->insert(GEODIR_ATTACHMENT_TABLE, $image_data);
2739
2740
			geodir_set_wp_featured_image($tr_post_id);
2741
		}
2742
2743
		return true;
2744
	}
2745
2746
	return false;
2747
}
2748
2749
2750
/**
2751
 * Duplicate post review for WPML translation post.
2752
 *
2753
 * @since 1.6.16
2754
 *
2755
 * @global object $wpdb WordPress Database object.
2756
 * @global string $plugin_prefix Geodirectory plugin table prefix.
2757
 *
2758
 * @param int $master_comment_id Original Comment ID.
2759
 * @param int $master_post_id Original Post ID.
2760
 * @param int $tr_post_id Translation Post ID.
2761
 * @param string $lang Language code for translating post.
2762
 * @return bool True for success, False for fail.
2763
 */
2764
function geodir_wpml_duplicate_post_review($master_comment_id, $master_post_id, $tr_post_id, $lang) {
0 ignored issues
show
Unused Code introduced by
The parameter $lang 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...
2765
	global $wpdb, $plugin_prefix, $sitepress;
2766
2767
	$review = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . GEODIR_REVIEW_TABLE . " WHERE comment_id=%d ORDER BY id ASC", $master_comment_id), ARRAY_A);
2768
2769
	if (empty($review)) {
2770
		return false;
2771
	}
2772
	if ($review['post_id'] != $master_post_id) {
2773
		$wpdb->query($wpdb->prepare("UPDATE " . GEODIR_REVIEW_TABLE . " SET post_id=%d WHERE comment_id=%d", $master_post_id, $master_comment_id));
2774
		geodir_update_postrating($master_post_id, $post_type);
0 ignored issues
show
Bug introduced by
The variable $post_type seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
2775
	}
2776
2777
	$tr_comment_id = geodir_wpml_duplicate_comment_exists($tr_post_id, $master_comment_id);
2778
2779
	if (empty($tr_comment_id)) {
2780
		return false;
2781
	}
2782
2783
	$post_type = get_post_type($master_post_id);
2784
	$post_table = $plugin_prefix . $post_type . '_detail';
2785
2786
	$translated_post = $wpdb->get_row($wpdb->prepare("SELECT post_title, post_latitude, post_longitude, post_city, post_region, post_country FROM " . $post_table . " WHERE post_id = %d", $tr_post_id), ARRAY_A);
2787
	if (empty($translated_post)) {
2788
		return false;
2789
	}
2790
2791
	$review['comment_id'] = $tr_comment_id;
2792
	$review['post_id'] = $tr_post_id;
2793
	$review['post_title'] = $translated_post['post_title'];
2794
	$review['post_city'] = $translated_post['post_city'];
2795
	$review['post_region'] = $translated_post['post_region'];
2796
	$review['post_country'] = $translated_post['post_country'];
2797
	$review['post_latitude'] = $translated_post['post_latitude'];
2798
	$review['post_longitude'] = $translated_post['post_longitude'];
2799
2800
	if (isset($review['id'])) {
2801
		unset($review['id']);
2802
	}
2803
2804
	$tr_review_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . GEODIR_REVIEW_TABLE . " WHERE comment_id=%d AND post_id=%d ORDER BY id ASC", $tr_comment_id, $tr_post_id));
2805
2806
	if ($tr_review_id) { // update review
2807
		$wpdb->update(GEODIR_REVIEW_TABLE, $review, array('id' => $tr_review_id));
2808
	} else { // insert review
2809
		$wpdb->insert(GEODIR_REVIEW_TABLE, $review);
2810
		$tr_review_id = $wpdb->insert_id;
2811
	}
2812
2813
	if ($tr_post_id) {
2814
		geodir_update_postrating($tr_post_id, $post_type);
2815
2816
		if (defined('GEODIRREVIEWRATING_VERSION') && get_option('geodir_reviewrating_enable_review') && $sitepress->get_setting('sync_comments_on_duplicates')) {
2817
			$wpdb->query($wpdb->prepare("DELETE FROM " . GEODIR_COMMENTS_REVIEWS_TABLE . " WHERE comment_id = %d", array($tr_comment_id)));
2818
			$likes = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . GEODIR_COMMENTS_REVIEWS_TABLE . " WHERE comment_id=%d ORDER BY like_date ASC", $master_comment_id, $tr_post_id), ARRAY_A);
2819
2820
			if (!empty($likes)) {
2821
				foreach ($likes as $like) {
2822
					unset($like['like_id']);
2823
					$like['comment_id'] = $tr_comment_id;
2824
2825
					$wpdb->insert(GEODIR_COMMENTS_REVIEWS_TABLE, $like);
2826
				}
2827
			}
2828
		}
2829
	}
2830
2831
	return $tr_review_id;
2832
}
2833
2834
/**
2835
 * Synchronize review for WPML translation post.
2836
 *
2837
 * @since 1.6.16
2838
 *
2839
 * @global object $wpdb WordPress Database object.
2840
 * @global object $sitepress Sitepress WPML object.
2841
 * @global array $gd_wpml_posttypes Geodirectory post types array.
2842
 *
2843
 * @param int $comment_id The Comment ID.
2844
 */
2845
function gepdir_wpml_sync_comment($comment_id) {
2846
	global $wpdb, $sitepress, $gd_wpml_posttypes;
2847
2848
	if (empty($gd_post_types)) {
0 ignored issues
show
Bug introduced by
The variable $gd_post_types seems to never exist, and therefore empty should always return true. 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...
2849
		$gd_wpml_posttypes = geodir_get_posttypes();
2850
	}
2851
2852
	$comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID=%d", $comment_id), ARRAY_A);
2853
	if (empty($comment)) {
2854
		return;
2855
	}
2856
2857
	$post_id = $comment['comment_post_ID'];
2858
	$post_type = $post_id ? get_post_type($post_id) : NULL;
2859
2860
	if (!($post_type && in_array($post_type, $gd_wpml_posttypes))) {
2861
		return;
2862
	}
2863
2864
	$post_duplicates = $sitepress->get_duplicates($post_id);
2865
	if (empty($post_duplicates)) {
2866
		return;
2867
	}
2868
2869
	foreach ($post_duplicates as $lang => $dup_post_id) {
2870
		if (empty($comment['comment_parent'])) {
2871
			geodir_wpml_duplicate_post_review($comment_id, $post_id, $dup_post_id, $lang);
2872
		}
2873
	}
2874
2875
	return true;
2876
}
2877
2878
/**
2879
 * Get the WPML duplicate comment ID of the comment.
2880
 *
2881
 * @since 1.6.16
2882
 *
2883
 * @global object $dup_post_id WordPress Database object.
2884
 *
2885
 * @param int $dup_post_id The duplicate post ID.
2886
 * @param int $original_cid The original Comment ID.
2887
 * @return int The duplicate comment ID.
2888
 */
2889
function geodir_wpml_duplicate_comment_exists($dup_post_id, $original_cid) {
2890
	global $wpdb;
2891
2892
	$duplicate = $wpdb->get_var(
2893
		$wpdb->prepare(
2894
			"   SELECT comm.comment_ID
2895
                FROM {$wpdb->comments} comm
2896
                JOIN {$wpdb->commentmeta} cm
2897
                    ON comm.comment_ID = cm.comment_id
2898
                WHERE comm.comment_post_ID = %d
2899
                    AND cm.meta_key = '_icl_duplicate_of'
2900
                    AND cm.meta_value = %d
2901
                LIMIT 1",
2902
			$dup_post_id,
2903
			$original_cid
2904
		)
2905
	);
2906
2907
	return $duplicate;
2908
}
2909
2910
/**
2911
 * Get the CPT that disabled review stars.
2912
 *
2913
 * @since 1.6.16
2914
 *
2915
 * @param string $post_type WP post type or WP texonomy. Ex: gd_place.
0 ignored issues
show
Bug introduced by
There is no parameter named $post_type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
2916
 * @param bool $taxonomy Whether $post_type is taxonomy or not.
0 ignored issues
show
Bug introduced by
There is no parameter named $taxonomy. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
2917
 * @return bool True if review star disabled, otherwise false.
2918
 */
2919
function geodir_rating_disabled_post_types() {
2920
	$post_types = get_option( 'geodir_disable_rating_cpt' );
2921
2922
	/**
2923
	 * Filter the post types array which have rating disabled.
2924
	 *
2925
	 * @since 1.6.16
2926
	 *
2927
	 * @param array $post_types Array of post types which have rating starts disabled.
2928
	 */
2929
	return apply_filters( 'geodir_rating_disabled_post_types', $post_types );
2930
}
2931
2932
/**
2933
 * Check review star disabled for certain CPT.
2934
 *
2935
 * @since 1.6.16
2936
 *
2937
 * @param string|int $post_type WP post type or Post ID or WP texonomy. Ex: gd_place.
2938
 * @param bool $taxonomy Whether $post_type is taxonomy or not.
2939
 * @return bool True if review star disabled, otherwise false.
2940
 */
2941
function geodir_cpt_has_rating_disabled( $post_type = '', $taxonomy = false ) {
2942
	$post_types = geodir_rating_disabled_post_types();
2943
2944
	if ( empty( $post_types ) ) {
2945
		return false;
2946
	}
2947
2948
	if ( is_int( $post_type ) ) {
2949
		$post_type = get_post_type( $post_type );
2950
	}
2951
2952
	if ( $taxonomy && !empty( $post_types ) ) {
2953
		$posttypes = array();
2954
2955
		foreach ( $post_types as $posttype ) {
0 ignored issues
show
Bug introduced by
The expression $post_types of type boolean is not traversable.
Loading history...
2956
			$posttypes[] = $posttype . 'category';
2957
			$posttypes[] = $posttype . '_tags';
2958
		}
2959
2960
		$post_types = $posttypes;
2961
	}
2962
2963
	$return = false;
2964
	if ( $post_type != '' && !empty( $post_types ) && in_array( $post_type, $post_types ) ) {
2965
		$return = true;
2966
	}
2967
2968
	return $return;
2969
}
2970
2971
/**
2972
 * Checks that Yoast SEO is disabled on GD pages.
2973
 *
2974
 * @since 1.6.18
2975
 *
2976
 * @return bool True if Yoast SEO disabled on GD pages.
2977
 */
2978
function geodir_disable_yoast_seo_metas() {
2979
	return (bool)get_option( 'geodir_disable_yoast_meta' );
2980
}
2981
2982
/**
2983
 * Checks the user allowed to duplicate listing or not for WPML.
2984
 *
2985
 * @since 1.6.18
2986
 *
2987
 * @param int $post_id The post ID.
2988
 * @return bool True if allowed.
2989
 */
2990
function geodir_wpml_allowed_to_duplicate( $post_id ) {
2991
	$allowed = false;
2992
2993
	if ( !geodir_is_wpml() || empty( $post_id ) ) {
2994
		return $allowed;
2995
	}
2996
2997
	$user_id = (int)get_current_user_id();
2998
2999
	if ( empty( $user_id ) ) {
3000
		return $allowed;
3001
	}
3002
3003
	$post_type = get_post_type( $post_id );
3004
	if ( !geodir_wpml_is_post_type_translated( $post_type ) || get_post_meta( $post_id, '_icl_lang_duplicate_of', true ) ) {
3005
		return $allowed;
3006
	}
3007
3008
	if ( geodir_listing_belong_to_current_user( $post_id ) ) {
3009
		$allowed = true;
3010
	}
3011
3012
	$disable_cpts = get_option( 'geodir_wpml_disable_duplicate' );
3013
	if ( $allowed && !empty( $disable_cpts ) && in_array( $post_type, $disable_cpts ) ) {
3014
		$allowed = false;
3015
	}
3016
3017
	/**
3018
	 * Filter the user allowed to duplicate listing or not for WPML.
3019
	 *
3020
	 * @param bool $allowed True if allowed.
3021
	 * @param int $post_id The post ID.
3022
	 */
3023
	return apply_filters( 'geodir_wpml_allowed_to_duplicate', $allowed, $post_id );
3024
}
3025
3026
/**
3027
 * Display WPML languages option in sidebar to allow authors to duplicate their listing.
3028
 *
3029
 * @since 1.6.18
3030
 *
3031
 * @global WP_Post|null $post The current post.
3032
 * @global bool $preview True if the current page is add listing preview page. False if not.
3033
 * @global object $sitepress Sitepress WPML object.
3034
 *
3035
 * @param string $content_html The output html of the geodir_edit_post_link() function.
3036
 * @return string Filtered html of the geodir_edit_post_link() function.
3037
 */
3038
function geodir_wpml_frontend_duplicate_listing( $content_html ) {
3039
	global $post, $preview, $sitepress;
3040
3041
	if ( !empty( $post->ID ) && !$preview && geodir_is_page( 'detail' ) && geodir_wpml_allowed_to_duplicate( $post->ID ) ) {
3042
		$post_id = $post->ID;
3043
		$element_type = 'post_' . get_post_type( $post_id );
3044
		$original_post_id = $sitepress->get_original_element_id( $post_id, $element_type );
3045
3046
		if ( $original_post_id == $post_id ) {
3047
			$wpml_languages = $sitepress->get_active_languages();
3048
			$post_language = $sitepress->get_language_for_element( $post_id, $element_type );
3049
3050
			if ( !empty( $wpml_languages ) && isset( $wpml_languages[ $post_language ] ) ) {
3051
				unset( $wpml_languages[ $post_language ] );
3052
			}
3053
3054
			if ( !empty( $wpml_languages ) ) {
3055
				$trid  = $sitepress->get_element_trid( $post_id, $element_type );
3056
				$element_translations = $sitepress->get_element_translations( $trid, $element_type );
3057
				$duplicates = $sitepress->get_duplicates( $post_id );
3058
3059
				$wpml_content = '<div class="geodir-company_info gd-detail-duplicate"><h3 class="widget-title">' . __( 'Translate Listing', 'geodirectory' ) . '</h3>';
3060
				$wpml_content .= '<table class="gd-duplicate-table" style="width:100%;margin:0"><tbody>';
3061
				$wpml_content .= '<tr style="border-bottom:solid 1px #efefef"><th style="padding:0 2px 2px 2px">' . __( 'Language', 'geodirectory' ) . '</th><th style="width:25px;"></th><th style="width:5em;text-align:center">' . __( 'Translate', 'geodirectory' ) . '</th></tr>';
3062
3063
				$needs_translation = false;
3064
3065
				foreach ( $wpml_languages as $lang_code => $lang ) {
3066
					$duplicates_text = '';
3067
					$translated = false;
3068
3069
					if ( !empty( $element_translations ) && isset( $element_translations[$lang_code] ) ) {
3070
						$translated = true;
3071
3072
						if ( !empty( $duplicates ) && isset( $duplicates[$lang_code] ) ) {
3073
							$duplicates_text = ' ' . __( '(duplicate)', 'geodirectory' );
3074
						}
3075
					} else {
3076
						$needs_translation = true;
3077
					}
3078
3079
					$wpml_content .= '<tr><td style="padding:4px">' . $lang['english_name'] . $duplicates_text . '</td><td>&nbsp;</td><td style="text-align:center;">';
3080
3081
					if ( $translated ) {
3082
						$wpml_content .= '<i class="fa fa-check" style="color:orange"></i>';
3083
					} else {
3084
						$wpml_content .= '<input name="gd_icl_dup[]" value="' . $lang_code . '" title="' . esc_attr__( 'Create duplicate', 'geodirectory' ) . '" type="checkbox">';
3085
					}
3086
3087
					$wpml_content .= '</td></tr>';
3088
				}
3089
3090
				if ( $needs_translation ) {
3091
					$nonce = wp_create_nonce( 'geodir_duplicate_nonce' );
3092
					$wpml_content .= '<tr><td>&nbsp;</td><td style="vertical-align:middle;padding-top:13px"><i style="display:none" class="fa fa-spin fa-refresh"></i></td><td style="padding:15px 3px 3px 3px;text-align:right"><button data-nonce="' . esc_attr( $nonce ) . '" data-post-id="' . $post_id . '" id="gd_make_duplicates" class="button-secondary">' . __( 'Duplicate', 'geodirectory' ) . '</button></td></tr>';
3093
				}
3094
3095
				$wpml_content .= '</tbody></table>';
3096
				$wpml_content .= '</div>';
3097
3098
				$content_html .= $wpml_content;
3099
			}
3100
		}
3101
	}
3102
3103
	return $content_html;
3104
}
3105
3106
/**
3107
 * Add setting for WPML front-end duplicate translation in design page setting section.
3108
 *
3109
 * @since 1.6.18
3110
 *
3111
 * @param array $settings GD design settings array.
3112
 * @return array Filtered GD design settings array..
3113
 */
3114
function geodir_wpml_duplicate_settings( $settings = array() ) {
3115
	$new_settings = array();
3116
3117
	foreach ( $settings as $key => $setting ) {
3118
3119
		if ( isset( $setting['type'] ) && $setting['type'] == 'sectionend' && $setting['id'] == 'detail_page_settings' ) {
3120
			$new_settings[] = array(
3121
				'name' => __('Disable WPML duplicate translation', 'geodirectory'),
3122
				'desc' => __('Select post types to disable front end WPML duplicate translation. For selected post types the WPML duplicate option will be disabled from listing detail page sidebar.', 'geodirectory'),
3123
				'tip' => '',
3124
				'id' => 'geodir_wpml_disable_duplicate',
3125
				'css' => 'min-width:300px;',
3126
				'std' => '',
3127
				'type' => 'multiselect',
3128
				'placeholder_text' => __('Select post types', 'geodirectory'),
3129
				'class' => 'chosen_select',
3130
				'options' => array_unique(geodir_post_type_setting_fun())
3131
			);
3132
		}
3133
		$new_settings[] = $setting;
3134
	}
3135
3136
	return $new_settings;
3137
}
3138
3139
/**
3140
 * Checks if a given taxonomy is currently translated.
3141
 *
3142
 * @since 1.6.22
3143
 *
3144
 * @param string $taxonomy name/slug of a taxonomy.
3145
 * @return bool true if the taxonomy is currently set to being translatable in WPML.
3146
 */
3147 View Code Duplication
function geodir_wpml_is_taxonomy_translated( $taxonomy ) {
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...
3148 3
	if ( empty( $taxonomy ) || !geodir_is_wpml() || !function_exists( 'is_taxonomy_translated' ) ) {
3149 3
		return false;
3150
	}
3151
3152
	if ( is_taxonomy_translated( $taxonomy ) ) {
3153
		return true;
3154
	}
3155
3156
	return false;
3157
}
3158
3159
/**
3160
 * Checks if a given post_type is currently translated.
3161
 *
3162
 * @since 1.6.22
3163
 *
3164
 * @param string $post_type name/slug of a post_type.
3165
 * @return bool true if the post_type is currently set to being translatable in WPML.
3166
 */
3167 View Code Duplication
function geodir_wpml_is_post_type_translated( $post_type ) {
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...
3168 3
	if ( empty( $post_type ) || !geodir_is_wpml() || !function_exists( 'is_post_type_translated' ) ) {
3169 3
		return false;
3170
	}
3171
3172
	if ( is_post_type_translated( $post_type ) ) {
3173
		return true;
3174
	}
3175
3176
	return false;
3177
}
3178
3179
/**
3180
 * Get the element in the WPML current language.
3181
 *
3182
 * @since 1.6.22
3183
 *
3184
 * @param int         $element_id                 Use term_id for taxonomies, post_id for posts
3185
 * @param string      $element_type               Use post, page, {custom post type name}, nav_menu, nav_menu_item, category, tag, etc.
3186
 *                                                You can also pass 'any', to let WPML guess the type, but this will only work for posts.
3187
 * @param bool        $return_original_if_missing Optional, default is FALSE. If set to true it will always return a value (the original value, if translation is missing).
3188
 * @param string|NULL $language_code              Optional, default is NULL. If missing, it will use the current language.
0 ignored issues
show
Documentation introduced by
There is no parameter named $language_code. Did you maybe mean $ulanguage_code?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
3189
 *                                                If set to a language code, it will return a translation for that language code or
3190
 *                                                the original if the translation is missing and $return_original_if_missing is set to TRUE.
3191
 *
3192
 * @return int|NULL
3193
 */
3194
function geodir_wpml_object_id( $element_id, $element_type = 'post', $return_original_if_missing = false, $ulanguage_code = null ) {
3195
	if ( geodir_is_wpml() ) {
3196
		if ( function_exists( 'wpml_object_id_filter' ) ) {
3197
			return apply_filters( 'wpml_object_id', $element_id, $element_type, $return_original_if_missing, $ulanguage_code );
3198
		} else {
3199
			return icl_object_id( $element_id, $element_type, $return_original_if_missing, $ulanguage_code );
3200
		}
3201
	}
3202
3203
	return $element_id;
3204
}