Passed
Push — master ( fa9e4a...d9cbdc )
by Warwick
02:52
created

extras.php ➔ full_width_widget_custom_callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $context 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...
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 ( has_nav_menu( 'top-menu' ) || has_nav_menu( 'top-menu-left' ) ) {
75
			$classes[] = 'has-top-menu';
76
		}
77
78
		$fixed_header = get_theme_mod( 'lsx_header_fixed', false );
79
80
		if ( false !== $fixed_header ) {
81
			$classes[] = 'top-menu-fixed';
82
		}
83
84
		$search_form  = get_theme_mod( 'lsx_header_search', false );
85
86
		if ( false !== $search_form ) {
87
			$classes[] = 'has-header-search';
88
		}
89
90
		$preloader_content  = get_theme_mod( 'lsx_preloader_content_status', false );
91
92
		if ( false !== $preloader_content ) {
93
			$classes[] = 'preloader-content-enable';
94
		}
95
96
		return $classes;
97
	}
98
99
endif;
100
101
add_filter( 'body_class', 'lsx_body_class' );
102
103
if ( ! function_exists( 'lsx_embed_wrap' ) ) :
104
105
	/**
106
	 * Wrap embedded media as suggested by Readability.
107
	 *
108
	 * @package    lsx
109
	 * @subpackage extras
110
	 *
111
	 * @link https://gist.github.com/965956
112
	 * @link http://www.readability.com/publishers/guidelines#publisher
113
	 */
114
	function lsx_embed_wrap( $cache, $url, $attr = '', $post_id = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $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...
115
		if ( false !== strpos( $cache, '<iframe' ) ) {
116
			return '<div class="entry-content-asset">' . $cache . '</div>';
117
		}
118
119
		return $cache;
120
	}
121
122
endif;
123
124
add_filter( 'embed_oembed_html', 'lsx_embed_wrap', 10, 4 );
125
126
if ( ! function_exists( 'lsx_remove_self_closing_tags' ) ) :
127
128
	/**
129
	 * Remove unnecessary self-closing tags.
130
	 *
131
	 * @package    lsx
132
	 * @subpackage extras
133
	 */
134
	function lsx_remove_self_closing_tags( $input ) {
135
		return str_replace( ' />', '>', $input );
136
	}
137
138
endif;
139
140
add_filter( 'get_avatar',          'lsx_remove_self_closing_tags' ); // <img />
141
add_filter( 'comment_id_fields',   'lsx_remove_self_closing_tags' ); // <input />
142
add_filter( 'post_thumbnail_html', 'lsx_remove_self_closing_tags' ); // <img />
143
144
if ( ! function_exists( 'lsx_is_element_empty' ) ) :
145
146
	/**
147
	 * Checks if a Nav $element is empty or not.
148
	 *
149
	 * @package    lsx
150
	 * @subpackage extras
151
	 */
152
	function lsx_is_element_empty( $element ) {
153
		$element = trim( $element );
154
		return empty( $element ) ? false : true;
155
	}
156
157
endif;
158
159
if ( ! function_exists( 'lsx_get_thumbnail' ) ) :
160
161
	/**
162
	 * return the responsive images.
163
	 *
164
	 * @package    lsx
165
	 * @subpackage extras
166
	 */
167
	function lsx_get_thumbnail( $size, $image_src = false ) {
168
		if ( false === $image_src ) {
169
			$post_id           = get_the_ID();
170
			$post_thumbnail_id = get_post_thumbnail_id( $post_id );
171
		} elseif ( false !== $image_src ) {
172
			if ( is_numeric( $image_src ) ) {
173
				$post_thumbnail_id = $image_src;
174
			} else {
175
				$post_thumbnail_id = lsx_get_attachment_id_from_src( $image_src );
176
			}
177
		}
178
179
		$size = apply_filters( 'lsx_thumbnail_size', $size );
180
		$img = '';
181
182
		if ( 'lsx-thumbnail-single' === $size || 'lsx-thumbnail-wide' === $size || 'lsx-thumbnail-square' === $size || 'thumbnail' === $size ) {
183
			$srcset = false;
184
			$temp_img    = wp_get_attachment_image_src( $post_thumbnail_id, $size );
0 ignored issues
show
Bug introduced by
The variable $post_thumbnail_id 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...
185
			if ( ! empty( $temp_img ) ) {
186
				$img    = $temp_img[0];
187
			}
188
		} else {
189
			$srcset = true;
190
			$img = wp_get_attachment_image_srcset( $post_thumbnail_id, $size );
191
192
			if ( empty( $img ) ) {
193
				$srcset = false;
194
				$temp_img = wp_get_attachment_image_src( $post_thumbnail_id, $size );
195
				if ( ! empty( $temp_img ) ) {
196
					$img = $temp_img[0];
197
				}
198
			}
199
		}
200
201
		if ( '' !== $img ) {
202
			if ( $srcset ) {
203
				$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" srcset="' . esc_attr( $img ) . '" />';
204
			} else {
205
				$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" src="' . esc_url( $img ) . '" />';
206
			}
207
			$img = apply_filters( 'lsx_lazyload_filter_images', $img );
208
		}
209
210
		return $img;
211
	}
