Php 如何在类中使用wp_schedule_single_event()

Php 如何在类中使用wp_schedule_single_event(),php,wordpress,Php,Wordpress,由于某些原因,我无法使用wp\u schedule\u single\u event获取要触发的计划事件 class My_Class { public function __construct () { add_action ( 'after_my_event', array ($this, 'schedule_email_send'), 10, 3 ); add_action ( 'sendanemail', array ($this, 'se

由于某些原因,我无法使用
wp\u schedule\u single\u event
获取要触发的计划事件

class My_Class
{
    public function __construct ()
    {
        add_action ( 'after_my_event', array ($this, 'schedule_email_send'), 10, 3 );
        add_action ( 'sendanemail', array ($this, 'send_an_email'), 10, 1 );
    }

    // 3rd party action hook (fires successfully)
    public function schedule_email_send ( $arg1, $arg2, $arg3 )
    {
        wp_clear_scheduled_hook ( 'sendanemail'), array ($arg1) );
        wp_schedule_single_event ( time () + 60, 'sendanemail'), array ($arg1) );
    }

    // Cron task (is never fired)
    function send_an_email ( $arg1 )
    {
        $got_here = true;        // Breakpoint set

        ... code that sends an email.
    {
}
使用上述代码,第三方WP插件成功地在“我的电子邮件”事件后触发
,并执行
计划电子邮件发送()
,但从未到达
发送电子邮件()
中的断点。我在该例程中设置了一个断点,然后单击站点上的随机页面,但是
sendanemail
操作从未触发。当然,出于测试目的,我希望它能在1分钟内发射


有人知道我做错了什么吗?

这可能是由于在调用add_action函数之前触发了事件

例如,我在“woocommerce\u payment\u gateways”钩子期间创建了一个对象,该事件独立于该钩子触发,因此该事件没有对类、实例或操作的引用。我要么加载类,创建实例,并在每次页面加载时用操作注册它,要么将方法转换为独立于原始类的全局函数