PluginProbe
Tiered Pricing Table for WooCommerce / 2.1
Tiered Pricing Table for WooCommerce v2.1
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / BackgroundProcessing / AsyncRequest.php

AsyncRequest.php in Tiered Pricing Table for WooCommerce 2.1, at src/BackgroundProcessing/AsyncRequest.php

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