Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * Based on Evolution Twitter Timeline (http://wordpress.org/extend/plugins/evolution-twitter-timeline/) |
||
| 5 | * See: https://twitter.com/settings/widgets and https://dev.twitter.com/docs/embedded-timelines for details on Twitter Timelines |
||
| 6 | */ |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Register the widget for use in Appearance -> Widgets |
||
| 10 | */ |
||
| 11 | add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' ); |
||
| 12 | |||
| 13 | function jetpack_twitter_timeline_widget_init() { |
||
| 14 | register_widget( 'Jetpack_Twitter_Timeline_Widget' ); |
||
| 15 | } |
||
| 16 | |||
| 17 | class Jetpack_Twitter_Timeline_Widget extends WP_Widget { |
||
| 18 | /** |
||
| 19 | * Register widget with WordPress. |
||
| 20 | */ |
||
| 21 | View Code Duplication | public function __construct() { |
|
| 22 | parent::__construct( |
||
| 23 | 'twitter_timeline', |
||
| 24 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 25 | apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ), |
||
| 26 | array( |
||
| 27 | 'classname' => 'widget_twitter_timeline', |
||
| 28 | 'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ) |
||
| 29 | ) |
||
| 30 | ); |
||
| 31 | |||
| 32 | if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) { |
||
| 33 | add_action( 'wp_footer', array( $this, 'library' ) ); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Enqueue Twitter's widget library |
||
| 39 | */ |
||
| 40 | public function library() { |
||
| 41 | ?> |
||
| 42 | <script type="text/javascript"> |
||
| 43 | !function(d,s,id){ |
||
| 44 | var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https'; |
||
| 45 | if(!d.getElementById(id)){ |
||
| 46 | js=d.createElement(s); |
||
| 47 | js.id=id;js.src=p+"://platform.twitter.com/widgets.js"; |
||
| 48 | fjs.parentNode.insertBefore(js,fjs); |
||
| 49 | } |
||
| 50 | }(document,"script","twitter-wjs"); |
||
| 51 | </script> |
||
| 52 | <?php |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Front-end display of widget. |
||
| 57 | * |
||
| 58 | * @see WP_Widget::widget() |
||
| 59 | * |
||
| 60 | * @param array $args Widget arguments. |
||
| 61 | * @param array $instance Saved values from database. |
||
| 62 | */ |
||
| 63 | public function widget( $args, $instance ) { |
||
| 64 | $instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 ); |
||
| 65 | |||
| 66 | echo $args['before_widget']; |
||
| 67 | |||
| 68 | if ( $instance['title'] ) { |
||
| 69 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 70 | echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
||
| 71 | } |
||
| 72 | |||
| 73 | $data_attribs = array( 'widget-id', 'theme', 'link-color', 'border-color', 'chrome', 'tweet-limit' ); |
||
| 74 | $attribs = array( 'width', 'height', 'lang' ); |
||
| 75 | |||
| 76 | // Start tag output |
||
| 77 | echo '<a class="twitter-timeline"'; |
||
| 78 | |||
| 79 | foreach ( $data_attribs as $att ) { |
||
| 80 | if ( !empty( $instance[$att] ) ) { |
||
| 81 | if ( 'tweet-limit' == $att && 0 === $instance[$att] ) |
||
|
0 ignored issues
–
show
Unused Code
Bug
introduced
by
Loading history...
|
|||
| 82 | continue; |
||
| 83 | |||
| 84 | if ( is_array( $instance[$att] ) ) |
||
| 85 | echo ' data-' . esc_attr( $att ) . '="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"'; |
||
| 86 | View Code Duplication | else |
|
| 87 | echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[$att] ) . '"'; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | foreach ( $attribs as $att ) { |
||
| 92 | View Code Duplication | if ( !empty( $instance[$att] ) ) |
|
| 93 | echo ' ' . esc_attr( $att ) . '="' . esc_attr( $instance[$att] ) . '"'; |
||
| 94 | } |
||
| 95 | |||
| 96 | echo '>'; |
||
| 97 | |||
| 98 | $timeline_placeholder = __( 'My Tweets', 'jetpack' ); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Filter the Timeline placeholder text. |
||
| 102 | * |
||
| 103 | * @module widgets |
||
| 104 | * |
||
| 105 | * @since 3.4.0 |
||
| 106 | * |
||
| 107 | * @param string $timeline_placeholder Timeline placeholder text. |
||
| 108 | */ |
||
| 109 | $timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder ); |
||
| 110 | |||
| 111 | echo esc_html( $timeline_placeholder ) . '</a>'; |
||
| 112 | |||
| 113 | // End tag output |
||
| 114 | |||
| 115 | echo $args['after_widget']; |
||
| 116 | |||
| 117 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
| 118 | do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter_timeline' ); |
||
| 119 | } |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Sanitize widget form values as they are saved. |
||
| 124 | * |
||
| 125 | * @see WP_Widget::update() |
||
| 126 | * |
||
| 127 | * @param array $new_instance Values just sent to be saved. |
||
| 128 | * @param array $old_instance Previously saved values from database. |
||
| 129 | * |
||
| 130 | * @return array Updated safe values to be saved. |
||
| 131 | */ |
||
| 132 | public function update( $new_instance, $old_instance ) { |
||
| 133 | $hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/'; |
||
| 134 | $instance = array(); |
||
| 135 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
||
| 136 | $instance['width'] = (int) $new_instance['width']; |
||
| 137 | $instance['height'] = (int) $new_instance['height']; |
||
| 138 | $instance['width'] = ( 0 !== (int) $new_instance['width'] ) ? (int) $new_instance['width'] : ''; |
||
| 139 | $instance['height'] = ( 0 !== (int) $new_instance['height'] ) ? (int) $new_instance['height'] : ''; |
||
| 140 | $instance['tweet-limit'] = ( 0 !== (int) $new_instance['tweet-limit'] ) ? (int) $new_instance['tweet-limit'] : null; |
||
| 141 | |||
| 142 | // If they entered something that might be a full URL, try to parse it out |
||
| 143 | if ( is_string( $new_instance['widget-id'] ) ) { |
||
| 144 | if ( preg_match( '#https?://twitter\.com/settings/widgets/(\d+)#s', $new_instance['widget-id'], $matches ) ) { |
||
| 145 | $new_instance['widget-id'] = $matches[1]; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] ); |
||
| 150 | $instance['widget-id'] = is_numeric( $instance['widget-id'] ) ? $instance['widget-id'] : ''; |
||
| 151 | |||
| 152 | foreach ( array( 'link-color', 'border-color' ) as $color ) { |
||
| 153 | $new_color = sanitize_text_field( $new_instance[$color] ); |
||
| 154 | if ( preg_match( $hex_regex, $new_color ) ) { |
||
| 155 | $instance[$color] = $new_color; |
||
| 156 | } |
||
| 157 | |||
| 158 | } |
||
| 159 | |||
| 160 | $instance['theme'] = 'light'; |
||
| 161 | if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) |
||
| 162 | $instance['theme'] = $new_instance['theme']; |
||
| 163 | |||
| 164 | $instance['chrome'] = array(); |
||
| 165 | if ( isset( $new_instance['chrome'] ) ) { |
||
| 166 | foreach ( $new_instance['chrome'] as $chrome ) { |
||
| 167 | if ( in_array( $chrome, array( 'noheader', 'nofooter', 'noborders', 'noscrollbar', 'transparent' ) ) ) { |
||
| 168 | $instance['chrome'][] = $chrome; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | return $instance; |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Back-end widget form. |
||
| 179 | * |
||
| 180 | * @see WP_Widget::form() |
||
| 181 | * |
||
| 182 | * @param array $instance Previously saved values from database. |
||
| 183 | */ |
||
| 184 | public function form( $instance ) { |
||
| 185 | $defaults = array( |
||
| 186 | 'title' => esc_html__( 'Follow me on Twitter', 'jetpack' ), |
||
| 187 | 'width' => '', |
||
| 188 | 'height' => '400', |
||
| 189 | 'widget-id' => '', |
||
| 190 | 'link-color' => '#f96e5b', |
||
| 191 | 'border-color' => '#e8e8e8', |
||
| 192 | 'theme' => 'light', |
||
| 193 | 'chrome' => array(), |
||
| 194 | 'tweet-limit' => null, |
||
| 195 | ); |
||
| 196 | |||
| 197 | $instance = wp_parse_args( (array) $instance, $defaults ); |
||
| 198 | ?> |
||
| 199 | |||
| 200 | <p> |
||
| 201 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
||
| 202 | <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'] ); ?>" /> |
||
| 203 | </p> |
||
| 204 | |||
| 205 | <p> |
||
| 206 | <label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php esc_html_e( 'Width (px):', 'jetpack' ); ?></label> |
||
| 207 | <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'] ); ?>" /> |
||
| 208 | </p> |
||
| 209 | |||
| 210 | <p> |
||
| 211 | <label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php esc_html_e( 'Height (px):', 'jetpack' ); ?></label> |
||
| 212 | <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'] ); ?>" /> |
||
| 213 | </p> |
||
| 214 | |||
| 215 | <p> |
||
| 216 | <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"><?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?></label> |
||
| 217 | <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'] ); ?>" /> |
||
| 218 | </p> |
||
| 219 | |||
| 220 | <p><small> |
||
| 221 | <?php |
||
| 222 | echo wp_kses_post( |
||
| 223 | sprintf( |
||
| 224 | __( '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' ), |
||
| 225 | 'https://twitter.com/settings/widgets/new/user', |
||
| 226 | 'http://support.wordpress.com/widgets/twitter-timeline-widget/' |
||
| 227 | ) |
||
| 228 | ); |
||
| 229 | ?> |
||
| 230 | </small></p> |
||
| 231 | <p> |
||
| 232 | <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> |
||
| 233 | <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'] ); ?>" /> |
||
| 234 | </p> |
||
| 235 | |||
| 236 | <p> |
||
| 237 | <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"><?php esc_html_e( 'Layout Options:', 'jetpack' ); ?></label><br /> |
||
| 238 | <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 /> |
||
| 239 | <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 /> |
||
| 240 | <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 /> |
||
| 241 | <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 /> |
||
| 242 | <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> |
||
| 243 | </p> |
||
| 244 | |||
| 245 | <p> |
||
| 246 | <label for="<?php echo $this->get_field_id( 'link-color' ); ?>"><?php _e( 'Link Color (hex):', 'jetpack' ); ?></label> |
||
| 247 | <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'] ); ?>" /> |
||
| 248 | </p> |
||
| 249 | |||
| 250 | <p> |
||
| 251 | <label for="<?php echo $this->get_field_id( 'border-color' ); ?>"><?php _e( 'Border Color (hex):', 'jetpack' ); ?></label> |
||
| 252 | <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'] ); ?>" /> |
||
| 253 | </p> |
||
| 254 | |||
| 255 | <p> |
||
| 256 | <label for="<?php echo $this->get_field_id( 'theme' ); ?>"><?php _e( 'Timeline Theme:', 'jetpack' ); ?></label> |
||
| 257 | <select name="<?php echo $this->get_field_name( 'theme' ); ?>" id="<?php echo $this->get_field_id( 'theme' ); ?>" class="widefat"> |
||
| 258 | <option value="light"<?php selected( $instance['theme'], 'light' ); ?>><?php esc_html_e( 'Light', 'jetpack' ); ?></option> |
||
| 259 | <option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>><?php esc_html_e( 'Dark', 'jetpack' ); ?></option> |
||
| 260 | </select> |
||
| 261 | </p> |
||
| 262 | <?php |
||
| 263 | } |
||
| 264 | } |
||
| 265 |