Issues (1057)

classes/class-to-reviews-frontend.php (1 issue)

1
<?php
2
/**
3
 * LSX_TO_Reviews_Frontend
4
 *
5
 * @package   LSX_TO_Reviews_Frontend
6
 * @author    LightSpeed
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2016 LightSpeedDevelopment
10
 */
11
12
/**
13
 * Main plugin class.
14
 *
15
 * @package LSX_TO_Reviews_Frontend
16
 * @author  LightSpeed
17
 */
18
19
class LSX_TO_Reviews_Frontend extends LSX_TO_Reviews {
20
21
	/**
22
	 * Holds the $page_links array while its being built on the single review page.
23
	 *
24
	 * @var array
25
	 */
26
	public $page_links = false;
27
28
	/**
29
	 * Constructor
30
	 */
31
	public function __construct() {
32
		$this->set_vars();
33
34
		add_action( 'wp_head', array( $this, 'change_single_review_layout' ), 20, 1 );
35
36
		add_filter( 'lsx_to_entry_class', array( $this, 'entry_class' ) );
37
		add_action( 'init', array( $this, 'init' ) );
38
39
		if ( ! class_exists( 'LSX_TO_Template_Redirects' ) ) {
40
			require_once( LSX_TO_REVIEWS_PATH . 'classes/class-template-redirects.php' );
41
		}
42
43
		$this->redirects = new LSX_TO_Template_Redirects( LSX_TO_REVIEWS_PATH, array_keys( $this->post_types ) );
0 ignored issues
show
Bug Best Practice introduced by
The property redirects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
45
		add_action( 'lsx_to_review_content', array( $this->redirects, 'content_part' ), 10 , 2 );
46
47
		add_filter( 'lsx_to_page_navigation', array( $this, 'page_links' ) );
48
49
		add_action( 'lsx_entry_top',      array( $this, 'archive_entry_top' ), 15 );
50
		add_action( 'lsx_entry_bottom',   array( $this, 'archive_entry_bottom' ) );
51
		add_action( 'lsx_content_bottom', array( $this, 'single_content_bottom' ) );
52
		add_action( 'lsx_to_fast_facts', array( $this, 'single_fast_facts' ) );
53
	}
54
55
	/**
56
	 * Change single review layout.
57
	 */
58
	public function change_single_review_layout() {
59
		global $lsx_to_archive;
60
61
		if ( is_singular( 'review' ) && 1 !== $lsx_to_archive ) {
62
			remove_action( 'lsx_entry_bottom', 'lsx_to_single_entry_bottom' );
63
			add_action( 'lsx_entry_top', array( $this, 'lsx_to_single_entry_bottom' ) );
64
		}
65
	}
66
67
	/**
68
	 * Change single review layout.
69
	 */
70
	public function lsx_to_single_entry_bottom() {
71
		if ( is_singular( 'review' ) ) { ?>
72
			<div class="col-xs-12 col-sm-4 col-md-3">
73
				<figure class="lsx-to-review-thumb">
74
					<?php lsx_thumbnail( 'lsx-thumbnail-square' ); ?>
75
				</figure>
76
77
				<?php $reviewer_name = get_post_meta( get_the_ID(), 'reviewer_name', true ); ?>
78
				<h3 class="lsx-to-summary-title text-center"><?php echo esc_html( $reviewer_name ); ?></h3>
79
			</div>
80
		<?php }
81
	}
82
83
	/**
84
	 * Runs on init after all files have been parsed.
85
	 */
86
	public function init() {
87
		add_filter( 'lsx_to_custom_field_query',array( $this, 'rating' ),5,10 );
88
	}
89
90
	/**
91
	 * A filter to set the content area to a small column on single
92
	 */
93
	public function entry_class( $classes ) {
94
		global $lsx_to_archive;
95
96
		if ( 1 !== $lsx_to_archive ) {
97
			$lsx_to_archive = false;
98
		}
99
100
		if ( is_main_query() && is_singular( 'review' ) && false === $lsx_to_archive ) {
101
			//if ( lsx_to_has_enquiry_contact() ) {
102
				$classes[] = 'col-xs-12 col-sm-8 col-md-9';
103
			//} else {
104
				//$classes[] = 'col-sm-12';
105
			//}
106
		}
107
108
		return $classes;
109
	}
110
111
	/**
112
	 * Filter and make the star ratings
113
	 */
114
	public function rating( $html = '', $meta_key = false, $value = false, $before = '', $after = '' ) {
115
		if ( get_post_type() === 'review' && 'rating' === $meta_key ) {
116
			$ratings_array = array();
117
			$counter       = 5;
118
			$html          = '';
119
			if ( 0 !== (int) $value ) {
120
				while ( $counter > 0 ) {
121
					if ( $value > 0 ) {
122
						$ratings_array[] = '<i class="fa fa-star"></i>';
123
					} else {
124
						$ratings_array[] = '<i class="fa fa-star-o"></i>';
125
					}
126
127
					$counter--;
128
					$value--;
129
				}
130
131
				$html = $before . implode( '', $ratings_array ) . $after;
132
			}
133
		}
134
		return $html;
135
	}
136
137
	/**
138
	 * Adds our navigation links to the review single post
139
	 *
140
	 * @param $page_links array
141
	 * @return $page_links array
142
	 */
143
	public function page_links( $page_links ) {
144
		if ( is_singular( 'review' ) ) {
145
			$this->page_links = $page_links;
146
147
			$this->get_related_accommodation_link();
148
			$this->get_related_tours_link();
149
			$this->get_related_destinations_link();
150
			$this->get_gallery_link();
151
			$this->get_videos_link();
152
			$this->get_related_posts_link();
153
154
			$page_links = $this->page_links;
155
		}
156
157
		return $page_links;
158
	}
159
160
	/**
161
	 * Tests for the Related Accommodation and returns a link for the section
162
	 */
163
	public function get_related_accommodation_link() {
164
		$connected_accommodation = get_post_meta( get_the_ID(), 'accommodation_to_review', false );
165
166
		if ( post_type_exists( 'accommodation' ) && is_array( $connected_accommodation ) && ! empty( $connected_accommodation ) ) {
167
			$connected_accommodation = new \WP_Query( array(
168
				'post_type' => 'accommodation',
169
				'post__in' => $connected_accommodation,
170
				'post_status' => 'publish',
171
				'nopagin' => true,
172
				'posts_per_page' => '-1',
173
				'fields' => 'ids',
174
			) );
175
176
			$connected_accommodation = $connected_accommodation->posts;
177
178
			if ( is_array( $connected_accommodation ) && ! empty( $connected_accommodation ) ) {
179
				$this->page_links['accommodation'] = esc_html__( 'Accommodation', 'to-reviews' );
180
			}
181
		}
182
	}
183
184
	/**
185
	 * Tests for the Related Tours and returns a link for the section
186
	 */
187
	public function get_related_tours_link() {
188
		$connected_tours = get_post_meta( get_the_ID(), 'tour_to_review', false );
189
190
		if ( post_type_exists( 'tour' ) && is_array( $connected_tours ) && ! empty( $connected_tours ) ) {
191
			$connected_tours = new \WP_Query( array(
192
				'post_type' => 'tour',
193
				'post__in' => $connected_tours,
194
				'post_status' => 'publish',
195
				'nopagin' => true,
196
				'posts_per_page' => '-1',
197
				'fields' => 'ids',
198
			) );
199
200
			$connected_tours = $connected_tours->posts;
201
202
			if ( is_array( $connected_tours ) && ! empty( $connected_tours ) ) {
203
				$this->page_links['tours'] = esc_html__( 'Tours', 'to-reviews' );
204
			}
205
		}
206
	}
207
208
	/**
209
	 * Tests for the Related Destinations and returns a link for the section
210
	 */
211
	public function get_related_destinations_link() {
212
		$connected_destination  = '';
213
		$connected_destinations = get_post_meta( get_the_ID(), 'destination_to_review', false );
214
215
		if ( post_type_exists( 'destination' ) && is_array( $connected_destinations ) && ! empty( $connected_destinations ) ) {
216
			$connected_destination = new \WP_Query( array(
217
				'post_type' => 'destination',
218
				'post__in' => $connected_destination,
219
				'post_status' => 'publish',
220
				'nopagin' => true,
221
				'posts_per_page' => '-1',
222
				'fields' => 'ids',
223
			) );
224
225
			$connected_destination = $connected_destination->posts;
226
227
			if ( is_array( $connected_destination ) && ! empty( $connected_destination ) ) {
228
				$this->page_links['destinations'] = esc_html__( 'Destinations', 'to-reviews' );
229
			}
230
		}
231
	}
232
233
	/**
234
	 * Tests for the Gallery and returns a link for the section
235
	 */
236
	public function get_gallery_link() {
237
		$gallery_ids = get_post_meta( get_the_ID(), 'gallery', false );
238
		$envira_gallery = get_post_meta( get_the_ID(), 'envira_gallery', true );
239
240
		if ( ( ! empty( $gallery_ids ) && is_array( $gallery_ids ) ) || ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) ) {
241
			if ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) {
242
				// Envira Gallery
243
				$this->page_links['gallery'] = esc_html__( 'Gallery', 'to-reviews' );
244
				return;
245
			} else {
246
				if ( function_exists( 'envira_dynamic' ) ) {
247
					// Envira Gallery - Dynamic
248
					$this->page_links['gallery'] = esc_html__( 'Gallery', 'to-reviews' );
249
					return;
250
				} else {
251
					// WordPress Gallery
252
					$this->page_links['gallery'] = esc_html__( 'Gallery', 'to-reviews' );
253
					return;
254
				}
255
			}
256
		}
