Completed
Push — whitelist/sync ( 830219...fbc430 )
by
unknown
13:02 queued 02:23
created

getty.php ➔ jetpack_getty_shortcode()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 20
Code Lines 14

Duplication

Lines 8
Ratio 40 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 13
nop 2
dl 8
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Getty shortcode
4
 *
5
 * [getty src="82278805" width="$width" height="$height"]
6
 * <div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:462px;"><div style="padding:0;margin:0;text-align:left;"><a href="http://www.gettyimages.com/detail/82278805" target="_blank" style="color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none;display:inline-block;">Embed from Getty Images</a></div><div style="overflow:hidden;position:relative;height:0;padding:80.086580% 0 0 0;width:100%;"><iframe src="//embed.gettyimages.com/embed/82278805?et=jGiu6FXXSpJDGf1SnwLV2g&sig=TFVNFtqghwNw5iJQ1MFWnI8f4Y40_sfogfZLhai6SfA=" width="462" height="370" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;"></iframe></div><p style="margin:0;"></p></div>
7
 */
8
9
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
10
	add_action( 'init', 'jetpack_getty_enable_embeds' );
11
} else {
12
	jetpack_getty_enable_embeds( 'jetpack' );
13
}
14
15
/**
16
 * Register Getty as oembed provider. Add filter to reverse iframes to shortcode. Register [getty] shortcode.
17
 *
18
 * @since 4.5.0
19
 *
20
 * @param string $site Can be 'wpcom' or 'jetpack' and determines if we're in wpcom or in a Jetpack site.
21
 */
22
function jetpack_getty_enable_embeds( $site = 'wpcom' ) {
23
24
	// Set the caller argument to pass to Getty's oembed provider.
25
	$caller = 'jetpack' === $site
26
		? parse_url( get_home_url(), PHP_URL_HOST )
27
		: 'wordpress.com';
28
29
	// Support their oEmbed Endpoint
30
	wp_oembed_add_provider( '#https?://www\.gettyimages\.com/detail/.*#i', "https://embed.gettyimages.com/oembed/?caller=$caller", true );
31
	wp_oembed_add_provider( '#https?://(www\.)?gty\.im/.*#i',              "https://embed.gettyimages.com/oembed/?caller=$caller", true );
32
33
	// Allow iframes to be filtered to short code (so direct copy+paste can be done)
34
	add_filter( 'pre_kses', 'wpcom_shortcodereverse_getty' );
35
36
	// Actually display the Getty Embed
37
	add_shortcode( 'getty', 'jetpack_getty_shortcode' );
38
}
39
40
/**
41
 * Compose shortcode based on Getty iframes.
42
 *
43
 * @since 4.5.0
44
 *
45
 * @param string $content
46
 *
47
 * @return mixed
48
 */
49
function wpcom_shortcodereverse_getty( $content ) {
50
	if ( ! is_string( $content ) || false === stripos( $content, 'embed.gettyimages.com/embed' ) ) {
51
		return $content;
52
	}
53
54
	$regexp = '!<iframe\s+src=[\'"](https?:)?//embed\.gettyimages\.com/embed(/|/?\?assets=)(\d+(,\d+)*)[^\'"]*?[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
55
	$regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
56
57
	foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
58
		if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
59
			continue;
60
		}
61
62
		foreach ( $matches as $match ) {
63
			$ids = esc_html( $match[3] );
64
65
			$params = $match[5];
66
67
			if ( 'regexp_ent' == $reg ) {
68
				$params = html_entity_decode( $params );
69
			}
70
71
			$params = wp_kses_hair( $params, array( 'http' ) );
72
73
			$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
74
			$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
75
76
			$shortcode = '[getty src="' . esc_attr( $ids ) . '"';
77
			if ( $width ) {
78
				$shortcode .= ' width="' . esc_attr( $width ) . '"';
79
			}
80
			if ( $height ) {
81
				$shortcode .= ' height="' . esc_attr( $height ) . '"';
82
			}
83
			$shortcode .= ']';
84
85
			$content = str_replace( $match[0], $shortcode, $content );
86
		}
87
	}
88
89
	// strip out enclosing div and any other markup
90
	$regexp = '%<div class="getty\s[^>]*+>.*?<div[^>]*+>(\[getty[^\]]*+\])\s*</div>.*?</div>%is';
91
	$regexp_ent = str_replace( array( '&amp;#0*58;', '[^&gt;]' ), array( '&amp;#0*58;|&#0*58;', '[^&]' ), htmlspecialchars( $regexp, ENT_NOQUOTES ) );
92
93
	foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
94
		if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
95
			continue;
96
		}
97
98
		foreach ( $matches as $match ) {
99
			$content = str_replace( $match[0], $match[1], $content );
100
		}
101
	}
102
103
	/** This action is documented in modules/widgets/social-media-icons.php */
104
	do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'getty' );
105
106
	return $content;
107
}
108
109
/**
110
 * Parse shortcode arguments and render its output.
111
 *
112
 * @since 4.5.0
113
 *
114
 * @param array  $atts    Shortcode parameters.
115
 * @param string $content Content enclosed by shortcode tags.
116
 *
117
 * @return string
118
 */
119
function jetpack_getty_shortcode( $atts, $content = '' ) {
120
121 View Code Duplication
	if ( ! empty( $content ) ) {
122
		$src = $content;
123
	} elseif ( ! empty( $atts['src'] ) ) {
124
		$src = $atts['src'];
125
	} elseif ( ! empty( $atts[0] ) ) {
126
		$src = $atts[0];
127
	} else {
128
		return '<!-- Missing Getty Source ID -->';
129
	}
130
131
	$src = preg_replace( '/^(\d+(,\d+)*).*$/', '$1', $src );
132
133
	$args = array();
134
	$args['width'] = isset( $atts['width'] ) ? (int) $atts['width'] : '462';
135
	$args['height'] = isset( $atts['height'] ) ? (int) $atts['height'] : '370';
136
137
	return wp_oembed_get( 'https://gty.im/' . $src, $args );
138
}
139