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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
<?php
defined( 'ABSPATH' ) || exit();
abstract class WC_Stripe_Rest_Controller {
protected $namespace = '';
protected $error = null;
protected function register_authenticated_route( $route ) {
$routes = get_option( 'wc_stripe_authenticated_routes', array() );
$route = '/' . trim( $route, '/' );
$routes[ md5( $route ) ] = $route;
update_option( 'wc_stripe_authenticated_routes', $routes );
}
abstract public function register_routes();
public function rest_uri( $uri = '' ) {
$rest_uri = stripe_wc()->rest_api->rest_uri() . ( ! empty( $this->namespace ) ? $this->namespace : '' );
if ( $uri ) {
$rest_uri = trailingslashit( $rest_uri ) . $uri;
}
return trim( $rest_uri, '/' );
}
public function rest_url( $uri = '' ) {
$rest_url = stripe_wc()->rest_api->rest_url() . ( ! empty( $this->namespace ) ? trailingslashit( $this->namespace ) : '' );
if ( $uri ) {
$rest_url = trailingslashit( $rest_url ) . $uri;
}
return $rest_url;
}
public function admin_permission_check( $request ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new WP_Error( 'permission-error', __( 'You do not have permissions to access this resource.', 'woo-stripe-payment' ), array( 'status' => 403 ) );
}
return true;
}
protected function get_error_messages() {
return $this->get_messages( 'error' );
}
protected function get_messages( $types = 'all' ) {
$notices = wc_get_notices();
if ( $types !== 'all' ) {
$types = (array) $types;
foreach ( $notices as $type => $notice ) {
if ( ! in_array( $type, $types ) ) {
unset( $notices[ $type ] );
}
}
}
wc_set_notices( $notices );
ob_start();
$messages = wc_print_notices();
return ob_get_clean();
}
protected function add_validation_error( $error ) {
$data = $error->get_error_data();
if ( ! is_array( $data ) ) {
$data = array();
}
$error->add_data( array_merge( $data, array( 'status' => 200 ) ) );
$this->error = $error;
add_filter(
'rest_request_before_callbacks',
function ( $response ) {
return $this->error ? $this->error : response;
}
);
return $error;
}
}