Completed
Push — master ( 7f75f8...2de713 )
by David
03:20
created

Wordlift_Chord_Shortcode::render()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 71
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 38
nc 4
nop 1
dl 0
loc 71
rs 8.7238
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * The `wl_chord` shortcode.
5
 *
6
 * @since 3.5.4
7
 */
8
class Wordlift_Chord_Shortcode extends Wordlift_Shortcode {
9
10
	/**
11
	 * {@inheritdoc}
12
	 */
13
	const SHORTCODE = 'wl_chord';
14
15
	/**
16
	 * {@inheritdoc}
17
	 */
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
31
			if ( null === $post_id = wl_shortcode_chord_most_referenced_entity_id() ) {
32
				return "WordLift Chord: no entities found.";
33
			}
34
35
			$widget_id = 'wl_chord_global';
36
37
			// Use the provided height if any, otherwise use a default of 200px.
38
			//
39
			// See https://github.com/insideout10/wordlift-plugin/issues/443
40
			$chord_atts['height'] = isset( $chord_atts['height'] ) ? $chord_atts['height'] : '200px';
41
42
		} else {
43
44
			$post_id   = get_the_ID();
45
			$widget_id = 'wl_chord_' . $post_id;
46
47
		}
48
49
		// Adding css
50
		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
51
52
		// Adding javascript code
53
		wp_enqueue_script( 'd3', dirname( plugin_dir_url( __FILE__ ) ) . '/bower_components/d3/d3.min.js' );
54
55
		$this->enqueue_scripts();
56
57
		wp_localize_script( 'wordlift-ui', 'wl_chord_params', array(
58
				'ajax_url' => admin_url( 'admin-ajax.php' ),
59
				'action'   => 'wl_chord',
60
			)
61
		);
62
63
		// Escaping atts.
64
		$esc_class  = esc_attr( 'wl-chord' );
65
		$esc_id     = esc_attr( $widget_id );
66
		$esc_width  = esc_attr( $chord_atts['width'] );
67
		$esc_height = esc_attr( $chord_atts['height'] );
68
69
		$esc_post_id    = esc_attr( $post_id );
70
		$esc_depth      = esc_attr( $chord_atts['depth'] );
71
		$esc_main_color = esc_attr( $chord_atts['main_color'] );
72
73
		// Building template.
74
		// TODO: in the HTML code there are static CSS rules. Move them to the CSS file.
75
		return <<<EOF
76
<div class="$esc_class" 
77
	id="$esc_id"
78
	data-post-id="$esc_post_id"
79
    data-depth="$esc_depth"
80
    data-main-color="$esc_main_color"
81
	style="width:$esc_width;
82
        height:$esc_height;
83
        background-color:white;
84
        margin-top:10px;
85
        margin-bottom:10px">
86
</div>
87
EOF;
88
	}
89
90
}