Completed
Push — master ( a7a38c...d582e6 )
by Warwick
03:03 queued 16s
created

includes/extras.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * LSX functions and definitions - Integrations - Extras
4
 *
5
 * @package    lsx
6
 * @subpackage extras
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
/**
14
 * Enable shortcode for text widget.
15
 *
16
 * @package    lsx
17
 * @subpackage extras
18
 */
19
add_filter( 'widget_text', 'shortcode_unautop' );
20
add_filter( 'widget_text', 'do_shortcode' );
21
22
if ( ! function_exists( 'lsx_kses_allowed_html' ) ) :
23
24
	/**
25
	 * Enable extra attributes (srcset, sizes) in img tag.
26
	 *
27
	 * @package    lsx
28
	 * @subpackage extras
29
	 */
30
	function lsx_kses_allowed_html( $allowedtags, $context ) {
31
		$allowedtags['img']['srcset'] = true;
32
		$allowedtags['img']['sizes']  = true;
33
34
		$allowedtags['input']['name'] = true;
35
		$allowedtags['input']['type'] = true;
36
		$allowedtags['input']['value'] = true;
37
		$allowedtags['input']['class'] = true;
38
		$allowedtags['input']['id'] = true;
39
		$allowedtags['script']['type'] = true;
40
		return $allowedtags;
41
	}
42
43
endif;
44
45
add_filter( 'wp_kses_allowed_html', 'lsx_kses_allowed_html', 10, 2 );
46
47
if ( ! function_exists( 'lsx_body_class' ) ) :
48
49
	/**
50
	 * Add and remove body_class() classes.
51
	 *
52
	 * @package    lsx
53
	 * @subpackage extras
54
	 */
55
	function lsx_body_class( $classes ) {
56
		global $post;
57
58
		$header_layout = get_theme_mod( 'lsx_header_layout', 'inline' );
59
		$classes[]     = 'header-' . $header_layout;
60
61
		if ( isset( $post ) ) {
62
			$classes[] = $post->post_name;
63
		}
64
65
		if ( class_exists( 'LSX_Banners' ) || empty( apply_filters( 'lsx_banner_plugin_disable', false ) ) ) {
66
			$post_types = array( 'page', 'post' );
67
			$post_types = apply_filters( 'lsx_allowed_post_type_banners', $post_types );
68
69
			if ( is_singular( $post_types ) && has_post_thumbnail() ) {
70
				$classes[] = 'page-has-banner';
71
			}
72
		}
73
74
		if ( function_exists( 'tour_operator' ) ) {
75
			$post_types = array( 'page', 'post' );
76
77
			$classes[] = 'to-active';
78
		}
79
80
		if ( has_nav_menu( 'top-menu' ) || has_nav_menu( 'top-menu-left' ) ) {
81
			$classes[] = 'has-top-menu';
82
		}
83
84
		$fixed_header = get_theme_mod( 'lsx_header_fixed', false );
85
86
		if ( false !== $fixed_header ) {
87
			$classes[] = 'top-menu-fixed';
88
		}
89
90
		$search_form  = get_theme_mod( 'lsx_header_search', false );
91
92
		if ( false !== $search_form ) {
93
			$classes[] = 'has-header-search';
94
		}
95
96
		$preloader_content  = get_theme_mod( 'lsx_preloader_content_status', false );
97
98
		if ( false !== $preloader_content ) {
99
			$classes[] = 'preloader-content-enable';
100
		}
101
102
		$register_enabled = get_option( 'users_can_register', false );
103
		if ( ( $register_enabled ) && is_page( 'my-account' ) && is_singular() ) {
104
			$classes[] = 'register-enabled';
105
		}
106
107
		return $classes;
108
	}
109
110
endif;
111
112
add_filter( 'body_class', 'lsx_body_class' );
113
114
if ( ! function_exists( 'lsx_embed_wrap' ) ) :
115
116
	/**
117
	 * Wrap embedded media as suggested by Readability.
118
	 *
119
	 * @package    lsx
120
	 * @subpackage extras
121
	 *
122
	 * @link https://gist.github.com/965956
123
	 * @link http://www.readability.com/publishers/guidelines#publisher
124
	 */