257
	}
258
259
	/**
260
	 * Tests for the Videos and returns a link for the section
261
	 */
262
	public function get_videos_link() {
263
		$videos_id = false;
264
265
		if ( class_exists( 'Envira_Videos' ) ) {
266
			$videos_id = get_post_meta( get_the_ID(), 'envira_video', true );
267
		}
268
269
		if ( empty( $videos_id ) && function_exists( 'lsx_to_videos' ) ) {
270
			$videos_id = get_post_meta( get_the_ID(), 'videos', true );
271
		}
272
273
		if ( ! empty( $videos_id ) ) {
274
			$this->page_links['videos'] = esc_html__( 'Videos', 'to-reviews' );
275
		}
276
	}
277
278
	/**
279
	 * Tests for the Related Posts and returns a link for the section
280
	 */
281
	public function get_related_posts_link() {
282
		$connected_posts = get_post_meta( get_the_ID(), 'post_to_review', false );
283
284
		if ( is_array( $connected_posts ) && ! empty( $connected_posts ) ) {
285
			$connected_posts = new \WP_Query( array(
286
				'post_type' => 'post',
287
				'post__in' => $connected_posts,
288
				'post_status' => 'publish',
289
				'nopagin' => true,
290
				'posts_per_page' => '-1',
291
				'fields' => 'ids',
292
			) );
293
294
			$connected_posts = $connected_posts->posts;
295
296
			if ( is_array( $connected_posts ) && ! empty( $connected_posts ) ) {
297
				$this->page_links['posts'] = esc_html__( 'Posts', 'to-reviews' );
298
			}
299
		}
300
	}
