Completed
Push — develop ( 5c7568...6a9ba1 )
by David
14:52
created

Wordlift_Geomap_Shortcode::render()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 57
rs 8.9381
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
 * Shortcodes: Geomap Shortcode.
4
 *
5
 * `wl_geomap` implementation.
6
 *
7
 * @since      3.5.4
8
 * @package    Wordlift
9
 * @subpackage Wordlift/includes
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Geomap_Shortcode} class.
14
 *
15
 * @since      3.5.4
16
 * @package    Wordlift
17
 * @subpackage Wordlift/includes
18
 */
19
class Wordlift_Geomap_Shortcode extends Wordlift_Shortcode {
20
21
	const SHORTCODE = 'wl_geomap';
22
23
	/**
24
	 * Create a {@link Wordlift_Geomap_Shortcode} instance.
25
	 *
26
	 * @since 3.5.4
27
	 */
28
	public function __construct() {
29
		parent::__construct();
30
31
		// Hook to the `amp_post_template_css` to hide ourselves when in AMP
32
		// rendering.
33
		add_action( 'amp_post_template_css', array(
34
			$this,
35
			'amp_post_template_css',
36
		) );
37
38
	}
39
40
	/**
41
	 * Render the shortcode.
42
	 *
43
	 * @since 3.5.4
44
	 *
45
	 * @param array $atts An array of shortcode attributes as set by the editor.
46
	 *
47
	 * @return string The output html code.
48
	 */
49
	public function render( $atts ) {
50
51
		// Extract attributes and set default values.
52
		$geomap_atts = shortcode_atts( array(
53
			'width'  => '100%',
54
			'height' => '300px',
55
			'global' => false,
56
		), $atts );
57
58
		// Get id of the post
59
		$post_id = get_the_ID();
60
61
		if ( $geomap_atts['global'] || is_null( $post_id ) ) {
62
			// Global geomap
63
			$geomap_id = 'wl_geomap_global';
64
			$post_id   = null;
65
		} else {
66
			// Post-specific geomap
67
			$geomap_id = 'wl_geomap_' . $post_id;
68
		}
69
70
		// Add leaflet css and library.
71
		wp_enqueue_style(
72
			'leaflet',
73
			dirname( plugin_dir_url( __FILE__ ) ) . '/bower_components/leaflet/dist/leaflet.css'
74
		);
75
		wp_enqueue_script(
76
			'leaflet',
77
			dirname( plugin_dir_url( __FILE__ ) ) . '/bower_components/leaflet/dist/leaflet.js'
78
		);
79
80
		// Use the registered style which define an optional dependency to font-awesome.
81
		//
82
		// @see https://github.com/insideout10/wordlift-plugin/issues/699
83
		//		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
		wp_enqueue_style( 'wordlift-ui' );
85
86
		$this->enqueue_scripts();
87
88
		wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array(
89
			'ajax_url' => admin_url( 'admin-ajax.php' ),    // Global param
90
			'action'   => 'wl_geomap'            // Global param
91
		) );
92
93
		// Escaping atts.
94
		$esc_id      = esc_attr( $geomap_id );
95
		$esc_width   = esc_attr( $geomap_atts['width'] );
96
		$esc_height  = esc_attr( $geomap_atts['height'] );
97
		$esc_post_id = esc_attr( $post_id );
98
99
		// Return HTML template.
100
		return <<<EOF
101
<div class="wl-geomap"  id="$esc_id" data-post-id="$esc_post_id"
102
	style="width:$esc_width; height:$esc_height; background-color: gray;">
103
</div>
104
EOF;
105
	}
106
107
	/**
108
	 * Customize the CSS when in AMP.
109
	 *
110
	 * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css
111
	 *
112
	 * @since 3.13.0
113
	 *
114
	 * @param object $amp_template The template.
115
	 */
116
	public function amp_post_template_css( $amp_template ) {
0 ignored issues
show
Unused Code introduced by
The parameter $amp_template is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
118
		// Hide the `wl-geomap` when in AMP.
119
		?>
120
		.wl-geomap { display: none; }
121
		<?php
122
	}
123
124
}
125