This repository was archived by the owner on Oct 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrest.php
More file actions
60 lines (54 loc) · 1.94 KB
/
rest.php
File metadata and controls
60 lines (54 loc) · 1.94 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
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'magazine/v1', '/pdf', array(
'methods' => 'POST',
'callback' => 'renderPDFForRest',
'permission_callback' => function () {
return is_user_logged_in();
},
'args' => array(
'ids' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return isset($param) && is_array($param);
},
'type' => 'array',
'description' => 'An array of Post IDs.'
),
'theme' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return isset($param) && trim($param) != '';
},
'type' => 'string',
'description' => 'The Theme name as string.'
),
'base64' => array(
'required' => false,
'type' => 'boolean',
'description' => 'Whether to return the PDF as base64 encoded string.'
),
)
) );
} );
function renderPDFForRest( WP_REST_Request $request ) {
$aParameters = $request->get_params();
$aThemes = magazine_template::_getTemplateNames();
if(!in_array($aParameters['theme'], $aThemes)){
return new WP_Error( 'theme_not_found', 'Can not find the provided theme name.', array( 'status' => 404 ) );
}
$aRenderResult = magazine_pdf::_renderPDF($aParameters['ids'], $aParameters['theme']);
$http_status = $aRenderResult['status_code'];
$pdfContent = $aRenderResult['content'];
if($http_status == 200 && $aParameters['base64'] === false){
header('Content-Type: application/pdf');
echo $pdfContent;
exit;
}else if($http_status == 200 && $aParameters['base64'] === true){
header('Content-Type: text/plain');
echo base64_encode($pdfContent);
exit;
}else{
return new WP_REST_Response(json_decode($pdfContent), $http_status);
}
}