假设我们有:
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
}
}
如何确保在函数a完成时执行函数b(从而更新storedResult)?
编辑,我已经尝试过了:
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;
}
.........
........
....
}
}
}
}
但它不会工作,我的令牌值仍然是null,而不是http请求获得的值。
事实上,通过这样做,我的脚本将不会加载。