| Conditions | 3 |
| Paths | 3 |
| Total Lines | 63 |
| Code Lines | 39 |
| 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 |
||
| 18 | public function render( $atts ) { |
||
| 19 | |||
| 20 | //extract attributes and set default values |
||
| 21 | $chord_atts = shortcode_atts( array( |
||
| 22 | 'width' => '100%', |
||
| 23 | 'height' => '500px', |
||
| 24 | 'main_color' => '000', |
||
| 25 | 'depth' => 2, |
||
| 26 | 'global' => FALSE |
||
| 27 | ), $atts ); |
||
| 28 | |||
| 29 | if ( $chord_atts['global'] ) { |
||
| 30 | $post_id = wl_shortcode_chord_most_referenced_entity_id(); |
||
| 31 | if ( $post_id == NULL ) { |
||
| 32 | return "WordLift Chord: no entities found."; |
||
| 33 | } |
||
| 34 | $widget_id = 'wl_chord_global'; |
||
| 35 | $chord_atts['height'] = '200px'; |
||
| 36 | } else { |
||
| 37 | $post_id = get_the_ID(); |
||
| 38 | $widget_id = 'wl_chord_' . $post_id; |
||
| 39 | } |
||
| 40 | |||
| 41 | // Adding css |
||
| 42 | wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
||
| 43 | |||
| 44 | // Adding javascript code |
||
| 45 | wp_enqueue_script( 'd3', dirname( plugin_dir_url( __FILE__ ) ) . '/bower_components/d3/d3.min.js' ); |
||
| 46 | |||
| 47 | $this->enqueue_scripts(); |
||
| 48 | |||
| 49 | wp_localize_script( 'wordlift-ui', 'wl_chord_params', array( |
||
| 50 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
| 51 | 'action' => 'wl_chord' |
||
| 52 | ) |
||
| 53 | ); |
||
| 54 | |||
| 55 | // Escaping atts. |
||
| 56 | $esc_class = esc_attr( 'wl-chord' ); |
||
| 57 | $esc_id = esc_attr( $widget_id ); |
||
| 58 | $esc_width = esc_attr( $chord_atts['width'] ); |
||
| 59 | $esc_height = esc_attr( $chord_atts['height'] ); |
||
| 60 | |||
| 61 | $esc_post_id = esc_attr( $post_id ); |
||
| 62 | $esc_depth = esc_attr( $chord_atts['depth'] ); |
||
| 63 | $esc_main_color = esc_attr( $chord_atts['main_color'] ); |
||
| 64 | |||
| 65 | // Building template. |
||
| 66 | // TODO: in the HTML code there are static CSS rules. Move them to the CSS file. |
||
| 67 | return <<<EOF |
||
| 68 | <div class="$esc_class" |
||
| 69 | id="$esc_id" |
||
| 70 | data-post-id="$esc_post_id" |
||
| 71 | data-depth="$esc_depth" |
||
| 72 | data-main-color="$esc_main_color" |
||
| 73 | style="width:$esc_width; |
||
| 74 | height:$esc_height; |
||
| 75 | background-color:white; |
||
| 76 | margin-top:10px; |
||
| 77 | margin-bottom:10px"> |
||
| 78 | </div> |
||
| 79 | EOF; |
||
| 80 | } |
||
| 81 | |||
| 82 | } |