| 1 |
<?php |
| 2 |
|
| 3 |
include_once dirname( __FILE__ ) . '/api/api-group.php'; |
| 4 |
include_once dirname( __FILE__ ) . '/api/api-redirect.php'; |
| 5 |
include_once dirname( __FILE__ ) . '/api/api-log.php'; |
| 6 |
include_once dirname( __FILE__ ) . '/api/api-404.php'; |
| 7 |
include_once dirname( __FILE__ ) . '/api/api-settings.php'; |
| 8 |
include_once dirname( __FILE__ ) . '/api/api-plugin.php'; |
| 9 |
include_once dirname( __FILE__ ) . '/api/api-import.php'; |
| 10 |
include_once dirname( __FILE__ ) . '/api/api-export.php'; |
| 11 |
|
| 12 |
define( 'REDIRECTION_API_NAMESPACE', 'redirection/v1' ); |
| 13 |
|
| 14 |
class Redirection_Api_Route { |
| 15 |
protected function add_error_details( WP_Error $error, $line, $code = 400 ) { |
| 16 |
global $wpdb; |
| 17 |
|
| 18 |
$data = array( |
| 19 |
'status' => $code, |
| 20 |
'error_code' => $line, |
| 21 |
); |
| 22 |
|
| 23 |
if ( isset( $wpdb->last_error ) && $wpdb->last_error ) { |
| 24 |
$data['wpdb'] = $wpdb->last_error; |
| 25 |
} |
| 26 |
|
| 27 |
$error->add_data( $data ); |
| 28 |
return $error; |
| 29 |
} |
| 30 |
|
| 31 |
public function permission_callback( WP_REST_Request $request ) { |
| 32 |
return current_user_can( apply_filters( 'redirection_role', 'manage_options' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
public function get_route( $method, $callback ) { |
| 36 |
return array( |
| 37 |
'methods' => $method, |
| 38 |
'callback' => array( $this, $callback ), |
| 39 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 40 |
); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
class Redirection_Api_Filter_Route extends Redirection_Api_Route { |
| 45 |
protected function get_filter_args( $filter_fields, $order_fields ) { |
| 46 |
return array( |
| 47 |
'filterBy' => array( |
| 48 |
'description' => 'Field to filter by', |
| 49 |
'type' => 'enum', |
| 50 |
'enum' => $filter_fields, |
| 51 |
), |
| 52 |
'filter' => array( |
| 53 |
'description' => 'Value to filter by', |
| 54 |
'type' => 'string', |
| 55 |
), |
| 56 |
'orderby' => array( |
| 57 |
'description' => 'Field to order results by', |
| 58 |
'type' => 'enum', |
| 59 |
'enum' => $order_fields, |
| 60 |
), |
| 61 |
'direction' => array( |
| 62 |
'description' => 'Direction of ordered results', |
| 63 |
'type' => 'enum', |
| 64 |
'default' => 'desc', |
| 65 |
'enum' => array( 'asc', 'desc' ), |
| 66 |
), |
| 67 |
'per_page' => array( |
| 68 |
'description' => 'Number of results per page', |
| 69 |
'type' => 'integer', |
| 70 |
'default' => 25, |
| 71 |
'minimum' => 5, |
| 72 |
'maximum' => RED_MAX_PER_PAGE, |
| 73 |
), |
| 74 |
'page' => array( |
| 75 |
'description' => 'Page offset', |
| 76 |
'type' => 'integer', |
| 77 |
'minimum' => 0, |
| 78 |
'default' => 0, |
| 79 |
), |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
public function register_bulk( $namespace, $route, $filters, $orders, $callback ) { |
| 84 |
register_rest_route( $namespace, $route, array( |
| 85 |
$this->get_route( WP_REST_Server::EDITABLE, $callback ), |
| 86 |
'args' => array_merge( $this->get_filter_args( $filters, $orders ), array( |
| 87 |
'items' => array( |
| 88 |
'description' => 'Comma separated list of item IDs to perform action on', |
| 89 |
'type' => 'string|integer', |
| 90 |
'required' => true, |
| 91 |
), |
| 92 |
) ), |
| 93 |
) ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
class Redirection_Api { |
| 98 |
private static $instance = null; |
| 99 |
private $routes = array(); |
| 100 |
|
| 101 |
static function init() { |
| 102 |
if ( is_null( self::$instance ) ) { |
| 103 |
self::$instance = new Redirection_Api(); |
| 104 |
} |
| 105 |
|
| 106 |
return self::$instance; |
| 107 |
} |
| 108 |
|
| 109 |
public function __construct() { |
| 110 |
global $wpdb; |
| 111 |
|
| 112 |
$wpdb->hide_errors(); |
| 113 |
|
| 114 |
$this->routes[] = new Redirection_Api_Redirect( REDIRECTION_API_NAMESPACE ); |
| 115 |
$this->routes[] = new Redirection_Api_Group( REDIRECTION_API_NAMESPACE ); |
| 116 |
$this->routes[] = new Redirection_Api_Log( REDIRECTION_API_NAMESPACE ); |
| 117 |
$this->routes[] = new Redirection_Api_404( REDIRECTION_API_NAMESPACE ); |
| 118 |
$this->routes[] = new Redirection_Api_Settings( REDIRECTION_API_NAMESPACE ); |
| 119 |
$this->routes[] = new Redirection_Api_Plugin( REDIRECTION_API_NAMESPACE ); |
| 120 |
$this->routes[] = new Redirection_Api_Import( REDIRECTION_API_NAMESPACE ); |
| 121 |
$this->routes[] = new Redirection_Api_Export( REDIRECTION_API_NAMESPACE ); |
| 122 |
} |
| 123 |
} |
| 124 |
|