212
213
endif;
214
215
if ( ! function_exists( 'lsx_thumbnail' ) ) :
216
217
	/**
218
	 * Output the Resonsive Images.
219
	 *
220
	 * @package    lsx
221
	 * @subpackage extras
222
	 */
223
	function lsx_thumbnail( $size = 'thumbnail', $image_src = false ) {
224
		echo wp_kses_post( lsx_get_thumbnail( $size, $image_src ) );
225
	}
226
227
endif;
228
229
if ( ! function_exists( 'lsx_get_attachment_id_from_src' ) ) :
230
231
	/**
232
	 * Gets the attachments ID from the src.
233
	 *
234
	 * @package    lsx
235
	 * @subpackage extras
236
	 */
237
	function lsx_get_attachment_id_from_src( $image_src ) {
238
		$post_id = wp_cache_get( $image_src, 'lsx_get_attachment_id_from_src' );
239
240
		if ( false === $post_id ) {
241
			global $wpdb;
242
			$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid='%s' LIMIT 1", $image_src ) );
243
			wp_cache_set( $image_src, $post_id, 'lsx_get_attachment_id_from_src', 3600 );
244
		}
245
246
		return $post_id;
247
	}
248
249
endif;
250
251
if ( ! function_exists( 'lsx_page_banner' ) ) :
252
253
	/**
254
	 * Add Featured Image as Banner on Single Pages.
255
	 *
256
	 * @package    lsx
257
	 * @subpackage extras
258
	 */
259
	function lsx_page_banner() {
260
		if ( true === apply_filters( 'lsx_page_banner_disable', false ) ) {
261
			return;
262
		}
263
264
		$post_types = array( 'page', 'post' );
265
		$post_types = apply_filters( 'lsx_allowed_post_type_banners', $post_types );
266
267
		if ( is_singular( $post_types ) && has_post_thumbnail() ) :
268
			$bg_image = '';
269
270
			if ( has_post_thumbnail() ) {
271
				$temp_bg_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
272
				if ( ! empty( $temp_bg_image ) ) {
273
					$bg_image = $temp_bg_image[0];
274
				}
275
			}
276
277
			if ( '' !== $bg_image ) :
278
				?>
279
					<div class="page-banner-wrap">
280
						<div class="page-banner">
281
							<?php lsx_banner_inner_top(); ?>
282
283
							<div class="page-banner-image" style="background-image:url(<?php echo esc_attr( $bg_image ); ?>);"></div>
284
285
							<div class="container">
286
								<header class="page-header">
287
									<h1 class="page-title"><?php the_title(); ?></h1>
288
									<?php lsx_banner_content(); ?>
289
								</header>
290
							</div>
291
292
							<?php lsx_banner_inner_bottom(); ?>
293
						</div>
294
					</div>
295
				<?php
296
			endif;
297
		endif;
298
	}
299
300
endif;
301
302
add_filter( 'lsx_banner_disable', 'lsx_disable_banner_for_blocks' );
303
add_filter( 'lsx_global_header_disable', 'lsx_disable_banner_for_blocks' );
304
305
306
if ( ! function_exists( 'lsx_disable_banner_for_blocks' ) ) :
307
308
	/**
309
	 * Disable the Banner if the page is using Blocks
310
	 *
311
	 * @package    lsx
312
	 * @subpackage extras
313
	 *
314
	 * @param  $disable boolean
315
	 * @return boolean
316
	 */
317
	function lsx_disable_banner_for_blocks( $disable ) {
318
		$queried_object = get_queried_object();
319
		$show_on_front  = get_option( 'show_on_front' );
320
321
		if ( 'page' === $show_on_front && (int) get_option( 'page_for_posts' ) === $queried_object->ID ) {
322
			return $disable;
323
		}
324
325
		if ( function_exists( 'has_blocks' ) && has_blocks() ) {
326
			$disable = true;
327
		}
328
		return $disable;
329
	}
