PluginProbe
Redirection / 5.8.1
Redirection v5.8.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / api / api-export.php

api-export.php in Redirection 5.8.1, at api/api-export.php

88 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @api {get} /redirection/v1/export/:module/:format Export redirects
5 * @apiName Export
6 * @apiDescription Export redirects for a module to Apache, CSV, Nginx, or JSON format
7 * @apiGroup Import/Export
8 *
9 * @apiParam (URL) {String="1","2","3","all"} :module The module to export, with 1 being WordPress, 2 is Apache, and 3 is Nginx
10 * @apiParam (URL) {String="csv","apache","nginx","json"} :format The format of the export
11 *
12 * @apiSuccess {String} data Exported data
13 * @apiSuccess {Integer} total Number of items exported
14 *
15 * @apiUse 401Error
16 * @apiUse 404Error
17 * @apiError redirect_export_invalid_module Invalid module
18 * @apiErrorExample {json} 404 Error Response:
19 * HTTP/1.1 400 Bad Request
20 * {
21 * "code": "redirect_export_invalid_module",
22 * "message": "Invalid module"
23 * }
24 */
25
26 /**
27 * @phpstan-type ExportResponse array{
28 * data: string,
29 * total: int
30 * }
31 */
32 class Redirection_Api_Export extends Redirection_Api_Route {
33 /**
34 * Export API endpoint constructor
35 *
36 * @param non-falsy-string $api_namespace Namespace.
37 */
38 public function __construct( $api_namespace ) {
39 // GET /export/:module/:format - Export redirects to specified format
40 register_rest_route(
41 $api_namespace,
42 '/export/(?P<module>1|2|3|all)/(?P<format>csv|apache|nginx|json)',
43 [
44 [
45 'methods' => WP_REST_Server::READABLE,
46 'callback' => [ $this, 'route_export' ],
47 'permission_callback' => [ $this, 'permission_callback_manage' ],
48 ],
49 ]
50 );
51 }
52
53 /**
54 * Check if the user has permission to manage import/export
55 *
56 * @param WP_REST_Request<array<string, mixed>> $request
57 * @return bool
58 */
59 public function permission_callback_manage( WP_REST_Request $request ) {
60 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_IO_MANAGE );
61 }
62
63 /**
64 * Export redirects to a specified format
65 *
66 * @param WP_REST_Request<array<string, mixed>> $request
67 * @return ExportResponse|WP_Error
68 */
69 public function route_export( WP_REST_Request $request ) {
70 $module = sanitize_text_field( $request['module'] );
71 $format = 'json';
72
73 if ( in_array( $request['format'], [ 'csv', 'apache', 'nginx', 'json' ], true ) ) {
74 $format = sanitize_text_field( $request['format'] );
75 }
76
77 $export = Red_FileIO::export( $module, $format );
78 if ( $export === false ) {
79 return $this->add_error_details( new WP_Error( 'redirect_export_invalid_module', 'Invalid module' ), __LINE__ );
80 }
81
82 return array(
83 'data' => $export['data'],
84 'total' => $export['total'],
85 );
86 }
87 }
88