Passed
Push — master ( 355e93...4b5b0c )
by Fernando
02:15
created

extras.php ➔ lsx_body_class()   C

Complexity

Conditions 11
Paths 96

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 23
nc 96
nop 1
dl 0
loc 43
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

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
 * 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
		return $allowedtags;
34
	}
35
36
endif;
37
38
add_filter( 'wp_kses_allowed_html', 'lsx_kses_allowed_html', 10, 2 );
39
40
if ( ! function_exists( 'lsx_body_class' ) ) :
41
42
	/**
43
	 * Add and remove body_class() classes.
44
	 *
45
	 * @package    lsx
46
	 * @subpackage extras
47
	 */
48
	function lsx_body_class( $classes ) {
49
		global $post;
50
51
		$header_layout = get_theme_mod( 'lsx_header_layout', 'inline' );
52
		$classes[]     = 'header-' . $header_layout;
53
54
		if ( isset( $post ) ) {
55
			$classes[] = $post->post_name;
56
		}
57
58
		if ( ! class_exists( 'LSX_Banners' ) || ! empty( apply_filters( 'lsx_banner_plugin_disable', false ) ) ) {
59
			$post_types = array( 'page', 'post' );
60
			$post_types = apply_filters( 'lsx_allowed_post_type_banners', $post_types );
61
62
			if ( is_singular( $post_types ) && has_post_thumbnail() ) {
63
				$classes[] = 'page-has-banner';
64
			}
65
		}
66
67
		if ( has_nav_menu( 'top-menu' ) || has_nav_menu( 'top-menu-left' ) ) {
68
			$classes[] = 'has-top-menu';
69
		}
70
71
		$fixed_header = get_theme_mod( 'lsx_header_fixed', false );
72
73
		if ( false !== $fixed_header ) {
74
			$classes[] = 'top-menu-fixed';
75
		}
76
77
		$search_form  = get_theme_mod( 'lsx_header_search', false );
78
79
		if ( false !== $search_form ) {
80
			$classes[] = 'has-header-search';
81
		}
82
83
		$preloader_content  = get_theme_mod( 'lsx_preloader_content_status', false );
84
85
		if ( false !== $preloader_content ) {
86
			$classes[] = 'preloader-content-enable';
87
		}
88
89
		return $classes;
90
	}
91
92
endif;
93
94
add_filter( 'body_class', 'lsx_body_class' );
95
96
if ( ! function_exists( 'lsx_embed_wrap' ) ) :
97
98
	/**
99
	 * Wrap embedded media as suggested by Readability.
100
	 *
101
	 * @package    lsx
102
	 * @subpackage extras
103
	 *
104
	 * @link https://gist.github.com/965956
105
	 * @link http://www.readability.com/publishers/guidelines#publisher
106
	 */
107
	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...
108
		if ( false !== strpos( $cache, '<iframe' ) ) {
109
			return '<div class="entry-content-asset">' . $cache . '</div>';
110
		}
111
112
		return $cache;
113
	}
114
115
endif;
116
117
add_filter( 'embed_oembed_html', 'lsx_embed_wrap', 10, 4 );
118
119
if ( ! function_exists( 'lsx_remove_self_closing_tags' ) ) :
120
121
	/**
122
	 * Remove unnecessary self-closing tags.
123
	 *
124
	 * @package    lsx
125
	 * @subpackage extras
126
	 */
127
	function lsx_remove_self_closing_tags( $input ) {
128
		return str_replace( ' />', '>', $input );
129
	}
130
131
endif;
132
133
add_filter( 'get_avatar',          'lsx_remove_self_closing_tags' ); // <img />
134
add_filter( 'comment_id_fields',   'lsx_remove_self_closing_tags' ); // <input />
135
add_filter( 'post_thumbnail_html', 'lsx_remove_self_closing_tags' ); // <img />
136
137
if ( ! function_exists( 'lsx_is_element_empty' ) ) :
138
139
	/**
140
	 * Checks if a Nav $element is empty or not.
141
	 *
142
	 * @package    lsx
143
	 * @subpackage extras
144
	 */
145
	function lsx_is_element_empty( $element ) {
146
		$element = trim( $element );
147
		return empty( $element ) ? false : true;
148
	}
