Completed
Push — master ( 9f3eac...f0c1b5 )
by David
02:49 queued 13s
created

Wordlift_Geomap_Shortcode   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A render() 0 50 3
A amp_post_template_css() 0 7 1
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( $this, 'amp_post_template_css', ) );
34
35
	}
36
37
	/**
38
	 * Render the shortcode.
39
	 *
40
	 * @param array $atts An array of shortcode attributes as set by the editor.
41
	 *
42
	 * @return string The output html code.
43
	 * @since 3.5.4
44
	 *
45
	 */
46
	public function render( $atts ) {
47
48
		// Extract attributes and set default values.
49
		$geomap_atts = shortcode_atts( array(
50
			'width'  => '100%',
51
			'height' => '300px',
52
			'global' => false,
53
		), $atts );
54
55
		// Get id of the post
56
		$post_id = get_the_ID();
57
58
		if ( $geomap_atts['global'] || is_null( $post_id ) ) {
59
			// Global geomap
60
			$geomap_id = 'wl_geomap_global';
61
			$post_id   = null;
62
		} else {
63
			// Post-specific geomap
64
			$geomap_id = 'wl_geomap_' . $post_id;
65
		}
66
67
		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
68
		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
69
70
		// Use the registered style which define an optional dependency to font-awesome.
71
		//
72
		// @see https://github.com/insideout10/wordlift-plugin/issues/699
73
		//		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
74
		wp_enqueue_style( 'wordlift-ui' );
75
76
		$this->enqueue_scripts();
77
78
		wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array(
79
			'ajax_url' => admin_url( 'admin-ajax.php' ),    // Global param
80
			'action'   => 'wl_geomap'            // Global param
81
		) );
82
83
		// Escaping atts.
84
		$esc_id      = esc_attr( $geomap_id );
85
		$esc_width   = esc_attr( $geomap_atts['width'] );
86
		$esc_height  = esc_attr( $geomap_atts['height'] );
87
		$esc_post_id = esc_attr( $post_id );
88
89
		// Return HTML template.
90
		return <<<EOF
91
<div class="wl-geomap"  id="$esc_id" data-post-id="$esc_post_id"
92
	style="width:$esc_width; height:$esc_height; background-color: gray;">
93
</div>
94
EOF;
95
	}
96
97
	/**
98
	 * Customize the CSS when in AMP.
99
	 *
100
	 * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css
101
	 *
102
	 * @param object $amp_template The template.
103
	 *
104
	 * @since 3.13.0
105
	 *
106
	 */
107
	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...
108
109
		// Hide the `wl-geomap` when in AMP.
110
		?>
111
        .wl-geomap { display: none; }
112
		<?php
113
	}
114
115
}
116