PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.3
Jetpack – WP Security, Backup, Speed, & Growth v7.2.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / twitter-timeline.php
twitter-timeline.php
504 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Based on Evolution Twitter Timeline
5 * (https://wordpress.org/extend/plugins/evolution-twitter-timeline/)
6 * For details on Twitter Timelines see:
7 * - https://twitter.com/settings/widgets
8 * - https://dev.twitter.com/docs/embedded-timelines
9 */
10
11 /**
12 * Register the widget for use in Appearance -> Widgets
13 */
14 add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' );
15
16 function jetpack_twitter_timeline_widget_init() {
17 register_widget( 'Jetpack_Twitter_Timeline_Widget' );
18 }
19
20 class Jetpack_Twitter_Timeline_Widget extends WP_Widget {
21 /**
22 * Register widget with WordPress.
23 */
24 public function __construct() {
25 parent::__construct(
26 'twitter_timeline',
27 /** This filter is documented in modules/widgets/facebook-likebox.php */
28 apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),
29 array(
30 'classname' => 'widget_twitter_timeline',
31 'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ),
32 'customize_selective_refresh' => true,
33 )
34 );
35
36 if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
37 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
38 }
39
40 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
41 }
42
43 /**
44 * Enqueue scripts.
45 */
46 public function enqueue_scripts() {
47 wp_enqueue_script( 'jetpack-twitter-timeline' );
48 }
49
50 /**
51 * Enqueue Twitter's widget library.
52 *
53 * @deprecated
54 */
55 public function library() {
56 _deprecated_function( __METHOD__, '4.0.0' );
57 wp_print_scripts( array( 'jetpack-twitter-timeline' ) );
58 }
59
60 /**
61 * Enqueue script to improve admin UI
62 */
63 public function admin_scripts( $hook ) {
64 // This is still 'widgets.php' when managing widgets via the Customizer.
65 if ( 'widgets.php' === $hook ) {
66 wp_enqueue_script(
67 'twitter-timeline-admin',
68 Jetpack::get_file_url_for_environment(
69 '_inc/build/widgets/twitter-timeline-admin.min.js',
70 'modules/widgets/twitter-timeline-admin.js'
71 )
72 );
73 }
74 }
75
76 /**
77 * Front-end display of widget.
78 *
79 * @see WP_Widget::widget()
80 *
81 * @param array $args Widget arguments.
82 * @param array $instance Saved values from database.
83 */
84 public function widget( $args, $instance ) {
85 // Twitter deprecated `data-widget-id` on 2018-05-25,
86 // with cease support deadline on 2018-07-27.
87 if ( isset( $instance['type'] ) && 'widget-id' === $instance['type'] ) {
88 if ( current_user_can( 'edit_theme_options' ) ) {
89 echo $args['before_widget'];
90 echo $args['before_title'] . esc_html__( 'Twitter Timeline', 'jetpack' ) . $args['after_title'];
91 echo '<p>' . esc_html__( "The Twitter Timeline widget can't display tweets based on searches or hashtags. To display a simple list of tweets instead, change the Widget ID to a Twitter username. Otherwise, delete this widget.", 'jetpack' ) . '</p>';
92 echo '<p>' . esc_html__( '(Only administrators will see this message.)', 'jetpack' ) . '</p>';
93 echo $args['after_widget'];
94 }
95 return;
96 }
97
98 $instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );
99
100 echo $args['before_widget'];
101
102 $title = isset( $instance['title'] ) ? $instance['title'] : '';
103
104 /** This filter is documented in core/src/wp-includes/default-widgets.php */
105 $title = apply_filters( 'widget_title', $title );
106 if ( ! empty( $title ) ) {
107 echo $args['before_title'] . $title . $args['after_title'];
108 }
109
110 if ( isset( $instance['type'] ) && 'widget-id' === $instance['type'] && current_user_can( 'edit_theme_options' ) ) {
111 echo '<p>' . esc_html__( 'As of July 27, 2018, the Twitter Timeline widget will no longer display tweets based on searches or hashtags. To display a simple list of tweets instead, change the Widget ID to a Twitter username.', 'jetpack' ) . '</p>';
112 echo '<p>' . esc_html__( '(Only administrators will see this message.)', 'jetpack' ) . '</p>';
113 }
114
115 // Start tag output
116 // This tag is transformed into the widget markup by Twitter's
117 // widgets.js code
118 echo '<a class="twitter-timeline"';
119
120 $data_attribs = array(
121 'width',
122 'height',
123 'theme',
124 'link-color',
125 'border-color',
126 'tweet-limit',
127 'lang',
128 );
129 foreach ( $data_attribs as $att ) {
130 if ( ! empty( $instance[ $att ] ) && ! is_array( $instance[ $att ] ) ) {
131 echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[ $att ] ) . '"';
132 }
133 }
134
135 /** This filter is documented in modules/shortcodes/tweet.php */
136 $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
137 if ( ! empty( $partner ) ) {
138 echo ' data-partner="' . esc_attr( $partner ) . '"';
139 }
140
141 /**
142 * Allow the activation of Do Not Track for the Twitter Timeline Widget.
143 *
144 * @see https://developer.twitter.com/en/docs/twitter-for-websites/timelines/guides/parameter-reference.html
145 *
146 * @module widgets
147 *
148 * @since 6.9.0
149 *
150 * @param bool false Should the Twitter Timeline use the DNT attribute? Default to false.
151 */
152 $dnt = apply_filters( 'jetpack_twitter_timeline_default_dnt', false );
153 if ( true === $dnt ) {
154 echo ' data-dnt="true"';
155 }
156
157 if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
158 echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
159 }
160
161 $type = ( isset( $instance['type'] ) ? $instance['type'] : '' );
162 $widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
163 switch ( $type ) {
164 case 'profile':
165 echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
166 break;
167 case 'widget-id':
168 default:
169 echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
170 break;
171 }
172 echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
173
174 // End tag output
175 echo '>';
176
177 $timeline_placeholder = __( 'My Tweets', 'jetpack' );
178
179 /**
180 * Filter the Timeline placeholder text.
181 *
182 * @module widgets
183 *
184 * @since 3.4.0
185 *
186 * @param string $timeline_placeholder Timeline placeholder text.
187 */
188 $timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );
189
190 echo esc_html( $timeline_placeholder ) . '</a>';
191
192 // End tag output
193
194 echo $args['after_widget'];
195
196 /** This action is documented in modules/widgets/gravatar-profile.php */
197 do_action( 'jetpack_stats_extra', 'widget_view', 'twitter_timeline' );
198 }
199
200
201 /**
202 * Sanitize widget form values as they are saved.
203 *
204 * @see WP_Widget::update()
205 *
206 * @param array $new_instance Values just sent to be saved.
207 * @param array $old_instance Previously saved values from database.
208 *
209 * @return array Updated safe values to be saved.
210 */
211 public function update( $new_instance, $old_instance ) {
212 $instance = array();
213
214 $instance['title'] = sanitize_text_field( $new_instance['title'] );
215
216 $width = (int) $new_instance['width'];
217 if ( $width ) {
218 // From publish.twitter.com: 220 <= width <= 1200
219 $instance['width'] = min( max( $width, 220 ), 1200 );
220 } else {
221 $instance['width'] = '';
222 }
223
224 $height = (int) $new_instance['height'];
225 if ( $height ) {
226 // From publish.twitter.com: height >= 200
227 $instance['height'] = max( $height, 200 );
228 } else {
229 $instance['height'] = '';
230 }
231
232 $tweet_limit = (int) $new_instance['tweet-limit'];
233 if ( $tweet_limit ) {
234 $instance['tweet-limit'] = min( max( $tweet_limit, 1 ), 20 );
235 /**
236 * A timeline with a specified limit is expanded to the height of those Tweets.
237 * The specified height value no longer applies, so reject the height value
238 * when a valid limit is set: a widget attempting to save both limit 5 and
239 * height 400 would be saved with just limit 5.
240 */
241 $instance['height'] = '';
242 } else {
243 $instance['tweet-limit'] = null;
244 }
245
246 // If they entered something that might be a full URL, try to parse it out
247 if ( is_string( $new_instance['widget-id'] ) ) {
248 if ( preg_match(
249 '#https?://twitter\.com/settings/widgets/(\d+)#s',
250 $new_instance['widget-id'],
251 $matches
252 ) ) {
253 $new_instance['widget-id'] = $matches[1];
254 }
255 }
256
257 $instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
258
259 $hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/';
260 foreach ( array( 'link-color', 'border-color' ) as $color ) {
261 $new_color = sanitize_text_field( $new_instance[ $color ] );
262 if ( preg_match( $hex_regex, $new_color ) ) {
263 $instance[ $color ] = $new_color;
264 }
265 }
266
267 $instance['type'] = 'profile';
268
269 $instance['theme'] = 'light';
270 if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
271 $instance['theme'] = $new_instance['theme'];
272 }
273
274 $instance['chrome'] = array();
275 $chrome_settings = array(
276 'noheader',
277 'nofooter',
278 'noborders',
279 'transparent',
280 'noscrollbar',
281 );
282 if ( isset( $new_instance['chrome'] ) ) {
283 foreach ( $new_instance['chrome'] as $chrome ) {
284 if ( in_array( $chrome, $chrome_settings ) ) {
285 $instance['chrome'][] = $chrome;
286 }
287 }
288 }
289
290 return $instance;
291 }
292
293 /**
294 * Returns a link to the documentation for a feature of this widget on
295 * Jetpack or WordPress.com.
296 */
297 public function get_docs_link( $hash = '' ) {
298 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
299 $base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
300 } else {
301 $base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
302 }
303 return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
304 }
305
306 /**
307 * Back end widget form.
308 *
309 * @see WP_Widget::form()
310 *
311 * @param array $instance Previously saved values from database.
312 */
313 public function form( $instance ) {
314 $defaults = array(
315 'title' => esc_html__( 'Follow me on Twitter', 'jetpack' ),
316 'width' => '',
317 'height' => '400',
318 'type' => 'profile',
319 'widget-id' => '',
320 'link-color' => '#f96e5b',
321 'border-color' => '#e8e8e8',
322 'theme' => 'light',
323 'chrome' => array(),
324 'tweet-limit' => null,
325 );
326
327 $instance = wp_parse_args( (array) $instance, $defaults );
328
329 if ( 'widget-id' === $instance['type'] ) {
330 $instance['widget-id'] = '';
331 }
332
333 $instance['type'] = 'profile';
334 ?>
335
336 <p>
337 <label for="<?php echo $this->get_field_id( 'title' ); ?>">
338 <?php esc_html_e( 'Title:', 'jetpack' ); ?>
339 </label>
340 <input
341 class="widefat"
342 id="<?php echo $this->get_field_id( 'title' ); ?>"
343 name="<?php echo $this->get_field_name( 'title' ); ?>"
344 type="text"
345 value="<?php echo esc_attr( $instance['title'] ); ?>"
346 />
347 </p>
348
349 <p>
350 <label for="<?php echo $this->get_field_id( 'width' ); ?>">
351 <?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
352 </label>
353 <input
354 class="widefat"
355 id="<?php echo $this->get_field_id( 'width' ); ?>"
356 name="<?php echo $this->get_field_name( 'width' ); ?>"
357 type="number" min="220" max="1200"
358 value="<?php echo esc_attr( $instance['width'] ); ?>"
359 />
360 </p>
361
362 <p>
363 <label for="<?php echo $this->get_field_id( 'height' ); ?>">
364 <?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
365 </label>
366 <input
367 class="widefat"
368 id="<?php echo $this->get_field_id( 'height' ); ?>"
369 name="<?php echo $this->get_field_name( 'height' ); ?>"
370 type="number" min="200"
371 value="<?php echo esc_attr( $instance['height'] ); ?>"
372 />
373 </p>
374
375 <p>
376 <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
377 <?php esc_html_e( '# of Tweets Shown (1 to 20):', 'jetpack' ); ?>
378 </label>
379 <input
380 class="widefat"
381 id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"
382 name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>"
383 type="number" min="1" max="20"
384 value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>"
385 />
386 </p>
387
388 <p class="jetpack-twitter-timeline-widget-id-container">
389 <label for="<?php echo $this->get_field_id( 'widget-id' ); ?>">
390 <?php esc_html_e( 'Twitter Username:', 'jetpack' ); ?>
391 <?php echo $this->get_docs_link( '#twitter-username' ); ?>
392 </label>
393 <input
394 class="widefat"
395 id="<?php echo $this->get_field_id( 'widget-id' ); ?>"
396 name="<?php echo $this->get_field_name( 'widget-id' ); ?>"
397 type="text"
398 value="<?php echo esc_attr( $instance['widget-id'] ); ?>"
399 />
400 </p>
401
402 <p>
403 <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
404 <?php esc_html_e( 'Layout Options:', 'jetpack' ); ?>
405 </label>
406 <br />
407 <input
408 type="checkbox"<?php checked( in_array( 'noheader', $instance['chrome'] ) ); ?>
409 id="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"
410 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
411 value="noheader"
412 />
413 <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
414 <?php esc_html_e( 'No Header', 'jetpack' ); ?>
415 </label>
416 <br />
417 <input
418 type="checkbox"<?php checked( in_array( 'nofooter', $instance['chrome'] ) ); ?>
419 id="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>"
420 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
421 value="nofooter"
422 />
423 <label for="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>">
424 <?php esc_html_e( 'No Footer', 'jetpack' ); ?>
425 </label>
426 <br />
427 <input
428 type="checkbox"<?php checked( in_array( 'noborders', $instance['chrome'] ) ); ?>
429 id="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>"
430 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
431 value="noborders"
432 />
433 <label for="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>">
434 <?php esc_html_e( 'No Borders', 'jetpack' ); ?>
435 </label>
436 <br />
437 <input
438 type="checkbox"<?php checked( in_array( 'noscrollbar', $instance['chrome'] ) ); ?>
439 id="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>"
440 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
441 value="noscrollbar"
442 />
443 <label for="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>">
444 <?php esc_html_e( 'No Scrollbar', 'jetpack' ); ?>
445 </label>
446 <br />
447 <input
448 type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?>
449 id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"
450 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
451 value="transparent"
452 />
453 <label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>">
454 <?php esc_html_e( 'Transparent Background', 'jetpack' ); ?>
455 </label>
456 </p>
457
458 <p>
459 <label for="<?php echo $this->get_field_id( 'link-color' ); ?>">
460 <?php _e( 'Link Color (hex):', 'jetpack' ); ?>
461 </label>
462 <input
463 class="widefat"
464 id="<?php echo $this->get_field_id( 'link-color' ); ?>"
465 name="<?php echo $this->get_field_name( 'link-color' ); ?>"
466 type="text"
467 value="<?php echo esc_attr( $instance['link-color'] ); ?>"
468 />
469 </p>
470
471 <p>
472 <label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
473 <?php _e( 'Border Color (hex):', 'jetpack' ); ?>
474 </label>
475 <input
476 class="widefat"
477 id="<?php echo $this->get_field_id( 'border-color' ); ?>"
478 name="<?php echo $this->get_field_name( 'border-color' ); ?>"
479 type="text"
480 value="<?php echo esc_attr( $instance['border-color'] ); ?>"
481 />
482 </p>
483
484 <p>
485 <label for="<?php echo $this->get_field_id( 'theme' ); ?>">
486 <?php _e( 'Timeline Theme:', 'jetpack' ); ?>
487 </label>
488 <select
489 name="<?php echo $this->get_field_name( 'theme' ); ?>"
490 id="<?php echo $this->get_field_id( 'theme' ); ?>"
491 class="widefat"
492 >
493 <option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
494 <?php esc_html_e( 'Light', 'jetpack' ); ?>
495 </option>
496 <option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
497 <?php esc_html_e( 'Dark', 'jetpack' ); ?>
498 </option>
499 </select>
500 </p>
501 <?php
502 }
503 }
504