Completed
Push — master ( 957be0...c9782b )
by David
08:25
created

Wordlift_Geomap_Shortcode   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 75
rs 10
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A render() 0 60 3
1
<?php
2
3
/**
4
 * The `wl_geomap` implementation.
5
 *
6
 * @since 3.5.4
7
 */
8
class Wordlift_Geomap_Shortcode extends Wordlift_Shortcode {
9
10
	const SHORTCODE = 'wl_geomap';
11
12
	/**
13
	 * Render the shortcode.
14
	 *
15
	 * @since 3.5.4
16
	 *
17
	 * @param array $atts An array of shortcode attributes as set by the editor.
18
	 *
19
	 * @return string The output html code.
20
	 */
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
82
}
83