Completed
Push — master ( bcb77d...aa4fea )
by
unknown
03:19
created

extras.php ➔ mv_browser_body_class()   D

Complexity

Conditions 14
Paths 144

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 23
nc 144
nop 1
dl 0
loc 26
rs 4.7877
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
if ( ! defined( 'ABSPATH' ) ) return; // Exit if accessed directly
3
4
/**
5
 * Add and remove body_class() classes
6
 */
7
function lsx_body_class($classes) {
8
	/*
9
	 * Add the header layout class
10
	 */
11
	$header_layout = get_theme_mod('lsx_header_layout','inline');
12
	$classes[] = 'header-'.$header_layout;
13
		
14
	
15
  // Add post/page slug
16
  if (is_single() || is_page() && !is_front_page()) {
17
    $classes[] = basename(get_permalink());
18
  }
19
  
20
  if(!class_exists('Lsx_Banners')){
21
		$post_types = array('page','post');
22
		$post_types = apply_filters('lsx_allowed_post_type_banners',$post_types);  
23
24
		if((is_singular($post_types) && has_post_thumbnail()) 
25
		|| (is_singular('jetpack-portfolio'))){
26
			$classes[] = 'page-has-banner';
27
		}
28
	}
29
30
  if (has_nav_menu('top-menu')) {
31
  	$classes[] = 'has-top-menu';
32
  }
33
34
	if ( get_theme_mod( 'lsx_preloader_content_status', '1' ) === '1' ) {
35
		$classes[] = 'preloader-content-enable';
36
	}
37
38
  // Remove unnecessary classes
39
  $home_id_class = 'page-id-' . get_option('page_on_front');
40
  $remove_classes = array(
41
    'page-template-default',
42
    $home_id_class
43
  );
44
  $classes = array_diff($classes, $remove_classes);
45
46
  return $classes;
47
}
48
add_filter('body_class', 'lsx_body_class');
49
50
51
/**
52
 * Filters wp_title to print a neat <title> tag based on what is being viewed.
53
 *
54
 * @param string $title Default title text for current view.
55
 * @param string $sep Optional separator.
56
 * @return string The filtered title.
57
 */
58
function lsx_wp_title( $title, $sep ) {
59
	global $page, $paged;
60
61
	if ( is_feed() ) {
62
		return $title;
63
	}
64
65
	// Add the blog name
66
	$title .= get_bloginfo( 'name' );
67
68
	// Add the blog description for the home/front page.
69
	$site_description = get_bloginfo( 'description', 'display' );
70
	if ( $site_description && ( is_home() || is_front_page() ) ) {
71
		$title .= " $sep $site_description";
72
	}
73
74
	// Add a page number if necessary:
75
	if ( $paged >= 2 || $page >= 2 ) {
76
		$title .= " $sep " . sprintf( __( 'Page %s', 'lsx' ), max( $paged, $page ) );
77
	}
78
79
	return $title;
80
}
81
add_filter( 'wp_title', 'lsx_wp_title', 10, 2 );
82
83
84
/**
85
 * Wrap embedded media as suggested by Readability
86
 *
87
 * @link https://gist.github.com/965956
88
 * @link http://www.readability.com/publishers/guidelines#publisher
89
 */
90
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...
91
  return '<div class="entry-content-asset">' . $cache . '</div>';
92
}
93
add_filter('embed_oembed_html', 'lsx_embed_wrap', 10, 4);
94
95
96
/**
97
 * Remove unnecessary self-closing tags
98
 */
