1

Supposing we have :

class AwesomeClass{
    
    private storedResult;

    private __construct(){
        add_action( 'set_logged_in_cookie', array($this, 'a'),10,1);
        add_action('wp_enqueue_scripts', array($this, 'b')); 
    }

    Function a(p1){
        //some interesting process with p1
        // UPDATE $this->storedResult
    }

    Function b(){
        //some interesting process
        // USAGE OF $this->storedResult
    }
}

How can i be sure to execute the function b when the function a is finished (so the storedResult is updated) ?

EDIT

As you chown, i've tried this :

class IC_Chat {

public $token = null;

public function __construct()
{
    add_action( 'set_logged_in_cookie', array($this, 'ic_askForToken'),10,1);
}

// Triggered when the cookie for intrachat is set or if there were an authentification
public function ic_load_scripts() {     
    wp_register_script( 'socket.IO-client', plugins_url('../js/socket.io.js', __FILE__), array( 'jquery' ), '', true ); // Enqueue thescript in the footer of the page
    wp_register_script( 'ic-client-script', plugins_url('../js/ic-client-script.js', __FILE__), array( 'jquery', 'socket.IO-client' ), '', true ); // Enqueue thescript in the footer of the page
    wp_register_style('ic_css', plugins_url('../css/style.css', __FILE__));
    
    wp_enqueue_script('jquery');
    wp_enqueue_style('ic_css');
    wp_enqueue_script('socket.IO-client');
    wp_enqueue_script('ic-client-script');
    wp_localize_script('ic-client-script', 'wp_include_vars', array(
            'token' => $this->token //$_COOKIE[$this->cookieIcLoggedInName]
        )
    );
}

// Triggered only when the user connect
public function ic_askForToken($cookie){

    //HTTP post request who work
    
    if ( is_wp_error($resp) ) {
        $error_message = $resp->get_error_message();
        echo $resp->get_error_code().' '.$error_message;
    } else {
        switch($resp['response']['code'] ){
            case 200: {
                $token = json_decode( $resp['body'], true );
                setcookie($this->cookieIcLoggedInName, $token["token"], 0, SITECOOKIEPATH, COOKIE_DOMAIN, 1, $secure);
                $this->token = $token['token'];

                //Launch the script
                add_action('wp_enqueue_scripts', array($this,'ic_load_scripts')); // chargement du script
                break;
            }
            .........
            ........
            ....
        }
    }
}   

}

But it will not work, my token value taken will still be null and not the value got by http request.

In fact, by doing this, my script will not load.

5
  • don't think you need to worry, action hooks are still run sequentially... so function a hooked on set_logged_in_cookie is going to run before function b hooked onwp_enqueue_scripts anyway.
    – majick
    Apr 27, 2016 at 9:19
  • Yes, normally, but when my function b is triggered, it will not use the updated value. Apr 27, 2016 at 9:20
  • 1
    erm, shouldn't that be private $storedResult; ? is still a variable...
    – majick
    Apr 27, 2016 at 9:23
  • yes, i've corrected this, but will still don't work, as chown in my edit Apr 28, 2016 at 7:29
  • is the cookie value being set as $token['token']? be sure to clear it then run again to check... if not print_r($token); to be sure there is a value for the key 'token' in token... if you know what i'm token about.
    – majick
    Apr 28, 2016 at 10:40

1 Answer 1

0

There are various ways to do that, e.g. by moving the add_action for method b inside a

class AwesomeClass{

    private storedResult;

    private __construct(){
        add_action( 'set_logged_in_cookie', array($this, 'a'),10,1);
    }

    Function a(p1){
        //some interesting process with p1
        // UPDATE $this->storedResult
        add_action('wp_enqueue_scripts', array($this, 'b')); 
    }

    Function b(){
        //some interesting process
        // USAGE OF $this->storedResult
    }
}
8
  • i updated the post tho show you more specificely my trouble, itstill don't work. Apr 28, 2016 at 7:28
  • so you want it to run even if token is null? Apr 28, 2016 at 9:02
  • no, i don't want it, just when the token isn't null. Apr 28, 2016 at 9:05
  • where is $resp coming from? what does it have? :/ Apr 28, 2016 at 9:09
  • $resp is from a wp_remote_post(), so he have the result of the http request Apr 28, 2016 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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