125
	function lsx_embed_wrap( $cache, $url, $attr = '', $post_id = '' ) {
126
		if ( false !== strpos( $cache, '<iframe' ) ) {
127
			return '<div class="entry-content-asset">' . $cache . '</div>';
128
		}
129
130
		return $cache;
131
	}
132
133
endif;
134
135
add_filter( 'embed_oembed_html', 'lsx_embed_wrap', 10, 4 );
136
137
if ( ! function_exists( 'lsx_remove_self_closing_tags' ) ) :
138
139
	/**
140
	 * Remove unnecessary self-closing tags.
141
	 *
142
	 * @package    lsx
143
	 * @subpackage extras
144
	 */
145
	function lsx_remove_self_closing_tags( $input ) {
146
		return str_replace( ' />', '>', $input );
147
	}
148
149
endif;
150
151
add_filter( 'get_avatar',          'lsx_remove_self_closing_tags' ); // <img />
152
add_filter( 'comment_id_fields',   'lsx_remove_self_closing_tags' ); // <input />
153
add_filter( 'post_thumbnail_html', 'lsx_remove_self_closing_tags' ); // <img />
154
155
if ( ! function_exists( 'lsx_is_element_empty' ) ) :
156
157
	/**
158
	 * Checks if a Nav $element is empty or not.
159
	 *
160
	 * @package    lsx
161
	 * @subpackage extras
162
	 */
163
	function lsx_is_element_empty( $element ) {
164
		$element = trim( $element );
165
		return empty( $element ) ? false : true;
166
	}
167
168
endif;
169
170
if ( ! function_exists( 'lsx_get_thumbnail' ) ) :
171
172
	/**
173
	 * return the responsive images.
174
	 *
175
	 * @package    lsx
176
	 * @subpackage extras
177
	 */
178
	function lsx_get_thumbnail( $size, $image_src = false ) {
179
		if ( false === $image_src ) {
180
			$post_id           = get_the_ID();
181
			$post_thumbnail_id = get_post_thumbnail_id( $post_id );
182
		} elseif ( false !== $image_src ) {
183
			if ( is_numeric( $image_src ) ) {
184
				$post_thumbnail_id = $image_src;
185
			} else {
186
				$post_thumbnail_id = lsx_get_attachment_id_from_src( $image_src );
187
			}
188
		}
189
190
		$size = apply_filters( 'lsx_thumbnail_size', $size );
191
		$img       = '';
0 ignored issues
show
$img 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...
192
		$lazy_img  = '';
193
		$image_url = '';
194
195
		if ( 'lsx-thumbnail-single' === $size || 'lsx-thumbnail-wide' === $size || 'lsx-thumbnail-square' === $size || 'thumbnail' === $size ) {
196
			$srcset = false;
197
			$img    = get_the_post_thumbnail_url( get_the_ID(), 'lsx-thumbnail-wide' );
198
		} else {
199
			$srcset = true;
200
			$img = wp_get_attachment_image_srcset( $post_thumbnail_id, $size );
201
202
			$temp_lazy = wp_get_attachment_image_src( $post_thumbnail_id, $size );
203
			if ( ! empty( $temp_lazy ) ) {
204
				$lazy_img = $temp_lazy[0];
205
			}
206
207
			if ( empty( $img ) ) {
208
				$srcset = false;
209
				if ( ! empty( $lazy_img ) ) {
210
					$img = $lazy_img;
211
				}
212
			}
213
		}
214
215
		if ( '' !== $img ) {
216
			$image_url = $img;
217
			$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" ';
218
			if ( $srcset ) {
219
				$img .= 'srcset="' . esc_attr( $image_url ) . '" ';
220
			} else {
221
				$img .= 'src="' . esc_url( $image_url ) . '" ';
222
			}
223
			$img .= '/>';
224
225
			$img = apply_filters( 'lsx_lazyload_filter_images', $img );
226
			$img = apply_filters( 'lsx_lazyload_slider_images', $img, $post_thumbnail_id, $size, $srcset, $image_url );
227
		}
228
229
		return $img;
230
	}
231
232
endif;
233
234
if ( ! function_exists( 'lsx_thumbnail' ) ) :
235
236
	/**
237
	 * Output the Resonsive Images.
238
	 *
239
	 * @package    lsx
240
	 * @subpackage extras
241
	 */