99
function lsx_remove_self_closing_tags($input) {
100
  return str_replace(' />', '>', $input);
101
}
102
add_filter('get_avatar',          'lsx_remove_self_closing_tags'); // <img />
103
add_filter('comment_id_fields',   'lsx_remove_self_closing_tags'); // <input />
104
add_filter('post_thumbnail_html', 'lsx_remove_self_closing_tags'); // <img />
105
106
107
if (!function_exists('lsx_get_attachment_id')) {
108
	/**
109
	 * Get the Attachment ID for a given image URL.
110
	 *
111
	 * @link   http://wordpress.stackexchange.com/a/7094
112
	 * @param  string $url
113
	 * @return boolean|integer
114
	 */
115
	function lsx_get_attachment_id($url) {
116
		$dir = wp_upload_dir();
117
		// baseurl never has a trailing slash
118
		if (false === strpos($url, $dir['baseurl'].'/')) {
119
			// URL points to a place outside of upload directory
120
			return false;
121
		}
122
		$file  = basename($url);
123
		$query = array(
124
				'post_type'  => 'attachment',
125
				'fields'     => 'ids',
126
				'meta_query' => array(
127
						array(
128
								'value'   => $file,
129
								'compare' => 'LIKE',
130
						),
131
				)
132
		);
133
		$query['meta_query'][0]['key'] = '_wp_attached_file';
134
		// query attachments
135
		$ids = get_posts($query);
136
		if (!empty($ids)) {
137
			foreach ($ids as $id) {
138
				// first entry of returned array is the URL
139
				$temp_url = wp_get_attachment_image_src($id, 'full');
140
				if ($url === array_shift($temp_url)) {
141
					return $id;
142
				}
143
			}
144
		}
145
		$query['meta_query'][0]['key'] = '_wp_attachment_metadata';
146
		// query attachments again
147
		$ids = get_posts($query);
148
		if (empty($ids)) {
149
			return false;
150
		}
151
		foreach ($ids as $id) {
152
			$meta = wp_get_attachment_metadata($id);
153
			foreach ($meta['sizes'] as $size => $values) {
154
				if ($values['file'] === $file && $url === array_shift(wp_get_attachment_image_src($id, $size))) {
0 ignored issues
show
Bug introduced by
wp_get_attachment_image_src($id, $size) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
155
					return $id;
156
				}
157
			}
158
		}
159
		return false;
160
	}
161
}
162
163
164
/**
165
 * Get the Avatar Url
166
 */
167
function lsx_get_avatar_url($author_id, $size) {
168
	$get_avatar = get_avatar($author_id, $size);
169
	preg_match("/src='(.*?)'/i", $get_avatar, $matches);
170
	return ($matches[1]);
171
}
172
173
174
/**
175
 * Checks if a Nav $element is empty or not
176
 */
177
function lsx_is_element_empty($element) {
178
	$element = trim($element);
179
	return empty($element)?false:true;
180
}
181
182
183
/**
184
 * return the responsive images.
185
 * 
186
 * @package lsx
187
 * @subpackage extras
188
 * @category thumbnails
189
 */
190
function lsx_get_thumbnail($size,$image_src = false){
191
	
192
	if(false === $image_src){
193
		$post_id = get_the_ID(); 
194
		$post_thumbnail_id = get_post_thumbnail_id( $post_id );
195
	}elseif(false != $image_src	){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
196
		if(is_numeric($image_src)){
197
			$post_thumbnail_id = $image_src;
198
		}else{
199
			$post_thumbnail_id = lsx_get_attachment_id_from_src($image_src);
200
		}
201
	}
202
	$size = apply_filters('lsx_thumbnail_size',$size);
203
	$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...
204
	if($size === 'lsx-thumbnail-wide' || $size === 'thumbnail'){
205
		$srcset = false;
206
		$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...
207
		$img = $img[0];
208
	}else{
209
		$srcset = true;
210
		$img = wp_get_attachment_image_srcset($post_thumbnail_id,$size);
211
		if($img == false) {
212
			$srcset = false;
213
			$img = wp_get_attachment_image_src($post_thumbnail_id,$size);
214
			$img = $img[0];
215
		}
216
	}
217
	if ($srcset) {
218
		$img = '<img alt="'.get_the_title(get_the_ID()).'" class="attachment-responsive wp-post-image lsx-responsive" srcset="'.$img.'" />';
219
	} else {
220
		$img = '<img alt="'.get_the_title(get_the_ID()).'" class="attachment-responsive wp-post-image lsx-responsive" src="'.$img.'" />';
221
	}
222
	$img = apply_filters('lsx_lazyload_filter_images',$img);
223
	return $img;
224
}
225
226
/**
227
 * Output the Resonsive Images
228
 * 
229
 * @package lsx
230
 * @subpackage extras
231
 * @category thumbnails
232
 */
