Completed
Push — develop ( 370152...d94e50 )
by David
03:11
created

Wordlift_Geomap_Shortcode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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
		// Add wordlift-ui css and library.
81
		wp_enqueue_style( 'wordlift-ui-css', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
82
83
		$this->enqueue_scripts();
84
85
		wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array(
86
			'ajax_url' => admin_url( 'admin-ajax.php' ),    // Global param
87
			'action'   => 'wl_geomap'            // Global param
88
		) );
89
90
		// Escaping atts.
91
		$esc_id      = esc_attr( $geomap_id );
92
		$esc_width   = esc_attr( $geomap_atts['width'] );
93
		$esc_height  = esc_attr( $geomap_atts['height'] );
94
		$esc_post_id = esc_attr( $post_id );
95
96
		// Return HTML template.
97
		return <<<EOF
98
<div class="wl-geomap"  id="$esc_id" data-post-id="$esc_post_id"
99
	style="width:$esc_width; height:$esc_height; background-color: gray;">
100
</div>
101
EOF;
102
	}
103
104
	/**
105
	 * Customize the CSS when in AMP.
106
	 *
107
	 * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css
108
	 *
109
	 * @since 3.13.0
110
	 *
111
	 * @param object $amp_template The template.
112
	 */
113
	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...
114
115
		// Hide the `wl-geomap` when in AMP.
116
		?>
117
		.wl-geomap { display: none; }
118
		<?php
119
	}
120
121
}
122