-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwp-proud-location.php
More file actions
425 lines (389 loc) · 13.2 KB
/
wp-proud-location.php
File metadata and controls
425 lines (389 loc) · 13.2 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/*
Plugin Name: Proud Location
Plugin URI: http://proudcity.com/
Description: Declares an Location custom post type.
Version: 2025.12.03.1131
Author: ProudCity
Author URI: http://proudcity.com/
License: Affero GPL v3
*/
namespace Proud\Location;
// Load Extendible
// -----------------------
if ( ! class_exists( 'ProudPlugin' ) ) {
require_once( plugin_dir_path(__FILE__) . '../wp-proud-core/proud-plugin.class.php' );
}
class ProudLocation extends \ProudPlugin {
public function __construct() {
parent::__construct( array(
'textdomain' => 'wp-proud-location',
'plugin_path' => __FILE__,
) );
$this->hook( 'init', 'create_location' );
$this->hook( 'rest_api_init', 'location_rest_support' );
$this->hook( 'init', 'create_taxonomy' );
add_filter( 'proud_search_exclude', array( $this, 'searchfilter' ) );
}
/**
* Adds locations to search blacklist
*/
public function searchfilter($posts) {
array_push($posts, 'proud_location');
return $posts;
}
public function create_location() {
$labels = array(
'name' => _x( 'Locations', 'post name', 'wp-location' ),
'singular_name' => _x( 'Location', 'post type singular name', 'wp-location' ),
'menu_name' => _x( 'Locations', 'admin menu', 'wp-location' ),
'name_admin_bar' => _x( 'Location', 'add new on admin bar', 'wp-location' ),
'add_new' => _x( 'Add New', 'location', 'wp-location' ),
'add_new_item' => __( 'Add New Location', 'wp-location' ),
'new_item' => __( 'New Location', 'wp-location' ),
'edit_item' => __( 'Edit Location', 'wp-location' ),
'view_item' => __( 'View Location', 'wp-location' ),
'all_items' => __( 'All Locations', 'wp-location' ),
'search_items' => __( 'Search location', 'wp-location' ),
'parent_item_colon' => __( 'Parent location:', 'wp-location' ),
'not_found' => __( 'No locations found.', 'wp-location' ),
'not_found_in_trash' => __( 'No locations found in Trash.', 'wp-location' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'wp-location' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-location-alt',
'query_var' => true,
'rewrite' => array( 'slug' => 'locations' ),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'show_in_rest' => true,
'rest_base' => 'locations',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'supports' => array( 'title', 'editor', 'thumbnail',)
);
register_post_type( 'proud_location', $args );
}
function create_taxonomy() {
register_taxonomy(
'location-taxonomy',
'proud_location',
array(
'labels' => array(
'name' => 'Location Layers',
'add_new_item' => 'Add New Location Layer',
'new_item_name' => "New Layer"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true
)
);
}
public function location_rest_support() {
register_rest_field( 'proud_location',
'meta',
array(
'get_callback' => array( $this, 'location_rest_metadata' ),
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Alter the REST endpoint.
* Add metadata to the post response
*/
public function location_rest_metadata( $object, $field_name, $request ) {
$Address = new LocationAddress;
$return = $Address->get_options( $object[ 'id' ] );
// Get our terms
$return['terms'] = wp_get_post_terms( $object['id'], 'location-taxonomy', array( "fields" => "all" ) );
// Try to get primary term from SEO
if( class_exists( '\\WPSEO_Primary_Term' ) ) {
$primary = new \WPSEO_Primary_Term('location-taxonomy', $object[ 'id' ]);
$primary_term = $primary->get_primary_term();
}
$term_layer = null;
foreach ( $return['terms'] as $term ) {
// We have a primary term, so use that
if ( $primary_term && $term->term_id === $primary_term ) {
$term_layer = $term;
break;
}
if ( empty( $return['icon'] ) && $term->slug != 'featured' && $term->slug != 'all' ) {
$term_layer = $term;
}
}
// Try to attach taxonomy icon, color
if( isset( $term_layer->term_id ) ) {
$meta = get_term_meta( $term_layer->term_id );
$return['icon'] = !empty( $meta['icon'] ) ? $meta['icon'][0] : '';
$return['color'] = !empty( $meta['color'] ) ? $meta['color'][0] : '';
$return['active_term'] = $term_layer->slug;
}
return $return;
}
} // class
new ProudLocation;
// LocationAddress meta box
if (class_exists('ProudMetaBox')) {
class LocationAddress extends \ProudMetaBox {
public $options = [ // Meta options, key => default
'address' => '',
'address2' => '',
'city' => '',
'state' => '',
'zip' => '',
'custom_latlng' => '',
'lat' => '',
'lng' => '',
'email' => '',
'phone' => '',
'website' => '',
'hours' => '',
];
public function __construct() {
parent::__construct(
'location_address', // key
'Address', // title
'proud_location', // screen
'normal', // position
'high' // priority
);
}
/**
* Called on form creation
* @param $displaying : false if just building form, true if about to display
* Use displaying:true to do any difficult loading that should only occur when
* the form actually will display
*/
public function set_fields( $displaying ) {
// Already set, no loading necessary
if( $displaying ) {
return;
}
$this->fields = [
'address' => [
'#type' => 'text',
'#title' => __pcHelp('Address'),
'#args' => array('autocomplete' => 'false')
],
'address2' => [
'#type' => 'text',
'#title' => __pcHelp('Address 2'),
],
'city' => [
'#type' => 'text',
'#title' => __pcHelp('City'),
],
'state' => [
'#type' => 'text',
'#title' => __pcHelp('State'),
],
'zip' => [
'#type' => 'text',
'#title' => __pcHelp('Zip'),
],
'custom_latlng' => [
'#type' => 'checkbox',
'#title' => __pcHelp('Customize lat/lng'),
'#return_value' => '1',
'#label_above' => false,
'#replace_title' => __pcHelp( 'Enter custom Latitude/Longitude' ),
],
'lat' => [
'#type' => 'text',
'#title' => __pcHelp('Latitude'),
'#states' => [
'visible' => [
'custom_latlng' => [
'operator' => '==',
'value' => ['1'],
'glue' => '||'
],
],
],
],
'lng' => [
'#type' => 'text',
'#title' => __pcHelp('Longitude'),
'#description' => __pcHelp('To automatically geocode the lat/lng from your address fields, leave both the Latitude and Longitude fields blank.'),
'#states' => [
'visible' => [
'custom_latlng' => [
'operator' => '==',
'value' => ['1'],
'glue' => '||'
],
],
],
],
'location_html' => [
'#type' => 'html',
'#html' => '<hr><p><strong>Contact Information</strong></p>',
],
'email' => [
'#type' => 'text',
'#title' => __pcHelp('Email'),
],
'phone' => [
'#type' => 'text',
'#title' => __pcHelp('Phone'),
],
'website' => [
'#type' => 'text',
'#title' => __pcHelp('Website'),
],
'hours' => [
'#type' => 'textarea',
'#title' => __pcHelp('Hours'),
],
];
}
/**
* Prints form
*/
public function settings_content( $post ) {
parent::settings_content( $post );
// Enqueue JS
$path = plugins_url('assets/',__FILE__);
wp_enqueue_script( 'google-places-api', '//maps.googleapis.com/maps/api/js?key='.get_option('google_api_key', true) .'&libraries=places' );
// Autocomplete
wp_register_script( 'google-places-field', $path . 'google-places.js' );
// Get field ids
$options = $this->get_field_ids();
// Set global lat / lng
$options['lat'] = get_option('lat', true);
$options['lng'] = get_option('lng', true);
wp_localize_script( 'google-places-field', 'proud_location', $options );
wp_enqueue_script( 'google-places-field' );
}
/**
* Returns a (string) $address from an (object|array) $location.
*/
public function address_string($location) {
$location = (array) $location;
return $location['address'] .
(!empty($location['address2']) ? ', ' . $location['address'] : '') .
$location['city'] . ', ' . $location['state'] . ' ' . $location['zip'];
}
/**
* Saves form values
*/
public function save_meta( $post_id, $post, $update ) {
// Grab form values from Request
$values = $this->validate_values( $post );
if( !empty( $values ) ) {
if( empty( $values['lat'] ) || empty( $values['lat'] ) ) {
// @todo: use google_api_key here?
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode( $this->address_string( $values ) );
$response = wp_remote_get( $url );
if( is_array($response) ) {
$body = json_decode($response['body']);
if ( !empty($body->results[0]) ) {
$geo = $body->results[0]->geometry->location; // use the content
print_r($geo);
$values['lat'] = $geo->lat;
$values['lng'] = $geo->lng;
}
}
}
$this->save_all( $values, $post_id );
}
}
}
if( is_admin() )
new LocationAddress;
}
// Location desc meta box (empty for body)
if (class_exists('ProudMetaBox')) {
class LocationDescription extends \ProudMetaBox {
public $options = [ // Meta options, key => default
];
public function __construct() {
parent::__construct(
'location_description', // key
'Description', // title
'proud_location', // screen
'normal', // position
'high' // priority
);
}
/**
* Called on form creation
* @param $displaying : false if just building form, true if about to display
* Use displaying:true to do any difficult loading that should only occur when
* the form actually will display
*/
public function set_fields( $displaying ) {
$this->fields = [];
}
}
if( is_admin() )
new LocationDescription;
// Location desc meta box (empty for body)
class LocationLayer extends \ProudTermMetaBox {
public $options = [ // Meta options, key => default
'icon' => '',
'color' => '',
];
public function __construct() {
parent::__construct(
'location-taxonomy', // key
'Settings' // title
);
}
private function colors() {
return [
'' => ' - Select - ',
'#ED9356' => 'Orange',
'#456D9C' => 'Blue',
'#E76C6D' => 'Red',
'#5A97C4' => 'Dark blue',
'#4DC3FF' => 'Baby blue',
'#9BBF6A' => 'Green',
];
}
/**
* Called on form creation
* @param $displaying : false if just building form, true if about to display
* Use displaying:true to do any difficult loading that should only occur when
* the form actually will display
*/
public function set_fields( $displaying ) {
// Already set, no loading necessary
if( $displaying ) {
return;
}
global $proudcore;
$this->fields = [
'icon' => [
'#title' => 'Icon',
'#type' => 'fa-icon',
'#default_value' => '',
'#to_js_settings' => false
],
'color' => [
'#title' => 'Color',
'#type' => 'select',
'#options' => $this->colors(),
'#default_value' => '',
'#to_js_settings' => false
],
'markup' => [
'#type' => 'html',
'#html' => '<style type="text/css">.term-description-wrap { display: none; }</style>',
],
];
}
}
if( is_admin() )
new LocationLayer;
}