diff --git a/includes/abstract-integration.php b/includes/abstract-integration.php index 9c84d778a30e7ebefdbbf2a37325399ae290c460..5f1cb5e3b66f219233e77038870cef7a1b64e4b5 100644 --- a/includes/abstract-integration.php +++ b/includes/abstract-integration.php @@ -7,8 +7,6 @@ use WP_Error; use Exception; use TypeError; -use function WPCT_HTTP\wpct_http_post; - /** * Integration abstract class. * @@ -196,9 +194,9 @@ abstract class Integration extends Singleton ); $this->apply_pipes($hook['pipes'], $payload); $headers = $backend->get_headers(); - if (isset($hook['endpoint'])) { + if (isset($hook['method'], $hook['endpoint'])) { $url = $backend->get_endpoint_url($hook['endpoint']); - $res = wpct_http_post($url, [ + $res = $this->submit_rest($hook['method'], $url, [ 'data' => $payload, 'files' => $attachments, 'headers' => $headers, @@ -350,6 +348,36 @@ abstract class Integration extends Singleton ); } + /** + * Submit REST requests over HTTP methods. + * + * @since 3.0.4 + * + * @param string $method HTTP method (GET, POST, PUT, DELETE). + * @param string $url Target URL. + * @param array $args Request arguments. + * @return array|WP_Error Request response. + */ + private function submit_rest($method, $url, $args) + { + $m = strtolower($method); + $func = "\WPCT_HTTP\wpct_http_{$m}"; + if (!is_callable($func)) { + return new WP_Error( + 'http_method_not_allowed', + __("Unkown HTTP method: {$method}", 'wpct-erp-forms'), + ['status' => 405] + ); + } + + if (in_array($method, ['GET', 'DELETE'])) { + unset($args['headers']['Content-Type']); + $args['params'] = $args['data']; + } + + return $func($url, $args); + } + /** * JSON RPC login request. * diff --git a/includes/class-settings.php b/includes/class-settings.php index 0be9dc11299eca2263d2094abab4978ed1212ed3..6d5596c0165ad0d54cc82a863128b370264cab29 100644 --- a/includes/class-settings.php +++ b/includes/class-settings.php @@ -128,6 +128,10 @@ class Settings extends BaseSettings 'backend' => ['type' => 'string'], 'form_id' => ['type' => 'string'], 'endpoint' => ['type' => 'string'], + 'method' => [ + 'type' => 'string', + 'enum' => ['GET', 'POST', 'PUT', 'DELETE'], + ], 'pipes' => [ 'type' => 'array', 'items' => [