-
Notifications
You must be signed in to change notification settings - Fork 0
/
locate-block.php
212 lines (185 loc) · 8.29 KB
/
locate-block.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
<?php
/**
* Plugin Name: Locate Block
* Description: Gutenberg block for LocateAndFilter plugin.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: locate-block
*
* @package CreateBlock
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function create_block_locate_block_block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', 'create_block_locate_block_block_init' );
// add custom field to rest API
function register_locateandfiltermap_custom_fields() {
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-map-provider', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-map-width', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-map-height', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-source', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-show-filters', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-start-position', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-start-zoom', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-scrollWheelZoom', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-display_filters', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-default-tooltip-template', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
register_rest_field(
'locateandfiltermap', // Your custom post type name
'locate-anything-tooltip-preset', // Name of the custom field
array(
'get_callback' => 'get_locateandfiltermap_custom_field_value', // Callback function to retrieve field value
'update_callback' => 'update_locateandfiltermap_custom_field_value', // Callback function to update field value
'schema' => null,
)
);
}
function get_locateandfiltermap_custom_field_value($object, $field_name, $request) {
return get_post_meta($object['id'], $field_name, true);
}
function update_locateandfiltermap_custom_field_value($value, $object, $field_name) {
return update_post_meta($object->ID, $field_name, $value);
}
add_action('rest_api_init', 'register_locateandfiltermap_custom_fields');
// create new route - add all map json to rest API
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/cache/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_custom_cache_file',
'args' => array(
'id' => array(
'validate_callback' => function ($param, $request, $key) {
return is_numeric($param);
}
),
),
));
});
function get_custom_cache_file($request) {
$id = $request['id'];
$file_path = WP_CONTENT_DIR . '/uploads/locateandfilter-cache/cache-' . $id . '.json';
if (file_exists($file_path)) {
$file_contents = file_get_contents($file_path);
$data = json_decode($file_contents, true);
if (json_last_error() === JSON_ERROR_NONE) {
return new WP_REST_Response($data, 200);
} else {
return new WP_Error('json_error', 'Error decoding JSON file', array('status' => 500));
}
} else {
return new WP_Error('file_not_found', 'File not found', array('status' => 404));
}
}
add_action('rest_api_init', function() {
register_rest_route('custom/v1', '/map-options', array(
'methods' => 'GET',
'callback' => 'get_map_options',
));
});
function get_map_options() {
$googlemaps_key = unserialize(get_option('locate-anything-option-googlemaps-key'));
$enable_marker_bouncing_js = unserialize(get_option('locate-anything-option-enable_markerBouncingJS'));
return rest_ensure_response(array(
'googlemaps_key' => $googlemaps_key,
'enable_marker_bouncing_js' => $enable_marker_bouncing_js,
));
}
function enable_show_in_rest_for_all_taxonomies($args, $taxonomy) {
$args['show_in_rest'] = true;
return $args;
}
add_filter('register_taxonomy_args', 'enable_show_in_rest_for_all_taxonomies', 10, 2);