0

I have some problem with the wp-background-processing that's when I change the $url in another file using $this->process_single->url('https://anotherexample.com'); the url won't update in handle function :/ but when I echo it in dispatch, it shows the updated url

here is my class-example-request.php file:

<?php

class WP_Example_Request extends WP_Async_Request {

    /**
     * @var string
     */
    protected $action = 'example_request';
    protected $url = "https://example.com";

    public function url($url){
        $this->url = $url;
        return $this;
    }

    public function get_url(){
        return $this->url;
    }

    public function handle() {
        $response = wp_remote_get( esc_url_raw( $this->get_url() ) );//here, the url won't update!
    }

    public function dispatch() {
        $url  = add_query_arg( $this->get_query_args(), $this->get_query_url() );
        $args = $this->get_post_args();
        echo $this->get_url(); // this echos the updated url
        return wp_remote_post( esc_url_raw( $url ), $args );
    }
}

any ideas?

update:

This is my main plugin file

<?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->url('https://httpbin.org/anything');

        $this->process_single->data( array() );
        $this->process_single->dispatch();
    }

}
new Example_Background_Processing();
10
  • When are you updating the URL? If you're updating the URL in a separate request then the URL isn't going to be updated for subsequent requests. If you want the change to be persistent you need to write it to and read it from the database. Sep 20 at 14:26
  • I've changed it before I called the dispatch function in another file. Sep 20 at 14:34
  • Can you share an example? Sep 20 at 14:41
  • @JacobPeattie I've updated the question Sep 20 at 14:50
  • Is there a reason you’re redefining the dispatch method? That’s not how this library is supposed to be used, going off the documentation anyway. The way you’ve written it it’s not async anymore. Sep 20 at 15:03

2 Answers 2

1

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.

-1

You need to change the method use for the variable $url

  • public - the property or method can be accessed from everywhere. This is default

  • protected - the property or method can be accessed within the class and by classes derived from that class

  • private - the property or method can ONLY be accessed within the class

In your case your script is not allowed to modify a protected variable. Change it to public.

1
  • I appreciate your answer but I used functions to change the protected values and that's a convenient way, but I've changed them to public and nothing has changed. keep in mind that the $url shows correctly when I echo it on dispatch function! Sep 20 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.