Conditions | 19 |
Paths | 77 |
Total Lines | 88 |
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 |
||
40 | function github_gist_shortcode( $atts, $content = '' ) { |
||
41 | |||
42 | if ( empty( $atts[0] ) && empty( $content ) ) { |
||
43 | return '<!-- Missing Gist ID -->'; |
||
44 | } |
||
45 | |||
46 | $id = ( ! empty( $content ) ) ? $content : $atts[0]; |
||
47 | |||
48 | // Parse a URL |
||
49 | if ( ! is_numeric( $id ) ) { |
||
50 | $id = preg_replace( '#https?://gist.github.com/([a-zA-Z0-9]+)#', '$1', $id ); |
||
51 | } |
||
52 | |||
53 | if ( ! $id ) { |
||
54 | return '<!-- Invalid Gist ID -->'; |
||
55 | } |
||
56 | |||
57 | if ( Jetpack_AMP_Support::is_amp_request() ) { |
||
58 | /* |
||
59 | * According to <https://www.ampproject.org/docs/reference/components/amp-gist#height-(required)>: |
||
60 | * |
||
61 | * > Note: You must find the height of the gist by inspecting it with your browser (e.g., Chrome Developer Tools). |
||
62 | * |
||
63 | * However, this does not seem to be the case any longer. The actual height of the content does get set in the |
||
64 | * page after loading. So this is just the initial height. |
||
65 | * See <https://github.com/ampproject/amphtml/pull/17738>. |
||
66 | */ |
||
67 | $height = 240; |
||
68 | |||
69 | $parsed_url = wp_parse_url( $id ); |
||
70 | |||
71 | // Strip Github user name. |
||
72 | $id = preg_replace( '#^.*/(?=[a-z0-9]+)#', '', $parsed_url['path'] ); |
||
73 | |||
74 | $file = null; |
||
75 | if ( ! empty( $parsed_url['query'] ) ) { |
||
76 | $query_args = wp_parse_args( $parsed_url['query'] ); |
||
77 | if ( isset( $query_args['file'] ) ) { |
||
78 | $file = $query_args['file']; |
||
79 | } |
||
80 | } |
||
81 | if ( ! $file && ! empty( $parsed_url['fragment'] ) && preg_match( '#^file-(.+)#', $parsed_url['fragment'], $matches ) ) { |
||
82 | $file = $matches[1]; |
||
83 | |||
84 | // Make best guess of file for fragment that was slugified. |
||
85 | $file = preg_replace( '/-(\w+)/', '.$1', $file ); |
||
86 | } |
||
87 | |||
88 | $amp_tag = sprintf( |
||
89 | '<amp-gist layout="fixed-height" data-gistid="%s" height="%s"', |
||
90 | esc_attr( basename( $id, '.json' ) ), |
||
91 | esc_attr( $height ) |
||
92 | ); |
||
93 | if ( ! empty( $file ) ) { |
||
94 | $amp_tag .= sprintf( ' data-file="%s"', esc_attr( $file ) ); |
||
95 | } |
||
96 | $amp_tag .= '></amp-gist>'; |
||
97 | return $amp_tag; |
||
98 | } |
||
99 | |||
100 | wp_enqueue_script( |
||
101 | 'jetpack-gist-embed', |
||
102 | Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ), |
||
103 | array( 'jquery' ), |
||
104 | false, |
||
105 | true |
||
106 | ); |
||
107 | |||
108 | if ( false !== strpos( $id, '#file-' ) ) { |
||
109 | // URL points to a specific file in the gist |
||
110 | $id = str_replace( '#file-', '.json?file=', $id ); |
||
111 | $id = preg_replace( '/\-(?!.*\-)/', '.', $id ); |
||
112 | } else { |
||
113 | $file = ( ! empty( $atts['file'] ) ) ? '?file=' . urlencode( $atts['file'] ) : ''; |
||
114 | // URL points to the entire gist |
||
115 | $id .= ".json$file"; |
||
116 | } |
||
117 | |||
118 | // inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables |
||
119 | $return = '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="' . esc_attr( $id ) . '"></div>'; |
||
120 | |||
121 | if ( isset( $_POST['type'] ) && 'embed' === $_POST['type'] && |
||
122 | isset( $_POST['action'] ) && 'parse-embed' === $_POST['action'] ) { |
||
123 | return github_gist_simple_embed( $id ); |
||
124 | } |
||
125 | |||
126 | return $return; |
||
127 | } |
||
128 | |||
142 |