|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WP Async Request |
| 4 | + * |
| 5 | + * @package WP-Background-Processing |
| 6 | + */ |
| 7 | + |
| 8 | +// phpcs:disable Generic.Commenting.DocComment.MissingShort |
| 9 | +/** @noinspection PhpIllegalPsrClassPathInspection */ |
| 10 | +/** @noinspection AutoloadingIssuesInspection */ |
| 11 | +// phpcs:disable Generic.Commenting.DocComment.MissingShort |
| 12 | + |
| 13 | +/** |
| 14 | + * Abstract WPT_WP_Async_Request class. |
| 15 | + * |
| 16 | + * @abstract |
| 17 | + */ |
| 18 | +abstract class WPT_WP_Async_Request { |
| 19 | + |
| 20 | + /** |
| 21 | + * Prefix |
| 22 | + * |
| 23 | + * (default value: 'wp') |
| 24 | + * |
| 25 | + * @var string |
| 26 | + * @access protected |
| 27 | + */ |
| 28 | + protected $prefix = 'wp'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Action |
| 32 | + * |
| 33 | + * (default value: 'async_request') |
| 34 | + * |
| 35 | + * @var string |
| 36 | + * @access protected |
| 37 | + */ |
| 38 | + protected $action = 'async_request'; |
| 39 | + |
| 40 | + /** |
| 41 | + * Identifier |
| 42 | + * |
| 43 | + * @var mixed |
| 44 | + * @access protected |
| 45 | + */ |
| 46 | + protected $identifier; |
| 47 | + |
| 48 | + /** |
| 49 | + * Data |
| 50 | + * |
| 51 | + * (default value: array()) |
| 52 | + * |
| 53 | + * @var array |
| 54 | + * @access protected |
| 55 | + */ |
| 56 | + protected $data = array(); |
| 57 | + |
| 58 | + /** |
| 59 | + * Initiate new async request. |
| 60 | + */ |
| 61 | + public function __construct() { |
| 62 | + $this->identifier = $this->prefix . '_' . $this->action; |
| 63 | + |
| 64 | + add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
| 65 | + add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Set data used during the request. |
| 70 | + * |
| 71 | + * @param array $data Data. |
| 72 | + * |
| 73 | + * @return $this |
| 74 | + */ |
| 75 | + public function data( $data ) { |
| 76 | + $this->data = $data; |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Dispatch the async request. |
| 83 | + * |
| 84 | + * @return array|WP_Error|false HTTP Response array, WP_Error on failure, or false if not attempted. |
| 85 | + */ |
| 86 | + public function dispatch() { |
| 87 | + $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
| 88 | + $args = $this->get_post_args(); |
| 89 | + |
| 90 | + return wp_remote_post( esc_url_raw( $url ), $args ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get query args. |
| 95 | + * |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + protected function get_query_args() { |
| 99 | + if ( property_exists( $this, 'query_args' ) ) { |
| 100 | + return $this->query_args; |
| 101 | + } |
| 102 | + |
| 103 | + $args = array( |
| 104 | + 'action' => $this->identifier, |
| 105 | + 'nonce' => wp_create_nonce( $this->identifier ), |
| 106 | + ); |
| 107 | + |
| 108 | + /** |
| 109 | + * Filters the post arguments used during an async request. |
| 110 | + * |
| 111 | + * @param array $url |
| 112 | + */ |
| 113 | + return apply_filters( $this->identifier . '_query_args', $args ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get query URL. |
| 118 | + * |
| 119 | + * @return string |
| 120 | + */ |
| 121 | + protected function get_query_url() { |
| 122 | + if ( property_exists( $this, 'query_url' ) ) { |
| 123 | + return $this->query_url; |
| 124 | + } |
| 125 | + |
| 126 | + $url = admin_url( 'admin-ajax.php' ); |
| 127 | + |
| 128 | + /** |
| 129 | + * Filters the post arguments used during an async request. |
| 130 | + * |
| 131 | + * @param string $url |
| 132 | + */ |
| 133 | + return apply_filters( $this->identifier . '_query_url', $url ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get post args. |
| 138 | + * |
| 139 | + * @return array |
| 140 | + */ |
| 141 | + protected function get_post_args() { |
| 142 | + if ( property_exists( $this, 'post_args' ) ) { |
| 143 | + return $this->post_args; |
| 144 | + } |
| 145 | + |
| 146 | + $args = array( |
| 147 | + 'timeout' => 5, |
| 148 | + 'blocking' => false, |
| 149 | + 'body' => $this->data, |
| 150 | + 'cookies' => $_COOKIE, // Passing cookies ensures request is performed as initiating user. |
| 151 | + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // Local requests, fine to pass false. |
| 152 | + ); |
| 153 | + |
| 154 | + /** |
| 155 | + * Filters the post arguments used during an async request. |
| 156 | + * |
| 157 | + * @param array $args |
| 158 | + */ |
| 159 | + return apply_filters( $this->identifier . '_post_args', $args ); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Maybe handle a dispatched request. |
| 164 | + * |
| 165 | + * Check for correct nonce and pass to handler. |
| 166 | + * |
| 167 | + * @return void|mixed |
| 168 | + */ |
| 169 | + public function maybe_handle() { |
| 170 | + // Don't lock up other requests while processing. |
| 171 | + session_write_close(); |
| 172 | + |
| 173 | + check_ajax_referer( $this->identifier, 'nonce' ); |
| 174 | + |
| 175 | + $this->handle(); |
| 176 | + |
| 177 | + return $this->maybe_wp_die(); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Should the process exit with wp_die? |
| 182 | + * |
| 183 | + * @param mixed $return What to return if filter says don't die, default is null. |
| 184 | + * |
| 185 | + * @return void|mixed |
| 186 | + * @noinspection ForgottenDebugOutputInspection |
| 187 | + */ |
| 188 | + protected function maybe_wp_die( $return = null ) { |
| 189 | + /** |
| 190 | + * Should wp_die be used? |
| 191 | + * |
| 192 | + * @return bool |
| 193 | + */ |
| 194 | + if ( apply_filters( $this->identifier . '_wp_die', true ) ) { |
| 195 | + wp_die(); |
| 196 | + } |
| 197 | + |
| 198 | + return $return; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Handle a dispatched request. |
| 203 | + * |
| 204 | + * Override this method to perform any actions required |
| 205 | + * during the async request. |
| 206 | + */ |
| 207 | + abstract protected function handle(); |
| 208 | +} |
0 commit comments