| Conditions | 3 |
| Paths | 2 |
| Total Lines | 55 |
| 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 |
||
| 69 | function load_assets( $attr, $content ) { |
||
| 70 | $access_token = Jetpack_Mapbox_Helper::get_access_token(); |
||
| 71 | |||
| 72 | wpcom_load_event( $access_token['source'] ); |
||
| 73 | |||
| 74 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
||
| 75 | // Get the original state. |
||
| 76 | $scripts_queue = wp_scripts()->queue; |
||
| 77 | $scripts_done = wp_scripts()->done; |
||
| 78 | $styles_queue = wp_styles()->queue; |
||
| 79 | $styles_done = wp_styles()->done; |
||
| 80 | |||
| 81 | // Empty out everything. |
||
| 82 | wp_scripts()->queue = array(); |
||
| 83 | wp_scripts()->done = array(); |
||
| 84 | wp_styles()->queue = array(); |
||
| 85 | wp_styles()->done = array(); |
||
| 86 | |||
| 87 | // Get what we need. |
||
| 88 | ob_start(); |
||
| 89 | add_filter( 'jetpack_is_amp_request', '__return_false' ); |
||
| 90 | Jetpack_Gutenberg::load_assets_as_required( 'map' ); |
||
| 91 | wp_scripts()->do_items(); |
||
| 92 | wp_styles()->do_items(); |
||
| 93 | add_filter( 'jetpack_is_amp_request', '__return_true' ); |
||
| 94 | $assets_html = ob_get_clean(); |
||
| 95 | |||
| 96 | // Restore to the original state. |
||
| 97 | wp_scripts()->queue = $scripts_queue; |
||
| 98 | wp_scripts()->done = $scripts_done; |
||
| 99 | wp_styles()->queue = $styles_queue; |
||
| 100 | wp_styles()->done = $styles_done; |
||
| 101 | |||
| 102 | $html = sprintf( |
||
| 103 | '<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>', |
||
| 104 | $assets_html, |
||
| 105 | preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $content, 1 ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | $placeholder = preg_replace( '/(?<=<div\s)/', 'placeholder ', $content ); |
||
| 109 | |||
| 110 | // @todo Is intrinsic size right? Is content_width the right dimensions? |
||
| 111 | return sprintf( |
||
| 112 | '<amp-iframe srcdoc="%s" width="%d" height="%d" layout="intrinsic" allowfullscreen sandbox="allow-scripts">%s</amp-iframe>', |
||
| 113 | htmlspecialchars( $html ), |
||
| 114 | Jetpack::get_content_width(), |
||
| 115 | Jetpack::get_content_width(), |
||
| 116 | $placeholder |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
| 121 | |||
| 122 | return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $content, 1 ); |
||
| 123 | } |
||
| 124 | |||
| 187 |
If you suppress an error, we recommend checking for the error condition explicitly: