-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_api.php
125 lines (95 loc) · 3.22 KB
/
rest_api.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
<?php
namespace inseri_core;
require_once plugin_dir_path(__FILE__) . 'export.php';
require_once plugin_dir_path(__FILE__) . 'readme.php';
const OPTION_KEY = 'inseri-core-export-enabled';
class RestApi {
private $builder;
public function __construct($builder) {
$this->builder = $builder;
}
public function register_hooks() {
add_action('rest_api_init', [$this, 'register_routes']);
}
public function can_publish() {
return current_user_can('publish_posts');
}
public function can_export($request) {
$params = $request->get_params();
$data = get_option(OPTION_KEY, []);
return in_array($params['post_id'], $data);
}
public function register_routes() {
register_rest_route('inseri-core/v1', '/archive/(?P<post_id>\d+)', [
'methods' => 'GET',
'callback' => [$this, 'make_zip'],
'permission_callback' => [$this, 'can_export'],
]);
register_rest_route('inseri-core/v1', '/export-enabled/(?P<id>\d+)', [
'methods' => 'POST',
'callback' => [$this, 'add_post_id'],
'permission_callback' => [$this, 'can_publish'],
]);
register_rest_route('inseri-core/v1', '/export-enabled/(?P<id>\d+)', [
'methods' => 'DELETE',
'callback' => [$this, 'remove_post_id'],
'permission_callback' => [$this, 'can_publish'],
]);
}
public function add_post_id($request) {
$params = $request->get_params();
$data = get_option(OPTION_KEY, []);
if (!in_array($params['id'], $data)) {
array_push($data, $params['id']);
update_option(OPTION_KEY, $data);
}
return new \WP_REST_Response(null, 201);
}
public function remove_post_id($request) {
$params = $request->get_params();
$data = get_option(OPTION_KEY, []);
$data = array_diff($data, [$params['id']]);
update_option(OPTION_KEY, $data);
return new \WP_REST_Response(null, 200);
}
private function get_blueprint($params) {
return $this->builder->generate($params);
}
private function get_wxr($params) {
ob_start();
inseri_core_export_wp($params);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
private function get_readme($params, $blueprint) {
ob_start();
inseri_core_make_readme($params['post_id'], $blueprint);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
public function make_zip($request) {
$params = $request->get_params();
$blueprint = $this->get_blueprint($params);
$upload_dir = wp_upload_dir();
$unique_name = wp_unique_filename($upload_dir['basedir'], 'archive.zip');
$file = path_join($upload_dir['basedir'], $unique_name);
$zip = new \ZipArchive();
if ($zip->open($file, \ZipArchive::CREATE) !== true) {
return new \WP_Error('inseri_core_failed_zip', 'server cannot open zip file');
}
$zip->addFromString('post.xml', $this->get_wxr($params));
$zip->addFromString('blueprint.json', wp_json_encode($blueprint, JSON_PRETTY_PRINT));
$zip->addFromString('readme.md', $this->get_readme($params, wp_json_encode($blueprint)));
$zip->close();
header('Content-Disposition: attachment; filename=archive.zip;');
header('Content-Type: application/zip');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
echo file_get_contents($file);
unlink($file);
exit();
}
}