Skip to content

Commit 595193a

Browse files
committed
Init
1 parent 2210139 commit 595193a

4 files changed

Lines changed: 589 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# VanillePlugin-Task
2-
Add Asynchronous Tasks for VanillePlugin
2+
Add Asynchronous Tasks for VanillePlugin

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "jakiboy/vanilleplugin-task",
3+
"version" : "0.1.0",
4+
"type": "library",
5+
"description": "Add Asynchronous Tasks for VanillePlugin",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Jihad Sinnaour",
10+
"email": "mail@jihadsinnaour.com",
11+
"role": "Founder"
12+
},
13+
{
14+
"name": "Softgine",
15+
"email": "e-mailing@soft-gine.com",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": ">=5.6.30",
21+
"jakiboy/vanilleplugin": "^0.5"
22+
},
23+
"autoload": {
24+
"psr-4" : {
25+
"VanillePluginTask\\" : "src/"
26+
}
27+
}
28+
}

src/AbstractAsyncRequest.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* @author : JIHAD SINNAOUR
4+
* @package : VanillePluginTask
5+
* @version : 0.1.0
6+
* @copyright : (c) 2018 - 2021 JIHAD SINNAOUR <mail@jihadsinnaour.com>
7+
* @link : https://jakiboy.github.io/VanillePluginTask/
8+
* @license : MIT
9+
*
10+
* This file if a part of VanillePluginTask Framework
11+
*/
12+
13+
namespace VanillePluginTask\lib;
14+
15+
use VanillePlugin\lib\PluginOptions;
16+
use VanillePlugin\lib\Request;
17+
use VanillePlugin\inc\Stringify;
18+
use VanillePlugin\inc\Server;
19+
20+
abstract class AbstractAsyncRequest extends PluginOptions
21+
{
22+
/**
23+
* @access protected
24+
* @var string $action
25+
* @var string $id
26+
* @var array $data
27+
*/
28+
protected $action = 'async-request';
29+
protected $id;
30+
protected $data = [];
31+
32+
/**
33+
* Initiate new async request
34+
*
35+
* @param void
36+
*/
37+
public function __construct()
38+
{
39+
$this->id = "{$this->getNameSpace()}-{$this->action}";
40+
$this->addAction("wp_ajax_{$this->id}", [$this,'maybeHandle']);
41+
$this->addAction("wp_ajax_nopriv_{$this->id}", [$this,'maybeHandle']);
42+
}
43+
44+
/**
45+
* Set data used during the request
46+
*
47+
* @param array $data
48+
* @return object AbstractAsyncRequest
49+
*/
50+
public function setData($data)
51+
{
52+
$this->data = $data;
53+
return $this;
54+
}
55+
56+
/**
57+
* Dispatch the async request
58+
*
59+
* @param void
60+
* @return array|object
61+
*/
62+
public function dispatch()
63+
{
64+
$url = Request::addQueryArg($this->getQueryArgs(),$this->getQueryUrl());
65+
$req = new Request();
66+
$res = $req->post(Stringify::escapeUrl($url),$this->getPostArgs());
67+
return $res->getBody();
68+
}
69+
70+
/**
71+
* Get request query args
72+
*
73+
* @param void
74+
* @return array
75+
*/
76+
protected function getQueryArgs()
77+
{
78+
return [
79+
'action' => $this->id,
80+
'nonce' => $this->createNonce($this->id)
81+
];
82+
}
83+
84+
/**
85+
* Close session
86+
*
87+
* @param void
88+
* @return void
89+
*/
90+
protected function closeSession()
91+
{
92+
session_write_close();
93+
}
94+
95+
/**
96+
* Get request query URL
97+
*
98+
* @param void
99+
* @return string
100+
*/
101+
protected function getQueryUrl()
102+
{
103+
return $this->getAdminUrl('admin-ajax.php');
104+
}
105+
106+
/**
107+
* Get post request args
108+
*
109+
* @param void
110+
* @return array
111+
*/
112+
protected function getPostArgs()
113+
{
114+
return [
115+
'timeout' => 0.01,
116+
'blocking' => false,
117+
'body' => $this->data,
118+
'cookies' => $_COOKIE,
119+
'sslverify' => Server::isHttps()
120+
];
121+
}
122+
123+
/**
124+
* Check for correct nonce and pass to handler
125+
*
126+
* @param void
127+
* @return void
128+
*/
129+
public function maybeHandle()
130+
{
131+
// Prevent other requests while processing
132+
$this->closeSession();
133+
134+
// Security
135+
$this->checkAjaxReferer($this->id,'nonce');
136+
137+
// Handle request
138+
$this->handle();
139+
die();
140+
}
141+
142+
/**
143+
* Handle request
144+
*
145+
* @param void
146+
* @return mixed
147+
*/
148+
abstract protected function handle();
149+
}

0 commit comments

Comments
 (0)