149
150
endif;
151
152
if ( ! function_exists( 'lsx_get_thumbnail' ) ) :
153
154
	/**
155
	 * return the responsive images.
156
	 *
157
	 * @package    lsx
158
	 * @subpackage extras
159
	 */
160
	function lsx_get_thumbnail( $size, $image_src = false ) {
161
		if ( false === $image_src ) {
162
			$post_id           = get_the_ID();
163
			$post_thumbnail_id = get_post_thumbnail_id( $post_id );
164
		} elseif ( false !== $image_src ) {
165
			if ( is_numeric( $image_src ) ) {
166
				$post_thumbnail_id = $image_src;
167
			} else {
168
				$post_thumbnail_id = lsx_get_attachment_id_from_src( $image_src );
169
			}
170
		}
171
172
		$size = apply_filters( 'lsx_thumbnail_size', $size );
173
		$img  = false;
0 ignored issues
show
Unused Code introduced by
$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...
174
175
		if ( 'lsx-thumbnail-single' === $size || 'lsx-thumbnail-wide' === $size || 'lsx-thumbnail-square' === $size || 'thumbnail' === $size ) {
176
			$srcset = false;
177
			$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...
178
			$img    = $img[0];
179
		} else {
180
			$srcset = true;
181
			$img = wp_get_attachment_image_srcset( $post_thumbnail_id, $size );
182
183
			if ( false === $img ) {
184
				$srcset = false;
185
				$img = wp_get_attachment_image_src( $post_thumbnail_id, $size );
186
				$img = $img[0];
187
			}
188
		}
189
190
		if ( $srcset ) {
191
			$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" srcset="' . esc_attr( $img ) . '" />';
192
		} else {
193
			$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" src="' . esc_url( $img ) . '" />';
194
		}
195
196
		$img = apply_filters( 'lsx_lazyload_filter_images', $img );
197
		return $img;
198
	}
199
200
endif;
201
202
if ( ! function_exists( 'lsx_thumbnail' ) ) :
203
204
	/**
205
	 * Output the Resonsive Images.
206
	 *
207
	 * @package    lsx
208
	 * @subpackage extras
209
	 */
210
	function lsx_thumbnail( $size = 'thumbnail', $image_src = false ) {
211
		echo wp_kses_post( lsx_get_thumbnail( $size, $image_src ) );
212
	}
213
214
endif;
215
216
if ( ! function_exists( 'lsx_get_attachment_id_from_src' ) ) :
217
218
	/**
219
	 * Gets the attachments ID from the src.
220
	 *
221
	 * @package    lsx
222
	 * @subpackage extras
223
	 */
224
	function lsx_get_attachment_id_from_src( $image_src ) {
225
		$post_id = wp_cache_get( $image_src, 'lsx_get_attachment_id_from_src' );
226
227
		if ( false === $post_id ) {
228
			global $wpdb;
229
			$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid='%s' LIMIT 1", $image_src ) );
230
			wp_cache_set( $image_src, $post_id, 'lsx_get_attachment_id_from_src', 3600 );
231
		}
232
233
		return $post_id;
234
	}
235
236
endif;
237
238
if ( ! function_exists( 'lsx_page_banner' ) ) :
239
240
	/**
241
	 * Add Featured Image as Banner on Single Pages.
242
	 *
243
	 * @package    lsx
244
	 * @subpackage extras
245
	 */
246
	function lsx_page_banner() {
247
		if ( true === apply_filters( 'lsx_page_banner_disable', false ) ) {
248
			return;
249
		}
250
251
		$post_types = array( 'page', 'post' );
252
		$post_types = apply_filters( 'lsx_allowed_post_type_banners', $post_types );
253
254
		if ( is_singular( $post_types ) && has_post_thumbnail() ) :
255
			$bg_image = '';
256
257
			if ( has_post_thumbnail() ) {
258
				$bg_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
259
				$bg_image = $bg_image[0];
260
			}
261
262
			if ( ! empty( $bg_image ) ) :
263
				?>
264
					<div class="page-banner-wrap">
265
						<div class="page-banner">
266
							<div class="page-banner-image" style="background-image:url(<?php echo esc_attr( $bg_image ); ?>);"></div>
267
268
							<div class="container">
269
								<header class="page-header">
270
									<h1 class="page-title"><?php the_title(); ?></h1>
271
									<?php lsx_banner_content(); ?>
272
								</header>
273
							</div>
274
275
							<?php lsx_banner_inner_bottom(); ?>
276
						</div>
277
					</div>
278
				<?php
279
			endif;
280
		endif;
281
	}
