Completed
Push — master ( 0fb8d7...6333ec )
by Fernando
03:59
created

extras.php ➔ lsx_kses_allowed_html()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) return; // Exit if accessed directly
3
4
/**
5
 * Enable extra attributes (srcset, sizes) in img tag
6
 */
7
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...
8
	$allowedtags['img']['srcset'] = true;
9
	$allowedtags['img']['sizes'] = true;
10
11
	return $allowedtags;
12
}
13
add_filter( 'wp_kses_allowed_html', 'lsx_kses_allowed_html', 10, 2 );
14
15
/**
16
 * Add and remove body_class() classes
17
 */
18
function lsx_body_class($classes) {
19
	/*
20
	 * Add the header layout class
21
	 */
22
	$header_layout = get_theme_mod('lsx_header_layout','inline');
23
	$classes[] = 'header-'.$header_layout;
24
		
25
	
26
  // Add post/page slug
27
  if (is_single() || is_page() && !is_front_page()) {
28
    $classes[] = basename(get_permalink());
29
  }
30
  
31
  if(!class_exists('Lsx_Banners')){
32
		$post_types = array('page','post');
33
		$post_types = apply_filters('lsx_allowed_post_type_banners',$post_types);  
34
35
		if((is_singular($post_types) && has_post_thumbnail()) 
36
		|| (is_singular('jetpack-portfolio'))){
37
			$classes[] = 'page-has-banner';
38
		}
39
	}
40
41
  if (has_nav_menu('top-menu')) {
42
  	$classes[] = 'has-top-menu';
43
  }
44
45
	if ( get_theme_mod( 'lsx_preloader_content_status', '1' ) === '1' ) {
46
		$classes[] = 'preloader-content-enable';
47
	}
48
49
  // Remove unnecessary classes
50
  $home_id_class = 'page-id-' . get_option('page_on_front');
51
  $remove_classes = array(
52
    'page-template-default',
53
    $home_id_class
54
  );
55
  $classes = array_diff($classes, $remove_classes);
56
57
  return $classes;
58
}
59
add_filter('body_class', 'lsx_body_class');
60
61
62
/**
63
 * Filters wp_title to print a neat <title> tag based on what is being viewed.
64
 *
65
 * @param string $title Default title text for current view.
66
 * @param string $sep Optional separator.
67
 * @return string The filtered title.
68
 */
69
function lsx_wp_title( $title, $sep ) {
70
	global $page, $paged;
71
72
	if ( is_feed() ) {
73
		return $title;
74
	}
75
76
	// Add the blog name
77
	$title .= get_bloginfo( 'name' );
78
79
	// Add the blog description for the home/front page.
80
	$site_description = get_bloginfo( 'description', 'display' );
81
	if ( $site_description && ( is_home() || is_front_page() ) ) {
82
		$title .= " $sep $site_description";
83
	}
84
85
	// Add a page number if necessary:
86
	if ( $paged >= 2 || $page >= 2 ) {
87
		$title .= " $sep " . sprintf( __( 'Page %s', 'lsx' ), max( $paged, $page ) );
88
	}
89
90
	return $title;
91
}
92
add_filter( 'wp_title', 'lsx_wp_title', 10, 2 );
93
94
95
/**
96
 * Wrap embedded media as suggested by Readability
97
 *
98
 * @link https://gist.github.com/965956
99
 * @link http://www.readability.com/publishers/guidelines#publisher
100
 */
101
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...
102
  return '<div class="entry-content-asset">' . $cache . '</div>';
103
}
104
add_filter('embed_oembed_html', 'lsx_embed_wrap', 10, 4);
105
106
107
/**
108
 * Remove unnecessary self-closing tags
109
 */
