PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blog.old/wp-content/plugins/jetpack/modules/widgets/twitter-timeline.php

https://github.com/chopsuei3/oscc
PHP | 223 lines | 149 code | 36 blank | 38 comment | 15 complexity | 8d5ece0998029d542cd5ca6464f8bdde MD5 | raw file
  1. <?php
  2. /*
  3. * Based on Evolution Twitter Timeline (http://wordpress.org/extend/plugins/evolution-twitter-timeline/)
  4. * See: https://twitter.com/settings/widgets and https://dev.twitter.com/docs/embedded-timelines for details on Twitter Timelines
  5. */
  6. /**
  7. * Register the widget for use in Appearance -> Widgets
  8. */
  9. add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' );
  10. function jetpack_twitter_timeline_widget_init() {
  11. register_widget( 'Jetpack_Twitter_Timeline_Widget' );
  12. }
  13. class Jetpack_Twitter_Timeline_Widget extends WP_Widget {
  14. /**
  15. * Register widget with WordPress.
  16. */
  17. public function __construct() {
  18. parent::__construct(
  19. 'twitter_timeline',
  20. apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),
  21. array(
  22. 'classname' => 'widget_twitter_timeline',
  23. 'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' )
  24. )
  25. );
  26. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) {
  27. wp_enqueue_script( 'twitter-widgets', '//platform.twitter.com/widgets.js', '', '', true );
  28. }
  29. }
  30. /**
  31. * Front-end display of widget.
  32. *
  33. * @see WP_Widget::widget()
  34. *
  35. * @param array $args Widget arguments.
  36. * @param array $instance Saved values from database.
  37. */
  38. public function widget( $args, $instance ) {
  39. $instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );
  40. echo $args['before_widget'];
  41. if ( $instance['title'] )
  42. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
  43. $data_attribs = array( 'widget-id', 'theme', 'link-color', 'border-color', 'chrome', 'tweet-limit' );
  44. $attribs = array( 'width', 'height', 'lang' );
  45. // Start tag output
  46. echo '<a class="twitter-timeline"';
  47. foreach ( $data_attribs as $att ) {
  48. if ( !empty( $instance[$att] ) ) {
  49. if ( 'tweet-limit' == $att && 0 === $instance[$att] )
  50. continue;
  51. if ( is_array( $instance[$att] ) )
  52. echo ' data-' . esc_attr( $att ) . '="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
  53. else
  54. echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[$att] ) . '"';
  55. }
  56. }
  57. foreach ( $attribs as $att ) {
  58. if ( !empty( $instance[$att] ) )
  59. echo ' ' . esc_attr( $att ) . '="' . esc_attr( $instance[$att] ) . '"';
  60. }
  61. echo '>' . esc_html__( 'My Tweets', 'jetpack' ) . '</a>';
  62. // End tag output
  63. echo $args['after_widget'];
  64. do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter_timeline' );
  65. }
  66. /**
  67. * Sanitize widget form values as they are saved.
  68. *
  69. * @see WP_Widget::update()
  70. *
  71. * @param array $new_instance Values just sent to be saved.
  72. * @param array $old_instance Previously saved values from database.
  73. *
  74. * @return array Updated safe values to be saved.
  75. */
  76. public function update( $new_instance, $old_instance ) {
  77. $non_hex_regex = '/[^a-f0-9]/';
  78. $instance = array();
  79. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  80. $instance['width'] = (int) $new_instance['width'];
  81. $instance['height'] = (int) $new_instance['height'];
  82. $instance['width'] = ( 0 !== (int) $new_instance['width'] ) ? (int) $new_instance['width'] : 225;
  83. $instance['height'] = ( 0 !== (int) $new_instance['height'] ) ? (int) $new_instance['height'] : 400;
  84. $instance['tweet-limit'] = (int) $new_instance['tweet-limit'];
  85. // If they entered something that might be a full URL, try to parse it out
  86. if ( is_string( $new_instance['widget-id'] ) ) {
  87. if ( preg_match( '#https?://twitter\.com/settings/widgets/(\d+)#s', $new_instance['widget-id'], $matches ) ) {
  88. $new_instance['widget-id'] = $matches[1];
  89. }
  90. }
  91. $instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
  92. $instance['widget-id'] = is_numeric( $instance['widget-id'] ) ? $instance['widget-id'] : '';
  93. foreach ( array( 'link-color', 'border-color' ) as $color ) {
  94. $clean = preg_replace( $non_hex_regex, '', sanitize_text_field( $new_instance[$color] ) );
  95. if ( $clean )
  96. $instance[$color] = '#' . $clean;
  97. }
  98. $instance['theme'] = 'light';
  99. if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) )
  100. $instance['theme'] = $new_instance['theme'];
  101. $instance['chrome'] = array();
  102. if ( isset( $new_instance['chrome'] ) ) {
  103. foreach ( $new_instance['chrome'] as $chrome ) {
  104. if ( in_array( $chrome, array( 'noheader', 'nofooter', 'noborders', 'noscrollbar', 'transparent' ) ) ) {
  105. $instance['chrome'][] = $chrome;
  106. }
  107. }
  108. }
  109. return $instance;
  110. }
  111. /**
  112. * Back-end widget form.
  113. *
  114. * @see WP_Widget::form()
  115. *
  116. * @param array $instance Previously saved values from database.
  117. */
  118. public function form( $instance ) {
  119. $defaults = array(
  120. 'title' => esc_html__( 'Follow me on Twitter', 'jetpack' ),
  121. 'width' => '',
  122. 'height' => '400',
  123. 'widget-id' => '',
  124. 'link-color' => '#f96e5b',
  125. 'border-color' => '#e8e8e8',
  126. 'theme' => 'light',
  127. 'chrome' => array(),
  128. );
  129. $instance = wp_parse_args( (array) $instance, $defaults );
  130. ?>
  131. <p>
  132. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
  133. <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'] ); ?>" />
  134. </p>
  135. <p>
  136. <label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php esc_html_e( 'Width (px):', 'jetpack' ); ?></label>
  137. <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'] ); ?>" />
  138. </p>
  139. <p>
  140. <label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php esc_html_e( 'Height (px):', 'jetpack' ); ?></label>
  141. <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'] ); ?>" />
  142. </p>
  143. <p>
  144. <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"><?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?></label>
  145. <input class="widefat" id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>" name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>" type="text" value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>" />
  146. </p>
  147. <p><small>
  148. <?php
  149. echo wp_kses_post(
  150. sprintf(
  151. __( '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' ),
  152. 'https://twitter.com/settings/widgets/new/user',
  153. 'http://support.wordpress.com/widgets/twitter-timeline-widget/'
  154. )
  155. );
  156. ?>
  157. </small></p>
  158. <p>
  159. <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>
  160. <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'] ); ?>" />
  161. </p>
  162. <p>
  163. <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"><?php esc_html_e( 'Layout Options:', 'jetpack' ); ?></label><br />
  164. <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 />
  165. <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 />
  166. <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 />
  167. <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 />
  168. <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>
  169. </p>
  170. <p>
  171. <label for="<?php echo $this->get_field_id( 'link-color' ); ?>"><?php _e( 'Link Color (hex):', 'jetpack' ); ?></label>
  172. <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'] ); ?>" />
  173. </p>
  174. <p>
  175. <label for="<?php echo $this->get_field_id( 'border-color' ); ?>"><?php _e( 'Border Color (hex):', 'jetpack' ); ?></label>
  176. <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'] ); ?>" />
  177. </p>
  178. <p>
  179. <label for="<?php echo $this->get_field_id( 'theme' ); ?>"><?php _e( 'Timeline Theme:', 'jetpack' ); ?></label>
  180. <select name="<?php echo $this->get_field_name( 'theme' ); ?>" id="<?php echo $this->get_field_id( 'theme' ); ?>" class="widefat">
  181. <option value="light"<?php selected( $instance['theme'], 'light' ); ?>><?php esc_html_e( 'Light', 'jetpack' ); ?></option>
  182. <option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>><?php esc_html_e( 'Dark', 'jetpack' ); ?></option>
  183. </select>
  184. </p>
  185. <?php
  186. }
  187. }