-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-web-maps.php
More file actions
404 lines (360 loc) · 12.7 KB
/
wp-web-maps.php
File metadata and controls
404 lines (360 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* Plugin Name: WP Web Maps
* Plugin URI: https://wordpress.org/plugins/wp-web-maps/
* Description: A plugin for creating a Leaflet JS map with a shortcode..
* Author: pjhooker
* Author URI: https://www.cityplanner.biz/
* Text Domain: wp-web-maps
* Version: 0.1.3
* License: GPL2
* Leaflet Map is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Leaflet Map is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Leaflet Map.
*/
define('WPWEBMAPS_PLUGIN_VERSION', '0.2.0');
define('WPWEBMAPS_PLUGIN_FILE', __FILE__);
define('WPWEBMAPS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WPWEBMAPS_PLUGIN_URL', plugins_url('/', __FILE__));
/**
* Add actions and filters
*/
//init_hooks
// register shortcode
add_shortcode('webmap', 'wpwebmaps_shortcode');
add_action( 'wp_enqueue_scripts','wpwebmaps_enqueue_and_register');
// 3 wp_loaded >> After WordPress is fully loaded
add_action('wp_head', 'wpwebmaps_inline_start');
/**
* Enqueue and register styles and scripts (called in __construct)
*/
if ( ! function_exists( 'wpwebmaps_enqueue_and_register' ) ) {
function wpwebmaps_enqueue_and_register(){
$wpwebmaps_options = get_option( 'wpwebmaps_option_name' );
$js_url = WPWEBMAPS_PLUGIN_URL.'vendor/leaflet.js';
$css_url = WPWEBMAPS_PLUGIN_URL.'vendor/leaflet.css';
$apiGoogle='https://maps.googleapis.com/maps/api/js?key='.html_entity_decode($wpwebmaps_options['google_api_0']).'';
$jsGMutant_url = WPWEBMAPS_PLUGIN_URL.'vendor/Leaflet.GoogleMutant.js';
wp_register_style('leaflet_stylesheet', $css_url, Array(), null, false);
wp_enqueue_style('leaflet_stylesheet');
wp_register_script('leaflet_js', $js_url, Array(), null, true);
wp_enqueue_script('leaflet_js');
wp_register_script('google_api', $apiGoogle, Array(), null, true);
wp_enqueue_script('google_api');
wp_register_script('leaflet_gmutant_js', $jsGMutant_url, Array(), null, true);
wp_enqueue_script('leaflet_gmutant_js');
}
}
// function that runs when shortcode is called
if ( ! function_exists( 'wpwebmaps_shortcode' ) ) {
function wpwebmaps_shortcode($atts) {
//extract(shortcode_atts(array('wpwebmapsID' => 1,), $atts));
// Things that you want to do.
$custom_html = '<div id="mapid" style="height: 400px;" wpwebmapsID="'.$atts['wpwebmapsid'].'"></div>';
$wpwebmaps_ID = $atts['wpwebmapsid'];
$jscode = get_post_meta( $wpwebmaps_ID, '_wpwebmaps_cf_' . 'custom-js-code', true );
wp_add_inline_script( 'leaflet_gmutant_js',
//wp-web-map ID: '.$wpwebmaps_ID.
'.$jscode.'');
// Output needs to be return
return $custom_html;
}
}
if ( ! function_exists( 'wpwebmaps_inline_start' ) ) {
function wpwebmaps_inline_start(){
$wpwebmaps_options = get_option( 'wpwebmaps_option_name' );
wp_add_inline_script( 'leaflet_gmutant_js',
var mymap = L.map(\'mapid\').setView([51.505, -0.09], 13);
var googleLayer = L.gridLayer.googleMutant({
type: \'roadmap\',
styles: [
{
"featureType": "landscape.man_made",
"elementType": "geometry.fill",
"stylers": [
{
"saturation": -15
},
{
"lightness": -25
},
{
"weight": 3
}
]
},
{
"featureType": "landscape.man_made",
"elementType": "geometry.stroke",
"stylers": [
{
"weight": 2
}
]
},
{
"featureType": "poi",
"stylers": [
{
"visibility": "off"
}
]
}
]
});
googleLayer.addTo(mymap);
');
}
}
/**
* Generated by the WordPress Option Page generator
* at http://jeremyhixon.com/wp-tools/option-page/
*/
class WPWebMaps {
private $wpwebmaps_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'wpwebmaps_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'wpwebmaps_page_init' ) );
}
public function wpwebmaps_add_plugin_page() {
add_menu_page(
'WP Web Maps', // page_title
'WP Web Maps', // menu_title
'manage_options', // capability
'wp-web-maps', // menu_slug
array( $this, 'wpwebmaps_create_admin_page' ), // function
'dashicons-location', // icon_url
81 // position
);
}
public function wpwebmaps_create_admin_page() {
$this->wpwebmaps_options = get_option( 'wpwebmaps_option_name' ); ?>
<div class="wrap">
<h2>WP Web Maps</h2>
<p>In this page you can choose Google API key. More settings, coming soon!</p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'wpwebmaps_option_group' );
do_settings_sections( 'wp-web-maps-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function wpwebmaps_page_init() {
register_setting(
'wpwebmaps_option_group', // option_group
'wpwebmaps_option_name', // option_name
array( $this, 'wpwebmaps_sanitize' ) // sanitize_callback
);
add_settings_section(
'wpwebmaps_setting_section', // id
'Settings', // title
array( $this, 'wpwebmaps_section_info' ), // callback
'wp-web-maps-admin' // page
);
add_settings_field(
'google_api_0', // id
'Google API', // title
array( $this, 'google_api_0_callback' ), // callback
'wp-web-maps-admin', // page
'wpwebmaps_setting_section' // section
);
}
public function wpwebmaps_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['google_api_0'] ) ) {
$sanitary_values['google_api_0'] = esc_textarea( $input['google_api_0'] );
}
return $sanitary_values;
}
public function wpwebmaps_section_info() {
}
public function google_api_0_callback() {
printf(
'<textarea class="large-text" rows="5" name="wpwebmaps_option_name[google_api_0]" id="google_api_0">%s</textarea>
<br>Get your code via Google API Console',
isset( $this->wpwebmaps_options['google_api_0'] ) ? esc_attr( $this->wpwebmaps_options['google_api_0']) : ''
);
}
}
if ( is_admin() )
$wpwebmaps = new WPWebMaps();
/*
* Retrieve this value with:
* $wpwebmaps_options = get_option( 'wpwebmaps_option_name' ); // Array of All Options
* $google_api_0 = $wpwebmaps_options['google_api_0']; // Google API
*/
// Our custom post type function
function wpwebmaps_create_posttype() {
register_post_type( 'webmaps',
// CPT Options
array(
'labels' => array(
'name' => __( 'Web Maps' ),
'singular_name' => __( 'Web Map' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'webmaps'),
'show_in_rest' => true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'wpwebmaps_create_posttype' );
if ( !class_exists('wpwebmaps_CF') ) {
class wpwebmaps_CF {
/**
* @var string $prefix The prefix for storing custom fields in the postmeta table
*/
var $prefix = '_wpwebmaps_cf_';
/**
* @var array $postTypes An array of public custom post types, plus the standard "post" and "page" - add the custom types you want to include here
*/
var $postTypes = array( "webmaps" );
/**
* @var array $customFields Defines the custom fields available
*/
var $customFields = array(
array(
"name" => "custom-js-code",
"title" => "Custom JS code",
"description" => "",
"type" => "textarea",
"scope" => array( "post","webmaps" ),
"capability" => "edit_pages"
),
);
/**
* PHP 4 Compatible Constructor
*/
function wpwebmaps_CF() { $this->__construct(); }
/**
* PHP 5 Constructor
*/
function __construct() {
add_action( 'admin_menu', array( &$this, 'wpwebmaps_createCF' ) );
add_action( 'save_post', array( &$this, 'wpwebmaps_saveCF' ), 1, 2 );
// Comment this line out if you want to keep default custom fields meta box
add_action( 'do_meta_boxes', array( &$this, 'wpwebmaps_removeDefaultCF' ), 10, 3 );
}
/**
* Remove the default Custom Fields meta box
*/
function wpwebmaps_removeDefaultCF( $type, $context, $post ) {
foreach ( array( 'normal', 'advanced', 'side' ) as $context ) {
foreach ( $this->postTypes as $postType ) {
remove_meta_box( 'postcustom', $postType, $context );
}
}
}
/**
* Create the new Custom Fields meta box
*/
function wpwebmaps_createCF() {
if ( function_exists( 'add_meta_box' ) ) {
foreach ( $this->postTypes as $postType ) {
add_meta_box( 'wpwebmaps-custom-fields', 'Custom Map', array(&$this,'wpwebmaps_displayCF') , $postType, 'normal', 'high' );
}
}
}
/**
* Display the new Custom Fields meta box
*/
function wpwebmaps_displayCF() {
global $post;
?>
<div class="form-wrap">
<?php
wp_nonce_field( 'wpwebmaps-custom-fields', 'wpwebmaps-custom-fields_wpnonce', false, true );
//echo "Here5";
//print_r($this->customFields);
//exit;
foreach ( $this->customFields as $customField ) {
// Check scope
$scope = $customField[ 'scope' ];
$output = false;
foreach ( $scope as $scopeItem ) {
switch ( $scopeItem ) {
default: {
if ( $post->post_type == $scopeItem )
$output = true;
break;
}
}
if ( $output ) break;
}
// Check capability
if ( !current_user_can( $customField['capability'], $post->ID ) )
$output = false;
// Output if allowed
if ( $output ) { ?>
<div class="form-field form-required">
<?php
switch ( $customField[ 'type' ] ) {
case "textarea":{
// Plain text field
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b> (For complete instruction view this page <a href="https://www.cityplanner.biz/wp-web-maps/">WP Web Maps</a>)</label>';
echo '<textarea rows="20" name="' . $this->prefix . $customField[ 'name' ] . '" id="' .
$this->prefix . $customField[ 'name' ] . '" >' .
htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '</textarea>';
break;
}
default: {
// Plain text field
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
echo '<input type="text" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" />';
break;
}
}
?>
<?php if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>'; ?>
<?php
}
else{
}
} ?>
</div>
<?php
}
/**
* Save the new Custom Fields values
*/
function wpwebmaps_saveCF( $post_id, $post ) {
if ( !isset( $_POST[ 'wpwebmaps-custom-fields_wpnonce' ] ) || !wp_verify_nonce( $_POST[ 'wpwebmaps-custom-fields_wpnonce' ], 'wpwebmaps-custom-fields' ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if ( ! in_array( $post->post_type, $this->postTypes ) )
return;
foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ $this->prefix . $customField['name'] ] ) ) {
$value = $_POST[ $this->prefix . $customField['name'] ];
// Auto-paragraphs for any WYSIWYG
if ( $customField['type'] == "wysiwyg" ) $value = wpautop( $value );
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ $this->prefix . $customField['name'] ] );
} else {
delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
}
}
}
}
} // End Class
} // End if class exists statement
// Instantiate the class
if ( class_exists('wpwebmaps_CF') ) {
$wpwebmaps_CF_var = new wpwebmaps_CF();
}
?>