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