A huge thanks to @JacobPeattie
We should pass the url to data
function and retrieve it using _POST
so the main file will be:
<?php
/*
Plugin info...
*/
class Example_Background_Processing {
protected $process_single;
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ) );
add_action( 'woocommerce_add_to_cart', array($this, 'add_to_cart_callback'), 10, 2);
}
public function init() {
require_once plugin_dir_path( __FILE__ ) . 'async-requests/class-example-request.php';
$this->process_single = new WP_Example_Request();
}
public function add_to_cart_callback($cart_item_data, $productId){
$this->process_single->data( array('url'=>'https://httpbin.org/anything') )->dispatch();
}
}
new Example_Background_Processing();
and the class-example-request.php
file will be:
<?php
class WP_Example_Request extends WP_Async_Request {
/**
* @var string
*/
protected $action = 'example_request';
public function handle() {
$url = $_POST['url'];
$response = wp_remote_get( esc_url_raw( $url );
}
}
if the request has payloads, we could pass it like the url.
@JacobPeattie feel free to edit the answer.