Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
30 / 36
50.00% covered (danger)
50.00%
1 / 2
CRAP
n/a
0 / 0
twitter_timeline_shortcode
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
8
twitter_timeline_js
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Twitter Timeline Shortcode.
4 *
5 * Examples:
6 * [twitter-timeline username=jetpack]
7 *
8 * @package automattic/jetpack
9 */
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * Render the Twitter shortcode.
17 *
18 * @param array $atts Shortcode attributes.
19 */
20function twitter_timeline_shortcode( $atts ) {
21    $default_atts = array(
22        'username' => '',
23        'id'       => '',
24        'width'    => '450',
25        'height'   => '282',
26    );
27
28    $atts = shortcode_atts( $default_atts, $atts, 'twitter-timeline' );
29
30    $atts['username'] = preg_replace( '/[^A-Za-z0-9_]+/', '', $atts['username'] );
31
32    if ( empty( $atts['username'] ) && ! is_numeric( $atts['id'] ) ) {
33        return '<!-- ' . __( 'Must specify Twitter Timeline id or username.', 'jetpack' ) . ' -->';
34    }
35
36    $output = '<a class="twitter-timeline"';
37
38    /** This filter is documented in modules/shortcodes/tweet.php */
39    $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
40    if ( ! empty( $partner ) ) {
41        $output .= ' data-partner="' . esc_attr( $partner ) . '"';
42    }
43    if ( is_numeric( $atts['width'] ) ) {
44        $output .= ' data-width="' . esc_attr( $atts['width'] ) . '"';
45    }
46    if ( is_numeric( $atts['height'] ) ) {
47        $output .= ' data-height="' . esc_attr( $atts['height'] ) . '"';
48    }
49    if ( is_numeric( $atts['id'] ) ) {
50        $output .= ' data-widget-id="' . esc_attr( $atts['id'] ) . '"';
51    }
52    if ( ! empty( $atts['username'] ) ) {
53        $output .= ' href="' . esc_url( 'https://twitter.com/' . $atts['username'] ) . '"';
54    }
55
56    $output .= '>';
57
58    $output .= sprintf(
59        /* Translators: placeholder is a Twitter username. */
60        __( 'Tweets by @%s', 'jetpack' ),
61        $atts['username']
62    );
63
64    $output .= '</a>';
65
66    wp_enqueue_script( 'jetpack-twitter-timeline' );
67
68    return $output;
69}
70add_shortcode( 'twitter-timeline', 'twitter_timeline_shortcode' );
71
72/**
73 * Enqueue the js used by the Twitter shortcode.
74 */
75function twitter_timeline_js() {
76    if ( is_customize_preview() ) {
77        wp_enqueue_script( 'jetpack-twitter-timeline' );
78    }
79}
80add_action( 'wp_enqueue_scripts', 'twitter_timeline_js' );