330
331
endif;
332
333
add_action( 'lsx_header_after', 'lsx_page_banner' );
334
335
if ( ! function_exists( 'lsx_form_submit_button' ) ) :
336
337
	/**
338
	 * filter the Gravity Forms button type.
339
	 *
340
	 * @package    lsx
341
	 * @subpackage extras
342
	 *
343
	 * @param  $button String
344
	 * @param  $form   Object
345
	 * @return String
346
	 */
347
	function lsx_form_submit_button( $button, $form ) {
0 ignored issues
show
Unused Code introduced by
The parameter $button 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...
348
		return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
349
	}
350
351
endif;
352
353
add_filter( 'gform_submit_button', 'lsx_form_submit_button', 10, 2 );
354
355
if ( ! function_exists( 'lsx_excerpt_more' ) ) :
356
357
	/**
358
	 * Replaces the excerpt "more" text by a link.
359
	 *
360
	 * @package    lsx
361
	 * @subpackage extras
362
	 */
363
	function lsx_excerpt_more( $more ) {
0 ignored issues
show
Unused Code introduced by
The parameter $more 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...
364
		return '...';
365
	}
366
367
endif;
368
369
add_filter( 'excerpt_more', 'lsx_excerpt_more' );
370
371
if ( ! function_exists( 'lsx_the_excerpt_filter' ) ) :
372
373
	/**
374
	 * Add a continue reading link to the excerpt.
375
	 *
376
	 * @package    lsx
377
	 * @subpackage extras
378
	 */
379
	function lsx_the_excerpt_filter( $excerpt ) {
380
381
		$post_formats = array(
382
			'aside'   => 'aside',
383
			'gallery' => 'gallery',
384
			'link'    => 'link',
385
			'image'   => 'image',
386
			'quote'   => 'quote',
387
			'status'  => 'status',
388
			'video'   => 'video',
389
			'audio'   => 'audio',
390
		);
391
392
		$show_full_content = has_post_format( apply_filters( 'lsx_the_excerpt_filter_post_types', $post_formats ) );
393
394
		if ( ! $show_full_content ) {
395
			if ( '' !== $excerpt && ! stristr( $excerpt, 'moretag' ) ) {
396
				$pagination = wp_link_pages( array(
397
					'before'      => '<div class="lsx-postnav-wrapper"><div class="lsx-postnav">',
398
					'after'       => '</div></div>',
399
					'link_before' => '<span>',
400
					'link_after'  => '</span>',
401
					'echo'        => 0,
402
				) );
403
404
				if ( ! empty( $pagination ) ) {
405
					$excerpt .= $pagination;
406
				} else {
407
					$excerpt_more = '<p><a class="moretag" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'Continue reading', 'lsx' ) . '</a></p>';
408
					$excerpt .= apply_filters( 'excerpt_more_p', $excerpt_more );
409
				}
410
			}
411
		}
412
413
		return $excerpt;
414
	}
415
416
endif;
417
418
add_filter( 'the_excerpt', 'lsx_the_excerpt_filter' , 1 , 20 );
419
420
if ( ! function_exists( 'lsx_full_width_widget_classes' ) ) :
421
422
	/**
423
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
424
	 *
425
	 * @package    lsx
426
	 * @subpackage extras
427
	 */
428
	function lsx_full_width_widget_classes( $params ) {
429
		if ( is_admin() ) {
430
			return $params;
431
		}
432
433
		global $wp_registered_widgets;
434
435
		$widget_id   = $params[0]['widget_id'];
436
		$widget_name = $params[0]['widget_name'];
437
438
		if ( 'Text' === $widget_name ) {
439
			$wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
440
			$wp_registered_widgets[ $widget_id ]['callback'] = 'lsx_full_width_widget_custom_callback';
441
		}
442
443
		return $params;
444
	}
445
446
endif;
447
448
add_filter( 'dynamic_sidebar_params', 'lsx_full_width_widget_classes' );
449
450
if ( ! function_exists( 'lsx_full_width_widget_custom_callback' ) ) :
451
452
	/**
453
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
454
	 *
455
	 * @package    lsx
456
	 * @subpackage extras
457
	 */
458
	function full_width_widget_custom_callback() {
459
		global $wp_registered_widgets;
460
461
		$original_callback_params = func_get_args();
462
		$widget_id = $original_callback_params[0]['widget_id'];
463
464
		$original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
465
		$wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
466
467
		$widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
468
469
		$widget_classname = '';
470
471
		if ( is_callable( $original_callback ) ) {
472
			ob_start();
473
			call_user_func_array( $original_callback, $original_callback_params );
474
			$widget_output = ob_get_clean();
475
476
			echo wp_kses_post( apply_filters( 'lsx_widget_output', $widget_output, $widget_id_base, $widget_classname, $widget_id ) );
477
		}
478
	}
