Newer
Older
<?php
/**
* Cxn controller class.
*
* CiviConnect endpoint, replacement for CiviCRM's 'extern/cxn.php'.
*
* @since 0.1
*/
namespace CiviCRM_WP_REST\Controller;
class Cxn extends Base {
/**
* The base route.
*
* @since 0.1
* @var string
*/
protected $rest_base = 'cxn';
/**
* Registers routes.
*
* @since 0.1
*/
public function register_routes() {
register_rest_route( $this->get_namespace(), $this->get_rest_base(), [
[
'methods' => \WP_REST_Server::ALLMETHODS,
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'callback' => [ $this, 'get_item' ]
]
] );
}
/**
* Get items.
*
* @since 0.1
* @param WP_REST_Request $request
*/
public function get_item( $request ) {
/**
* Filter request params.
*
* @since 0.1
* @param array $params
* @param WP_REST_Request $request
*/
$params = apply_filters( 'civi_wp_rest/controller/cxn/params', $request->get_params(), $request );
// init connection server
$cxn = \CRM_Cxn_BAO_Cxn::createApiServer();
/**
* Filter connection server object.
*
* @param Civi\Cxn\Rpc\ApiServer $cxn
* @param array $params
* @param WP_REST_Request $request
*/
$cxn = apply_filters( 'civi_wp_rest/controller/cxn/instance', $cxn, $params, $request );
try {
$result = $cxn->handle( $request->get_body() );
} catch ( Civi\Cxn\Rpc\Exception\CxnException $e ) {
return $this->civi_rest_error( $e->getMessage() );
} catch ( Civi\Cxn\Rpc\Exception\ExpiredCertException $e ) {
return $this->civi_rest_error( $e->getMessage() );
} catch ( Civi\Cxn\Rpc\Exception\InvalidCertException $e ) {
return $this->civi_rest_error( $e->getMessage() );
} catch ( Civi\Cxn\Rpc\Exception\InvalidMessageException $e ) {
return $this->civi_rest_error( $e->getMessage() );
} catch ( Civi\Cxn\Rpc\Exception\GarbledMessageException $e ) {
return $this->civi_rest_error( $e->getMessage() );
}
/**
* Bypass WP and send request from Cxn.
*/
add_filter( 'rest_pre_serve_request', function( $served, $response, $request, $server ) use ( $result ) {
// Civi\Cxn\Rpc\Message->send()
$result->send();
return true;
}, 10, 4 );
return rest_ensure_response( $result );
}
/**
* Item schema.
*
* @since 0.1
* @return array $schema
*/
public function get_item_schema() {}
/**
* Item arguments.
*
* @since 0.1
* @return array $arguments
*/
public function get_item_args() {}
}