twitter-timeline.php 14.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
<?php

/*
 * Based on Evolution Twitter Timeline
 * (http://wordpress.org/extend/plugins/evolution-twitter-timeline/)
 * For details on Twitter Timelines see:
 *  - https://twitter.com/settings/widgets
 *  - https://dev.twitter.com/docs/embedded-timelines
 */

/**
 * Register the widget for use in Appearance -> Widgets
 */
add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' );

function jetpack_twitter_timeline_widget_init() {
	register_widget( 'Jetpack_Twitter_Timeline_Widget' );
}

class Jetpack_Twitter_Timeline_Widget extends WP_Widget {
	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {
		parent::__construct(
			'twitter_timeline',
			/** This filter is documented in modules/widgets/facebook-likebox.php */
			apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),
			array(
				'classname' => 'widget_twitter_timeline',
				'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ),
				'customize_selective_refresh' => true,
			)
		);

		if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		}

		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
	}

	/**
	 * Enqueue scripts.
	 */
	public function enqueue_scripts() {
		wp_enqueue_script( 'jetpack-twitter-timeline' );
	}

	/**
	 * Enqueue Twitter's widget library.
	 *
	 * @deprecated
	 */
	public function library() {
		_deprecated_function( __METHOD__, '4.0.0' );
		wp_print_scripts( array( 'jetpack-twitter-timeline' ) );
	}

	/**
	 * Enqueue script to improve admin UI
	 */
	public function admin_scripts( $hook ) {
		// This is still 'widgets.php' when managing widgets via the Customizer.
		if ( 'widgets.php' === $hook ) {
			wp_enqueue_script( 'twitter-timeline-admin', plugins_url( 'twitter-timeline-admin.js', __FILE__ ) );
		}
	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		$instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );

		echo $args['before_widget'];

		if ( isset( $instance['title'] ) ) {
			/** This filter is documented in core/src/wp-includes/default-widgets.php */
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
		}

		// Start tag output
		// This tag is transformed into the widget markup by Twitter's
		// widgets.js code
		echo '<a class="twitter-timeline"';

		$data_attribs = array(
			'width',
			'height',
			'theme',
			'link-color',
			'border-color',
			'tweet-limit',
			'lang'
		);
		foreach ( $data_attribs as $att ) {
			if ( ! empty( $instance[ $att ] ) && ! is_array( $instance[ $att ] ) ) {
				echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[ $att ] ) . '"';
			}
		}

		if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
			echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
		}

		$type      = ( isset( $instance['type'] ) ? $instance['type'] : '' );
		$widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
		switch ( $type ) {
			case 'profile':
				echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
				break;
			case 'widget-id':
			default:
				echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
				break;
		}

		// End tag output
		echo '>';

		$timeline_placeholder = __( 'My Tweets', 'jetpack' );

		/**
		 * Filter the Timeline placeholder text.
		 *
		 * @module widgets
		 *
		 * @since 3.4.0
		 *
		 * @param string $timeline_placeholder Timeline placeholder text.
		 */
		$timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );

		echo esc_html( $timeline_placeholder ) . '</a>';

		// End tag output

		echo $args['after_widget'];

		/** This action is documented in modules/widgets/social-media-icons.php */
		do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter_timeline' );
	}


	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = array();

		$instance['title'] = sanitize_text_field( $new_instance['title'] );

		$width = (int) $new_instance['width'];
		if ( $width ) {
			// From publish.twitter.com: 220 <= width <= 1200
			$instance['width'] = min( max( $width, 220 ), 1200 );
		} else {
			$instance['width'] = '';
		}

		$height = (int) $new_instance['height'];
		if ( $height ) {
			// From publish.twitter.com: height >= 200
			$instance['height'] = max( $height, 200 );
		} else {
			$instance['height'] = '';
		}

		$tweet_limit = (int) $new_instance['tweet-limit'];
		$instance['tweet-limit'] = ( $tweet_limit ? $tweet_limit : null );

		// If they entered something that might be a full URL, try to parse it out
		if ( is_string( $new_instance['widget-id'] ) ) {
			if ( preg_match(
				'#https?://twitter\.com/settings/widgets/(\d+)#s',
				$new_instance['widget-id'],
				$matches
			) ) {
				$new_instance['widget-id'] = $matches[1];
			}
		}

		$instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );

		$hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/';
		foreach ( array( 'link-color', 'border-color' ) as $color ) {
			$new_color = sanitize_text_field( $new_instance[ $color ] );
			if ( preg_match( $hex_regex, $new_color ) ) {
				$instance[ $color ] = $new_color;
			}

		}

		$instance['type'] = 'widget-id';
		if ( in_array( $new_instance['type'], array( 'widget-id', 'profile' ) ) ) {
			$instance['type'] = $new_instance['type'];
		}

		$instance['theme'] = 'light';
		if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
			$instance['theme'] = $new_instance['theme'];
		}

		$instance['chrome'] = array();
		$chrome_settings = array(
			'noheader',
			'nofooter',
			'noborders',
			'transparent'
		);
		if ( isset( $new_instance['chrome'] ) ) {
			foreach ( $new_instance['chrome'] as $chrome ) {
				if ( in_array( $chrome, $chrome_settings ) ) {
					$instance['chrome'][] = $chrome;
				}
			}
		}

		return $instance;
	}

	/**
 	 * Returns a link to the documentation for a feature of this widget on
 	 * Jetpack or WordPress.com.
 	 */
	public function get_docs_link( $hash = '' ) {
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
			$base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
		} else {
			$base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
		}
		return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
		$defaults = array(
			'title'        => esc_html__( 'Follow me on Twitter', 'jetpack' ),
			'width'        => '',
			'height'       => '400',
			'widget-id'    => '',
			'link-color'   => '#f96e5b',
			'border-color' => '#e8e8e8',
			'theme'        => 'light',
			'chrome'       => array(),
			'tweet-limit'  => null,
		);

		$instance = wp_parse_args( (array) $instance, $defaults );

		if ( empty( $instance['type'] ) ) {
			// Decide the correct widget type.  If this is a pre-existing
			// widget with a numeric widget ID, then the type should be
			// 'widget-id', otherwise it should be 'profile'.
			if ( ! empty( $instance['widget-id'] ) && is_numeric( $instance['widget-id'] ) ) {
				$instance['type'] = 'widget-id';
			} else {
				$instance['type'] = 'profile';
			}
		}
		?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
				<?php esc_html_e( 'Title:', 'jetpack' ); ?>
			</label>
			<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'] ); ?>"
			/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'width' ); ?>">
				<?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
			</label>
			<input
				class="widefat"
				id="<?php echo $this->get_field_id( 'width' ); ?>"
				name="<?php echo $this->get_field_name( 'width' ); ?>"
				type="number" min="220" max="1200"
				value="<?php echo esc_attr( $instance['width'] ); ?>"
			/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'height' ); ?>">
				<?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
			</label>
			<input
				class="widefat"
				id="<?php echo $this->get_field_id( 'height' ); ?>"
				name="<?php echo $this->get_field_name( 'height' ); ?>"
				type="number" min="200"
				value="<?php echo esc_attr( $instance['height'] ); ?>"
			/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
				<?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?>
			</label>
			<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'] ); ?>"
			/>
		</p>

		<p class="jetpack-twitter-timeline-widget-type-container">
			<label for="<?php echo $this->get_field_id( 'type' ); ?>">
				<?php esc_html_e( 'Widget Type:', 'jetpack' ); ?>
				<?php echo $this->get_docs_link( '#widget-type' ); ?>
			</label>
			<select
				name="<?php echo $this->get_field_name( 'type' ); ?>"
				id="<?php echo $this->get_field_id( 'type' ); ?>"
				class="jetpack-twitter-timeline-widget-type widefat"
			>
				<option value="profile"<?php selected( $instance['type'], 'profile' ); ?>>
					<?php esc_html_e( 'Profile', 'jetpack' ); ?>
				</option>
				<option value="widget-id"<?php selected( $instance['type'], 'widget-id' ); ?>>
					<?php esc_html_e( 'Widget ID', 'jetpack' ); ?>
				</option>
			</select>
		</p>

		<p class="jetpack-twitter-timeline-widget-id-container">
			<label
				for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
				data-widget-type="widget-id"
				<?php echo ( 'widget-id' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
			>
				<?php esc_html_e( 'Widget ID:', 'jetpack' ); ?>
				<?php echo $this->get_docs_link( '#widget-id' ); ?>
			</label>
			<label
				for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
				data-widget-type="profile"
				<?php echo ( 'profile' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
			>
				<?php esc_html_e( 'Twitter Username:', 'jetpack' ); ?>
				<?php echo $this->get_docs_link( '#twitter-username' ); ?>
			</label>
			<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'] ); ?>"
			/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
				<?php esc_html_e( 'Layout Options:', 'jetpack' ); ?>
			</label>
			<br />
			<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 />
			<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 />
			<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 />
			<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>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'link-color' ); ?>">
				<?php _e( 'Link Color (hex):', 'jetpack' ); ?>
			</label>
			<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'] ); ?>"
			/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
				<?php _e( 'Border Color (hex):', 'jetpack' ); ?>
			</label>
			<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'] ); ?>"
			/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'theme' ); ?>">
				<?php _e( 'Timeline Theme:', 'jetpack' ); ?>
			</label>
			<select
				name="<?php echo $this->get_field_name( 'theme' ); ?>"
				id="<?php echo $this->get_field_id( 'theme' ); ?>"
				class="widefat"
			>
				<option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
					<?php esc_html_e( 'Light', 'jetpack' ); ?>
				</option>
				<option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
					<?php esc_html_e( 'Dark', 'jetpack' ); ?>
				</option>
			</select>
		</p>
	<?php
	}
}