301
302
	/**
303
	 * Adds the template tags to the top of the archive review
304
	 */
305
	public function archive_entry_top() {
306
		global $lsx_to_archive;
307
308
		if ( 'review' === get_post_type() && ( is_archive() || $lsx_to_archive ) ) {
309
			if ( is_search() || empty( tour_operator()->options[ get_post_type() ]['disable_entry_metadata'] ) ) { ?>
310
				<div class="lsx-to-archive-meta-data lsx-to-archive-meta-data-grid-mode">
311
					<?php
312
						$meta_class = 'lsx-to-meta-data lsx-to-meta-data-';
313
314
						lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . __( 'Accommodation', 'to-reviews' ) . ':</span> ', '</span>' );
315
						lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . __( 'Tours', 'to-reviews' ) . ':</span> ', '</span>' );
316
						lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . __( 'Destinations', 'to-reviews' ) . ':</span> ', '</span>' );
317
					?>
318
				</div>
319
			<?php } ?>
320
		<?php }
321
	}
322
323
	/**
324
	 * Adds the template tags to the bottom of the archive review
325
	 */
326
	public function archive_entry_bottom() {
327
		global $lsx_to_archive;
328
329
		if ( 'review' === get_post_type() && ( is_archive() || $lsx_to_archive ) ) { ?>
330
				</div>
331
332
				<?php if ( is_search() || empty( tour_operator()->options[ get_post_type() ]['disable_entry_metadata'] ) ) { ?>
333
					<div class="lsx-to-archive-meta-data lsx-to-archive-meta-data-list-mode">
334
						<?php
335
							$meta_class = 'lsx-to-meta-data lsx-to-meta-data-';
336
337
							lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . __( 'Accommodation', 'to-reviews' ) . ':</span> ', '</span>' );
338
							lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . __( 'Tours', 'to-reviews' ) . ':</span> ', '</span>' );
339
							lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . __( 'Destinations', 'to-reviews' ) . ':</span> ', '</span>' );