242
	function lsx_thumbnail( $size = 'thumbnail', $image_src = false ) {
243
		echo wp_kses_post( lsx_get_thumbnail( $size, $image_src ) );
244
	}
245
246
endif;
247
248
if ( ! function_exists( 'lsx_get_attachment_id_from_src' ) ) :
249
250
	/**
251
	 * Gets the attachments ID from the src.
252
	 *
253
	 * @package    lsx
254
	 * @subpackage extras
255
	 */
256
	function lsx_get_attachment_id_from_src( $image_src ) {
257
		$post_id = wp_cache_get( $image_src, 'lsx_get_attachment_id_from_src' );
258
259
		if ( false === $post_id ) {
260
			global $wpdb;
261
			$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid='%s' LIMIT 1", $image_src ) );
262
			wp_cache_set( $image_src, $post_id, 'lsx_get_attachment_id_from_src', 3600 );
263
		}
264
265
		return $post_id;
266
	}
267
268
endif;
269
270
if ( ! function_exists( 'lsx_page_banner' ) ) :
271
272
	/**
273
	 * Add Featured Image as Banner on Single Pages.
274
	 *
275
	 * @package    lsx
276
	 * @subpackage extras
277
	 */
278
	function lsx_page_banner() {
279
		if ( true === apply_filters( 'lsx_page_banner_disable', false ) ) {
280
			return;
281
		}
282
283
		$post_types = array( 'page', 'post' );
284
		$post_types = apply_filters( 'lsx_allowed_post_type_banners', $post_types );
285
286
		if ( is_singular( $post_types ) && has_post_thumbnail() ) :
287
			$bg_image = '';
288
289
			if ( has_post_thumbnail() ) {
290
				$temp_bg_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
291
				if ( ! empty( $temp_bg_image ) ) {
292
					$bg_image = $temp_bg_image[0];
293
				}
294
			}
295
296
			if ( '' !== $bg_image ) :
297
				?>
298
					<div class="page-banner-wrap">
299
						<div class="page-banner">
300
							<?php lsx_banner_inner_top(); ?>
301
302
							<div class="page-banner-image" style="background-image:url(<?php echo esc_attr( $bg_image ); ?>);"></div>
303
304
							<div class="container">
305
								<header class="page-header">
306
									<h1 class="page-title"><?php the_title(); ?></h1>
307
									<?php lsx_banner_content(); ?>
308
								</header>
309
							</div>
310
311
							<?php lsx_banner_inner_bottom(); ?>
312
						</div>
313
					</div>
314
				<?php
315
			endif;
316
		endif;
317
	}
318
319
endif;
320
321
add_filter( 'lsx_banner_disable', 'lsx_disable_banner_for_blocks' );
322
add_filter( 'lsx_global_header_disable', 'lsx_disable_banner_for_blocks' );
323
324
325
if ( ! function_exists( 'lsx_disable_banner_for_blocks' ) ) :
326
327
	/**
328
	 * Disable the Banner if the page is using Blocks
329
	 *
330
	 * @package    lsx
331
	 * @subpackage extras
332
	 *
333
	 * @param  $disable boolean
334
	 * @return boolean
335
	 */
336
	function lsx_disable_banner_for_blocks( $disable ) {
337
		$queried_object = get_queried_object_id();
338
		$show_on_front  = get_option( 'show_on_front' );
339
340
		if ( 'page' === $show_on_front && (int) get_option( 'page_for_posts' ) === $queried_object ) {
341
			return $disable;
342
		}
343
344
		if ( function_exists( 'has_blocks' ) && has_blocks() && ( ! is_archive() ) ) {
345
			$disable = true;
346
		}
347
		return $disable;
348
	}
349
350
endif;
351
352
add_action( 'lsx_header_after', 'lsx_page_banner' );
353
354
if ( ! function_exists( 'lsx_form_submit_button' ) ) :
355
356
	/**
357
	 * filter the Gravity Forms button type.
358
	 *
359
	 * @package    lsx
360
	 * @subpackage extras
361
	 *
362
	 * @param  $button String
363
	 * @param  $form   Object
364
	 * @return String
365
	 */
366
	function lsx_form_submit_button( $button, $form ) {
367
		return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
368
	}
