PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.1
Jetpack – WP Security, Backup, Speed, & Growth v6.1
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 in Jetpack – WP Security, Backup, Speed, & Growth 6.1, at modules/widgets/twitter-timeline.php

508 lines 15.3 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 $instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );
86
87 echo $args['before_widget'];
88
89 $title = isset( $instance['title'] ) ? $instance['title'] : '';
90
91 /** This filter is documented in core/src/wp-includes/default-widgets.php */
92 $title = apply_filters( 'widget_title', $title );
93 if ( ! empty( $title ) ) {
94 echo $args['before_title'] . $title . $args['after_title'];
95 }
96
97 // Start tag output
98 // This tag is transformed into the widget markup by Twitter's
99 // widgets.js code
100 echo '<a class="twitter-timeline"';
101
102 $data_attribs = array(
103 'width',
104 'height',
105 'theme',
106 'link-color',
107 'border-color',
108 'tweet-limit',
109 'lang'
110 );
111 foreach ( $data_attribs as $att ) {
112 if ( ! empty( $instance[ $att ] ) && ! is_array( $instance[ $att ] ) ) {
113 echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[ $att ] ) . '"';
114 }
115 }
116
117 /** This filter is documented in modules/shortcodes/tweet.php */
118 $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
119 if ( ! empty( $partner ) ) {
120 echo ' data-partner="' . esc_attr( $partner ) . '"';
121 }
122
123 if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
124 echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
125 }
126
127 $type = ( isset( $instance['type'] ) ? $instance['type'] : '' );
128 $widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
129 switch ( $type ) {
130 case 'profile':
131 echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
132 break;
133 case 'widget-id':
134 default:
135 echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
136 break;
137 }
138
139 // End tag output
140 echo '>';
141
142 $timeline_placeholder = __( 'My Tweets', 'jetpack' );
143
144 /**
145 * Filter the Timeline placeholder text.
146 *
147 * @module widgets
148 *
149 * @since 3.4.0
150 *
151 * @param string $timeline_placeholder Timeline placeholder text.
152 */
153 $timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );
154
155 echo esc_html( $timeline_placeholder ) . '</a>';
156
157 // End tag output
158
159 echo $args['after_widget'];
160
161 /** This action is documented in modules/widgets/gravatar-profile.php */
162 do_action( 'jetpack_stats_extra', 'widget_view', 'twitter_timeline' );
163 }
164
165
166 /**
167 * Sanitize widget form values as they are saved.
168 *
169 * @see WP_Widget::update()
170 *
171 * @param array $new_instance Values just sent to be saved.
172 * @param array $old_instance Previously saved values from database.
173 *
174 * @return array Updated safe values to be saved.
175 */
176 public function update( $new_instance, $old_instance ) {
177 $instance = array();
178
179 $instance['title'] = sanitize_text_field( $new_instance['title'] );
180
181 $width = (int) $new_instance['width'];
182 if ( $width ) {
183 // From publish.twitter.com: 220 <= width <= 1200
184 $instance['width'] = min( max( $width, 220 ), 1200 );
185 } else {
186 $instance['width'] = '';
187 }
188
189 $height = (int) $new_instance['height'];
190 if ( $height ) {
191 // From publish.twitter.com: height >= 200
192 $instance['height'] = max( $height, 200 );
193 } else {
194 $instance['height'] = '';
195 }
196
197 $tweet_limit = (int) $new_instance['tweet-limit'];
198 if ( $tweet_limit ) {
199 $instance['tweet-limit'] = min( max( $tweet_limit, 1 ), 20 );
200 /**
201 * A timeline with a specified limit is expanded to the height of those Tweets.
202 * The specified height value no longer applies, so reject the height value
203 * when a valid limit is set: a widget attempting to save both limit 5 and
204 * height 400 would be saved with just limit 5.
205 */
206 $instance['height'] = '';
207 } else {
208 $instance['tweet-limit'] = null;
209 }
210
211 // If they entered something that might be a full URL, try to parse it out
212 if ( is_string( $new_instance['widget-id'] ) ) {
213 if ( preg_match(
214 '#https?://twitter\.com/settings/widgets/(\d+)#s',
215 $new_instance['widget-id'],
216 $matches
217 ) ) {
218 $new_instance['widget-id'] = $matches[1];
219 }
220 }
221
222 $instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
223
224 $hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/';
225 foreach ( array( 'link-color', 'border-color' ) as $color ) {
226 $new_color = sanitize_text_field( $new_instance[ $color ] );
227 if ( preg_match( $hex_regex, $new_color ) ) {
228 $instance[ $color ] = $new_color;
229 }
230
231 }
232
233 $instance['type'] = 'widget-id';
234 if ( in_array( $new_instance['type'], array( 'widget-id', 'profile' ) ) ) {
235 $instance['type'] = $new_instance['type'];
236 }
237
238 $instance['theme'] = 'light';
239 if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
240 $instance['theme'] = $new_instance['theme'];
241 }
242
243 $instance['chrome'] = array();
244 $chrome_settings = array(
245 'noheader',
246 'nofooter',
247 'noborders',
248 'transparent',
249 'noscrollbar',
250 );
251 if ( isset( $new_instance['chrome'] ) ) {
252 foreach ( $new_instance['chrome'] as $chrome ) {
253 if ( in_array( $chrome, $chrome_settings ) ) {
254 $instance['chrome'][] = $chrome;
255 }
256 }
257 }
258
259 return $instance;
260 }
261
262 /**
263 * Returns a link to the documentation for a feature of this widget on
264 * Jetpack or WordPress.com.
265 */
266 public function get_docs_link( $hash = '' ) {
267 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
268 $base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
269 } else {
270 $base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
271 }
272 return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
273 }
274
275 /**
276 * Back end widget form.
277 *
278 * @see WP_Widget::form()
279 *
280 * @param array $instance Previously saved values from database.
281 */
282 public function form( $instance ) {
283 $defaults = array(
284 'title' => esc_html__( 'Follow me on Twitter', 'jetpack' ),
285 'width' => '',
286 'height' => '400',
287 'widget-id' => '',
288 'link-color' => '#f96e5b',
289 'border-color' => '#e8e8e8',
290 'theme' => 'light',
291 'chrome' => array(),
292 'tweet-limit' => null,
293 );
294
295 $instance = wp_parse_args( (array) $instance, $defaults );
296
297 if ( empty( $instance['type'] ) ) {
298 // Decide the correct widget type. If this is a pre-existing
299 // widget with a numeric widget ID, then the type should be
300 // 'widget-id', otherwise it should be 'profile'.
301 if ( ! empty( $instance['widget-id'] ) && is_numeric( $instance['widget-id'] ) ) {
302 $instance['type'] = 'widget-id';
303 } else {
304 $instance['type'] = 'profile';
305 }
306 }
307 ?>
308
309 <p>
310 <label for="<?php echo $this->get_field_id( 'title' ); ?>">
311 <?php esc_html_e( 'Title:', 'jetpack' ); ?>
312 </label>
313 <input
314 class="widefat"
315 id="<?php echo $this->get_field_id( 'title' ); ?>"
316 name="<?php echo $this->get_field_name( 'title' ); ?>"
317 type="text"
318 value="<?php echo esc_attr( $instance['title'] ); ?>"
319 />
320 </p>
321
322 <p>
323 <label for="<?php echo $this->get_field_id( 'width' ); ?>">
324 <?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
325 </label>
326 <input
327 class="widefat"
328 id="<?php echo $this->get_field_id( 'width' ); ?>"
329 name="<?php echo $this->get_field_name( 'width' ); ?>"
330 type="number" min="220" max="1200"
331 value="<?php echo esc_attr( $instance['width'] ); ?>"
332 />
333 </p>
334
335 <p>
336 <label for="<?php echo $this->get_field_id( 'height' ); ?>">
337 <?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
338 </label>
339 <input
340 class="widefat"
341 id="<?php echo $this->get_field_id( 'height' ); ?>"
342 name="<?php echo $this->get_field_name( 'height' ); ?>"
343 type="number" min="200"
344 value="<?php echo esc_attr( $instance['height'] ); ?>"
345 />
346 </p>
347
348 <p>
349 <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
350 <?php esc_html_e( '# of Tweets Shown (1 to 20):', 'jetpack' ); ?>
351 </label>
352 <input
353 class="widefat"
354 id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"
355 name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>"
356 type="number" min="1" max="20"
357 value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>"
358 />
359 </p>
360
361 <p class="jetpack-twitter-timeline-widget-type-container">
362 <label for="<?php echo $this->get_field_id( 'type' ); ?>">
363 <?php esc_html_e( 'Widget Type:', 'jetpack' ); ?>
364 <?php echo $this->get_docs_link( '#widget-type' ); ?>
365 </label>
366 <select
367 name="<?php echo $this->get_field_name( 'type' ); ?>"
368 id="<?php echo $this->get_field_id( 'type' ); ?>"
369 class="jetpack-twitter-timeline-widget-type widefat"
370 >
371 <option value="profile"<?php selected( $instance['type'], 'profile' ); ?>>
372 <?php esc_html_e( 'Profile', 'jetpack' ); ?>
373 </option>
374 <option value="widget-id"<?php selected( $instance['type'], 'widget-id' ); ?>>
375 <?php esc_html_e( 'Widget ID', 'jetpack' ); ?>
376 </option>
377 </select>
378 </p>
379
380 <p class="jetpack-twitter-timeline-widget-id-container">
381 <label
382 for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
383 data-widget-type="widget-id"
384 <?php echo ( 'widget-id' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
385 >
386 <?php esc_html_e( 'Widget ID:', 'jetpack' ); ?>
387 <?php echo $this->get_docs_link( '#widget-id' ); ?>
388 </label>
389 <label
390 for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
391 data-widget-type="profile"
392 <?php echo ( 'profile' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
393 >
394 <?php esc_html_e( 'Twitter Username:', 'jetpack' ); ?>
395 <?php echo $this->get_docs_link( '#twitter-username' ); ?>
396 </label>
397 <input
398 class="widefat"
399 id="<?php echo $this->get_field_id( 'widget-id' ); ?>"
400 name="<?php echo $this->get_field_name( 'widget-id' ); ?>"
401 type="text"
402 value="<?php echo esc_attr( $instance['widget-id'] ); ?>"
403 />
404 </p>
405
406 <p>
407 <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
408 <?php esc_html_e( 'Layout Options:', 'jetpack' ); ?>
409 </label>
410 <br />
411 <input
412 type="checkbox"<?php checked( in_array( 'noheader', $instance['chrome'] ) ); ?>
413 id="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"
414 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
415 value="noheader"
416 />
417 <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
418 <?php esc_html_e( 'No Header', 'jetpack' ); ?>
419 </label>
420 <br />
421 <input
422 type="checkbox"<?php checked( in_array( 'nofooter', $instance['chrome'] ) ); ?>
423 id="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>"
424 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
425 value="nofooter"
426 />
427 <label for="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>">
428 <?php esc_html_e( 'No Footer', 'jetpack' ); ?>
429 </label>
430 <br />
431 <input
432 type="checkbox"<?php checked( in_array( 'noborders', $instance['chrome'] ) ); ?>
433 id="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>"
434 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
435 value="noborders"
436 />
437 <label for="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>">
438 <?php esc_html_e( 'No Borders', 'jetpack' ); ?>
439 </label>
440 <br />
441 <input
442 type="checkbox"<?php checked( in_array( 'noscrollbar', $instance['chrome'] ) ); ?>
443 id="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>"
444 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
445 value="noscrollbar"
446 />
447 <label for="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>">
448 <?php esc_html_e( 'No Scrollbar', 'jetpack' ); ?>
449 </label>
450 <br />
451 <input
452 type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?>
453 id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"
454 name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
455 value="transparent"
456 />
457 <label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>">
458 <?php esc_html_e( 'Transparent Background', 'jetpack' ); ?>
459 </label>
460 </p>
461
462 <p>
463 <label for="<?php echo $this->get_field_id( 'link-color' ); ?>">
464 <?php _e( 'Link Color (hex):', 'jetpack' ); ?>
465 </label>
466 <input
467 class="widefat"
468 id="<?php echo $this->get_field_id( 'link-color' ); ?>"
469 name="<?php echo $this->get_field_name( 'link-color' ); ?>"
470 type="text"
471 value="<?php echo esc_attr( $instance['link-color'] ); ?>"
472 />
473 </p>
474
475 <p>
476 <label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
477 <?php _e( 'Border Color (hex):', 'jetpack' ); ?>
478 </label>
479 <input
480 class="widefat"
481 id="<?php echo $this->get_field_id( 'border-color' ); ?>"
482 name="<?php echo $this->get_field_name( 'border-color' ); ?>"
483 type="text"
484 value="<?php echo esc_attr( $instance['border-color'] ); ?>"
485 />
486 </p>
487
488 <p>
489 <label for="<?php echo $this->get_field_id( 'theme' ); ?>">
490 <?php _e( 'Timeline Theme:', 'jetpack' ); ?>
491 </label>
492 <select
493 name="<?php echo $this->get_field_name( 'theme' ); ?>"
494 id="<?php echo $this->get_field_id( 'theme' ); ?>"
495 class="widefat"
496 >
497 <option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
498 <?php esc_html_e( 'Light', 'jetpack' ); ?>
499 </option>
500 <option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
501 <?php esc_html_e( 'Dark', 'jetpack' ); ?>
502 </option>
503 </select>
504 </p>
505 <?php
506 }
507 }
508