forked from EvanAgee/vuejs-wordpress-theme-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions-custom.php
356 lines (307 loc) · 9.69 KB
/
functions-custom.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
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
<?php
/**
* @done: make sure acf is installed
*
* @todo: use the acf-string to customize the map fields in the backend.
* @see: https://www.advancedcustomfields.com/resources/register-fields-via-php/
*
* @todo think about the front-page:
* - we currently use the url /home which brings some disadvantages...
*
* @todo improve structure, move map-post type to class?
*
* @todo looks we still fetch things twice...
* @see cats: http://wp.local/wp-json/wp/v2/categories?sort=name&hide_empty=true&per_page=10
*
* @todo remove acf frontend dependency!
*/
require_once 'includes/classes/class-handle-acf.php';
require_once 'includes/classes/class-handle-mapbox.php';
require_once 'includes/classes/class-customize-theme.php';
require_once 'includes/classes/class-manage-local-storage.php';
require_once 'includes/classes/class-add-performance.php';
/**
* @todo rename the "solution" post type to "map".
* @todo rename feature (alias feature-collection) in js & php because its easy to confuse with feature-image and move here.
*/
require_once 'includes/classes/class-map-post-type.php';
// add the solution post type. (this is going to be renamed to map).
$map_post_type = new MapPostType();
$fields = include 'includes/add_custom_fields.php';
// make sure the ACF-Plugin is there and has the relevant API-Keys.
$my_acf = new HandleAcf($fields );
// add custom fields to the solution post type.
if ( function_exists( 'acf_add_local_field_group' ) ) {
require_once 'includes/add_custom_fields.php';
} else {
error_log( esc_html__( 'Make sure the Plugin "Advanced Custom Fields" is installed.' ) );
}
$my_mapbox = new HandleMapbox();
$my_theme = new CustomizeTheme();
$my_storage = new MangeLocalStorage();
$my_performance = new AddPerformance();
function cookie_update_redirect() {
// we are not interested in ajax or rest requests.
if ( wp_doing_ajax() || is_rest() || is_admin() ) {
return;
}
$cookie_name = 'intro';
if (
'/' == $_SERVER['REQUEST_URI'] // you are visiting the main site.
&& ! isset( $_COOKIE[ $cookie_name ] ) // you haven't visited the main site before.
) {
setcookie( $cookie_name, time(), time() + 1209600, SITECOOKIEPATH, COOKIE_DOMAIN, false, true );
$pageslug = 'home';
wp_safe_redirect( get_site_url() . '/' . $pageslug );
exit;
}
}
add_action( 'init', 'cookie_update_redirect' );
function load_scripts_styles() {
wp_enqueue_script( 'swiper', get_stylesheet_directory_uri() . '/node_modules/swiper/swiper-bundle.min.js', array(), '8.1', true );
wp_enqueue_style( 'swiper-css', get_stylesheet_directory_uri() . '/node_modules/swiper/swiper.min.css', false, null );
if ( ! is_admin() ) {
wp_deregister_script( 'jquery' );
}
}
add_action( 'wp_enqueue_scripts', 'load_scripts_styles', 100 );
$feature_transient_name = 'feature_transient';
add_action( 'save_post_solution', 'delete_solution_transient' );
add_action( 'update_post_solution', 'delete_solution_transient' );
add_action( 'delete_post_solution', 'delete_solution_transient' );
function delete_solution_transient() {
global $feature_transient_name;
error_log( 'del trans' );
delete_transient( $feature_transient_name );
}
/**
* @todo: we can probably think about a better way to posts and features (<-rename them!), so we don't fetch things so redundant.
*
* @param [type] $data
* @return void
*/
function feature_collection( $data ) {
global $feature_transient_name;
$feature_collection = get_transient( $feature_transient_name );
// $data['id']; <- thats how we fetch stuffs...
$post_type_name = $data['type'];
$args = array(
'post_type' => array( $post_type_name ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'orderby' => 'meta_value',
'meta_type' => 'NUMERIC', // because prio is a number.
'meta_key' => 'priority',
'order' => 'DESC',
// 'orderby' => 'menu_order',
);
$query = new WP_Query( $args );
$features = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
global $post;
$query->the_post();
$location = get_field( 'location' );
$priority = get_field( 'priority' );
if ( ! empty( $location ) ) {
array_push(
$features,
array(
'type' => 'Feature',
'properties' => array(
'post_id' => get_the_ID(),
'slug' => $post->post_name,
'title' => get_the_title(),
'subtitle' => get_field( 'subtitle' ),
'priority' => intval( $priority ? $priority : 5 ),
'thumbnail' => get_the_post_thumbnail_url( null, 'medium' ),
'location_name' => get_field( 'location_name' ),
'since' => get_field( 'since' ),
'number_of_employees' => get_field( 'number_of_employees' ),
'link_relative' => make_link_relative_to_blog( get_permalink() ),
),
'geometry' => array(
'type' => 'Point',
'coordinates' => array( $location['lng'], $location['lat'] ),
),
)
);
}
}
}
$feature_collection =
array(
'type' => 'FeatureCollection',
'features' => $features,
);
$bool_response = set_transient( $feature_transient_name, $feature_collection, 86400 );
// Restore original Post Data.
wp_reset_postdata();
return $feature_collection;
}
function menu_callback( $data ) {
wp_nav_menu( array( 'theme_location' => $data['type'] ) );
die();
}
function get_subtitle( $object ) {
return get_field( 'subtitle', $object['id'] );
}
function get_priority( $object ) {
return (int) get_field( 'priority', $object['id'] );
}
function get_feature( $object ) {
return array(
'thumbnail' => get_the_post_thumbnail_url( $object['id'], 'thumbnail' ),
'large' => get_the_post_thumbnail_url( $object['id'], 'large' ),
);
}
function get_edit_url( $object ) {
return get_edit_post_link( $object['id'] );
}
/**
* Get the value of the "link_relative" field
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request.
*
* @return mixed
*/
function slug_get_link_relative( $object, $field_name, $request ) {
return make_link_relative_to_blog( $object['link'] );
// return wp_make_link_relative( $object['link'] );
}
/**
* Remove blog-specific things form a link.
* If the link is http://wp.org/blogname/postname
* this returns just /postname
*
* @param string $link absolute link.
* @return string relative link.
*/
function make_link_relative_to_blog( $link ) {
$site_url = get_site_url();
$new_link = str_replace( $site_url, '', $link );
error_log( $new_link . ' | ' . $site_url . ' | ' . $link );
return $new_link;
}
add_action( 'rest_api_init', 'register_routes' );
function register_routes() {
register_rest_route(
'map/v1',
'/frontpage/',
array(
'methods' => 'GET',
'callback' => 'get_frontpage',
)
);
// http://wp.local/wp-json/map/v1/features/solution
register_rest_route(
'map/v1',
'/features/(?P<type>.+)',
// '/features',
array(
'methods' => 'GET',
'callback' => 'feature_collection',
)
);
// http://wp.local/wp-json/map/v1/menus/footer
register_rest_route(
'map/v1',
'/menus/(?P<type>.+)',
array(
'methods' => 'GET',
'callback' => 'menu_callback',
)
);
register_rest_field(
array( 'post', 'solution', 'page' ),
'link_relative',
array(
'get_callback' => 'slug_get_link_relative',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
array( 'post', 'solution', 'page' ),
'edit_url',
array(
'get_callback' => 'get_edit_url',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
array( 'post', 'solution', 'page' ),
'feature',
array(
'get_callback' => 'get_feature',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
array( 'post', 'solution', 'page' ),
'subtitle',
array(
'get_callback' => 'get_subtitle',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
array( 'solution' ),
'priority',
array(
'get_callback' => 'get_priority',
'update_callback' => null,
'schema' => null,
)
);
}
function get_frontpage( $request ) {
// Get the ID of the static frontpage. If not set it's 0.
$pid = (int) get_option( 'page_on_front' );
// Get the corresponding post object (let's show our intention explicitly).
$post = ( $pid > 0 ) ? get_post( $pid ) : null;
// No static frontpage is set.
if ( ! is_a( $post, 'WP_Post' ) ) {
return array(
'post_title' => 'No static frontpage.',
'post_content' => 'Set a static frontpage in the backend under Settings->Reading.',
);
}
$post->post_content = apply_filters( 'the_content', $post->post_content );
$post->thumbnail = get_the_post_thumbnail_url( $pid, 'large' );
// Response setup.
return new WP_REST_Response( $post, 200 );
}
register_nav_menus(
array(
'footer' => __( 'Footer', 'blankslate' ),
)
);
/**
* Checks if the current request is a WP REST API request.
*
* Case #1: After WP_REST_Request initialisation
* Case #2: Support "plain" permalink settings
* Case #3: URL Path begins with wp-json/ (your REST prefix)
* Also supports WP installations in subfolders
*
* @returns boolean
* @author matzeeable
*/
function is_rest() {
$prefix = rest_get_url_prefix();
if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
|| isset( $_GET['rest_route'] ) // (#2)
&& strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0 ) {
return true;
}
// (#3)
$rest_url = wp_parse_url( site_url( $prefix ) );
$current_url = wp_parse_url( add_query_arg( array() ) );
return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
}