Completed
Push — branch-4.0 ( fc0d6f...bf547c )
by
unknown
08:22
created

slideshare.php ➔ slideshare_shortcode()   F

Complexity

Conditions 22
Paths 1154

Size

Total Lines 90
Code Lines 52

Duplication

Lines 9
Ratio 10 %

Importance

Changes 0
Metric Value
cc 22
eloc 52
nc 1154
nop 1
dl 9
loc 90
rs 2
c 0
b 0
f 0

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
 *	pro   = Slideshare Pro      (yes/no)
21
 *	style = Inline CSS          (string)
22
 **/
23
24
add_shortcode( 'slideshare', 'slideshare_shortcode' );
25
26
function slideshare_shortcode( $atts ) {
27
	global $content_width;
28
29
	$params = shortcode_new_to_old_params( $atts );
30
	parse_str( $params, $arguments );
31
32
	if ( empty( $arguments ) ) {
33
		return '<!-- SlideShare error: no arguments -->';
34
	}
35
36
	$attr = shortcode_atts(
37
		array(
38
			'id'    => '',
39
			'w'     => '',
40
			'h'     => '',
41
			'fb'    => '',
42
			'mw'    => '',
43
			'mh'    => '',
44
			'sc'    => '',
45
			'pro'   => '',
46
			'style' => '',
47
		), $arguments
48
	);
49
50
	// check that the Slideshare ID contains letters, numbers and query strings
51
	$pattern = '/[^-_a-zA-Z0-9?=&]/';
52
	if ( empty( $attr['id'] ) || preg_match( $pattern, $attr['id'] ) ) {
53
		return '<!-- SlideShare error: id is missing or has illegal characters -->';
54
	}
55
56
	// check the width/height
57
	$w = $attr['w'];
58
	if ( empty( $w ) && ! empty( $content_width ) ) {
59
		$w = intval( $content_width );
60
	} elseif ( ! ( $w = intval( $w ) ) || $w < 300 || $w > 1600 ) {
61
		$w = 425;
62
	} else {
63
		$w = intval( $w );
64
	}
65
66
	$h = ceil( $w * 348 / 425 ); // Note: user-supplied height is ignored.
67
68
	if ( isset( $attr['pro'] ) && $attr['pro'] ) {
69
		$source = 'https://www.slideshare.net/slidesharepro/' . $attr['id'];
70
	} else {
71
		$source = 'https://www.slideshare.net/slideshow/embed_code/' . $attr['id'];
72
	}
73
74
	if ( isset( $rel ) ) {
0 ignored issues
show
Bug introduced by
The variable $rel seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
75
		$source = add_query_arg( 'rel', intval( $rel ), $source );
76
	}
77
78
	if ( isset( $startSlide ) ) {
0 ignored issues
show
Bug introduced by
The variable $startSlide seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
79
		$source = add_query_arg( 'startSlide', intval( $startSlide ), $source );
80
	}
81
82
	$player = sprintf( "<iframe src='%s' width='%d' height='%d'", esc_url( $source ), $w, $h );
83
84
	// check the frameborder
85 View Code Duplication
	if ( ! empty( $attr['fb'] ) || '0' === $attr['fb'] ) {
86
		$player .= " frameborder='" . intval( $attr['fb'] ) . "'";
87
	}
88
89
	// check the margin width; if not empty, cast as int
90 View Code Duplication
	if ( ! empty( $attr['mw'] ) || '0' === $attr['mw'] ) {
91
		$player .= " marginwidth='" . intval( $attr['mw'] ) . "'";
92
	}
93
94
	// check the margin height, if not empty, cast as int
95 View Code Duplication
	if ( ! empty( $attr['mh'] ) || '0' === $attr['mh'] ) {
96
		$player .= " marginheight='" . intval( $attr['mh'] ) . "'";
97
	}
98
99
	if ( ! empty( $attr['style'] ) ) {
100
		$player .= " style='" . esc_attr( $attr['style'] ) . "'";
101
	}
102
103
	// check the scrollbar; cast as a lowercase string for comparison
104
	if ( ! empty( $attr['sc'] ) ) {
105
		$sc = strtolower( $attr['sc'] );
106
107
		if ( in_array( $sc, array( 'yes', 'no' ) ) ) {
108
			$player .= " scrolling='" . $sc . "'";
109
		}
110
	}
111
112
	$player .= ' allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>';
113
114
	return $player;
115
}