Conditions | 12 |
Paths | 28 |
Total Lines | 71 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | static public function jetpack_tweet_shortcode( $atts ) { |
||
35 | $default_atts = array( |
||
36 | 'tweet' => '', |
||
37 | 'align' => 'none', |
||
38 | 'width' => '', |
||
39 | 'lang' => 'en', |
||
40 | 'hide_thread' => 'false', |
||
41 | 'hide_media' => 'false', |
||
42 | ); |
||
43 | |||
44 | $attr = shortcode_atts( $default_atts, $atts ); |
||
45 | |||
46 | self::$provider_args = $attr; |
||
47 | |||
48 | // figure out the tweet id for the requested tweet |
||
49 | // supporting both omitted attributes and tweet="tweet_id" |
||
50 | // and supporting both an id and a URL |
||
51 | if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) { |
||
52 | $attr['tweet'] = $atts[0]; |
||
53 | } |
||
54 | |||
55 | if ( ctype_digit( $attr['tweet'] ) ) { |
||
56 | $transient = "jpt_{$attr['tweet']}"; |
||
57 | if ( false === $cached_url = get_transient( $transient ) ) { |
||
58 | $response = wp_remote_get( "https://twitter.com/statuses/{$attr['tweet']}" ); |
||
59 | if ( 200 !== wp_remote_retrieve_response_code( $response ) || ! isset( $response['http_response'] ) ) { |
||
60 | |||
61 | // Cache for 15 minutes to avoid making the request over and over. |
||
62 | set_transient( $transient, '', 60 * 15 ); |
||
63 | return ''; |
||
64 | } |
||
65 | $http_response = $response['http_response']; |
||
66 | $http_response_object = $http_response->get_response_object(); |
||
67 | if ( empty( $http_response_object->url ) ) { |
||
68 | |||
69 | // Cache for 15 minutes to avoid making the request over and over. |
||
70 | set_transient( $transient, '', 60 * 15 ); |
||
71 | return ''; |
||
72 | } |
||
73 | $cached_url = isset( $http_response_object->url ) && ! empty( $http_response_object->url ) |
||
74 | ? $http_response_object->url |
||
75 | : ''; |
||
76 | set_transient( $transient, $cached_url, DAY_IN_SECONDS ); |
||
77 | } |
||
78 | $attr['tweet'] = $cached_url; |
||
79 | } |
||
80 | |||
81 | preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits ); |
||
82 | if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) { |
||
83 | $id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] ); |
||
84 | } else { |
||
85 | return '<!-- Invalid tweet id -->'; |
||
86 | } |
||
87 | |||
88 | // Add shortcode arguments to provider URL |
||
89 | add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 ); |
||
90 | |||
91 | // Fetch tweet |
||
92 | $output = wp_oembed_get( $id, $atts ); |
||
93 | |||
94 | // Clean up filter |
||
95 | remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 ); |
||
96 | |||
97 | // Add Twitter widgets.js script to the footer. |
||
98 | add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) ); |
||
99 | |||
100 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
101 | do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' ); |
||
102 | |||
103 | return $output; |
||
104 | } |
||
105 | |||
151 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.