Completed
Push — master ( c57a13...d2a983 )
by Warwick
02:48
created

extras.php ➔ lsx_custom_wp_trim_excerpt()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 75
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 49
nc 8
nop 1
dl 0
loc 75
rs 6.5862
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
 * 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 ( empty( $img ) ) {
184
				$srcset = false;
185
				$img = wp_get_attachment_image_src( $post_thumbnail_id, $size );
186
				$img = $img[0];
187
			}
188
		}
189
190
		if ( ! empty( $img ) ) {
191
			if ( $srcset ) {
192
				$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" srcset="' . esc_attr( $img ) . '" />';
193
			} else {
194
				$img = '<img alt="' . the_title_attribute( 'echo=0' ) . '" class="attachment-responsive wp-post-image lsx-responsive" src="' . esc_url( $img ) . '" />';
195
			}
196
197
			$img = apply_filters( 'lsx_lazyload_filter_images', $img );
198
		}
199
200
		return $img;
201
	}
202
203
endif;
204
205
if ( ! function_exists( 'lsx_thumbnail' ) ) :
206
207
	/**
208
	 * Output the Resonsive Images.
209
	 *
210
	 * @package    lsx
211
	 * @subpackage extras
212
	 */
213
	function lsx_thumbnail( $size = 'thumbnail', $image_src = false ) {
214
		echo wp_kses_post( lsx_get_thumbnail( $size, $image_src ) );
215
	}
216
217
endif;
218
219
if ( ! function_exists( 'lsx_get_attachment_id_from_src' ) ) :
220
221
	/**
222
	 * Gets the attachments ID from the src.
223
	 *
224
	 * @package    lsx
225
	 * @subpackage extras
226
	 */
227
	function lsx_get_attachment_id_from_src( $image_src ) {
228
		$post_id = wp_cache_get( $image_src, 'lsx_get_attachment_id_from_src' );
229
230
		if ( false === $post_id ) {
231
			global $wpdb;
232
			$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid='%s' LIMIT 1", $image_src ) );
233
			wp_cache_set( $image_src, $post_id, 'lsx_get_attachment_id_from_src', 3600 );
234
		}
235
236
		return $post_id;
237
	}
238
239
endif;
240
241
if ( ! function_exists( 'lsx_page_banner' ) ) :
242
243
	/**
244
	 * Add Featured Image as Banner on Single Pages.
245
	 *
246
	 * @package    lsx
247
	 * @subpackage extras
248
	 */
249
	function lsx_page_banner() {
250
		if ( true === apply_filters( 'lsx_page_banner_disable', false ) ) {
251
			return;
252
		}
253
254
		$post_types = array( 'page', 'post' );
255
		$post_types = apply_filters( 'lsx_allowed_post_type_banners', $post_types );
256
257
		if ( is_singular( $post_types ) && has_post_thumbnail() ) :
258
			$bg_image = '';
259
260
			if ( has_post_thumbnail() ) {
261
				$bg_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
262
				$bg_image = $bg_image[0];
263
			}
264
265
			if ( ! empty( $bg_image ) ) :
266
				?>
267
					<div class="page-banner-wrap">
268
						<div class="page-banner">
269
							<div class="page-banner-image" style="background-image:url(<?php echo esc_attr( $bg_image ); ?>);"></div>
270
271
							<div class="container">
272
								<header class="page-header">
273
									<h1 class="page-title"><?php the_title(); ?></h1>
274
									<?php lsx_banner_content(); ?>
275
								</header>
276
							</div>
277
278
							<?php lsx_banner_inner_bottom(); ?>
279
						</div>
280
					</div>
281
				<?php
282
			endif;
283
		endif;
284
	}
285
286
endif;
287
288
add_action( 'lsx_header_after', 'lsx_page_banner' );
289
290
if ( ! function_exists( 'lsx_form_submit_button' ) ) :
291
292
	/**
293
	 * filter the Gravity Forms button type.
294
	 *
295
	 * @package    lsx
296
	 * @subpackage extras
297
	 *
298
	 * @param  $button String
299
	 * @param  $form   Object
300
	 * @return String
301
	 */
302
	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...
303
		return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
304
	}
