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