Completed
Push — gutenberg/tweet-block ( 728f07 )
by George
13:12
created

Jetpack_Tweet::enqueue_block_editor_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Tweet', 'enqueue_block_editor_assets' ) );
21
22
class Jetpack_Tweet {
23
24
	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...
25
26
	/**
27
	 * Parse shortcode arguments and render its output.
28
	 *
29
	 * @since 4.5.0
30
	 *
31
	 * @param array $atts Shortcode parameters.
32
	 *
33
	 * @return string
34
	 */
35
	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...
36
		$default_atts = array(
37
			'tweet'       => '',
38
			'align'       => 'none',
39
			'width'       => '',
40
			'lang'        => 'en',
41
			'hide_thread' => 'false',
42
			'hide_media'  => 'false',
43
		);
44
45
		$attr = shortcode_atts( $default_atts, $atts );
46
47
		self::$provider_args = $attr;
48
49
		// figure out the tweet id for the requested tweet
50
		// supporting both omitted attributes and tweet="tweet_id"
51
		// and supporting both an id and a URL
52
		if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) {
53
			$attr['tweet'] = $atts[0];
54
		}
55
56
		if ( ctype_digit( $attr['tweet'] ) ) {
57
			$id = 'https://twitter.com/jetpack/status/' . $attr['tweet'];
58
		} else {
59
			preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits );
60
61
			if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) {
62
				$id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] );
63
			} else {
64
				return '<!-- Invalid tweet id -->';
65
			}
66
		}
67
68
		// Add shortcode arguments to provider URL
69
		add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 );
70
71
		// Fetch tweet
72
		$output = wp_oembed_get( $id, $atts );
73
74
		// Clean up filter
75
		remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
76
77
		// Add Twitter widgets.js script to the footer.
78
		add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
79
80
		/** This action is documented in modules/widgets/social-media-icons.php */
81
		do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );
82
83
		return $output;
84
	}
85
86
	/**
87
	 * Adds parameters to URL used to fetch the tweet.
88
	 *
89
	 * @since 4.5.0
90
	 *
91
	 * @param string $provider URL of provider that supplies the tweet we're requesting.
92
	 * @param string $url      URL of tweet to embed.
93
	 * @param array  $args     Parameters supplied to shortcode and passed to wp_oembed_get
94
	 *
95
	 * @return string
96
	 */
97
	static public function jetpack_tweet_url_extra_args( $provider, $url, $args = array() ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
98
		foreach ( self::$provider_args as $key => $value ) {
99
			switch ( $key ) {
100
				case 'align':
101
				case 'lang':
102
				case 'hide_thread':
103
				case 'hide_media':
104
					$provider = add_query_arg( $key, $value, $provider );
105
					break;
106
			}
107
		}
108
109
		// Disable script since we're enqueing it in our own way in the footer
110
		$provider = add_query_arg( 'omit_script', 'true', $provider );
111
112
		// Twitter doesn't support maxheight so don't send it
113
		$provider = remove_query_arg( 'maxheight', $provider );
114
115
		/**
116
		 * Filter the Twitter Partner ID.
117
		 *
118
		 * @module shortcodes
119
		 *
120
		 * @since 4.6.0
121
		 *
122
		 * @param string $partner_id Twitter partner ID.
123
		 */
124
		$partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
125
126
		// Add Twitter partner ID to track embeds from Jetpack
127
		if ( ! empty( $partner ) ) {
128
			$provider = add_query_arg( 'partner', $partner, $provider );
129
		}
130
131
		return $provider;
132
	}
133
134
	/**
135
	 * Enqueue front end assets.
136
	 *
137
	 * @since 4.5.0
138
	 */
139
	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...
140
		if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
141
			wp_register_script( 'twitter-widgets', set_url_scheme( 'http://platform.twitter.com/widgets.js' ), array(), JETPACK__VERSION, true );
142
			wp_print_scripts( 'twitter-widgets' );
143
		}
144
	}
145
146
	static public function enqueue_block_editor_assets() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
147
		wp_enqueue_script( 'wp-blocks' );
148
		wp_enqueue_script( 'wp-i18n' );
149
		wp_enqueue_script( 'wp-element' );
150
		wp_enqueue_script( 'shortcode' );
151
		add_action( 'admin_print_footer_scripts', array( __CLASS__, 'admin_footer' ), 999 );
152
	}
153
154
	static public function admin_footer() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
155
?>
156
<script>
157
( function( wp ) {
158
	var blockStyle = {
159
			backgroundColor: '#900',
160
			color: '#fff',
161
			padding: '20px'
162
		};
163
164
	wp.blocks.registerBlockType( 'jetpack/tweet', {
165
		title: wp.i18n.__( 'Tweet', 'jetpack' ),
166
		icon: 'twitter',
167
		category: 'layout',
168
169
		attributes : {
170
			tweet : wp.blocks.query.query( 'input[name=tweet]').value
171
		},
172
173
		edit : function( props ) {
174
			return wp.element.createElement(
175
				'input',
176
				{
177
					name : 'tweet',
178
					type : 'url',
179
					value : props.tweet
180
				},
181
				null
182
			);
183
		},
184
185
		save : function( props ) {
186
			return wp.shortcode.string({
187
				tag     : 'tweet',
188
				attrs   : {
189
					named   : {},
190
					numeric : [
191
						props.tweet
192
					]
193
				}
194
			});
195
		}
196
197
	} );
198
} )( window.wp );
199
</script>
200
<?php
201
	}
202
203
} // class end
204