Completed
Push — branch-4.5 ( 385f73...f7e9a2 )
by
unknown
138:36 queued 131:41
created

Jetpack_Tweet   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B jetpack_tweet_url_extra_args() 0 20 6
A jetpack_tweet_shortcode_script() 0 6 2
C jetpack_tweet_shortcode() 0 71 12
1
<?php
2
/**
3
 * Tweet shortcode.
4
 * Params map to key value pairs, and all but tweet are optional:
5
 * tweet = id or permalink url* (Required)
6
 * align = none|left|right|center
7
 * width = number in pixels  example: width="300"
8
 * lang  =  en|fr|de|ko|etc...  language country code.
9
 * hide_thread = true | false **
10
 * hide_media  = true | false **
11
 *
12
 * Basic:
13
 * [tweet https://twitter.com/jack/statuses/20 width="350"]
14
 *
15
 * More parameters and another tweet syntax admitted:
16
 * [tweet tweet="https://twitter.com/jack/statuses/20" align="left" width="350" align="center" lang="es"]
17
 */
18
19
add_shortcode( 'tweet', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode' ) );
20
21
class Jetpack_Tweet {
22
23
	static $provider_args;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $provider_args.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
24
25
	/**
26
	 * Parse shortcode arguments and render its output.
27
	 *
28
	 * @since 4.5.0
29
	 *
30
	 * @param array $atts Shortcode parameters.
31
	 *
32
	 * @return string
33
	 */
34
	static public function jetpack_tweet_shortcode( $atts ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
35
		$default_atts = array(
36
			'tweet'       => '',
37
			'align'       => 'none',
38
			'width'       => '',
39
			'lang'        => 'en',
40
			'hide_thread' => 'false',
41
			'hide_media'  => 'false',
42
		);
43
44
		$attr = shortcode_atts( $default_atts, $atts );
45
46
		self::$provider_args = $attr;
47
48
		// figure out the tweet id for the requested tweet
49
		// supporting both omitted attributes and tweet="tweet_id"
50
		// and supporting both an id and a URL
51
		if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) {
52
			$attr['tweet'] = $atts[0];
53
		}
54
55
		if ( ctype_digit( $attr['tweet'] ) ) {
56
			$transient = "jpt_{$attr['tweet']}";
57
			if ( false === $cached_url = get_transient( $transient ) ) {
58
				$response = wp_remote_get( "https://twitter.com/statuses/{$attr['tweet']}" );
59
				if ( 200 !== wp_remote_retrieve_response_code( $response ) || ! isset( $response['http_response'] ) ) {
60
61
					// Cache for 15 minutes to avoid making the request over and over.
62
					set_transient( $transient, '', 60 * 15 );
63
					return '';
64
				}
65
				$http_response = $response['http_response'];
66
				$http_response_object = $http_response->get_response_object();
67
				if ( empty( $http_response_object->url ) ) {
68
69
					// Cache for 15 minutes to avoid making the request over and over.
70
					set_transient( $transient, '', 60 * 15 );
71
					return '';
72
				}
73
				$cached_url = isset( $http_response_object->url ) && ! empty( $http_response_object->url )
74
					? $http_response_object->url
75
					: '';
76
				set_transient( $transient, $cached_url, DAY_IN_SECONDS );
77
			}
78
			$attr['tweet'] = $cached_url;
79
		}
80
81
		preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits );
82
		if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) {
83
			$id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] );
84
		} else {
85
			return '<!-- Invalid tweet id -->';
86
		}
87
88
		// Add shortcode arguments to provider URL
89
		add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 );
90
91
		// Fetch tweet
92
		$output = wp_oembed_get( $id, $atts );
93
94
		// Clean up filter
95
		remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
96
97
		// Add Twitter widgets.js script to the footer.
98
		add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
99
100
		/** This action is documented in modules/widgets/social-media-icons.php */
101
		do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );
102
103
		return $output;
104
	}
105
106
	/**
107
	 * Adds parameters to URL used to fetch the tweet.
108
	 *
109
	 * @since 4.5.0
110
	 *
111
	 * @param string $provider URL of provider that supplies the tweet we're requesting.
112
	 * @param string $url      URL of tweet to embed.
113
	 * @param array  $args     Parameters supplied to shortcode and passed to wp_oembed_get
114
	 *
115
	 * @return string
116
	 */
117
	static public function jetpack_tweet_url_extra_args( $provider, $url, $args = array() ) {
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 $args 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
118
		foreach ( self::$provider_args as $key => $value ) {
119
			switch ( $key ) {
120
				case 'align':
121
				case 'lang':
122
				case 'hide_thread':
123
				case 'hide_media':
124
					$provider = add_query_arg( $key, $value, $provider );
125
					break;
126
			}
127
		}
128
129
		// Disable script since we're enqueing it in our own way in the footer
130
		$provider = add_query_arg( 'omit_script', 'true', $provider );
131
132
		// Twitter doesn't support maxheight so don't send it
133
		$provider = remove_query_arg( 'maxheight', $provider );
134
135
		return $provider;
136
	}
137
138
	/**
139
	 * Enqueue front end assets.
140
	 *
141
	 * @since 4.5.0
142
	 */
143
	static public function jetpack_tweet_shortcode_script() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
144
		if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
145
			wp_register_script( 'twitter-widgets', set_url_scheme( 'http://platform.twitter.com/widgets.js' ), array(), JETPACK__VERSION, true );
146
			wp_print_scripts( 'twitter-widgets' );
147
		}
148
	}
149
150
} // class end
151