340
						?>
341
					</div>
342
				<?php } ?>
343
			</div>
344
345
			<?php $has_single = ! lsx_to_is_single_disabled(); ?>
346
347
			<?php if ( $has_single && 'grid' === tour_operator()->archive_layout ) : ?>
348
				<a href="<?php the_permalink(); ?>" class="moretag"><?php esc_html_e( 'View more', 'to-reviews' ); ?></a>
349
			<?php endif; ?>
350
		<?php }
351
	}
352
353
	/**
354
	 * Adds the template tags fast facts
355
	 */
356
	public function single_fast_facts() {
357
		if ( is_singular( 'review' ) ) { ?>
358
			<section id="fast-facts">
359
				<div class="lsx-to-single-meta-data">
360
					<?php
361
						$meta_class = 'lsx-to-meta-data lsx-to-meta-data-';
362
363
						lsx_to_review_rating( '<span class="' . $meta_class . 'rating"><span class="lsx-to-meta-data-key">' . esc_html__( 'Rating', 'to-reviews' ) . ':</span> ', '</span>' );
364
						lsx_to_review_dates( '<span class="' . $meta_class . 'travel-dates"><span class="lsx-to-meta-data-key">' . esc_html__( 'Date Travelled', 'to-reviews' ) . ':</span> ', '</span>' );
365
						lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . esc_html__( 'Accommodation', 'to-reviews' ) . ':</span> ', '</span>' );
366
						lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . esc_html__( 'Tours', 'to-reviews' ) . ':</span> ', '</span>' );
367
						lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . esc_html__( 'Destinations', 'to-reviews' ) . ':</span> ', '</span>' );
368
					?>
369
				</div>
370
			</section>
371
		<?php }
372
	}
373
374
	/**
375
	 * Adds the template tags to the bottom of the single review
376
	 */
377
	public function single_content_bottom() {
378
		if ( is_singular( 'review' ) ) {
379
			lsx_to_review_accommodation();
380
381
			lsx_to_review_tour();
382
383
			lsx_to_review_destination();
384
385
			lsx_to_gallery( '<section id="gallery" class="lsx-to-section lsx-to-collapse-section"><h2 class="lsx-to-section-title lsx-to-collapse-title lsx-title" data-toggle="collapse" data-target="#collapse-gallery">' . esc_html__( 'Gallery', 'to-reviews' ) . '</h2><div id="collapse-gallery" class="collapse in"><div class="collapse-inner">', '</div></div></section>' );
386
387
			if ( function_exists( 'lsx_to_videos' ) ) {
388
				lsx_to_videos( '<section id="videos" class="lsx-to-section lsx-to-collapse-section"><h2 class="lsx-to-section-title lsx-to-collapse-title lsx-title" data-toggle="collapse" data-target="#collapse-videos">' . esc_html__( 'Videos', 'to-reviews' ) . '</h2><div id="collapse-videos" class="collapse in"><div class="collapse-inner">', '</div></div></section>' );
389
			} elseif ( class_exists( 'Envira_Videos' ) ) {
390
				lsx_to_envira_videos( '<section id="videos" class="lsx-to-section lsx-to-collapse-section"><h2 class="lsx-to-section-title lsx-to-collapse-title lsx-title" data-toggle="collapse" data-target="#collapse-videos">' . esc_html__( 'Videos', 'to-reviews' ) . '</h2><div id="collapse-videos" class="collapse in"><div class="collapse-inner">', '</div></div></section>' );
391
			}
392
393
			lsx_to_review_posts();
394
		}
395
	}
396
397
}
398
399
new LSX_TO_Reviews_Frontend();
400