Conditions | 24 |
Paths | 10 |
Total Lines | 88 |
Code Lines | 49 |
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 |
||
84 | function wpcom_shortcodereverse_getty( $content ) { |
||
85 | if ( ! is_string( $content ) || false === stripos( $content, '.gettyimages.com/' ) ) { |
||
86 | return $content; |
||
87 | } |
||
88 | |||
89 | $regexp = '!<iframe\s+src=[\'"](https?:)?//embed\.gettyimages\.com/embed(/|/?\?assets=)([a-z0-9_-]+(,[a-z0-9_-]+)*)[^\'"]*?[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i'; |
||
90 | $regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
||
91 | |||
92 | |||
93 | // Markup pattern for 2017 embed syntax with significant differences from |
||
94 | // the prior pattern: |
||
95 | $regexp_2017 = '!<a.+?class=\'gie-(single|slideshow)\'.+?gie\.widgets\.load\({([^}]+)}\).+?embed-cdn\.gettyimages\.com/widgets\.js.+?</script>!'; |
||
96 | $regexp_2017_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp_2017, ENT_NOQUOTES ) ); |
||
97 | |||
98 | foreach ( array( 'regexp_2017', 'regexp_2017_ent', 'regexp', 'regexp_ent' ) as $reg ) { |
||
99 | if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) { |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | foreach ( $matches as $match ) { |
||
104 | if ( 'regexp_2017' === $reg || 'regexp_2017_ent' === $reg ) { |
||
105 | // Extract individual keys from the matched JavaScript object |
||
106 | $params = $match[2]; |
||
107 | if ( ! preg_match_all( '!(?P<key>\w+)\s*:\s*([\'"](?P<value>[^\'"]*?)(px)?[\'"])!', $params, $key_matches, PREG_SET_ORDER ) ) { |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | foreach ( $key_matches as $key_match ) { |
||
112 | switch ( $key_match['key'] ) { |
||
113 | case 'items': $ids = $key_match['value']; break; |
||
114 | case 'w': $width = (int) $key_match['value']; break; |
||
115 | case 'h': $height = (int) $key_match['value']; break; |
||
116 | case 'tld': $tld = $key_match['value']; break; |
||
117 | } |
||
118 | } |
||
119 | } else { |
||
120 | $params = $match[5]; |
||
121 | if ( 'regexp_ent' === $reg ) { |
||
122 | $params = html_entity_decode( $params ); |
||
123 | } |
||
124 | $params = wp_kses_hair( $params, array( 'http' ) ); |
||
125 | |||
126 | $ids = esc_html( $match[3] ); |
||
127 | $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; |
||
128 | $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; |
||
129 | } |
||
130 | |||
131 | if ( empty( $ids ) ) { |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | $shortcode = '[getty src="' . esc_attr( $ids ) . '"'; |
||
136 | if ( ! empty( $width ) ) { |
||
137 | $shortcode .= ' width="' . esc_attr( $width ) . '"'; |
||
138 | } |
||
139 | if ( ! empty( $height ) ) { |
||
140 | $shortcode .= ' height="' . esc_attr( $height ) . '"'; |
||
141 | } |
||
142 | // While it does not appear to have any practical impact, Getty has |
||
143 | // requested that we include TLD in the embed request |
||
144 | if ( ! empty( $tld ) ) { |
||
145 | $shortcode .= ' tld="' . esc_attr( $tld ). '"'; |
||
146 | } |
||
147 | $shortcode .= ']'; |
||
148 | |||
149 | $content = str_replace( $match[0], $shortcode, $content ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // strip out enclosing div and any other markup |
||
154 | $regexp = '%<div class="getty\s[^>]*+>.*?<div[^>]*+>(\[getty[^\]]*+\])\s*</div>.*?</div>%is'; |
||
155 | $regexp_ent = str_replace( array( '&#0*58;', '[^>]' ), array( '&#0*58;|�*58;', '[^&]' ), htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
||
156 | |||
157 | foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) { |
||
158 | if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) { |
||
159 | continue; |
||
160 | } |
||
161 | |||
162 | foreach ( $matches as $match ) { |
||
163 | $content = str_replace( $match[0], $match[1], $content ); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
168 | do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'getty' ); |
||
169 | |||
170 | return $content; |
||
171 | } |
||
172 | |||
208 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.