369
370
endif;
371
372
add_filter( 'gform_submit_button', 'lsx_form_submit_button', 10, 2 );
373
374
if ( ! function_exists( 'lsx_excerpt_more' ) ) :
375
376
	/**
377
	 * Replaces the excerpt "more" text by a link.
378
	 *
379
	 * @package    lsx
380
	 * @subpackage extras
381
	 */
382
	function lsx_excerpt_more( $more ) {
383
		return '...';
384
	}
385
386
endif;
387
388
add_filter( 'excerpt_more', 'lsx_excerpt_more' );
389
390
if ( ! function_exists( 'lsx_the_excerpt_filter' ) ) :
391
392
	/**
393
	 * Add a continue reading link to the excerpt.
394
	 *
395
	 * @package    lsx
396
	 * @subpackage extras
397
	 */
398
	function lsx_the_excerpt_filter( $excerpt ) {
399
400
		$post_formats = array(
401
			'aside'   => 'aside',
402
			'gallery' => 'gallery',
403
			'link'    => 'link',
404
			'image'   => 'image',
405
			'quote'   => 'quote',
406
			'status'  => 'status',
407
			'video'   => 'video',
408
			'audio'   => 'audio',
409
		);
410
411
		$show_full_content = has_post_format( apply_filters( 'lsx_the_excerpt_filter_post_types', $post_formats ) );
412
413
		if ( ! $show_full_content ) {
414
			if ( '' !== $excerpt && ! stristr( $excerpt, 'moretag' ) ) {
415
				$pagination = wp_link_pages( array(
416
					'before'      => '<div class="lsx-postnav-wrapper"><div class="lsx-postnav">',
417
					'after'       => '</div></div>',
418
					'link_before' => '<span>',
419
					'link_after'  => '</span>',
420
					'echo'        => 0,
421
				) );
422
423
				if ( ! empty( $pagination ) ) {
424
					$excerpt .= $pagination;
425
				} else {
426
					$excerpt_more = '<p><a class="moretag" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'Read More', 'lsx' ) . '</a></p>';
427
					$excerpt .= apply_filters( 'excerpt_more_p', $excerpt_more );
428
				}
429
			}
430
		}
431
432
		return $excerpt;
433
	}
434
435
endif;
436
437
add_filter( 'the_excerpt', 'lsx_the_excerpt_filter' , 1 , 20 );
438
439
if ( ! function_exists( 'lsx_full_width_widget_classes' ) ) :
440
441
	/**
442
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
443
	 *
444
	 * @package    lsx
445
	 * @subpackage extras
446
	 */
447
	function lsx_full_width_widget_classes( $params ) {
448
		if ( is_admin() ) {
449
			return $params;
450
		}
451
452
		global $wp_registered_widgets;
453
454
		$widget_id   = $params[0]['widget_id'];
455
		$widget_name = $params[0]['widget_name'];
456
457
		if ( 'Text' === $widget_name ) {
458
			$wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
459
			$wp_registered_widgets[ $widget_id ]['callback'] = 'lsx_full_width_widget_custom_callback';
460
		}
461
462
		return $params;
463
	}
464
465
endif;
466
467
add_filter( 'dynamic_sidebar_params', 'lsx_full_width_widget_classes' );
468
469
if ( ! function_exists( 'lsx_full_width_widget_custom_callback' ) ) :
470
471
	/**
472
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
473
	 *
474
	 * @package    lsx
475
	 * @subpackage extras
476
	 */
477
	function full_width_widget_custom_callback() {
478
		global $wp_registered_widgets;
479
480
		$original_callback_params = func_get_args();
481
		$widget_id = $original_callback_params[0]['widget_id'];
482
483
		$original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
484
		$wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
485
486
		$widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
487
488
		$widget_classname = '';
489
490
		if ( is_callable( $original_callback ) ) {
491
			ob_start();
492
			call_user_func_array( $original_callback, $original_callback_params );
493
			$widget_output = ob_get_clean();
494
495
			echo wp_kses_post( apply_filters( 'lsx_widget_output', $widget_output, $widget_id_base, $widget_classname, $widget_id ) );
496
		}
497
	}
