Completed
Push — add/80443split ( 25998f...41be19 )
by
unknown
09:21
created

slideshare.php ➔ slideshare_shortcode()   F

Complexity

Conditions 18
Paths 1538

Size

Total Lines 65
Code Lines 39

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 18
eloc 39
nc 1538
nop 1
dl 0
loc 65
rs 2.7958

How to fix   Long Method    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
// guarantee use of https
4
wp_oembed_remove_provider( '#https?://(www\.)?slideshare\.net/.*#i' );
5
wp_oembed_add_provider( '#https?://(www\.)?slideshare\.net/.*#i', 'https://www.slideshare.net/api/oembed/2', true );
6
7
/*
8
 * Slideshare shortcode format:
9
 * Old style (still compatible): [slideshare id=5342235&doc=camprock-101002163655-phpapp01&w=300&h=200]
10
 * New style: [slideshare id=5342235&w=300&h=200&fb=0&mw=0&mh=0&sc=no]
11
 *
12
 * Legend:
13
 *	id 	= 	Document ID provided by Slideshare
14
 *	w	=	Width of iFrame 	(int)
15
 *	h	=	Height of iFrame 	(int)
16
 *	fb	=	iFrame frameborder 	(int)
17
 *	mw	=	iFrame marginwidth 	(int)
18
 *	mh	=	iFrame marginheight	(int)
19
 *	sc	=	iFrame Scrollbar	(yes/no)
20
 **/
21
22
add_shortcode( 'slideshare', 'slideshare_shortcode' );
23
24
function slideshare_shortcode( $atts ) {
25
	global $content_width;
26
27
	$params = shortcode_new_to_old_params( $atts );
28
	parse_str( $params, $arguments );
29
30
	if ( empty( $arguments ) )
31
		return '<!-- SlideShare error: no arguments -->';
32
33
	extract( $arguments );
34
35
	// check that the Slideshare ID contains letters, numbers and query strings
36
	$pattern = '/[^-_a-zA-Z0-9?=&]/';
37
	if ( empty( $id ) || preg_match( $pattern, $id ) )
38
		return '<!-- SlideShare error: id is missing or has illegal characters -->';
39
40
	// check the width/height
41
	if ( empty( $w ) && ! empty( $content_width ) )
42
		$w = intval( $content_width );
43
	elseif ( ! ( $w = intval( $w ) ) || $w < 300 || $w > 1600 )
44
		$w = 425;
45
	else
46
		$w = intval( $w );
47
48
	$h = ceil( $w * 348 / 425 );
49
50
	if ( isset( $pro ) ) {
51
		$source = "https://www.slideshare.net/slidesharepro/$id";
52
	} else {
53
		$source = "https://www.slideshare.net/slideshow/embed_code/$id";
54
	}
55
56
	if ( isset( $rel ) )
57
		$source = add_query_arg( 'rel', intval( $rel ), $source );
58
59
	if ( isset( $startSlide ) )
60
		$source = add_query_arg( 'startSlide', intval( $startSlide ), $source );
61
62
	$player = sprintf( "<iframe src='%s' width='%d' height='%d'", esc_url( $source ), $w, $h );
63
64
	// check the frameborder
65
	if ( isset( $fb ) )
66
		$player .= " frameborder='" . intval( $fb ) . "'";
67
68
	// check the margin width; if not empty, cast as int
69
	if ( isset( $mw ) )
70
		$player .= " marginwidth='" . intval( $mw ) . "'";
71
72
	// check the margin height, if not empty, cast as int
73
	if ( isset( $mh ) )
74
		$player .= " marginheight='" . intval( $mh ) . "'";
75
76
	if ( ! empty( $style ) )
77
		$player .= " style='" . $style . "'";
78
79
	// check the scrollbar; cast as a lowercase string for comparison
80
	$sc = isset( $sc ) ? strtolower( $sc ) : '';
81
82
	if ( in_array( $sc, array( 'yes', 'no' ) ) )
83
		$player .= " scrolling='" . $sc . "'";
84
85
	$player .= ' allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>';
86
87
	return $player;
88
}