Completed
Push — update/tweet-shortcode-use-id-... ( 99bd6e )
by
unknown
24:36 queued 15:47
created

Jetpack_Tweet   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
C jetpack_tweet_shortcode() 0 68 12
B jetpack_tweet_url_extra_args() 0 20 6
A jetpack_tweet_shortcode_script() 0 6 2
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 ) ) {
60
					return '';
61
				}
62
				if ( ! isset( $response['http_response'] ) ) {
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
					return '';
69
				}
70
				$cached_url = isset( $http_response_object->url ) && ! empty( $http_response_object->url )
71
					? $http_response_object->url
72
					: '';
73
				set_transient( $transient, $cached_url, DAY_IN_SECONDS );
74
			}
75
			$attr['tweet'] = $cached_url;
76
		}
77
78
		preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits );
79
		if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) {
80
			$id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] );
81
		} else {
82
			return '<!-- Invalid tweet id -->';
83
		}
84
85
		// Add shortcode arguments to provider URL
86
		add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 );
87
88
		// Fetch tweet
89
		$output = wp_oembed_get( $id, $atts );
90
91
		// Clean up filter
92
		remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
93
94
		// Add Twitter widgets.js script to the footer.
95
		add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
96
97
		/** This action is documented in modules/widgets/social-media-icons.php */
98
		do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );
99
100
		return $output;
101
	}
102
103
	/**
104
	 * Adds parameters to URL used to fetch the tweet.
105
	 *
106
	 * @since 4.5.0
107
	 *
108
	 * @param string $provider URL of provider that supplies the tweet we're requesting.
109
	 * @param string $url      URL of tweet to embed.
110
	 * @param array  $args     Parameters supplied to shortcode and passed to wp_oembed_get
111
	 *
112
	 * @return string
113
	 */
114
	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...
115
		foreach ( self::$provider_args as $key => $value ) {
116
			switch ( $key ) {
117
				case 'align':
118
				case 'lang':
119
				case 'hide_thread':
120
				case 'hide_media':
121
					$provider = add_query_arg( $key, $value, $provider );
122
					break;
123
			}
124
		}
125
126
		// Disable script since we're enqueing it in our own way in the footer
127
		$provider = add_query_arg( 'omit_script', 'true', $provider );
128
129
		// Twitter doesn't support maxheight so don't send it
130
		$provider = remove_query_arg( 'maxheight', $provider );
131
132
		return $provider;
133
	}
134
135
	/**
136
	 * Enqueue front end assets.
137
	 *
138
	 * @since 4.5.0
139
	 */
140
	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...
141
		if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
142
			wp_register_script( 'twitter-widgets', set_url_scheme( 'http://platform.twitter.com/widgets.js' ), array(), JETPACK__VERSION, true );
143
			wp_print_scripts( 'twitter-widgets' );
144
		}
145
	}
146
147
} // class end
148