479
480
endif;
481
482
if ( ! function_exists( 'lsx_full_width_widget_output' ) ) :
483
484
	/**
485
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
486
	 *
487
	 * @package    lsx
488
	 * @subpackage extras
489
	 */
490
	function lsx_full_width_widget_output( $widget_output, $widget_id_base, $widget_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $widget_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...
491
		if ( 'text' === $widget_id_base ) {
492
			if ( false !== strpos( $widget_output, '<div class="lsx-full-width-alt">' ) ) {
493
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width_alt"', $widget_output );
494
			} elseif ( false !== strpos( $widget_output, '<div class="lsx-full-width">' ) ) {
495
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width"', $widget_output );
496
			}
497
		}
498
499
		return $widget_output;
500
	}
501
502
endif;
503
504
add_filter( 'lsx_widget_output', 'lsx_full_width_widget_output', 10, 3 );
505
506
/**
507
 * Check if the content has a restricted post format that needs to show a full excerpt.
508
 */
509
function lsx_post_format_force_content_on_list() {
510
	$post_formats = apply_filters( 'lsx_post_format_force_content_on_list',
511
		array(
512
				'video' => 'video',
513
				'audio' => 'audio',
514
				'quote' => 'quote',
515
				'link' => 'link',
516
			)
517
	);
518
	$return = false;
519
	if ( ! has_post_format( $post_formats ) ) {
520
		$return = true;
521
	}
522
	return $return;
523
}
524
525
/**
526
 * Remove the Hentry Class Every
527
 */
528
function lsx_remove_hentry( $classes ) {
529
	if ( 'post' !== get_post_type() ) {
530
		$classes = array_diff( $classes, array( 'hentry' ) );
531
	}
532
	return $classes;
533
}
534
add_filter( 'post_class','lsx_remove_hentry' );
535
536
/**
537
 * Strip Excerpts.
538
 *
539
 */
540
function lsx_strip_excerpt( $content ) {
541
	if ( is_search() || is_archive() || ( is_blog_installed() && ! is_single() && ! is_page() ) ) {
542
		$content = strip_shortcodes( $content );
543
		$content = str_replace( ']]>', ']]&gt;', $content );
544
		$content = strip_tags( $content );
545
	}
546
	return $content;
547
}
548
add_filter( 'the_content', 'lsx_strip_excerpt' );
549
550
/**
551
 * Disable Gutenberg for LSX Custom Post Tpes.
552
 *
553
 */
554
function lsx_disable_gutenberg_product_type( $is_enabled, $post_type ) {
555
	if ( 'testimonial' === $post_type || 'team' === $post_type || 'project' === $post_type ) {
556
		return false;
557
	}
558
559
	return $is_enabled;
560
}
561
add_filter( 'gutenberg_add_edit_link_for_post_type', 'lsx_disable_gutenberg_product_type', 10, 2 );
562
563
/**
564
 * Add the "Blog" link to the breadcrumbs
565
 * @param $crumbs
566
 * @return array
567
 */
568
function lsx_breadcrumbs_blog_link( $crumbs ) {
569
570
	$show_on_front = get_option( 'show_on_front' );
571
572
	if ( 'page' === $show_on_front && ( is_category() || is_tag() ) ) {
573
574
		$blog_page = get_option( 'page_for_posts' );
575
		if ( false !== $blog_page && '' !== $blog_page ) {
576
577
			$new_crumbs = array();
578
			$new_crumbs[0] = $crumbs[0];
579
580
			if ( function_exists( 'woocommerce_breadcrumb' ) ) {
581
				$new_crumbs[1] = array(
582
					0	=> get_the_title( $blog_page ),
583
					1	=> get_permalink( $blog_page ),
584
				);
585
			} else {
586
				$new_crumbs[1] = array(
587
					'text'	=> get_the_title( $blog_page ),
588
					'url'	=> get_permalink( $blog_page ),
589
				);
590
			}
591
			$new_crumbs[2] = $crumbs[1];
592
			$crumbs = $new_crumbs;
593
594
		}
595
	}
596
	return $crumbs;
597
}
598
add_filter( 'wpseo_breadcrumb_links', 'lsx_breadcrumbs_blog_link', 30, 1 );
599
add_filter( 'woocommerce_get_breadcrumb', 'lsx_breadcrumbs_blog_link', 30, 1 );
600