Automate this function in any way?

Viewed 41
public function onSystemCron(&$logs, &$lastRun, $force)
    {
        $clear_tmp = true;
        $clear_logs = [];
        $log_files = $this->_application->Filter('system_admin_system_logs', []);
        foreach ($log_files as $log_name => $log) {
            $clear_logs[$log_name] = empty($log['days']) ? 365 : $log['days'];
        }
        if (!isset($lastRun[$this->_name])) {
            $lastRun[$this->_name] = [];
        } elseif (!is_array($lastRun[$this->_name])) {
            $lastRun[$this->_name] = [
                'tmp' => $lastRun[$this->_name]
            ];
        }
        if (!$force) {
            if (!empty($lastRun[$this->_name]['tmp'])
                && time() - $lastRun[$this->_name]['tmp'] < 604800
            ) {
                // Less than a week since last run so do not clear tmp
                $clear_tmp = false;
            }
<?php
namespace -\System\Tool;

class RunCronTool extends AbstractTool
{
    protected function _systemToolInfo()
    {
        return [
            'label' => __('Run cron', 'directories'),
            'description' => __('Use this tool to manually run cron.', 'directories'),
            'weight' => 15,
        ];
    }

    public function systemToolRunTask($task, array $settings, $iteration, $total, array &$storage, array &$logs)
    {
        $this->_application->callHelper('System_Cron', [&$logs, true]);

        return 1;
    }
}

I have an issue with a WordPress plugin. It offers a button which will force run cron. Usually, making use of this button is not necessary. I tested my WP Cron and it's working fine, so the plugin is the problem. Any data which is in relation to the plugin does not get updated by WP cron. So I'm forced to click this button manually.

So, is there any solution to automate the function of the button?

I mean, I have to open the plugin settings and click on a button to run Cron. Is there any chance to automate this process?

Can provide more information if needed.

Many thanks.

Yes, I have no idea what I'm doing thanks.

1 Answers

When the OS wants to "kick" WordPress's cron system, it does an HTTP(S) GET to example.com/wp-cron.php .

It's possible to do this from php code, like so.

if ( ! wp_doing_cron() ) {
  $url = get_site_url( null, 'wp-cron.php' );
  $req = new \WP_Http();
  $req->get( $url );
}

Do this last in a page view. I do it from the shutdown hook. It won't hang your page because wp-cron.php uses fastcgi_finish_request() to respond immediately to its client.

This lets you run cron from your code if DISABLE_WP_CRON is true or in other circumstances where it won't get called on its own.

But be careful. Don't use this unless you really need it. It's a bad idea to work around a site owner's need to set DISABLE_WP_CRON.

Related