| Conditions | 3 |
| Paths | 2 |
| Total Lines | 60 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 21 | public function render( $atts ) { |
||
| 22 | |||
| 23 | // Extract attributes and set default values. |
||
| 24 | $geomap_atts = shortcode_atts( array( |
||
| 25 | 'width' => '100%', |
||
| 26 | 'height' => '300px', |
||
| 27 | 'global' => FALSE |
||
| 28 | ), $atts ); |
||
| 29 | |||
| 30 | // Get id of the post |
||
| 31 | $post_id = get_the_ID(); |
||
| 32 | |||
| 33 | if ( $geomap_atts['global'] || is_null( $post_id ) ) { |
||
| 34 | // Global geomap |
||
| 35 | $geomap_id = 'wl_geomap_global'; |
||
| 36 | $post_id = NULL; |
||
| 37 | } else { |
||
| 38 | // Post-specific geomap |
||
| 39 | $geomap_id = 'wl_geomap_' . $post_id; |
||
| 40 | } |
||
| 41 | |||
| 42 | // Add leaflet css and library. |
||
| 43 | wp_enqueue_style( |
||
| 44 | 'leaflet', |
||
| 45 | dirname( plugin_dir_url( __FILE__ ) ) . '/bower_components/leaflet/dist/leaflet.css' |
||
| 46 | ); |
||
| 47 | wp_enqueue_script( |
||
| 48 | 'leaflet', |
||
| 49 | dirname( plugin_dir_url( __FILE__ ) ) . '/bower_components/leaflet/dist/leaflet.js' |
||
| 50 | ); |
||
| 51 | |||
| 52 | // Add wordlift-ui css and library. |
||
| 53 | wp_enqueue_style( 'wordlift-ui-css', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
||
| 54 | |||
| 55 | $this->enqueue_scripts(); |
||
| 56 | |||
| 57 | wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array( |
||
| 58 | 'ajax_url' => admin_url( 'admin-ajax.php' ), // Global param |
||
| 59 | 'action' => 'wl_geomap' // Global param |
||
| 60 | ) ); |
||
| 61 | |||
| 62 | // Escaping atts. |
||
| 63 | $esc_class = esc_attr( 'wl-geomap' ); |
||
| 64 | $esc_id = esc_attr( $geomap_id ); |
||
| 65 | $esc_width = esc_attr( $geomap_atts['width'] ); |
||
| 66 | $esc_height = esc_attr( $geomap_atts['height'] ); |
||
| 67 | $esc_post_id = esc_attr( $post_id ); |
||
| 68 | |||
| 69 | // Return HTML template. |
||
| 70 | return <<<EOF |
||
| 71 | <div class="$esc_class" |
||
| 72 | id="$esc_id" |
||
| 73 | data-post-id="$esc_post_id" |
||
| 74 | style="width:$esc_width; |
||
| 75 | height:$esc_height; |
||
| 76 | background-color:gray |
||
| 77 | "> |
||
| 78 | </div> |
||
| 79 | EOF; |
||
| 80 | } |
||
| 81 | |||
| 83 |