PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.2
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / background-process / abstract-lp-async-request.php

abstract-lp-async-request.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.2, at inc/background-process/abstract-lp-async-request.php

174 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Abstract LP_Async_Request class.
5 *
6 * @abstract
7 */
8 abstract class LP_Async_Request {
9 /**
10 * Prefix
11 * @var string
12 */
13 protected $prefix = 'lp';
14
15 /**
16 * Action
17 * @var string
18 */
19 protected $action = 'async_request';
20
21 /**
22 * Identifier
23 *
24 * @var string
25 */
26 protected $identifier;
27
28 /**
29 * Data
30 *
31 * (default value: array())
32 *
33 * @var array
34 */
35 protected $data = array();
36
37 /**
38 * Initiate new async request
39 */
40 public function __construct() {
41 $this->identifier = $this->prefix . '_' . $this->action;
42
43 add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
44 add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
45 }
46
47 /**
48 * Set data used during the request
49 *
50 * @param array $data Data.
51 *
52 * @return $this
53 */
54 public function data( $data ) {
55 $this->data = $data;
56
57 return $this;
58 }
59
60 /**
61 * Dispatch the async request
62 *
63 * @return array|WP_Error
64 */
65 public function dispatch() {
66 $url = esc_url_raw( add_query_arg( $this->get_query_args(), $this->get_query_url() ) );
67 $args = $this->get_post_args();
68
69 return wp_remote_post( $url, $args );
70 }
71
72 /**
73 * Get query args
74 *
75 * @return array
76 */
77 protected function get_query_args() {
78 if ( property_exists( $this, 'query_args' ) ) {
79 return $this->query_args;
80 }
81
82 $args = array(
83 'action' => $this->identifier,
84 'nonce' => wp_create_nonce( $this->identifier ),
85 );
86
87 /**
88 * Filters the post arguments used during an async request.
89 *
90 * @param array $url
91 */
92 return apply_filters( $this->identifier . '_query_args', $args );
93 }
94
95 /**
96 * Get query URL
97 *
98 * @return string
99 */
100 protected function get_query_url() {
101 if ( property_exists( $this, 'query_url' ) ) {
102 return $this->query_url;
103 }
104
105 $url = admin_url( 'admin-ajax.php' );
106
107 /**
108 * Filters the post arguments used during an async request.
109 *
110 * @param string $url
111 */
112 return apply_filters( $this->identifier . '_query_url', $url );
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 $args = array(
126 'timeout' => 0.01,
127 'blocking' => false,
128 'body' => $this->data,
129 'cookies' => $_COOKIE,
130 'sslverify' => is_ssl(),
131 );
132
133 /**
134 * Filters the post arguments used during an async request.
135 *
136 * @param array $args
137 */
138 return apply_filters( $this->identifier . '_post_args', $args );
139 }
140
141 /**
142 * Maybe handle
143 *
144 * Check for correct nonce and pass to handler.
145 */
146 public function maybe_handle() {
147 // Don't lock up other requests while processing
148 session_write_close();
149
150 /**
151 * set params $_POST['lp_no_check_referer'] = 1
152 * for case: send request when user not login, but get request when user logged
153 * @editor tungnx
154 * @modify 4.1.4
155 */
156 if ( ! isset( $_POST['lp_no_check_referer'] ) ) {
157 check_ajax_referer( $this->identifier, 'nonce' );
158 }
159
160 $this->handle();
161
162 wp_die();
163 }
164
165 /**
166 * Handle
167 *
168 * Override this method to perform any actions required
169 * during the async request.
170 */
171 abstract protected function handle();
172
173 }
174