diff --git a/.gitignore b/.gitignore index d083191..067cefc 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,6 @@ htdocs/wp-content/themes/* !htdocs/wp-content/themes/livecarta !htdocs/wp-content/themes/index.php +!htdocs/wp-content/plugins/wp-rest-api-event + vendor/ diff --git a/environments/dev/wp-config-local.php b/environments/dev/wp-config-local.php index cf31e18..cae4aeb 100644 --- a/environments/dev/wp-config-local.php +++ b/environments/dev/wp-config-local.php @@ -10,3 +10,5 @@ define('DOMAIN_CURRENT_SITE', 'dev1.lawcarta.com'); define('LAWCARTA_SUBDOMAIN', 'app-dev1'); define('LAWCARTA_PORT', ''); +define('LAWCARTA_WP_REST_API_REQUEST_URL', 'https://app-dev1.lawcarta.com/v1/event/request'); +define('LAWCARTA_WP_REST_API_RESPONSE_URL', 'https://app-dev1.lawcarta.com/v1/event/response'); diff --git a/environments/local/wp-config-local.php b/environments/local/wp-config-local.php index 70dff55..faa14c0 100644 --- a/environments/local/wp-config-local.php +++ b/environments/local/wp-config-local.php @@ -10,3 +10,5 @@ define('DOMAIN_CURRENT_SITE', 'lawcarta.loc'); define('LAWCARTA_SUBDOMAIN', 'app'); define('LAWCARTA_PORT', ''); +define('LAWCARTA_WP_REST_API_REQUEST_URL', 'https://app-dev1.lawcarta.com/v1/event/request'); +define('LAWCARTA_WP_REST_API_RESPONSE_URL', 'https://app-dev1.lawcarta.com/v1/event/response'); \ No newline at end of file diff --git a/htdocs/wp-content/plugins/wp-rest-api-event/HttpClient.php b/htdocs/wp-content/plugins/wp-rest-api-event/HttpClient.php new file mode 100644 index 0000000..29051cb --- /dev/null +++ b/htdocs/wp-content/plugins/wp-rest-api-event/HttpClient.php @@ -0,0 +1,63 @@ +doPostRequest(LAWCARTA_WP_REST_API_REQUEST_URL, $data); + } + + /** + * @param $sysLogId + * @param $data + * @return string|\WP_Error + */ + public function sendResponse($sysLogId, $data) + { + $data['sysLogId'] = $sysLogId; + return $this->doPostRequest(LAWCARTA_WP_REST_API_RESPONSE_URL, $data); + } + + /** + * @param $url + * @param $data + * @return string|\WP_Error + */ + private function doPostRequest($url, $data) + { + $response = wp_remote_post($url, [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'body' => json_encode($data), + 'sslverify' => false + + ]); + $sysLogId = null; + /** @var \WP_HTTP_Requests_Response $httpResponse */ + $httpResponse = $response['http_response']; + if ($httpResponse->get_status() == 200) { + $body = isset($response['body']) ? json_decode($response['body'], true) : null; + $sysLogId = $body['sysLogId'] ?? null; + } + + return $sysLogId; + } + + /** + * @return HttpClient + */ + public static function create() + { + return new self(); + } +} \ No newline at end of file diff --git a/htdocs/wp-content/plugins/wp-rest-api-event/event/EventSubscriber.php b/htdocs/wp-content/plugins/wp-rest-api-event/event/EventSubscriber.php new file mode 100644 index 0000000..be1d323 --- /dev/null +++ b/htdocs/wp-content/plugins/wp-rest-api-event/event/EventSubscriber.php @@ -0,0 +1,124 @@ +sendRequest(self::getSuperGlobals()); + } + + public static function onResponse(array $response, array $r, $url) + { + // todo: add response handler + } + + public static function onParseRequest() + { + // todo: add request data from wp + } + + public static function onShutdown() + { + $client = HttpClient::create(); + $client->sendResponse(self::$parentSysLogId, array_merge(self::getSuperGlobals(), [ + 'headers_list' => headers_list(), + 'response_code' => http_response_code() + ])); + } + + public static function setGlobals() + { + self::$get = $_GET; + self::$post = $_POST; + self::$cookie = $_COOKIE; + self::$session = $_SESSION; + self::$env = $_ENV; + self::$request = $_REQUEST; + self::$server = $_SERVER; + + if (function_exists('getallheaders')) { + self::$headers = getallheaders(); + } elseif (function_exists('http_get_request_headers')) { + self::$headers = http_get_request_headers(); + } + } + + /** + * @return array + */ + private static function getSuperGlobals() + { + return [ + 'request' => self::$request, + 'server' => self::$server, + 'get' => self::$get, + 'post' => self::$post, + 'cookie' => self::$cookie, + 'env' => self::$env, + 'headers' => self::$headers + ]; + } +} \ No newline at end of file diff --git a/htdocs/wp-content/plugins/wp-rest-api-event/wp-rest-api-event.php b/htdocs/wp-content/plugins/wp-rest-api-event/wp-rest-api-event.php new file mode 100644 index 0000000..b762798 --- /dev/null +++ b/htdocs/wp-content/plugins/wp-rest-api-event/wp-rest-api-event.php @@ -0,0 +1,28 @@ +