PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / trunk
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP vtrunk
1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 All 172 releases
userswp / includes / libraries / wp-async-request.php

wp-async-request.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP trunk, at includes/libraries/wp-async-request.php

159 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Async Request
4 */
5
6 defined( 'ABSPATH' ) || exit;
7
8 /**
9 * Abstract WP_Async_Request class.
10 */
11 abstract class WP_Async_Request {
12
13 /**
14 * Prefix
15 *
16 * (default value: 'wp')
17 *
18 * @var string
19 * @access protected
20 */
21 protected $prefix = 'wp';
22
23 /**
24 * Action
25 *
26 * (default value: 'async_request')
27 *
28 * @var string
29 * @access protected
30 */
31 protected $action = 'async_request';
32
33 /**
34 * Identifier
35 *
36 * @var mixed
37 * @access protected
38 */
39 protected $identifier;
40
41 /**
42 * Data
43 *
44 * (default value: array())
45 *
46 * @var array
47 * @access protected
48 */
49 protected $data = array();
50
51 /**
52 * Initiate new async request
53 */
54 public function __construct() {
55 $this->identifier = $this->prefix . '_' . $this->action;
56
57 add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
58 add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
59 }
60
61 /**
62 * Set data used during the request
63 *
64 * @param array $data Data.
65 *
66 * @return $this
67 */
68 public function data( $data ) {
69 $this->data = $data;
70
71 return $this;
72 }
73
74 /**
75 * Dispatch the async request
76 *
77 * @return array|WP_Error
78 */
79 public function dispatch() {
80 $url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
81 $args = $this->get_post_args();
82
83 return wp_remote_post( esc_url_raw( $url ), $args );
84 }
85
86 /**
87 * Get query args
88 *
89 * @return array
90 */
91 protected function get_query_args() {
92 if ( property_exists( $this, 'query_args' ) ) {
93 return $this->query_args;
94 }
95
96 return array(
97 'action' => $this->identifier,
98 'nonce' => wp_create_nonce( $this->identifier ),
99 );
100 }
101
102 /**
103 * Get query URL
104 *
105 * @return string
106 */
107 protected function get_query_url() {
108 if ( property_exists( $this, 'query_url' ) ) {
109 return $this->query_url;
110 }
111
112 return admin_url( 'admin-ajax.php' );
113 }
114
115 /**
116 * Get post args
117 *
118 * @return array
119 */
120 protected function get_post_args() {
121 if ( property_exists( $this, 'post_args' ) ) {
122 return $this->post_args;
123 }
124
125 return array(
126 'timeout' => 0.01,
127 'blocking' => false,
128 'body' => $this->data,
129 'cookies' => $_COOKIE,
130 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
131 );
132 }
133
134 /**
135 * Maybe handle
136 *
137 * Check for correct nonce and pass to handler.
138 */
139 public function maybe_handle() {
140 // Don't lock up other requests while processing
141 session_write_close();
142
143 check_ajax_referer( $this->identifier, 'nonce' );
144
145 $this->handle();
146
147 wp_die();
148 }
149
150 /**
151 * Handle
152 *
153 * Override this method to perform any actions required
154 * during the async request.
155 */
156 abstract protected function handle();
157
158 }
159