|
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' ); |
|
|
|
|
|
|
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 ) { |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
// Hide the `wl-geomap` when in AMP. |
|
119
|
|
|
?> |
|
120
|
|
|
.wl-geomap { display: none; } |
|
121
|
|
|
<?php |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
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.