Completed
Push — gutenberg/tweet-block ( 1fbb0d...681794 )
by George
10:34
created

Jetpack_Tweet   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 196
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B jetpack_tweet_shortcode() 0 50 6
C jetpack_tweet_url_extra_args() 0 36 7
A jetpack_tweet_shortcode_script() 0 6 2
A enqueue_block_editor_assets() 0 14 1
A jetpack_shortcode_tweet_gutenberg_script() 0 55 1
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_register_script(
148
			'jetpack-shortcode-tweet-gutenberg',
149
			null,
150
			array( 'wp-blocks', 'wp-i18n', 'wp-element', 'shortcode' )
151
		);
152
		wp_enqueue_script( 'jetpack-shortcode-tweet-gutenberg' );
153
154
		ob_start();
155
		self::jetpack_shortcode_tweet_gutenberg_script();
0 ignored issues
show
Unused Code introduced by
The call to the method Jetpack_Tweet::jetpack_s...weet_gutenberg_script() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
156
		$content = ob_get_clean();
157
158
		wp_script_add_data( 'jetpack-shortcode-tweet-gutenberg', 'data', $content );
159
	}
160
161
	static public function jetpack_shortcode_tweet_gutenberg_script() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
162
?>
163
// <script>
164
( function( wp ) {
165
	var blockStyle = {
166
			backgroundColor: '#900',
167
			color: '#fff',
168
			padding: '20px'
169
		};
170
171
	wp.blocks.registerBlockType( 'jetpack/tweet', {
172
		title: wp.i18n.__( 'Tweet', 'jetpack' ),
173
		icon: 'twitter',
174
		category: 'layout',
175
176
		attributes : {
177
			tweet : function ( node ) {
178
				console.log( node );
179
			}
180
		},
181
182
		edit : function( props ) {
183
			return wp.element.createElement(
184
				'input',
185
				{
186
					name : 'tweet',
187
					type : 'url',
188
					value : props.attributes.tweet,
189
					onInput: function( event ) {
190
						props.setAttributes({
191
							tweet: event.target.value
192
						});
193
					}
194
				},
195
				null
196
			);
197
		},
198
199
		save : function( props ) {
200
			return wp.shortcode.string({
201
				tag     : 'tweet',
202
				attrs   : {
203
					named   : {},
204
					numeric : [
205
						props.attributes.tweet
206
					]
207
				}
208
			});
209
		}
210
211
	} );
212
} )( window.wp );
213
// </script>
214
<?php
215
	}
216
217
} // class end
218