-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcapsule-server-import-export.php
90 lines (82 loc) · 2.41 KB
/
capsule-server-import-export.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
<?php //phpcs:disable Files.SideEffects.FoundWithSymbols
//phpcs:disable WordPress.Security.NonceVerification
/**
* Implement post import and taxonomy terms export.
*/
require_once 'inc/class-capsule-server-import-post.php';
require_once 'inc/class-capsule-server-export-terms.php';
/**
* Import export controller function.
*
* @return void
*/
function capsule_server_controller()
{
if (empty($_POST['capsule_server_action'])) {
return;
}
switch ($_POST['capsule_server_action']) {
case 'insert_post':
$response = array(
'result' => 'error',
);
// Cannot use nonce here as they're salted with unique keys, going to have to rely on api key.
if (isset($_POST['capsule_client_post_data'])) {
$data = $_POST['capsule_client_post_data'];
if (isset($data['post'], $data['tax'], $data['api_key'])) {
$user_id = capsule_server_validate_user($data['api_key']);
if ($user_id > 0) {
$capsule_import = new CrowdFavorite\Capsule\CapsuleServerImportPost(
$user_id,
$data['post'],
$data['tax']
);
$post_id = $capsule_import->import();
if ($capsule_import->local_post_id > 0) {
$response = array(
'result' => 'success',
'data' => array(
'permalink' => get_permalink($capsule_import->local_post_id),
),
);
}
} else {
header('HTTP/1.1 401 Unauthorized');
die();
}
}
}
wp_send_json($response);
break;
case 'get_terms':
if (
isset($_POST['capsule_client_post_data']['api_key']) &&
capsule_server_validate_user($_POST['capsule_client_post_data']['api_key'])
) {
$taxonomies = isset($_POST['capsule_client_post_data']['taxonomies'])
? $_POST['capsule_client_post_data']['taxonomies']
: [];
$term_exporter = new CrowdFavorite\Capsule\CapsuleServerExportTerms($taxonomies);
echo wp_json_encode($term_exporter->getTerms());
} else {
header('HTTP/1.1 401 Unauthorized');
}
die();
break;
case 'test_credentials':
if (
isset($_POST['capsule_client_post_data']['api_key']) &&
capsule_server_validate_user($_POST['capsule_client_post_data']['api_key'])
) {
echo 'authorized';
} else {
header('HTTP/1.1 401 Unauthorized');
}
die();
break;
default:
break;
}
}
// Come in after taxonomies are typically registered but before the wp-gatekeeper plugin.
add_action('init', 'capsule_server_controller', 11);