-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync.php
More file actions
161 lines (109 loc) · 3.29 KB
/
Async.php
File metadata and controls
161 lines (109 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Helper for background process
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate\Process;
use Throwable;
class Async {
/**
* @var array<string, int>
*/
private static array $storage = array();
private string $identifier;
/**
* @var callable
*/
private $callback_func;
/**
* @var array<int, mixed>
*/
private array $callback_args;
/**
* @var ?callable
*/
private $success_callback;
/**
* @var ?callable
*/
private $error_callback;
/**
* @var mixed
*/
private $success_output;
private string $error_output = '';
/** @param array<int, mixed> $callback_args */
public function __construct( callable $callback_func, array $callback_args = array() ) {
$this->callback_func = $callback_func;
$this->callback_args = $callback_args;
$this->generate_identifier();
add_action( 'wp_ajax_' . $this->identifier, array( $this, 'handle' ) );
add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'handle' ) );
}
private function generate_identifier(): void {
$cb_func = print_r( $this->callback_func, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
$cb_args = print_r( $this->callback_args, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
$this->identifier = 'tpa_' . md5( $cb_func . $cb_args );
$base_identifier = $this->identifier;
if ( array_key_exists( $this->identifier, self::$storage ) ) {
$count = self::$storage[ $this->identifier ] + 1;
$this->identifier .= '_' . $count;
self::$storage[ $base_identifier ] = $count;
}
self::$storage[ $this->identifier ] = 0;
}
public function get_identifier(): string {
return $this->identifier;
}
public function handle(): void {
session_write_close();
if ( wp_verify_nonce( $_REQUEST['nonce'], $this->identifier ) ) {
try {
$this->success_output = call_user_func_array( $this->callback_func, $this->callback_args );
} catch ( Throwable $e ) {
$this->error_output = $e->getMessage();
} finally {
$this->trigger();
}
}
wp_die();
}
public function dispatch( ?string $custom_url = null ): bool {
// phpcs:ignore Universal.Operators.DisallowShortTernary
$post_url = $custom_url ?: admin_url( 'admin-ajax.php' );
$post_args = array(
'timeout' => defined( 'PROCESS_ASYNC_TIMEOUT' ) ? PROCESS_ASYNC_TIMEOUT : 1,
'blocking' => false,
'body' => array(
'action' => $this->identifier,
'nonce' => wp_create_nonce( $this->identifier ),
'data' => array(
'callback_func' => $this->callback_func,
'callback_args' => $this->callback_args,
),
),
'cookies' => $_COOKIE,
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
);
$response = wp_remote_post( esc_url_raw( $post_url ), $post_args );
return is_wp_error( $response );
}
public function then( callable $callback ): Async {
$this->success_callback = $callback;
return $this;
}
public function catch( callable $callback ): Async {
$this->error_callback = $callback;
return $this;
}
private function trigger(): void {
if ( $this->error_callback && $this->error_output ) {
call_user_func( $this->error_callback, $this->error_output );
}
if ( $this->success_callback && $this->success_output ) {
call_user_func( $this->success_callback, $this->success_output );
}
}
}