-
Notifications
You must be signed in to change notification settings - Fork 28
/
functions.php
326 lines (247 loc) · 8.97 KB
/
functions.php
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
<?php
/**
* Author: Ole Fredrik Lie
* URL: http://olefredrik.com
*
* FoundationPress functions and definitions
*
* Set up the theme and provides some helper functions, which are used in the
* theme as custom template tags. Others are attached to action and filter
* hooks in WordPress to change core functionality.
*
* @link https://codex.wordpress.org/Theme_Development
* @package FoundationPress
* @since FoundationPress 1.0.0
*/
function search_breweries_by_geo( $lat, $lng ){
global $wpdb;
$earth_radius = 3959;
$distance = 15;
$results = $wpdb->prepare("
SELECT DISTINCT
p.ID,
p.post_title,
name.meta_value as name,
street.meta_value as street,
latitude.meta_value as latitude,
longitude.meta_value as longitude,
city.meta_value as city,
brewery_type.meta_value as brewery_type,
( %d * acos(
cos( radians( %s ) )
* cos( radians( latitude.meta_value ) )
* cos( radians( longitude.meta_value ) - radians( %s ) )
+ sin( radians( %s ) )
* sin( radians( latitude.meta_value ) )
) )
AS distance
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta latitude ON p.ID = latitude.post_id
INNER JOIN $wpdb->postmeta longitude ON p.ID = longitude.post_id
INNER JOIN $wpdb->postmeta name ON p.ID = name.post_id
INNER JOIN $wpdb->postmeta street ON p.ID = street.post_id
INNER JOIN $wpdb->postmeta city ON p.ID = city.post_id
INNER JOIN $wpdb->postmeta brewery_type ON p.ID = brewery_type.post_id
WHERE 1 = 1
AND p.post_type = 'brewery'
AND p.post_status = 'publish'
AND latitude.meta_key = 'latitude'
AND longitude.meta_key = 'longitude'
AND name.meta_key = 'name'
AND street.meta_key = 'street'
AND city.meta_key = 'city'
AND brewery_type.meta_key = 'brewery_type'
HAVING distance < %s
ORDER BY distance ASC",
$earth_radius,
$lat,
$lng,
$lat,
$distance
);
$nearbyLocations = $wpdb->get_results($results);
return $nearbyLocations;
}
add_action('wp_ajax_nopriv_autocomplete_breweries', 'autocomplete_breweries');
add_action('wp_ajax_autocomplete_breweries', 'autocomplete_breweries');
function autocomplete_breweries(){
global $wpdb;
$text_so_far = $wpdb->esc_like( $_GET['text_so_far'] ) . '%';
$results = $wpdb->get_results( $wpdb->prepare(
"SELECT
city, state_id, lat, lng
FROM
us_cities
WHERE
city
LIKE
%s
AND
population IS NOT NULL
ORDER BY
population
DESC
LIMIT
5
",
$text_so_far
) );
ob_start();
require get_stylesheet_directory() . '/template-parts/autocomplete.php';
wp_send_json_success( [
'html' => ob_get_clean(),
'results' => $results,
] );
}
add_action('wp_ajax_nopriv_search_breweries', 'search_breweries');
add_action('wp_ajax_search_breweries', 'search_breweries');
function search_breweries(){
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$breweries = search_breweries_by_geo( $lat, $lng );
ob_start();
require get_stylesheet_directory() . '/template-parts/cards-brewery.php';
wp_send_json_success( [ 'html' => ob_get_clean(), 'results' => $breweries ] );
}
function register_brewery_cpt() {
register_post_type( 'brewery', array(
'label' => 'Breweries',
'public' => true,
'capability_type' => 'post',
));
}
add_action( 'init', 'register_brewery_cpt' );
function clear_breweries_from_db() {
global $wpdb;
$wpdb->query("DELETE FROM wp_posts WHERE post_type='brewery'");
$wpdb->query("DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts);");
$wpdb->query("DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT id FROM wp_posts)");
}
// clear_breweries_from_db();
// if ( ! wp_next_scheduled( 'update_brewery_list' ) ) {
// wp_schedule_event( time(), 'weekly', 'update_brewery_list' );
// }
add_action( 'update_brewery_list', 'get_breweries_from_api' );
add_action( 'wp_ajax_nopriv_get_breweries_from_api', 'get_breweries_from_api' );
add_action( 'wp_ajax_get_breweries_from_api', 'get_breweries_from_api' );
function get_breweries_from_api() {
$current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
$breweries = [];
// Should return an array of objects
$results = wp_remote_retrieve_body( wp_remote_get('https://api.openbrewerydb.org/breweries/?page=' . $current_page . '&per_page=50') );
// turn it into a PHP array from JSON string
$results = json_decode( $results );
// Either the API is down or something else spooky happened. Just be done.
if( ! is_array( $results ) || empty( $results ) ){
return false;
}
$breweries[] = $results;
foreach( $breweries[0] as $brewery ){
$brewery_slug = slugify( $brewery->name . '-' . $brewery->id );
$existing_brewery = get_page_by_path( $brewery_slug, 'OBJECT', 'brewery' );
if( $existing_brewery === null ){
$inserted_brewery = wp_insert_post( [
'post_name' => $brewery_slug,
'post_title' => $brewery_slug,
'post_type' => 'brewery',
'post_status' => 'publish'
] );
if( is_wp_error( $inserted_brewery ) || $inserted_brewery === 0 ) {
// die('Could not insert brewery: ' . $brewery_slug);
// error_log( 'Could not insert brewery: ' . $brewery_slug );
continue;
}
// add meta fields
$fillable = [
'field_5cbcd9b769ec4' => 'name',
'field_5cbcd9cbead4e' => 'brewery_type',
'field_5cbcd9d720328' => 'street',
'field_5cbcd9dc20329' => 'city',
'field_5cbcd9e02032a' => 'state',
'field_5cbcd9e62032b' => 'postal_code',
'field_5cbcd9f22032c' => 'country',
'field_5cbcda0ab4375' => 'longitude',
'field_5cbcda11b4376' => 'latitude',
'field_5cbcda18b4377' => 'phone',
'field_5cbcda2f35476' => 'website',
'field_5cbcda3535477' => 'updated_at',
];
foreach( $fillable as $key => $name ) {
update_field( $key, $brewery->$name, $inserted_brewery );
}
} else {
$existing_brewery_id = $existing_brewery->ID;
$exisiting_brewerey_timestamp = get_field('updated_at', $existing_brewery_id);
if( $brewery->updated_at >= $exisiting_brewerey_timestamp ){
$fillable = [
'field_5cbcd9b769ec4' => 'name',
'field_5cbcd9cbead4e' => 'brewery_type',
'field_5cbcd9d720328' => 'street',
'field_5cbcd9dc20329' => 'city',
'field_5cbcd9e02032a' => 'state',
'field_5cbcd9e62032b' => 'postal_code',
'field_5cbcd9f22032c' => 'country',
'field_5cbcda0ab4375' => 'longitude',
'field_5cbcda11b4376' => 'latitude',
'field_5cbcda18b4377' => 'phone',
'field_5cbcda2f35476' => 'website',
'field_5cbcda3535477' => 'updated_at',
];
foreach( $fillable as $key => $name ){
update_field( $name, $brewery->$name, $existing_brewery_id);
}
}
}
}
$current_page = $current_page + 1;
wp_remote_post( admin_url('admin-ajax.php?action=get_breweries_from_api'), [
'blocking' => false,
'sslverify' => false, // we are sending this to ourselves, so trust it.
'body' => [
'current_page' => $current_page
]
] );
}
function slugify($text){
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
/** Various clean up functions */
require_once( 'library/cleanup.php' );
/** Required for Foundation to work properly */
require_once( 'library/foundation.php' );
/** Format comments */
require_once( 'library/class-foundationpress-comments.php' );
/** Register all navigation menus */
require_once( 'library/navigation.php' );
/** Add menu walkers for top-bar and off-canvas */
require_once( 'library/class-foundationpress-top-bar-walker.php' );
require_once( 'library/class-foundationpress-mobile-walker.php' );
/** Create widget areas in sidebar and footer */
require_once( 'library/widget-areas.php' );
/** Return entry meta information for posts */
require_once( 'library/entry-meta.php' );
/** Enqueue scripts */
require_once( 'library/enqueue-scripts.php' );
/** Add theme support */
require_once( 'library/theme-support.php' );
/** Add Nav Options to Customer */
require_once( 'library/custom-nav.php' );
/** Change WP's sticky post class */
require_once( 'library/sticky-posts.php' );
/** Configure responsive image sizes */
require_once( 'library/responsive-images.php' );
/** Gutenberg editor support */
require_once( 'library/gutenberg.php' );
/** If your site requires protocol relative url's for theme assets, uncomment the line below */
// require_once( 'library/class-foundationpress-protocol-relative-theme-assets.php' );