在自定义插件中设置wp_Schedule_Event

时间:2018-05-26 作者:Carol.Kar

我正在使用WordPress 4.9.6 我正在尝试创建一个插件,从外部API提取数据。

请参见以下我的当前代码:

myPlugin.php

<?php
/**
Plugin Name: myPlugin
description: Get Data via APIs
Version: 1.0
Author: Batman
License: GPLv2 or later
Text Domain: myPlugin
*/

if ( ! defined( \'ABSPATH\' ) ) {
    exit;
}

require_once \'vendor/autoload.php\';

class My_Plugin {

      /**
     * Constructor.
     */
    public function __construct() {
        define( \'MyPlugin_FILE\', __FILE__ );
        define( \'MyPlugin_DIR\', trailingslashit( dirname( __FILE__ ) ) );
        define( \'MyPlugin_VERSION\', \'0.0.1\' );
        register_activation_hook( basename( MyPlugin_DIR ) . \'/\' . basename( MyPlugin_FILE ), array( $this, \'activate\' ) );
        add_action( \'plugins_loaded\', array( $this, \'includes\' ) );
        add_action( \'init\', array( $this, \'maybe_update\' ) );
    }

    public function activate() {
        $this->includes();
        flush_rewrite_rules();
    }

    public function includes() {
        include_once( MyPlugin_DIR . \'includes/WhatToMineAPI.php\' );
    }

    /**
     * Maybe update MyPlugin.
     */
    public function maybe_update() {
        $version = get_option( \'MyPlugin_version\', 0 );
        if ( version_compare( $version, MyPlugin_VERSION, \'<\' ) ) {
            $this->create_tables();
            update_option( \'MyPlugin_version\', MyPlugin_VERSION );
        }
    }
}
new My_Plugin();

WhatToMineAPI.php

<?php

use GuzzleHttp\\Client;

class WhatToMineAPI {

    /**
     * Constructor.
     */
    public function __construct() {
        add_action( \'setupCronJob_whatToMine\', \'setupCronJob\');
        add_action( \'update_whatToMine_api\', \'updateWhatToMineAPI\');

        // variables
        $whatToMineURL = "http://whattomine.com/coins.json";
    }

    public function setupCronJob() {
        //Use wp_next_scheduled to check if the event is already scheduled
        $timestamp = wp_next_scheduled( \'update_whatToMine_api\' );

        //If $timestamp == false schedule daily backups since it hasn\'t been done previously
        if( $timestamp == false ){
            //Schedule the event for right now, then to repeat daily using the hook \'update_whatToMine_api\'
            wp_schedule_event( time(), \'twicedaily\', \'update_whatToMine_api\' );
        }
    }

    public function updateWhatToMineAPI() {

        $client = new GuzzleHttp\\Client();

        $response = $client->request(\'GET\', $whatToMineURL);

    }


}
new WhatToMineAPI();
我已经安装了插件WP-Control 了解我正在运行的cron作业的概况。然而,我的cron工作update_whatToMine_api 在表中找不到?

enter image description here

我当前的cron设置是否错误?

感谢您的回复!

1 个回复
最合适的回答,由SO网友:kero 整理而成

首先:如果你还没有读过,请检查official documentation on WP-Cron.

代码中的问题是这一行

    add_action( \'setupCronJob_whatToMine\', \'setupCronJob\');
您正在定义新操作setupCronJob_whatToMine 并添加setupCronJob 方法。但由于这是您刚刚创建的自定义操作,因此永远不会调用它(例如通过do_action(\'setupCronJob_whatToMine\');).

因此,您可以调用setupCronJob() 直接设置事件调度。

你可以在init 行动但是,这将为每个页面加载添加逻辑,这是不必要的。类cron只需设置一次。

所以我要在activation hook (不要忘记将其从停用挂钩中拆下)。

Simple code example:

MyPlugin.php

register_activation_hook( __FILE__, array(My_Plugin::class, \'activate\') );
register_deactivation_hook( __FILE__, array(My_Plugin::class, \'deactivate\') );

class MyPlugin
{

    public static function activate()
    {
        WhatToMineAPI::setupCronJob();
    }

    public static function deactivate()
    {
        WhatToMineAPI::unsetCronJob();
    }

    // your other methods

}

WhatToMineAPI.php

class WhatToMineAPI
{

    const CRON_HOOK = \'update_whatToMine_api\';

    public static function setupCronJob()
    {
        //Use wp_next_scheduled to check if the event is already scheduled
        $timestamp = wp_next_scheduled( self::CRON_HOOK );

        //If $timestamp === false schedule daily backups since it hasn\'t been done previously
        if( $timestamp === false ){
            //Schedule the event for right now, then to repeat daily using the hook \'update_whatToMine_api\'
            wp_schedule_event( time(), \'twicedaily\', self::CRON_HOOK );
        }
    }

    public static function unsetCronJob()
    {
        // Get the timestamp for the next event.
        $timestamp = wp_next_scheduled( self::CRON_HOOK );
        wp_unschedule_event( $timestamp, self::CRON_HOOK );
    }

    public function __construct()
    {
        add_action(self::CRON_HOOK, array($this, \'updateWhatToMineAPI\'));
    }

    public function updateWhatToMineAPI()
    {
        $client = new GuzzleHttp\\Client();
        $response = $client->request(\'GET\', $whatToMineURL);
    }

}
旁注:在上课时,你需要小心。要么使用add_action(\'...\', array($this, \'methodName\')) 但是你需要$this 或类似定义。

或者,您可以使用static 像这样的方法add_action(\'...\', array(MySuperClass::class, \'staticMethodName\'))

结束

相关推荐

如何强制admin-ajax.php文件通过HTTPS加载?

我最近有一个网站100%支持HTTPS。前端和后端的所有资产都通过HTTPS成功调用,除了admin ajax。php。这会导致任何依赖于文件的功能失败,包括但不限于CF7、Elementor和其他使用AJAX提交的表单等。我得到的错误,你可以在截图中看到是“混合内容:页面位于”https://toursoft.co/contact-us/\' 已通过HTTPS加载,但请求了不安全的XMLHttpRequest终结点\'http://toursoft.co/wp-admin/admin-ajax.php\