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 
	<?php
defined( 'ABSPATH' ) || exit();
abstract class WC_Braintree_Rest_Controller {
    protected $namespace = '';
    
    protected function register_authenticated_route( $route ) {
        $routes                  = get_option( 'wc_braintree_authenticated_routes', array() );
        $route                   = '/' . trim( $route, '/' );
        $routes[ md5( $route ) ] = $route;
        update_option( 'wc_braintree_authenticated_routes', $routes );
    }
    
    abstract public function register_routes();
    public function rest_uri() {
        return trim( braintree()->rest_api->rest_uri() . ( ! empty( $this->namespace ) ? $this->namespace : '' ), '/' );
    }
    public function rest_url() {
        return braintree()->rest_api->rest_url() . ( ! empty( $this->namespace ) ? trailingslashit( $this->namespace ) : '' );
    }
    
    public function deprecated_rest_uri() {
        return 'braintree-gateway/v1';
    }
    
    public function admin_permission_check( $request ) {
        if ( ! current_user_can( 'administrator' ) ) {
            return new WP_Error(
                'permission-error',
                __( 'You do not have permissions to access this resource.', 'woo-payment-gateway' ),
                array(
                    'status' => 403,
                )
            );
        }
        return true;
    }
    
    public function shop_manager_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-payment-gateway' ),
                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();
    }
}