started with wp-rest-api-event plugin

This commit is contained in:
Vitaly Strizhenok
2019-10-18 11:55:56 +03:00
parent d7fe90d11f
commit 33d9028868
6 changed files with 221 additions and 0 deletions

2
.gitignore vendored
View File

@@ -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/

View File

@@ -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');

View File

@@ -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');

View File

@@ -0,0 +1,63 @@
<?php
namespace app\wpRestApiEvent;
/**
* Class HttpClient
* @package app\wpRestApiEvent
*/
class HttpClient
{
/**
* @param $data
* @return null|string
*/
public function sendRequest($data)
{
return $this->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();
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace app\wpRestApiEvent\event;
use app\wpRestApiEvent\HttpClient;
/**
* Class EventSubscriber
* @package app\wpRestApiEvent\event
*/
class EventSubscriber
{
/**
* @var array
*/
private static $request;
/**
* @var array
*/
private static $get;
/**
* @var array
*/
private static $post;
/**
* @var array
*/
private static $cookie;
/**
* @var array
*/
private static $session;
/**
* @var array
*/
private static $env;
/**
* @var array
*/
private static $headers;
/**
* @var array
*/
private static $server;
/**
* @var string
*/
private static $parentSysLogId;
public static function onInit()
{
apply_filters('parse_request', [self::class, 'onParseRequest']);
apply_filters('http_response', [self::class, 'onResponse'], 0);
//apply_filters('shutdown', [self::class, 'onResponse'], 0);
register_shutdown_function([self::class, 'onShutdown']);
apply_filters('pre_http_request', [self::class, 'onRequest'], 0);
self::onRequest();
}
public static function onRequest()
{
$client = HttpClient::create();
self::$parentSysLogId = $client->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
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
Plugin Name: WP-REST-API Event
Version: 0.0.1
Description:
Author:
*/
function wp_rest_api_event_register() {
spl_autoload_register(function($class) {
$namespace = 'app\\wpRestApiEvent\\';
if (false === strpos($class, $namespace)) {
return false;
}
$filename = __DIR__ . '/' . str_replace('\\', '/', str_replace($namespace, '', $class)) . '.php';
if (!file_exists($filename)) {
return false;
}
include $filename;
return true;
});
\app\wpRestApiEvent\event\EventSubscriber::setGlobals();
add_action('init', [\app\wpRestApiEvent\event\EventSubscriber::class, 'onInit']);
}
wp_rest_api_event_register();