Conditions | 19 |
Paths | 405 |
Total Lines | 141 |
Code Lines | 52 |
Lines | 14 |
Ratio | 9.93 % |
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 |
||
194 | public static function get_by_path( $domain = '', $path = '', $segments = null ) { |
||
195 | global $wpdb; |
||
196 | |||
197 | $domains = array( $domain ); |
||
198 | $pieces = explode( '.', $domain ); |
||
199 | |||
200 | /* |
||
201 | * It's possible one domain to search is 'com', but it might as well |
||
202 | * be 'localhost' or some other locally mapped domain. |
||
203 | */ |
||
204 | while ( array_shift( $pieces ) ) { |
||
205 | if ( ! empty( $pieces ) ) { |
||
206 | $domains[] = implode( '.', $pieces ); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /* |
||
211 | * If we've gotten to this function during normal execution, there is |
||
212 | * more than one network installed. At this point, who knows how many |
||
213 | * we have. Attempt to optimize for the situation where networks are |
||
214 | * only domains, thus meaning paths never need to be considered. |
||
215 | * |
||
216 | * This is a very basic optimization; anything further could have |
||
217 | * drawbacks depending on the setup, so this is best done per-install. |
||
218 | */ |
||
219 | $using_paths = true; |
||
220 | View Code Duplication | if ( wp_using_ext_object_cache() ) { |
|
221 | $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' ); |
||
222 | if ( false === $using_paths ) { |
||
223 | $using_paths = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} WHERE path <> '/' LIMIT 1" ); |
||
224 | wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' ); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | $paths = array(); |
||
229 | if ( $using_paths ) { |
||
230 | $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) ); |
||
231 | |||
232 | /** |
||
233 | * Filter the number of path segments to consider when searching for a site. |
||
234 | * |
||
235 | * @since 3.9.0 |
||
236 | * |
||
237 | * @param int|null $segments The number of path segments to consider. WordPress by default looks at |
||
238 | * one path segment. The function default of null only makes sense when you |
||
239 | * know the requested path should match a network. |
||
240 | * @param string $domain The requested domain. |
||
241 | * @param string $path The requested path, in full. |
||
242 | */ |
||
243 | $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path ); |
||
244 | |||
245 | View Code Duplication | if ( ( null !== $segments ) && count( $path_segments ) > $segments ) { |
|
246 | $path_segments = array_slice( $path_segments, 0, $segments ); |
||
247 | } |
||
248 | |||
249 | View Code Duplication | while ( count( $path_segments ) ) { |
|
250 | $paths[] = '/' . implode( '/', $path_segments ) . '/'; |
||
251 | array_pop( $path_segments ); |
||
252 | } |
||
253 | |||
254 | $paths[] = '/'; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Determine a network by its domain and path. |
||
259 | * |
||
260 | * This allows one to short-circuit the default logic, perhaps by |
||
261 | * replacing it with a routine that is more optimal for your setup. |
||
262 | * |
||
263 | * Return null to avoid the short-circuit. Return false if no network |
||
264 | * can be found at the requested domain and path. Otherwise, return |
||
265 | * an object from wp_get_network(). |
||
266 | * |
||
267 | * @since 3.9.0 |
||
268 | * |
||
269 | * @param null|bool|object $network Network value to return by path. |
||
270 | * @param string $domain The requested domain. |
||
271 | * @param string $path The requested path, in full. |
||
272 | * @param int|null $segments The suggested number of paths to consult. |
||
273 | * Default null, meaning the entire path was to be consulted. |
||
274 | * @param array $paths The paths to search for, based on $path and $segments. |
||
275 | */ |
||
276 | $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths ); |
||
277 | if ( null !== $pre ) { |
||
278 | return $pre; |
||
279 | } |
||
280 | |||
281 | // @todo Consider additional optimization routes, perhaps as an opt-in for plugins. |
||
282 | // We already have paths covered. What about how far domains should be drilled down (including www)? |
||
283 | |||
284 | $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'"; |
||
285 | |||
286 | if ( ! $using_paths ) { |
||
287 | $network = $wpdb->get_row( " |
||
288 | SELECT * FROM {$wpdb->site} |
||
289 | WHERE domain IN ({$search_domains}) |
||
290 | ORDER BY CHAR_LENGTH(domain) |
||
291 | DESC LIMIT 1 |
||
292 | " ); |
||
293 | |||
294 | if ( ! empty( $network ) && ! is_wp_error( $network ) ) { |
||
295 | return new WP_Network( $network ); |
||
296 | } |
||
297 | |||
298 | return false; |
||
299 | |||
300 | } else { |
||
301 | $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'"; |
||
302 | $networks = $wpdb->get_results( " |
||
303 | SELECT * FROM {$wpdb->site} |
||
304 | WHERE domain IN ({$search_domains}) |
||
305 | AND path IN ({$search_paths}) |
||
306 | ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC |
||
307 | " ); |
||
308 | } |
||
309 | |||
310 | /* |
||
311 | * Domains are sorted by length of domain, then by length of path. |
||
312 | * The domain must match for the path to be considered. Otherwise, |
||
313 | * a network with the path of / will suffice. |
||
314 | */ |
||
315 | $found = false; |
||
316 | foreach ( $networks as $network ) { |
||
317 | if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) { |
||
318 | if ( in_array( $network->path, $paths, true ) ) { |
||
319 | $found = true; |
||
320 | break; |
||
321 | } |
||
322 | } |
||
323 | if ( $network->path === '/' ) { |
||
324 | $found = true; |
||
325 | break; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | if ( true === $found ) { |
||
330 | return new WP_Network( $network ); |
||
331 | } |
||
332 | |||
333 | return false; |
||
334 | } |
||
335 | } |
||
336 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.