498
499
endif;
500
501
if ( ! function_exists( 'lsx_full_width_widget_output' ) ) :
502
503
	/**
504
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
505
	 *
506
	 * @package    lsx
507
	 * @subpackage extras
508
	 */
509
	function lsx_full_width_widget_output( $widget_output, $widget_id_base, $widget_id ) {
510
		if ( 'text' === $widget_id_base ) {
511
			if ( false !== strpos( $widget_output, '<div class="lsx-full-width-alt">' ) ) {
512
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width_alt"', $widget_output );
513
			} elseif ( false !== strpos( $widget_output, '<div class="lsx-full-width">' ) ) {
514
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width"', $widget_output );
515
			}
516
		}
517
518
		return $widget_output;
519
	}
520
521
endif;
522
523
add_filter( 'lsx_widget_output', 'lsx_full_width_widget_output', 10, 3 );
524
525
/**
526
 * Check if the content has a restricted post format that needs to show a full excerpt.
527
 */
528
function lsx_post_format_force_content_on_list() {
529
	$post_formats = apply_filters( 'lsx_post_format_force_content_on_list',
530
		array(
531
				'video' => 'video',
532
				'audio' => 'audio',
533
				'quote' => 'quote',
534
				'link' => 'link',
535
			)
536
	);
537
	$return = false;
538
	if ( ! has_post_format( $post_formats ) ) {
539
		$return = true;
540
	}
541
	return $return;
542
}
543
544
/**
545
 * Remove the Hentry Class Every
546
 */
547
function lsx_remove_hentry( $classes ) {
548
	if ( 'post' !== get_post_type() ) {
549
		$classes = array_diff( $classes, array( 'hentry' ) );
550
	}
551
	return $classes;
552
}
553
add_filter( 'post_class','lsx_remove_hentry' );
554
555
/**
556
 * Strip Excerpts.
557
 *
558
 */
559
function lsx_strip_excerpt( $content ) {
560
	if ( is_search() || is_archive() || ( is_blog_installed() && ! is_single() && ! is_page() ) ) {
561
		$content = strip_shortcodes( $content );
562
		$content = str_replace( ']]>', ']]&gt;', $content );
563
		$content = strip_tags( $content );
564
	}
565
	return $content;
566
}
567
add_filter( 'the_content', 'lsx_strip_excerpt' );
568
569
/**
570
 * Disable Gutenberg for LSX Custom Post Tpes.
571
 *
572
 */
573
function lsx_disable_gutenberg_product_type( $is_enabled, $post_type ) {
574
	if ( 'testimonial' === $post_type || 'team' === $post_type || 'project' === $post_type ) {
575
		return false;
576
	}
577
578
	return $is_enabled;
579
}
580
add_filter( 'gutenberg_add_edit_link_for_post_type', 'lsx_disable_gutenberg_product_type', 10, 2 );
581
582
/**
583
 * Add the "Blog" link to the breadcrumbs
584
 * @param $crumbs
585
 * @return array
586
 */
587
function lsx_breadcrumbs_blog_link( $crumbs ) {
588
589
	$show_on_front = get_option( 'show_on_front' );
590
591
	if ( 'page' === $show_on_front && ( is_category() || is_tag() ) ) {
592
593
		$blog_page = get_option( 'page_for_posts' );
594
		if ( false !== $blog_page && '' !== $blog_page ) {
595
596
			$new_crumbs = array();
597
			$new_crumbs[0] = $crumbs[0];
598
599
			if ( function_exists( 'woocommerce_breadcrumb' ) ) {
600
				$new_crumbs[1] = array(
601
					0	=> get_the_title( $blog_page ),
602
					1	=> get_permalink( $blog_page ),
603
				);
604
			} else {
605
				$new_crumbs[1] = array(
606
					'text'	=> get_the_title( $blog_page ),
607
					'url'	=> get_permalink( $blog_page ),
608
				);
609
			}
610
			$new_crumbs[2] = $crumbs[1];
611
			$crumbs = $new_crumbs;
612
613
		}
614
	}
615
	return $crumbs;
616
}
617
add_filter( 'wpseo_breadcrumb_links', 'lsx_breadcrumbs_blog_link', 30, 1 );
618
add_filter( 'woocommerce_get_breadcrumb', 'lsx_breadcrumbs_blog_link', 30, 1 );
619