282
283
endif;
284
285
add_action( 'lsx_header_after', 'lsx_page_banner' );
286
287
if ( ! function_exists( 'lsx_form_submit_button' ) ) :
288
289
	/**
290
	 * filter the Gravity Forms button type.
291
	 *
292
	 * @package    lsx
293
	 * @subpackage extras
294
	 *
295
	 * @param  $button String
296
	 * @param  $form   Object
297
	 * @return String
298
	 */
299
	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...
300
		return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
301
	}
302
303
endif;
304
305
add_filter( 'gform_submit_button', 'lsx_form_submit_button', 10, 2 );
306
307
if ( ! function_exists( 'lsx_excerpt_more' ) ) :
308
309
	/**
310
	 * Replaces the excerpt "more" text by a link.
311
	 *
312
	 * @package    lsx
313
	 * @subpackage extras
314
	 */
315
	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...
316
		return '...';
317
	}
318
319
endif;
320
321
add_filter( 'excerpt_more', 'lsx_excerpt_more' );
322
323
if ( ! function_exists( 'lsx_the_excerpt_filter' ) ) :
324
325
	/**
326
	 * Add a continue reading link to the excerpt.
327
	 *
328
	 * @package    lsx
329
	 * @subpackage extras
330
	 */
331
	function lsx_the_excerpt_filter( $excerpt ) {
332
		$show_full_content = has_post_format( apply_filters( 'lsx_the_excerpt_filter_post_types', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio' ) ) );
333
334
		if ( ! $show_full_content ) {
335
			if ( '' !== $excerpt && ! stristr( $excerpt, 'moretag' ) ) {
336
				$pagination = wp_link_pages( array(
337
					'before'      => '<div class="lsx-postnav-wrapper"><div class="lsx-postnav">',
338
					'after'       => '</div></div>',
339
					'link_before' => '<span>',
340
					'link_after'  => '</span>',
341
					'echo'        => 0,
342
				) );
343
344
				if ( ! empty( $pagination ) ) {
345
					$excerpt .= $pagination;
346
				} else {
347
					$excerpt_more = '<p><a class="moretag" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'Continue reading', 'lsx' ) . '</a></p>';
348
					$excerpt .= apply_filters( 'excerpt_more_p', $excerpt_more );
349
				}
350
			}
351
		}
352
353
		return $excerpt;
354
	}
355
356
endif;
357
358
add_filter( 'the_excerpt', 'lsx_the_excerpt_filter' , 1 , 20 );
359
360
if ( ! function_exists( 'lsx_custom_wp_trim_excerpt' ) ) :
361
362
	/**
363
	 * Allow HTML tags in excerpt.
364
	 *
365
	 * @package    lsx
366
	 * @subpackage extras
367
	 */
368
	function lsx_custom_wp_trim_excerpt( $wpse_excerpt ) {
369
		global $post;
370
		$raw_excerpt = $wpse_excerpt;
371
372
		if ( empty( $wpse_excerpt ) ) {
373
			$wpse_excerpt      = get_the_content( '' );
374
			$show_full_content = has_post_format( array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio' ) );
375
376
			if ( ! $show_full_content ) {
377
				$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
378
				$wpse_excerpt = apply_filters( 'the_content', $wpse_excerpt );
379
				$wpse_excerpt = str_replace( ']]>', ']]>', $wpse_excerpt );
380
				$wpse_excerpt = strip_tags( $wpse_excerpt, apply_filters( 'excerpt_strip_tags', '<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<a>,<button>,<blockquote>,<p>,<br>,<b>,<strong>,<i>,<u>,<ul>,<ol>,<li>,<span>,<div>' ) );
381
382
				$excerpt_word_count = 50;
383
				$excerpt_word_count = apply_filters( 'excerpt_length', $excerpt_word_count );
384
385
				$tokens         = array();
386
				$excerpt_output = '';
387
				$has_more       = false;
388
				$count          = 0;
389
390
				preg_match_all( '/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens );
391
392
				foreach ( $tokens[0] as $token ) {
393
					if ( $count >= $excerpt_word_count ) {
394
						$excerpt_output .= trim( $token );
395
						$has_more = true;
396
						break;
397
					}
398
399
					$count++;
400
					$excerpt_output .= $token;
401
				}
402
403
				$wpse_excerpt = trim( force_balance_tags( $excerpt_output ) );
404
405
				if ( $has_more ) {
406
					$excerpt_end = '<a class="moretag" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'More', 'lsx' ) . '</a>';
407
					$excerpt_end = apply_filters( 'excerpt_more', ' ' . $excerpt_end );
408
409
					$pos = strrpos( $wpse_excerpt, '</' );
410
411
					if ( false !== $pos ) {
412
						// Inside last HTML tag
413
						$wpse_excerpt = substr_replace( $wpse_excerpt, $excerpt_end, $pos, 0 ); /* Add read more next to last word */
414
					} else {
415
						// After the content
416
						$wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */
417
					}
418
				}
419
			} else {
420
				$wpse_excerpt = apply_filters( 'the_content', $wpse_excerpt );
421
				$wpse_excerpt = str_replace( ']]>', ']]>', $wpse_excerpt );
422
				//$wpse_excerpt = strip_tags( $wpse_excerpt, '<blockquote>,<p>' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
423
				$wpse_excerpt = trim( force_balance_tags( $wpse_excerpt ) );
424
			}
425
426
			return $wpse_excerpt;
427
		}
428
429
		return apply_filters( 'lsx_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt );
430
	}
431
432
endif;
433
434
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
435
add_filter( 'get_the_excerpt', 'lsx_custom_wp_trim_excerpt' );
436
437
if ( ! function_exists( 'lsx_full_width_widget_classes' ) ) :
438
439
	/**
440
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
441
	 *
442
	 * @package    lsx
443
	 * @subpackage extras
444
	 */
445
	function lsx_full_width_widget_classes( $params ) {
446
		if ( is_admin() ) {
447
			return $params;
448
		}
449
450
		global $wp_registered_widgets;
451
452
		$widget_id   = $params[0]['widget_id'];
453
		$widget_name = $params[0]['widget_name'];
454
455
		if ( 'Text' === $widget_name ) {
456
			$wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
457
			$wp_registered_widgets[ $widget_id ]['callback'] = 'lsx_full_width_widget_custom_callback';
458
		}
459
460
		return $params;
461
	}
462
463
endif;
464
465
add_filter( 'dynamic_sidebar_params', 'lsx_full_width_widget_classes' );
466
467
if ( ! function_exists( 'lsx_full_width_widget_custom_callback' ) ) :
468
469
	/**
470
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
471
	 *
472
	 * @package    lsx
473
	 * @subpackage extras
474
	 */
475
	function lsx_full_width_widget_custom_callback() {
476
		global $wp_registered_widgets;
477
478
		$original_callback_params = func_get_args();
479
		$widget_id = $original_callback_params[0]['widget_id'];
480
481
		$original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
482
		$wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
483
484
		$widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
485
486
		$widget_classname = '';
487
488
		if ( is_callable( $original_callback ) ) {
489
			ob_start();
490
			call_user_func_array( $original_callback, $original_callback_params );
491
			$widget_output = ob_get_clean();
492
493
			echo wp_kses_post( apply_filters( 'lsx_widget_output', $widget_output, $widget_id_base, $widget_classname, $widget_id ) );
494
		}
495
	}
496
497
endif;
498
499
if ( ! function_exists( 'lsx_full_width_widget_output' ) ) :
500
501
	/**
502
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
503
	 *
504
	 * @package    lsx
505
	 * @subpackage extras
506
	 */
507
	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...
508
		if ( 'text' === $widget_id_base ) {
509
			if ( false !== strpos( $widget_output, '<div class="lsx-full-width-alt">' ) ) {
510
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width_alt"', $widget_output );
511
			} elseif ( false !== strpos( $widget_output, '<div class="lsx-full-width">' ) ) {
512
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width"', $widget_output );
513
			}
514
		}
515
516
		return $widget_output;
517
	}
518
519
endif;
520
521
add_filter( 'lsx_widget_output', 'lsx_full_width_widget_output', 10, 3 );
522