233
function lsx_thumbnail( $size = 'thumbnail', $image_src = false ) {
234
	echo wp_kses_post( lsx_get_thumbnail( $size, $image_src ) );
235
}
236
237
238
/**
239
 * Gets the attachments ID from the src
240
 * @package lsx
241
 * @subpackage extras
242
 * @category thumbnails
243
 */
244
function lsx_get_attachment_id_from_src( $image_src ) {
245
	$post_id = wp_cache_get( $image_src, 'lsx_get_attachment_id_from_src' );
246
	
247
	if ( false === $post_id ) {
248
		global $wpdb;
249
		$query = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid='%s' LIMIT 1", $image_src );
250
		$post_id = $wpdb->get_var( $query );
251
		wp_cache_set( $image_src, $post_id, 'lsx_get_attachment_id_from_src', 3600 );
252
	}
253
254
	return $post_id;
255
}
256
257
258
/**
259
 * Add Featured Image as Banner on Single Pages.
260
 *
261
 * @package lsx
262
 * @subpackage extras
263
 * @category banner
264
 */
265
if (!function_exists('lsx_page_banner')) {
266
	function lsx_page_banner() {
267
268
		$post_types = array('page','post');
269
		$post_types = apply_filters('lsx_allowed_post_type_banners',$post_types);	
270
		
271
		if ( (is_singular($post_types) && has_post_thumbnail())
272
		 || (is_singular('jetpack-portfolio')) ) { ?>
273
	        
274
	        <?php 
275
	        	$bg_image = '';
276
	        	if(has_post_thumbnail()){
277
	        		$bg_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),'full');
278
	        		$bg_image = $bg_image[0];
279
	        	}
280
	        ?>
281
282
	   		<?php if ( ! empty( $bg_image ) ) : ?>
283
	        
284
	        <div class="page-banner-wrap">
285
		        <div class="page-banner">
286
		        	<div class="page-banner-image" style="background-image:url(<?php echo esc_attr( $bg_image ); ?>);"></div>
287
288
		        	<div class="container">
289
			            <header class="page-header">
290
			            	<h1 class="page-title"><?php the_title(); ?></h1> 
291
			           		<?php lsx_banner_content(); ?>
292
			            </header>
293
			        </div>
294
		        </div>
295
		    </div>
296
297
	    	<?php endif ?>
298
299
	    <?php } 
300
	}
301
}
302
add_action( 'lsx_header_after', 'lsx_page_banner' );
303
304
305
/**
306
 * Add SMS support
307
 *
308
 * @package lsx
309
 * @subpackage extras
310
 * @category mobile
311
 */
312
function lsx_allow_sms_protocol( $protocols ) {
313
    $protocols[] = 'sms';
314
    return $protocols;
315
}
316
add_filter( 'kses_allowed_protocols', 'lsx_allow_sms_protocol' );
317
318
319
/**
320
 * Adding browser and user-agent classes to body
321
 */
322
function mv_browser_body_class($classes) {
323
		$http_user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
324
		$http_user_agent = ! empty( $http_user_agent ) ? $http_user_agent : '';
325
326
        global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
327
        if($is_lynx) $classes[] = 'lynx';
328
        elseif($is_gecko) $classes[] = 'gecko';
329
        elseif($is_opera) $classes[] = 'opera';
330
        elseif($is_NS4) $classes[] = 'ns4';
331
        elseif($is_safari) $classes[] = 'safari';
332
        elseif($is_chrome) $classes[] = 'chrome';
333
        elseif($is_IE) {
334
                $classes[] = 'ie';
335
                if(preg_match('/MSIE ([0-9]+)([a-zA-Z0-9.]+)/', $http_user_agent, $browser_version))
336
                $classes[] = 'ie'.$browser_version[1];
337
        } else $classes[] = 'unknown';
338
        if($is_iphone) $classes[] = 'iphone';
339
        if ( stristr( $http_user_agent, "mac") ) {
340
                 $classes[] = 'osx';
341
           } elseif ( stristr( $http_user_agent, "linux") ) {
342
                 $classes[] = 'linux';
343
           } elseif ( stristr( $http_user_agent, "windows") ) {
344
                 $classes[] = 'windows';
345
           }
346
        return $classes;
347
}
348
add_filter('body_class','mv_browser_body_class');
349
350
351
/**
352
 * filter the Gravity Forms button type
353
 * 
354
 * @subpackage 	extras
355
 * @category 	gforms
356
 * 
357
 * @param		$button		String
358
 * @param		$form		Object
359
 * @return		String
360
 */