110
function lsx_remove_self_closing_tags($input) {
111
  return str_replace(' />', '>', $input);
112
}
113
add_filter('get_avatar',          'lsx_remove_self_closing_tags'); // <img />
114
add_filter('comment_id_fields',   'lsx_remove_self_closing_tags'); // <input />
115
add_filter('post_thumbnail_html', 'lsx_remove_self_closing_tags'); // <img />
116
117
118
if (!function_exists('lsx_get_attachment_id')) {
119
	/**
120
	 * Get the Attachment ID for a given image URL.
121
	 *
122
	 * @link   http://wordpress.stackexchange.com/a/7094
123
	 * @param  string $url
124
	 * @return boolean|integer
125
	 */
126
	function lsx_get_attachment_id($url) {
127
		$dir = wp_upload_dir();
128
		// baseurl never has a trailing slash
129
		if (false === strpos($url, $dir['baseurl'].'/')) {
130
			// URL points to a place outside of upload directory
131
			return false;
132
		}
133
		$file  = basename($url);
134
		$query = array(
135
				'post_type'  => 'attachment',
136
				'fields'     => 'ids',
137
				'meta_query' => array(
138
						array(
139
								'value'   => $file,
140
								'compare' => 'LIKE',
141
						),
142
				)
143
		);
144
		$query['meta_query'][0]['key'] = '_wp_attached_file';
145
		// query attachments
146
		$ids = get_posts($query);
147
		if (!empty($ids)) {
148
			foreach ($ids as $id) {
149
				// first entry of returned array is the URL
150
				$temp_url = wp_get_attachment_image_src( $id, 'full' );
151
				if ( array_shift( $temp_url ) === $url ) {
152
					return $id;
153
				}
154
			}
155
		}
156
		$query['meta_query'][0]['key'] = '_wp_attachment_metadata';
157
		// query attachments again
158
		$ids = get_posts($query);
159
		if (empty($ids)) {
160
			return false;
161
		}
162
		foreach ($ids as $id) {
163
			$meta = wp_get_attachment_metadata($id);
164
			foreach ( $meta['sizes'] as $size => $values ) {
165
				if ( $values['file'] === $file && array_shift( wp_get_attachment_image_src( $id, $size ) ) === $url ) {
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...
166
					return $id;
167
				}
168
			}
169
		}
170
		return false;
171
	}
172
}
173
174
175
/**
176
 * Get the Avatar Url
177
 */
178
function lsx_get_avatar_url($author_id, $size) {
179
	$get_avatar = get_avatar($author_id, $size);
180
	preg_match("/src='(.*?)'/i", $get_avatar, $matches);
181
	return ($matches[1]);
182
}
183
184
185
/**
186
 * Checks if a Nav $element is empty or not
187
 */
188
function lsx_is_element_empty($element) {
189
	$element = trim($element);
190
	return empty($element)?false:true;
191
}
192
193
194
/**
195
 * return the responsive images.
196
 * 
197
 * @package lsx
198
 * @subpackage extras
199
 * @category thumbnails
200
 */
201
function lsx_get_thumbnail($size,$image_src = false){
202
	
203
	if(false === $image_src){
204
		$post_id = get_the_ID(); 
205
		$post_thumbnail_id = get_post_thumbnail_id( $post_id );
206
	}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...
207
		if(is_numeric($image_src)){
208
			$post_thumbnail_id = $image_src;
209
		}else{
210
			$post_thumbnail_id = lsx_get_attachment_id_from_src($image_src);
211
		}
212
	}
213
	$size = apply_filters('lsx_thumbnail_size',$size);
214
	$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...
215
	if ( 'lsx-thumbnail-wide' === $size || 'thumbnail' === $size ) {
216
		$srcset = false;
217
		$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...
218
		$img = $img[0];
219
	} else {
220
		$srcset = true;
221
		$img = wp_get_attachment_image_srcset($post_thumbnail_id,$size);
222
		if ( false == $img ) {
223
			$srcset = false;
224
			$img = wp_get_attachment_image_src($post_thumbnail_id,$size);
225
			$img = $img[0];
226
		}
227
	}
228
	if ( $srcset ) {
229
		$img = '<img alt="'.get_the_title(get_the_ID()).'" class="attachment-responsive wp-post-image lsx-responsive" srcset="'.$img.'" />';
230
	} else {
231
		$img = '<img alt="'.get_the_title(get_the_ID()).'" class="attachment-responsive wp-post-image lsx-responsive" src="'.$img.'" />';
232
	}
