| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @api {get} /redirection/v1/export/:module/:format Export redirects for a module in a format |
| 5 |
* @apiDescription Export redirects for a module in a format |
| 6 |
* @apiGroup Export |
| 7 |
* |
| 8 |
* @apiParam {String} module The module to export - 1, 2, 3, or 'all' |
| 9 |
* @apiParam {String} format The format of the export. Either 'csv', 'apache', 'nginx', or 'json' |
| 10 |
* |
| 11 |
* @apiSuccess {Array} ip Array of export data |
| 12 |
* @apiSuccess {Integer} total Number of items exported |
| 13 |
* |
| 14 |
* @apiUse 400Error |
| 15 |
*/ |
| 16 |
class Redirection_Api_Export extends Redirection_Api_Route { |
| 17 |
public function __construct( $namespace ) { |
| 18 |
register_rest_route( $namespace, '/export/(?P<module>1|2|3|all)/(?P<format>csv|apache|nginx|json)', array( |
| 19 |
$this->get_route( WP_REST_Server::READABLE, 'route_export' ), |
| 20 |
) ); |
| 21 |
} |
| 22 |
|
| 23 |
public function route_export( WP_REST_Request $request ) { |
| 24 |
$module = $request['module']; |
| 25 |
$format = 'json'; |
| 26 |
|
| 27 |
if ( in_array( $request['format'], array( 'csv', 'apache', 'nginx', 'json' ) ) ) { |
| 28 |
$format = $request['format']; |
| 29 |
} |
| 30 |
|
| 31 |
$export = Red_FileIO::export( $module, $format ); |
| 32 |
if ( $export === false ) { |
| 33 |
return $this->add_error_details( new WP_Error( 'redirect', 'Invalid module' ), __LINE__ ); |
| 34 |
} |
| 35 |
|
| 36 |
return array( |
| 37 |
'data' => $export['data'], |
| 38 |
'total' => $export['total'], |
| 39 |
); |
| 40 |
} |
| 41 |
} |
| 42 |
|