Jetpack_Twitter_Timeline_Widget

The Jetpack by WordPress.com Jetpack Twitter Timeline Widget class.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/twitter-timeline.php  
  1. class Jetpack_Twitter_Timeline_Widget extends WP_Widget { 
  2. /** 
  3. * Register widget with WordPress. 
  4. */ 
  5. public function __construct() { 
  6. parent::__construct( 
  7. 'twitter_timeline',  
  8. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  9. apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),  
  10. array( 
  11. 'classname' => 'widget_twitter_timeline',  
  12. 'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ) 
  13. ); 
  14.  
  15. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) { 
  16. add_action( 'wp_footer', array( $this, 'library' ) ); 
  17.  
  18. /** 
  19. * Enqueue Twitter's widget library 
  20. */ 
  21. public function library() { 
  22. ?> 
  23. <script type="text/javascript"> 
  24. !function(d, s, id) { 
  25. var js, fjs=d.getElementsByTagName(s)[0], p=/^http:/.test(d.location)?'http':'https'; 
  26. if(!d.getElementById(id)) { 
  27. js=d.createElement(s); 
  28. js.id=id;js.src=p+"://platform.twitter.com/widgets.js"; 
  29. fjs.parentNode.insertBefore(js, fjs); 
  30. }(document, "script", "twitter-wjs"); 
  31. </script> 
  32. <?php 
  33.  
  34. /** 
  35. * Front-end display of widget. 
  36. * @see WP_Widget::widget() 
  37. * @param array $args Widget arguments. 
  38. * @param array $instance Saved values from database. 
  39. */ 
  40. public function widget( $args, $instance ) { 
  41. $instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 ); 
  42.  
  43. echo $args['before_widget']; 
  44.  
  45. if ( $instance['title'] ) { 
  46. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  47. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; 
  48.  
  49. $data_attribs = array( 'widget-id', 'theme', 'link-color', 'border-color', 'chrome', 'tweet-limit' ); 
  50. $attribs = array( 'width', 'height', 'lang' ); 
  51.  
  52. // Start tag output 
  53. echo '<a class="twitter-timeline"'; 
  54.  
  55. foreach ( $data_attribs as $att ) { 
  56. if ( !empty( $instance[$att] ) ) { 
  57. if ( 'tweet-limit' == $att && 0 === $instance[$att] ) 
  58. continue; 
  59.  
  60. if ( is_array( $instance[$att] ) ) 
  61. echo ' data-' . esc_attr( $att ) . '="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"'; 
  62. else 
  63. echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[$att] ) . '"'; 
  64.  
  65. foreach ( $attribs as $att ) { 
  66. if ( !empty( $instance[$att] ) ) 
  67. echo ' ' . esc_attr( $att ) . '="' . esc_attr( $instance[$att] ) . '"'; 
  68.  
  69. echo '>'; 
  70.  
  71. $timeline_placeholder = __( 'My Tweets', 'jetpack' ); 
  72.  
  73. /** 
  74. * Filter the Timeline placeholder text. 
  75. * @module widgets 
  76. * @since 3.4.0 
  77. * @param string $timeline_placeholder Timeline placeholder text. 
  78. */ 
  79. $timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder ); 
  80.  
  81. echo esc_html( $timeline_placeholder ) . '</a>'; 
  82.  
  83. // End tag output 
  84.  
  85. echo $args['after_widget']; 
  86.  
  87. /** This action is documented in modules/widgets/social-media-icons.php */ 
  88. do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter_timeline' ); 
  89.  
  90.  
  91. /** 
  92. * Sanitize widget form values as they are saved. 
  93. * @see WP_Widget::update() 
  94. * @param array $new_instance Values just sent to be saved. 
  95. * @param array $old_instance Previously saved values from database. 
  96. * @return array Updated safe values to be saved. 
  97. */ 
  98. public function update( $new_instance, $old_instance ) { 
  99. $hex_regex = '/#([a-f]|[A-F]|[0-9]) {3}(([a-f]|[A-F]|[0-9]) {3})?\b/'; 
  100. $instance = array(); 
  101. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  102. $instance['width'] = (int) $new_instance['width']; 
  103. $instance['height'] = (int) $new_instance['height']; 
  104. $instance['width'] = ( 0 !== (int) $new_instance['width'] ) ? (int) $new_instance['width'] : ''; 
  105. $instance['height'] = ( 0 !== (int) $new_instance['height'] ) ? (int) $new_instance['height'] : ''; 
  106. $instance['tweet-limit'] = ( 0 !== (int) $new_instance['tweet-limit'] ) ? (int) $new_instance['tweet-limit'] : null; 
  107.  
  108. // If they entered something that might be a full URL, try to parse it out 
  109. if ( is_string( $new_instance['widget-id'] ) ) { 
  110. if ( preg_match( '#https?://twitter\.com/settings/widgets/(\d+)#s', $new_instance['widget-id'], $matches ) ) { 
  111. $new_instance['widget-id'] = $matches[1]; 
  112.  
  113. $instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] ); 
  114. $instance['widget-id'] = is_numeric( $instance['widget-id'] ) ? $instance['widget-id'] : ''; 
  115.  
  116. foreach ( array( 'link-color', 'border-color' ) as $color ) { 
  117. $new_color = sanitize_text_field( $new_instance[$color] ); 
  118. if ( preg_match( $hex_regex, $new_color ) ) { 
  119. $instance[$color] = $new_color; 
  120.  
  121.  
  122. $instance['theme'] = 'light'; 
  123. if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) 
  124. $instance['theme'] = $new_instance['theme']; 
  125.  
  126. $instance['chrome'] = array(); 
  127. if ( isset( $new_instance['chrome'] ) ) { 
  128. foreach ( $new_instance['chrome'] as $chrome ) { 
  129. if ( in_array( $chrome, array( 'noheader', 'nofooter', 'noborders', 'noscrollbar', 'transparent' ) ) ) { 
  130. $instance['chrome'][] = $chrome; 
  131.  
  132. return $instance; 
  133.  
  134.  
  135. /** 
  136. * Back-end widget form. 
  137. * @see WP_Widget::form() 
  138. * @param array $instance Previously saved values from database. 
  139. */ 
  140. public function form( $instance ) { 
  141. $defaults = array( 
  142. 'title' => esc_html__( 'Follow me on Twitter', 'jetpack' ),  
  143. 'width' => '',  
  144. 'height' => '400',  
  145. 'widget-id' => '',  
  146. 'link-color' => '#f96e5b',  
  147. 'border-color' => '#e8e8e8',  
  148. 'theme' => 'light',  
  149. 'chrome' => array(),  
  150. 'tweet-limit' => null,  
  151. ); 
  152.  
  153. $instance = wp_parse_args( (array) $instance, $defaults ); 
  154. ?> 
  155.  
  156. <p> 
  157. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> 
  158. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> 
  159. </p> 
  160.  
  161. <p> 
  162. <label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php esc_html_e( 'Width (px):', 'jetpack' ); ?></label> 
  163. <input class="widefat" id="<?php echo $this->get_field_id( 'width' ); ?>" name="<?php echo $this->get_field_name( 'width' ); ?>" type="text" value="<?php echo esc_attr( $instance['width'] ); ?>" /> 
  164. </p> 
  165.  
  166. <p> 
  167. <label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php esc_html_e( 'Height (px):', 'jetpack' ); ?></label> 
  168. <input class="widefat" id="<?php echo $this->get_field_id( 'height' ); ?>" name="<?php echo $this->get_field_name( 'height' ); ?>" type="text" value="<?php echo esc_attr( $instance['height'] ); ?>" /> 
  169. </p> 
  170.  
  171. <p> 
  172. <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"><?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?></label> 
  173. <input class="widefat" id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>" name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>" type="number" min="1" max="20" value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>" /> 
  174. </p> 
  175.  
  176. <p><small> 
  177. <?php 
  178. echo wp_kses_post( 
  179. sprintf( 
  180. __( 'You need to <a href="%1$s" target="_blank">create a widget at Twitter.com</a>, and then enter your widget id (the long number found in the URL of your widget\'s config page) in the field below. <a href="%2$s" target="_blank">Read more</a>.', 'jetpack' ),  
  181. 'https://twitter.com/settings/widgets/new/user',  
  182. 'http://support.wordpress.com/widgets/twitter-timeline-widget/' 
  183. ); 
  184. ?> 
  185. </small></p> 
  186. <p> 
  187. <label for="<?php echo $this->get_field_id( 'widget-id' ); ?>"><?php esc_html_e( 'Widget ID:', 'jetpack' ); ?> <a href="http://support.wordpress.com/widgets/twitter-timeline-widget/#widget-id" target="_blank">( ? )</a></label> 
  188. <input class="widefat" id="<?php echo $this->get_field_id( 'widget-id' ); ?>" name="<?php echo $this->get_field_name( 'widget-id' ); ?>" type="text" value="<?php echo esc_attr( $instance['widget-id'] ); ?>" /> 
  189. </p> 
  190.  
  191. <p> 
  192. <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"><?php esc_html_e( 'Layout Options:', 'jetpack' ); ?></label><br /> 
  193. <input type="checkbox"<?php checked( in_array( 'noheader', $instance['chrome'] ) ); ?> id="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>" name="<?php echo $this->get_field_name( 'chrome' ); ?>[]" value="noheader" /> <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"><?php esc_html_e( 'No Header', 'jetpack' ); ?></label><br /> 
  194. <input type="checkbox"<?php checked( in_array( 'nofooter', $instance['chrome'] ) ); ?> id="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>" name="<?php echo $this->get_field_name( 'chrome' ); ?>[]" value="nofooter" /> <label for="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>"><?php esc_html_e( 'No Footer', 'jetpack' ); ?></label><br /> 
  195. <input type="checkbox"<?php checked( in_array( 'noborders', $instance['chrome'] ) ); ?> id="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>" name="<?php echo $this->get_field_name( 'chrome' ); ?>[]" value="noborders" /> <label for="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>"><?php esc_html_e( 'No Borders', 'jetpack' ); ?></label><br /> 
  196. <input type="checkbox"<?php checked( in_array( 'noscrollbar', $instance['chrome'] ) ); ?> id="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>" name="<?php echo $this->get_field_name( 'chrome' ); ?>[]" value="noscrollbar" /> <label for="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>"><?php esc_html_e( 'No Scrollbar', 'jetpack' ); ?></label><br /> 
  197. <input type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?> id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>" name="<?php echo $this->get_field_name( 'chrome' ); ?>[]" value="transparent" /> <label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"><?php esc_html_e( 'Transparent Background', 'jetpack' ); ?></label> 
  198. </p> 
  199.  
  200. <p> 
  201. <label for="<?php echo $this->get_field_id( 'link-color' ); ?>"><?php _e( 'Link Color (hex):', 'jetpack' ); ?></label> 
  202. <input class="widefat" id="<?php echo $this->get_field_id( 'link-color' ); ?>" name="<?php echo $this->get_field_name( 'link-color' ); ?>" type="text" value="<?php echo esc_attr( $instance['link-color'] ); ?>" /> 
  203. </p> 
  204.  
  205. <p> 
  206. <label for="<?php echo $this->get_field_id( 'border-color' ); ?>"><?php _e( 'Border Color (hex):', 'jetpack' ); ?></label> 
  207. <input class="widefat" id="<?php echo $this->get_field_id( 'border-color' ); ?>" name="<?php echo $this->get_field_name( 'border-color' ); ?>" type="text" value="<?php echo esc_attr( $instance['border-color'] ); ?>" /> 
  208. </p> 
  209.  
  210. <p> 
  211. <label for="<?php echo $this->get_field_id( 'theme' ); ?>"><?php _e( 'Timeline Theme:', 'jetpack' ); ?></label> 
  212. <select name="<?php echo $this->get_field_name( 'theme' ); ?>" id="<?php echo $this->get_field_id( 'theme' ); ?>" class="widefat"> 
  213. <option value="light"<?php selected( $instance['theme'], 'light' ); ?>><?php esc_html_e( 'Light', 'jetpack' ); ?></option> 
  214. <option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>><?php esc_html_e( 'Dark', 'jetpack' ); ?></option> 
  215. </select> 
  216. </p> 
  217. <?php