233
	$img = apply_filters('lsx_lazyload_filter_images',$img);
234
	return $img;
235
}
236
237
/**
238
 * Output the Resonsive Images
239
 * 
240
 * @package lsx
241
 * @subpackage extras
242
 * @category thumbnails
243
 */
244
function lsx_thumbnail( $size = 'thumbnail', $image_src = false ) {
245
	echo wp_kses_post( lsx_get_thumbnail( $size, $image_src ) );
246
}
247
248
249
/**
250
 * Gets the attachments ID from the src
251
 * @package lsx
252
 * @subpackage extras
253
 * @category thumbnails
254
 */
255
function lsx_get_attachment_id_from_src( $image_src ) {
256
	$post_id = wp_cache_get( $image_src, 'lsx_get_attachment_id_from_src' );
257
	
258
	if ( false === $post_id ) {
259
		global $wpdb;
260
		$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid='%s' LIMIT 1", $image_src ) );
261
		wp_cache_set( $image_src, $post_id, 'lsx_get_attachment_id_from_src', 3600 );
262
	}
263
264
	return $post_id;
265
}
266
267
268
/**
269
 * Add Featured Image as Banner on Single Pages.
270
 *
271
 * @package lsx
272
 * @subpackage extras
273
 * @category banner
274
 */
275
if (!function_exists('lsx_page_banner')) {
276
	function lsx_page_banner() {
277
278
		$post_types = array('page','post');
279
		$post_types = apply_filters('lsx_allowed_post_type_banners',$post_types);	
280
		
281
		if ( (is_singular($post_types) && has_post_thumbnail())
282
		 || (is_singular('jetpack-portfolio')) ) { ?>
283
	        
284
	        <?php 
285
	        	$bg_image = '';
286
	        	if(has_post_thumbnail()){
287
	        		$bg_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),'full');
288
	        		$bg_image = $bg_image[0];
289
	        	}
290
	        ?>
291
292
	   		<?php if ( ! empty( $bg_image ) ) : ?>
293
	        
294
	        <div class="page-banner-wrap">
295
		        <div class="page-banner">
296
		        	<div class="page-banner-image" style="background-image:url(<?php echo esc_attr( $bg_image ); ?>);"></div>
297
298
		        	<div class="container">
299
			            <header class="page-header">
300
			            	<h1 class="page-title"><?php the_title(); ?></h1> 
301
			           		<?php lsx_banner_content(); ?>
302
			            </header>
303
			        </div>
304
		        </div>
305
		    </div>
306
307
	    	<?php endif ?>
308
309
	    <?php } 
310
	}
311
}
312
add_action( 'lsx_header_after', 'lsx_page_banner' );
313
314
315
/**
316
 * Add SMS support
317
 *
318
 * @package lsx
319
 * @subpackage extras
320
 * @category mobile
321
 */
322
function lsx_allow_sms_protocol( $protocols ) {
323
    $protocols[] = 'sms';
324
    return $protocols;
325
}
326
add_filter( 'kses_allowed_protocols', 'lsx_allow_sms_protocol' );
327
328
329
/**
330
 * Adding browser and user-agent classes to body
331
 */
