: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace Smush\Core\Modules\Background;
* Abstract WP_Async_Request class.
abstract class Async_Request {
* (default value: array())
protected $data = array();
* Initiate new async request
public function __construct( $identifier ) {
$this->identifier = $identifier;
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.
public function data( $data ) {
* Dispatch the async request
* @param int $instance_id
* @return array|\WP_Error
public function dispatch( $instance_id ) {
$query_args = $this->get_query_args( $instance_id );
$url = add_query_arg( $query_args, $this->get_query_url() );
$args = $this->get_post_args();
return wp_remote_post( esc_url_raw( $url ), $args );
protected function get_query_args( $instance_id ) {
if ( property_exists( $this, 'query_args' ) ) {
return $this->query_args;
'action' => $this->identifier,
'nonce' => wp_create_nonce( $this->identifier ),
'instance_id' => $instance_id,
protected function get_query_url() {
if ( property_exists( $this, 'query_url' ) ) {
return admin_url( 'admin-ajax.php' );
protected function get_process_headers() {
if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
return apply_filters( $this->identifier . '_process_headers', $headers );
protected function get_post_args() {
if ( property_exists( $this, 'post_args' ) ) {
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
$headers = $this->get_process_headers();
if ( ! empty( $headers ) ) {
$post_args['headers'] = $headers;
* Check for correct nonce and pass to handler.
public function maybe_handle() {
// Don't lock up other requests while processing
check_ajax_referer( $this->identifier, 'nonce' );
$instance_id = empty( $_GET['instance_id'] )
: sanitize_key( $_GET['instance_id'] );
$this->handle( $instance_id );
* Override this method to perform any actions required
* during the async request.
abstract protected function handle( $instance_id );