305
306
endif;
307
308
add_filter( 'gform_submit_button', 'lsx_form_submit_button', 10, 2 );
309
310
if ( ! function_exists( 'lsx_excerpt_more' ) ) :
311
312
	/**
313
	 * Replaces the excerpt "more" text by a link.
314
	 *
315
	 * @package    lsx
316
	 * @subpackage extras
317
	 */
318
	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...
319
		return '...';
320
	}
321
322
endif;
323
324
add_filter( 'excerpt_more', 'lsx_excerpt_more' );
325
326
if ( ! function_exists( 'lsx_the_excerpt_filter' ) ) :
327
328
	/**
329
	 * Add a continue reading link to the excerpt.
330
	 *
331
	 * @package    lsx
332
	 * @subpackage extras
333
	 */
334
	function lsx_the_excerpt_filter( $excerpt ) {
335
		$show_full_content = has_post_format( apply_filters( 'lsx_the_excerpt_filter_post_types', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio' ) ) );
336
337
		if ( ! $show_full_content ) {
338
			if ( '' !== $excerpt && ! stristr( $excerpt, 'moretag' ) ) {
339
				$pagination = wp_link_pages( array(
340
					'before'      => '<div class="lsx-postnav-wrapper"><div class="lsx-postnav">',
341
					'after'       => '</div></div>',
342
					'link_before' => '<span>',
343
					'link_after'  => '</span>',
344
					'echo'        => 0,
345
				) );
346
347
				if ( ! empty( $pagination ) ) {
348
					$excerpt .= $pagination;
349
				} else {
350
					$excerpt_more = '<p><a class="moretag" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'Continue reading', 'lsx' ) . '</a></p>';
351
					$excerpt .= apply_filters( 'excerpt_more_p', $excerpt_more );
352
				}
353
			}
354
		}
355
356
		return $excerpt;
357
	}
358
359
endif;
360
361
add_filter( 'the_excerpt', 'lsx_the_excerpt_filter' , 1 , 20 );
362
363
if ( ! function_exists( 'lsx_custom_wp_trim_excerpt' ) ) :
364
365
	/**
366
	 * Allow HTML tags in excerpt.
367
	 *
368
	 * @package    lsx
369
	 * @subpackage extras
370
	 */
371
	function lsx_custom_wp_trim_excerpt( $wpse_excerpt ) {
372
		global $post;
373
		$raw_excerpt = $wpse_excerpt;
374
375
		if ( empty( $wpse_excerpt ) ) {
376
			$wpse_excerpt      = get_the_content( '' );
377
378
			$post_formats = array(
379
				'aside' => 'aside',
380
				'gallery' => 'gallery',
381
				'link' => 'link',
382
				'image' => 'image',
383
				'quote' => 'quote',
384
				'status' => 'status',
385
				'video' => 'video',
386
				'audio' => 'audio'
387
			);
388
389
			$show_full_content = has_post_format( apply_filters( 'lsx_excerpt_read_more_post_formats', $post_formats )  );
390
391
			if ( ! $show_full_content ) {
392
				$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
393
				$wpse_excerpt = apply_filters( 'the_content', $wpse_excerpt );
394
				$wpse_excerpt = str_replace( ']]>', ']]>', $wpse_excerpt );
395
				$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>' ) );
396
397
				$excerpt_word_count = 50;
398
				$excerpt_word_count = apply_filters( 'excerpt_length', $excerpt_word_count );
399
400
				$tokens         = array();
401
				$excerpt_output = '';
402
				$has_more       = false;
403
				$count          = 0;
404
405
				preg_match_all( '/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens );
406
407
				foreach ( $tokens[0] as $token ) {
408
					if ( $count >= $excerpt_word_count ) {
409
						$excerpt_output .= trim( $token );
410
						$has_more = true;
411
						break;
412
					}
413
414
					$count++;
415
					$excerpt_output .= $token;
416
				}
417
418
				$wpse_excerpt = trim( force_balance_tags( $excerpt_output ) );
419
420
				if ( $has_more ) {
421
					$excerpt_end = '<a class="moretag" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'More', 'lsx' ) . '</a>';
422
					$excerpt_end = apply_filters( 'excerpt_more', ' ' . $excerpt_end );
423
424
					$pos = strrpos( $wpse_excerpt, '</' );
425
426
					if ( false !== $pos ) {
427
						// Inside last HTML tag
428
						$wpse_excerpt = substr_replace( $wpse_excerpt, $excerpt_end, $pos, 0 ); /* Add read more next to last word */
429
					} else {
430
						// After the content
431
						$wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */
432
					}
433
				}
434
			} else {
435
				$wpse_excerpt = apply_filters( 'the_content', $wpse_excerpt );
436
				$wpse_excerpt = str_replace( ']]>', ']]>', $wpse_excerpt );
437
				//$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...
438
				$wpse_excerpt = trim( force_balance_tags( $wpse_excerpt ) );
439
			}
440
441
			return $wpse_excerpt;
442
		}
443
444
		return apply_filters( 'lsx_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt );
445
	}
446
447
endif;
448
449
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
450
add_filter( 'get_the_excerpt', 'lsx_custom_wp_trim_excerpt' );
451
452
if ( ! function_exists( 'lsx_full_width_widget_classes' ) ) :
453
454
	/**
455
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
456
	 *
457
	 * @package    lsx
458
	 * @subpackage extras
459
	 */
460
	function lsx_full_width_widget_classes( $params ) {
461
		if ( is_admin() ) {
462
			return $params;
463
		}
464
465
		global $wp_registered_widgets;
466
467
		$widget_id   = $params[0]['widget_id'];
468
		$widget_name = $params[0]['widget_name'];
469
470
		if ( 'Text' === $widget_name ) {
471
			$wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
472
			$wp_registered_widgets[ $widget_id ]['callback'] = 'lsx_full_width_widget_custom_callback';
473
		}
474
475
		return $params;
476
	}
477
478
endif;
479
480
add_filter( 'dynamic_sidebar_params', 'lsx_full_width_widget_classes' );
481
482
if ( ! function_exists( 'lsx_full_width_widget_custom_callback' ) ) :
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_custom_callback() {
491
		global $wp_registered_widgets;
492
493
		$original_callback_params = func_get_args();
494
		$widget_id = $original_callback_params[0]['widget_id'];
495
496
		$original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
497
		$wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
498
499
		$widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
500
501
		$widget_classname = '';
502
503
		if ( is_callable( $original_callback ) ) {
504
			ob_start();
505
			call_user_func_array( $original_callback, $original_callback_params );
506
			$widget_output = ob_get_clean();
507
508
			echo wp_kses_post( apply_filters( 'lsx_widget_output', $widget_output, $widget_id_base, $widget_classname, $widget_id ) );
509
		}
510
	}
511
512
endif;
513
514
if ( ! function_exists( 'lsx_full_width_widget_output' ) ) :
515
516
	/**
517
	 * Filter sidebar widget params, to add the widget_lsx_full_width_alt or widget_lsx_full_width classes to the text widget.
518
	 *
519
	 * @package    lsx
520
	 * @subpackage extras
521
	 */
522
	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...
523
		if ( 'text' === $widget_id_base ) {
524
			if ( false !== strpos( $widget_output, '<div class="lsx-full-width-alt">' ) ) {
525
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width_alt"', $widget_output );
526
			} elseif ( false !== strpos( $widget_output, '<div class="lsx-full-width">' ) ) {
527
				$widget_output = str_replace( 'class="widget widget_text"', 'class="widget widget_text widget_lsx_full_width"', $widget_output );
528
			}
529
		}
530
531
		return $widget_output;
532
	}
533
534
endif;
535
536
add_filter( 'lsx_widget_output', 'lsx_full_width_widget_output', 10, 3 );
537
538
/**
539
 * Check if the content has a restricted post format that needs to show a full excerpt.
540
 */
541
function lsx_post_format_force_content_on_list() {
542
	$post_formats = apply_filters( 'lsx_post_format_force_content_on_list', array('video' => 'video', 'audio' => 'audio', 'quote' => 'quote', 'link' => 'link') );
543
	$return = false;
544
	if ( ! has_post_format( $post_formats ) ) {
545
		$return = true;
546
	}
547
	return $return;
548
}
549