-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcontroller.php
358 lines (334 loc) · 9.07 KB
/
controller.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
357
358
<?php //phpcs:disable Files.SideEffects
/**
* Capsule controller.
*
* @package capsule
*/
/**
* Capsule controller function.
*
* @return void
*/
function capsule_controller()
{
$action_get = filter_input(INPUT_GET, 'capsule_action');
if (! empty($action_get)) {
capsule_controller_action_get($action_get);
}
$action_post = filter_input(INPUT_POST, 'capsule_action');
if (! empty($action_post)) {
if (! current_user_can('edit_posts')) {
capsule_unauthorized_json();
}
capsule_controller_action_post($action_post);
}
}
add_action('init', 'capsule_controller', 11);
/**
* Process GET actions.
*
* @param string $action Current action.
* @return void
*/
function capsule_controller_action_get($action)
{
if ('search' === $action) {
global $wpdb;
$q = filter_input(INPUT_GET, 'q');
if (! empty($q) && in_array($q[0], array( '@', '#', '`' ), true)) {
$prefix = $q[0];
switch ($prefix) {
case '@':
$taxonomy = 'projects';
break;
case '#':
$taxonomy = 'post_tag';
break;
case '`':
$taxonomy = 'code';
break;
default:
$taxonomy = null;
break;
}
$term_name = stripslashes(substr($q, 1, strlen($q)));
if (! strlen($term_name) < 1) {
// Taken from wp_ajax_ajax_tag_search().
$results = $wpdb->get_col(
$wpdb->prepare(
'
SELECT t.name
FROM ' . $wpdb->term_taxonomy . ' AS tt
INNER JOIN ' . $wpdb->terms . ' AS t ON tt.term_id = t.term_id
WHERE tt.taxonomy = %s AND t.name LIKE (%s)
AND tt.count > 0',
$taxonomy,
$wpdb->esc_like($term_name) . '%'
)
);
$html = '';
foreach ($results as $result) {
$html .= $prefix . $result . "\n";
}
echo esc_html($html);
}
}
die();
}
if (0 === strpos($action, 'queue_')) {
$api_key = filter_input(INPUT_GET, 'api_key');
if (stripslashes($api_key) === capsule_queue_api_key()) {
switch ($action) {
case 'queue_run':
capsule_queue_run();
break;
case 'queue_post_to_server':
// Required params: post_id.
$post_id = (int) filter_input(INPUT_GET, 'post_id', FILTER_VALIDATE_INT);
if ($post_id > 0) {
capsule_queue_post_to_server($post_id);
}
break;
default:
break;
}
die();
}
}
if (! current_user_can('edit_posts')) {
capsule_unauthorized_json();
}
switch ($action) {
case 'post_excerpt':
case 'post_content':
// Required params: post_id.
$post_id = (int) filter_input(INPUT_GET, 'post_id', FILTER_VALIDATE_INT);
if ($post_id > 0) {
global $post;
$post = get_post($post_id);
setup_postdata($post);
$view = str_replace('post_', '', $action);
ob_start();
include get_template_directory() . '/ui/views/' . $view . '.php';
$html = ob_get_clean();
$response = compact('html');
header('Content-type: application/json');
wp_send_json($response);
}
break;
case 'post_editor':
// Required params: post_id.
$post_id = (int) filter_input(INPUT_GET, 'post_id', FILTER_VALIDATE_INT);
if ($post_id > 0) {
global $post;
$post = get_post($post_id);
setup_postdata($post);
ob_start();
include get_template_directory() . '/ui/views/edit.php';
$html = ob_get_clean();
$response = array(
'html' => $html,
'content' => $post->post_content,
);
header('Content-type: application/json');
wp_send_json($response);
}
break;
default:
do_action('capsule_controller_action_get', $action);
break;
}
}
/**
* Process POST actions.
*
* @param string $action Current action.
* @return void
*/
function capsule_controller_action_post($action)
{
switch ($action) {
case 'create_post':
global $post;
$post_id = wp_insert_post(
array(
'post_title' => time(),
'post_status' => 'draft',
'post_content' => '',
),
true
);
if (is_wp_error($post_id)) {
$result = 'error';
$msg = $post_id->get_error_message();
$response = compact('result', 'msg');
} else {
$result = 'success';
$msg = __('Post created.', 'capsule');
$post = get_post($post_id);
setup_postdata($post);
ob_start();
include get_template_directory() . '/ui/views/edit.php';
$html = ob_get_clean();
$ymd = get_the_time('Ymd', $post);
$response = array(
'post_id' => $post_id,
'result' => $result,
'msg' => $msg,
'html' => $html,
'content' => $post->post_content,
);
}
header('Content-type: application/json');
wp_send_json($response);
break;
case 'update_post':
// Required params: post_id, content.
$post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_VALIDATE_INT);
if ($post_id <= 0) {
die();
}
$post_title = '';
$taxonomies = array(
'projects' => array(),
'post_tag' => array(),
'code' => array(),
);
foreach ($taxonomies as $tax => $terms) {
$submitted_tax = filter_input(INPUT_POST, $tax);
$terms = json_decode($submitted_tax);
// There is no easy WP way assign terms by name to a post on the fly
// they must be created first and then use the slug (or ID for heirarchial).
foreach ($terms as $term_name) {
$term = get_term_by('name', $term_name, $tax);
if (! $term) {
$term_data = wp_insert_term($term_name, $tax);
if (! is_wp_error($term_data)) {
$term = get_term_by('id', $term_data['term_id'], $tax);
$taxonomies[ $tax ][] = $term->slug;
}
} else {
$taxonomies[ $tax ][] = $term->slug;
}
}
$post_title .= ' ' . implode(' ', $terms);
}
// If the content is empty, wp_update_post fails.
$content = filter_input(INPUT_POST, 'content');
if (empty($content)) {
$content = ' ';
}
$update = wp_update_post(
array(
'ID' => $post_id,
'post_title' => trim($post_title),
'post_content' => $content,
'post_status' => 'publish',
)
);
if ($update) {
foreach ($taxonomies as $tax => $terms) {
wp_set_post_terms($post_id, $terms, $tax);
}
$result = 'success';
$msg = 'Post saved.';
} else {
$result = 'error';
$msg = 'Saving post #' . $post_id . ' failed.';
}
$projects_html = capsule_term_list($post_id, 'projects');
$tags_html = capsule_term_list($post_id, 'post_tag');
$code_html = capsule_term_list($post_id, 'code');
$response = compact('post_id', 'result', 'msg', 'projects_html', 'tags_html', 'code_html');
header('Content-type: application/json');
wp_send_json($response);
break;
case 'delete_post':
// Required params: post_id.
$post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_VALIDATE_INT);
$delete = wp_delete_post($post_id);
if (false !== $delete) {
$post = get_post($post_id);
setup_postdata($post);
$result = 'success';
$msg = __('Post deleted', 'capsule');
ob_start();
include get_template_directory() . '/ui/views/deleted.php';
$html = ob_get_clean();
} else {
$result = 'error';
$msg = __('Post not deleted, please try again.', 'capsule');
$html = '';
}
$response = compact('post_id', 'result', 'msg', 'html');
header('Content-type: application/json');
wp_send_json($response);
break;
case 'undelete_post':
// Required params: post_id.
global $post;
$post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_VALIDATE_INT);
$post = wp_untrash_post($post_id);
if (false !== $post) {
$post = get_post($post_id);
setup_postdata($post);
$result = 'success';
$msg = __('Post recovered from trash.', 'capsule');
ob_start();
include get_template_directory() . '/ui/views/excerpt.php';
$html = ob_get_clean();
} else {
$result = 'error';
$msg = __('Post not restored, please try again.', 'capsule');
$html = '';
}
$response = compact('post_id', 'result', 'msg', 'html');
header('Content-type: application/json');
wp_send_json($response);
break;
case 'stick_post':
// Required params: post_id.
$post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_VALIDATE_INT);
$post = get_post($post_id);
if (! $post) {
die();
}
stick_post($post_id);
$response = array(
'post_id' => $post_id,
'result' => 'success',
'msg' => __('Post stuck.', 'capsule'),
'html' => '',
);
header('Content-type: application/json');
wp_send_json($response);
break;
case 'unstick_post':
// Required params: post_id.
$post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_VALIDATE_INT);
if ($post_id <= 0) {
die();
}
unstick_post($post_id);
$response = array(
'post_id' => $post_id,
'result' => 'success',
'msg' => __('Post unstuck.', 'capsule'),
'html' => '',
);
header('Content-type: application/json');
wp_send_json($response);
break;
case 'split_post':
// Required params: post_id, content, new_post_content.
// @TODO - implement.
break;
case 'merge_posts':
// Required params: post_ids (array).
// @TODO - implement.
break;
default:
do_action('capsule_controller_action_post', $action);
break;
}
}