PluginProbe
Redirection / 5.1.2
Redirection v5.1.2
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-import.php

api-import.php in Redirection 5.1.2, at api/api-import.php

92 lines 3.1 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/import/file/:group_id Import redirects
5 * @apiName Import
6 * @apiDescription Import redirects from CSV, JSON, or Apache .htaccess
7 * @apiGroup Import/Export
8 *
9 * @apiParam (URL) {Integer} :group_id The group ID to import into
10 * @apiParam (File) {File} file The multipart form upload containing the file to import
11 *
12 * @apiSuccess {Integer} imported Number of items imported
13 *
14 * @apiUse 401Error
15 * @apiUse 404Error
16 * @apiError (Error 400) redirect_import_invalid_group Invalid group
17 * @apiErrorExample {json} 404 Error Response:
18 * HTTP/1.1 400 Bad Request
19 * {
20 * "code": "redirect_import_invalid_group",
21 * "message": "Invalid group"
22 * }
23 * @apiError (Error 400) redirect_import_invalid_file Invalid file upload
24 * @apiErrorExample {json} 404 Error Response:
25 * HTTP/1.1 400 Bad Request
26 * {
27 * "code": "redirect_import_invalid_file",
28 * "message": "Invalid file upload"
29 * }
30 */
31 class Redirection_Api_Import extends Redirection_Api_Route {
32 public function __construct( $namespace ) {
33 register_rest_route( $namespace, '/import/file/(?P<group_id>\d+)', array(
34 $this->get_route( WP_REST_Server::EDITABLE, 'route_import_file', [ $this, 'permission_callback_manage' ] ),
35 ) );
36
37 register_rest_route( $namespace, '/import/plugin', array(
38 $this->get_route( WP_REST_Server::READABLE, 'route_plugin_import_list', [ $this, 'permission_callback_manage' ] ),
39 ) );
40
41 register_rest_route( $namespace, '/import/plugin', array(
42 $this->get_route( WP_REST_Server::EDITABLE, 'route_plugin_import', [ $this, 'permission_callback_manage' ] ),
43 ) );
44 }
45
46 public function permission_callback_manage( WP_REST_Request $request ) {
47 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_IO_MANAGE );
48 }
49
50 public function route_plugin_import_list( WP_REST_Request $request ) {
51 include_once dirname( __DIR__ ) . '/models/importer.php';
52
53 return array( 'importers' => Red_Plugin_Importer::get_plugins() );
54 }
55
56 public function route_plugin_import( WP_REST_Request $request ) {
57 include_once dirname( __DIR__ ) . '/models/importer.php';
58
59 $params = $request->get_params( $request );
60 $groups = Red_Group::get_all();
61 $plugins = is_array( $request['plugin'] ) ? $request['plugin'] : [ $request['plugin'] ];
62 $total = 0;
63
64 foreach ( $plugins as $plugin ) {
65 $total += Red_Plugin_Importer::import( $plugin, $groups[0]['id'] );
66 }
67
68 return [ 'imported' => $total ];
69 }
70
71 public function route_import_file( WP_REST_Request $request ) {
72 $upload = $request->get_file_params();
73 $upload = isset( $upload['file'] ) ? $upload['file'] : false;
74 $group_id = $request['group_id'];
75
76 if ( $upload && is_uploaded_file( $upload['tmp_name'] ) ) {
77 $count = Red_FileIO::import( $group_id, $upload );
78
79 if ( $count !== false ) {
80 return array(
81 'imported' => $count,
82 );
83 }
84
85 return $this->add_error_details( new WP_Error( 'redirect_import_invalid_group', 'Invalid group' ), __LINE__ );
86 }
87
88 return $this->add_error_details( new WP_Error( 'redirect_import_invalid_file', 'Invalid file' ), __LINE__ );
89 }
90
91 }
92