WordPress – Cron任务未执行

huangapple go评论69阅读模式
英文:

WordPress - Cron job not executed

问题

我在WordPress插件中安排自定义cron作业时遇到了一些问题。我在我的类中有以下代码,应该发送多封电子邮件到不同的地址,每封电子邮件都有不同的主题和消息。

在我的测试环境中,我已经激活了插件,但我注意到电子邮件将不会发送,可能是我在代码中漏掉了某些东西。

我需要修改什么以正确地安排每五分钟或每小时运行的cron作业?

英文:

I'm having some problems to schedule a custom cron job in a wordpress plugin.
I have this code inside my class and is supposed to send multiple emails to different adresses and each email will have a different subject and message.


class GreetingTicketScheduler {   

    public function __construct()
    {
        register_activation_hook(__FILE__, [$this, 'schedule_orders_forward']);
        add_action('forward_orders', [$this, 'send_confirmed_orders']);

    }

    public function schedule_orders_forward()
    {
        // Schedules the event if it's NOT already scheduled.
        if(!wp_next_scheduled('forward_orders')){
            wp_schedule_event( time(), '5min', 'send_confirmed_orders', [], true);
        }
    }

    public function send_confirmed_orders()
    {
        global $wpdb;
        $table = $wpdb->prefix . 'order_codes';
        //
        $sql = $wpdb->prepare("SELECT * FROM $table");
        //
        $results = $wpdb->get_results($sql, ARRAY_A);
        //
        if( !empty($results) ){
            //
            $pdv_subject = "Morning greetings of " . date('d-m-Y', time());
            //
            $pdv_message_a .= "Salut!\n";

            $pdv_email_a = 'mail.example@example.org';
            $pdv_headers_a[] = 'Cc: mail.example@example.org';

            //
            $pdv_message_b .= "Ciao!\n";

            $pdv_email_b = 'mail.example@example.org';
            $pdv_headers_b[] = 'Cc: mail.example@example.org';
            
            //
            $pdv_message_p .= "Hi!\n";

            $pdv_email_p = 'mail.example@example.org';
            $pdv_headers_p[] = 'Cc: mail.example@example.org';


            //
            foreach( $results as $key => $val ){
                if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['order_cpco'] === '6605'){
                    $pdv_message_a .= $val['order_file'] . "\n";
                } 
                //
                if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['order_cpco'] === '6200'){
                    $pdv_message_b .= $val['order_file'] . "\n";
                }
                // 
                if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['order_cpco'] === '6600' ){
                    $pdv_message_p .= $val['order_file'] . "\n";
                }
            }            
            //
            //wp_mail( $to:string|array, $subject:string, $message:string, $headers:string|array, $attachments:string|array )
            //
            wp_mail($pdv_email_a, $pdv_subject, $pdv_message_a, $pdv_headers_a);
            //
            wp_mail($pdv_email_b, $pdv_subject, $pdv_message_b, $pdv_headers_b);
            //
            wp_mail($pdv_email_p, $pdv_subject, $pdv_message_p, $pdv_headaers_p);

        }        
    }
}

In my staging envoirment I have the plugin activated but I've noticed that the emails will be not send, probably I've missed something in the code.

What I need to modify to correctly schedule the cron job every five minutes or every hour?

答案1

得分: 2

你需要使用一个动作挂钩来安排定时任务,然后调用函数。

class GreetingTicketScheduler {   

    public function __construct()
    {
        add_action('wp', [$this, 'schedule_orders_forward']);
        add_action('forward_orders', [$this, 'send_confirmed_orders']);
    }

    public function schedule_orders_forward()
    {
        if (!wp_next_scheduled('forward_orders')) {
            wp_schedule_event(time(), 'hourly', 'forward_orders');
        }
    }

    public function send_confirmed_orders()
    {
        // ... 用于发送邮件的代码 ...
    }
}
英文:

You need to use an action hook to schedule the cron job, and then call the function.

class GreetingTicketScheduler {   
public function __construct()
{
add_action('wp', [$this, 'schedule_orders_forward']);
add_action('forward_orders', [$this, 'send_confirmed_orders']);
}
public function schedule_orders_forward()
{
if (!wp_next_scheduled('forward_orders')) {
wp_schedule_event(time(), 'hourly', 'forward_orders');
}
}
public function send_confirmed_orders()
{
// ... code to send emails ...
}
}

huangapple
  • 本文由 发表于 2023年2月8日 16:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定