my-calendar-ajax.php
10.6 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
<?php
/**
* My Calendar AJAX actions. Miscellaneous tasks run in the admin via AJAX actions.
*
* @category Core
* @package My Calendar
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-calendar/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'wp_ajax_mc_core_autocomplete_search_pages', 'mc_core_autocomplete_search_pages' );
/**
* Add post lookup for assigning My Calendar main page
*/
function mc_core_autocomplete_search_pages() {
if ( isset( $_REQUEST['action'] ) && 'mc_core_autocomplete_search_pages' === $_REQUEST['action'] ) {
$security = $_REQUEST['security'];
if ( ! wp_verify_nonce( $security, 'mc-search-pages' ) ) {
wp_send_json(
array(
'success' => 0,
'response' => array( 'error' => 'My Calendar: invalid security check.' ),
)
);
}
$query = sanitize_text_field( $_REQUEST['data'] );
$args = array(
's' => $query,
'post_type' => 'any',
'orderby' => 'relevance',
);
$posts = get_posts( $args );
$response = array();
foreach ( $posts as $post ) {
$response[] = array(
'post_id' => absint( $post->ID ),
'post_title' => esc_html( html_entity_decode( strip_tags( $post->post_title ) ) ),
);
}
wp_send_json(
array(
'success' => 1,
'response' => $response,
)
);
}
}
add_action( 'wp_ajax_mc_core_autocomplete_search_icons', 'mc_core_autocomplete_search_icons' );
/**
* Add SVG icon lookup for category pages.
*/
function mc_core_autocomplete_search_icons() {
if ( isset( $_REQUEST['action'] ) && 'mc_core_autocomplete_search_icons' === $_REQUEST['action'] ) {
$security = $_REQUEST['security'];
if ( ! wp_verify_nonce( $security, 'mc-search-icons' ) ) {
wp_send_json(
array(
'success' => 0,
'response' => array( 'error' => 'My Calendar: invalid security check.' ),
)
);
}
$query = sanitize_text_field( $_REQUEST['data'] );
$dir = plugin_dir_path( __FILE__ );
if ( mc_is_custom_icon() ) {
$is_custom = true;
$directory = trailingslashit( str_replace( '/my-calendar', '', $dir ) ) . 'my-calendar-custom/icons/';
$iconlist = mc_directory_list( $directory );
} else {
$is_custom = false;
$directory = trailingslashit( dirname( __FILE__ ) ) . 'images/icons/';
$iconlist = mc_directory_list( $directory );
}
$results = array_filter(
$iconlist,
function( $el ) use ( $query ) {
return ( false !== stripos( $el, $query ) );
}
);
$response = array();
foreach ( $results as $result ) {
$response[] = array(
'filename' => esc_attr( $result ),
'svg' => mc_get_img( $result, $is_custom ),
);
}
wp_send_json(
array(
'success' => 1,
'response' => $response,
)
);
}
}
add_action( 'wp_ajax_add_category', 'mc_ajax_add_category' );
/**
* Delete a single occurrence of an event from the event manager.
*/
function mc_ajax_add_category() {
if ( ! check_ajax_referer( 'mc-add-category-nonce', 'security', false ) ) {
wp_send_json(
array(
'success' => 0,
'response' => __( 'My Calendar: invalid security check.', 'my-calendar' ),
)
);
}
if ( current_user_can( 'mc_edit_cats' ) ) {
global $wpdb;
$category_name = sanitize_text_field( $_REQUEST['category_name'] );
if ( '' === trim( $category_name ) ) {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'Empty category name.', 'my-calendar' ),
)
);
}
$category_id = mc_create_category(
array(
'category_name' => $category_name,
'category_color' => '',
'category_icon' => '',
)
);
if ( $category_id ) {
wp_send_json(
array(
'success' => 1,
'response' => esc_html__( 'New Category Created.', 'my-calendar' ),
'category_id' => $category_id,
)
);
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'Category not created.', 'my-calendar' ),
'category_id' => $category_id,
)
);
}
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'You are not authorized to perform this action', 'my-calendar' ),
)
);
}
}
add_action( 'wp_ajax_display_recurrence', 'mc_ajax_display_recurrence' );
/**
* Display the recurring settings in human-readable format.
*/
function mc_ajax_display_recurrence() {
if ( ! check_ajax_referer( 'mc-recurrence-nonce', 'security', false ) ) {
wp_send_json(
array(
'success' => 0,
'response' => __( 'My Calendar: invalid security check.', 'my-calendar' ),
)
);
}
$recur = sanitize_text_field( $_REQUEST['recur'] );
$every = (int) $_REQUEST['every'];
$until = sanitize_text_field( $_REQUEST['until'] );
$args = array(
'recur' => $recur,
'every' => $every,
'until' => $until,
);
$output = mc_recur_string( false, $args );
wp_send_json(
array(
'success' => 1,
'args' => $args,
'response' => $output,
)
);
}
add_action( 'wp_ajax_delete_occurrence', 'mc_ajax_delete_occurrence' );
/**
* Delete a single occurrence of an event from the event manager.
*/
function mc_ajax_delete_occurrence() {
if ( ! check_ajax_referer( 'mc-delete-nonce', 'security', false ) ) {
wp_send_json(
array(
'success' => 0,
'response' => __( 'My Calendar: invalid security check.', 'my-calendar' ),
)
);
}
if ( current_user_can( 'mc_manage_events' ) ) {
global $wpdb;
$occur_id = (int) $_REQUEST['occur_id'];
$event_id = (int) $_REQUEST['event_id'];
$begin = sanitize_text_field( $_REQUEST['occur_begin'] );
$end = sanitize_text_field( $_REQUEST['occur_end'] );
$delete = 'DELETE FROM `' . my_calendar_event_table() . '` WHERE occur_id = %d';
$result = $wpdb->query( $wpdb->prepare( $delete, $occur_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$event_post = mc_get_event_post( $event_id );
$instances = get_post_meta( $event_post, '_mc_deleted_instances', true );
$instances = ( ! is_array( $instances ) ) ? array() : $instances;
$instances[] = array(
'occur_event_id' => $event_id,
'occur_begin' => $begin,
'occur_end' => $end,
);
update_post_meta( $event_post, '_mc_deleted_instances', $instances );
if ( $result ) {
wp_send_json(
array(
'success' => 1,
'response' => esc_html__( 'Event instance has been deleted.', 'my-calendar' ),
)
);
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'Event instance was not deleted.', 'my-calendar' ),
)
);
}
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'You are not authorized to perform this action', 'my-calendar' ),
)
);
}
}
add_action( 'wp_ajax_add_date', 'mc_ajax_add_date' );
/**
* Add a single additional date for an event from the event manager.
*/
function mc_ajax_add_date() {
if ( ! check_ajax_referer( 'mc-delete-nonce', 'security', false ) ) {
wp_send_json(
array(
'success' => 0,
'response' => __( 'My Calendar: invalid security check.', 'my-calendar' ),
)
);
}
if ( current_user_can( 'mc_manage_events' ) ) {
global $wpdb;
$event_id = (int) $_REQUEST['event_id'];
if ( 0 === $event_id ) {
wp_send_json(
array(
'success' => 0,
'response' => __( 'No event ID in that request.', 'my-calendar' ),
)
);
}
$event_date = sanitize_text_field( $_REQUEST['event_date'] );
$event_end = isset( $_REQUEST['event_end'] ) ? sanitize_text_field( $_REQUEST['event_end'] ) : $event_date;
$event_time = sanitize_text_field( $_REQUEST['event_time'] );
$event_endtime = isset( $_REQUEST['event_endtime'] ) ? sanitize_text_field( $_REQUEST['event_endtime'] ) : '';
$group_id = (int) $_REQUEST['group_id'];
// event end can not be earlier than event start.
if ( ! $event_end || strtotime( $event_end ) < strtotime( $event_date ) ) {
$event_end = $event_date;
}
$begin = strtotime( $event_date . ' ' . $event_time );
$end = ( '' !== $event_endtime ) ? strtotime( $event_end . ' ' . $event_endtime ) : strtotime( $event_end . ' ' . $event_time ) + HOUR_IN_SECONDS;
$format = array( '%d', '%s', '%s', '%d' );
$data = array(
'occur_event_id' => $event_id,
'occur_begin' => mc_date( 'Y-m-d H:i:s', $begin, false ),
'occur_end' => mc_date( 'Y-m-d H:i:s', $end, false ),
'occur_group_id' => $group_id,
);
$result = $wpdb->insert( my_calendar_event_table(), $data, $format );
$id = $wpdb->insert_id;
$event_post = mc_get_event_post( $event_id );
$instances = get_post_meta( $event_post, '_mc_custom_instances', true );
$instances = ( ! is_array( $instances ) ) ? array() : $instances;
$instances[] = $data;
update_post_meta( $event_post, '_mc_custom_instances', $instances );
if ( $result ) {
wp_send_json(
array(
'success' => 1,
'id' => (int) $id,
'response' => esc_html__( 'A new date has been added to this event.', 'my-calendar' ),
)
);
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'Sorry! I failed to add that date to your event.', 'my-calendar' ),
)
);
}
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'You are not authorized to perform this action', 'my-calendar' ),
)
);
}
}
/**
* Get information about locations.
*/
function mc_core_autocomplete_search_locations() {
if ( isset( $_REQUEST['action'] ) && 'mc_core_autocomplete_search_locations' === $_REQUEST['action'] ) {
$security = $_REQUEST['security'];
if ( ! wp_verify_nonce( $security, 'mc-search-locations' ) ) {
wp_send_json(
array(
'success' => 0,
'response' => array( 'error' => 'My Calendar: invalid security check.' ),
)
);
}
$query = sanitize_text_field( $_REQUEST['data'] );
$locations = mc_core_search_locations( $query );
$response = array();
foreach ( $locations as $location ) {
$response[] = array(
'location_id' => (int) $location->location_id,
'location_label' => esc_html( strip_tags( $location->location_label ) ),
);
}
wp_send_json(
array(
'success' => 1,
'response' => $response,
)
);
}
}
add_action( 'wp_ajax_mc_core_autocomplete_search_locations', 'mc_core_autocomplete_search_locations' );
add_action( 'wp_ajax_nopriv_mc_core_autocomplete_search_locations', 'mc_core_autocomplete_search_locations' );