332
function mv_browser_body_class($classes) {
333
		$http_user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
334
		$http_user_agent = ! empty( $http_user_agent ) ? $http_user_agent : '';
335
336
        global $is_lynx, $is_gecko, $is_ie, $is_opera, $is_ns4, $is_safari, $is_chrome, $is_iphone;
337
        if($is_lynx) $classes[] = 'lynx';
338
        elseif($is_gecko) $classes[] = 'gecko';
339
        elseif($is_opera) $classes[] = 'opera';
340
        elseif($is_ns4) $classes[] = 'ns4';
341
        elseif($is_safari) $classes[] = 'safari';
342
        elseif($is_chrome) $classes[] = 'chrome';
343
        elseif($is_ie) {
344
                $classes[] = 'ie';
345
                if(preg_match('/MSIE ([0-9]+)([a-zA-Z0-9.]+)/', $http_user_agent, $browser_version))
346
                $classes[] = 'ie'.$browser_version[1];
347
        } else $classes[] = 'unknown';
348
        if($is_iphone) $classes[] = 'iphone';
349
        if ( stristr( $http_user_agent, "mac") ) {
350
                 $classes[] = 'osx';
351
           } elseif ( stristr( $http_user_agent, "linux") ) {
352
                 $classes[] = 'linux';
353
           } elseif ( stristr( $http_user_agent, "windows") ) {
354
                 $classes[] = 'windows';
355
           }
356
        return $classes;
357
}
358
add_filter('body_class','mv_browser_body_class');
359
360
361
/**
362
 * filter the Gravity Forms button type
363
 * 
364
 * @subpackage 	extras
365
 * @category 	gforms
366
 * 
367
 * @param		$button		String
368
 * @param		$form		Object
369
 * @return		String
370
 */
371
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...
372
	return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
373
}
374
add_filter("gform_submit_button", "lsx_form_submit_button", 10, 2);
375
376
377
/**
378
 * Replaces the excerpt "more" text by a link
379
 */
380
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...
381
	global $post;
382
	//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...
383
	return '...';
384
}
385
add_filter( 'excerpt_more', 'lsx_excerpt_more' );
386
387
388
/**
389
 * Add a continue reading link to the excerpt
390
 */
391
function lsx_the_excerpt_filter($excerpt) {
392
	$show_full_content = has_post_format(apply_filters('lsx_the_excerpt_filter_post_types', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio')));
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
			}
407
			else {
408
				$excerpt .= '<p><a class="moretag" href="'.get_permalink().'">'.__('Continue reading','lsx').'</a></p>';
409
			}
410
		}
411
	}
412
413
	return $excerpt;
414
}
415
add_filter( 'the_excerpt', 'lsx_the_excerpt_filter' , 1 , 20 );
416
417
418
/**
419
 * Allow HTML tags in excerpt
420
 */
421
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) {
422
	function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
423
		global $post;
424
		$raw_excerpt = $wpse_excerpt;
425
426
		if ( '' == $wpse_excerpt ) {
427
			$wpse_excerpt = get_the_content('');
428
			$show_full_content = has_post_format(array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio'));
429
430
			if (!$show_full_content) {
431
				$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
432
				$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
433
				$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
434
				$wpse_excerpt = strip_tags($wpse_excerpt, '<blockquote>,<p>,<br>,<b>,<strong>,<i>,<u>,<ul>,<li>,<span>,<div>');
435
436
				$excerpt_word_count = 50;
437
				$excerpt_word_count = apply_filters('excerpt_length', $excerpt_word_count);
438
				$tokens = array();
439
				$excerpt_output = '';
440
				$has_more = false;
441
				$count = 0;
442
443
				preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
444
445
				foreach ($tokens[0] as $token) { 
446
					if ($count >= $excerpt_word_count && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
447
						$excerpt_output .= trim($token);
448
						$has_more = true;
449
						break;
450
					}
451
452
					$count++;
453
					$excerpt_output .= $token;
454
				}
455
456
				$wpse_excerpt = trim(force_balance_tags($excerpt_output));
457
458
				if ($has_more) {
459
					$excerpt_end = '<a class="moretag" href="'.get_permalink().'">'.__('More','lsx').'</a>';
460
					$excerpt_end = apply_filters('excerpt_more', ' ' . $excerpt_end); 
461
462
					$pos = strrpos($wpse_excerpt, '</');
463
464
					if ( false !== $pos ) {
465
						// Inside last HTML tag
466
						$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
467
					} else {
468
						// After the content
469
						$wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */
470
					}
471
				}
472
			} else {
473
				$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
474
				$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
475
				//$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...
476
				$wpse_excerpt = trim(force_balance_tags($wpse_excerpt));
477
			}
478
479
			return $wpse_excerpt;
480
		}
481
482
		return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
483
	}
484
}
485
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
486
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
487
remove_filter( 'the_excerpt', 'wpautop' );
488