Completed
Push — update/shortcode-modal ( 2fa001...0cb818 )
by Jeremy
09:01
created

Jetpack_Twitter_Timeline_Widget::enqueue_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
				'customize_selective_refresh' => true,
30
			)
31
		);
32
33
		if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
34
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
35
		}
36
	}
37
38
	/**
39
	 * Enqueue scripts.
40
	 */
41
	public function enqueue_scripts() {
42
		wp_enqueue_script( 'jetpack-twitter-timeline' );
43
	}
44
45
	/**
46
	 * Enqueue Twitter's widget library.
47
	 *
48
	 * @deprecated
49
	 */
50
	public function library() {
51
		_deprecated_function( __METHOD__, '3.10' );
52
		wp_print_scripts( array( 'jetpack-twitter-timeline' ) );
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 );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
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
The strict comparison === seems to always evaluate to false as the types of 0 (integer) and $instance[$att] (string) can never be identical. Maybe you want to use a loose comparison == instead?
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( 'Maximum 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