361
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...
362
	return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
363
}
364
add_filter("gform_submit_button", "lsx_form_submit_button", 10, 2);
365
366
367
/**
368
 * Replaces the excerpt "more" text by a link
369
 */
370
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...
371
	global $post;
372
	//return ' ... <a class="moretag" href="'. get_permalink($post->ID) . '">'.__('Continue reading','lsx').'</a>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
373
	return '...';
374
}
375
add_filter( 'excerpt_more', 'lsx_excerpt_more' );
376
377
378
/**
379
 * Add a continue reading link to the excerpt
380
 */
381
function lsx_the_excerpt_filter($excerpt) {
382
	$show_full_content = has_post_format(apply_filters('lsx_the_excerpt_filter_post_types', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio')));
383
	
384
	if (!$show_full_content) {
385
		if ('' !== $excerpt  && !stristr($excerpt, 'moretag')) {
386
			$pagination = wp_link_pages( array(
387
							'before' => '<div class="lsx-postnav-wrapper"><div class="lsx-postnav">',
388
							'after' => '</div></div>',
389
							'link_before' => '<span>',
390
							'link_after' => '</span>',
391
							'echo' => 0
392
						) );
393
394
			if ( ! empty( $pagination ) ) {
395
				$excerpt .= $pagination;
396
			}
397
			else {
398
				$excerpt .= '<p><a class="moretag" href="'.get_permalink().'">'.__('Continue reading','lsx').'</a></p>';
399
			}
400
		}
401
	}
402
403
	return $excerpt;
404
}
405
add_filter( 'the_excerpt', 'lsx_the_excerpt_filter' , 1 , 20 );
406
407
408
/**
409
 * Allow HTML tags in excerpt
410
 */
411
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) {
412
	function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
413
		global $post;
414
		$raw_excerpt = $wpse_excerpt;
415
416
		if ( '' == $wpse_excerpt ) {
417
			$wpse_excerpt = get_the_content('');
418
			$show_full_content = has_post_format(array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio'));
419
420
			if (!$show_full_content) {
421
				$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
422
				$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
423
				$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
424
				//$wpse_excerpt = strip_tags($wpse_excerpt, '<blockquote>,<p>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
425
426
				$excerpt_word_count = 50;
427
				$excerpt_word_count = apply_filters('excerpt_length', $excerpt_word_count);
428
				$tokens = array();
429
				$excerptOutput = '';
430
				$has_more = false;
431
				$count = 0;
432
433
				preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
434
435
				foreach ($tokens[0] as $token) { 
436
					if ($count >= $excerpt_word_count && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
437
						$excerptOutput .= trim($token);
438
						$has_more = true;
439
						break;
440
					}
441
442
					$count++;
443
					$excerptOutput .= $token;
444
				}
445
446
				$wpse_excerpt = trim(force_balance_tags($excerptOutput));
447
448
				if ($has_more) {
449
					$excerpt_end = '<a class="moretag" href="'.get_permalink().'">'.__('More','lsx').'</a>';
450
					$excerpt_end = apply_filters('excerpt_more', ' ' . $excerpt_end); 
451
452
					$pos = strrpos($wpse_excerpt, '</');
453
454
					if ($pos !== false) {
455
						// Inside last HTML tag
456
						$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
457
					} else {
458
						// After the content
459
						$wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */
460
					}
461
				}
462
			} else {
463
				$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
464
				$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
465
				//$wpse_excerpt = strip_tags($wpse_excerpt, '<blockquote>,<p>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
466
				$wpse_excerpt = trim(force_balance_tags($wpse_excerpt));
467
			}
468
469
			return $wpse_excerpt;
470
		}
471
472
		return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
473
	}
474
}
475
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
476
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
477
remove_filter( 'the_excerpt', 'wpautop' );
478