Automate Function / Create a Hook for Cron Job Task (WordPress)

Viewed 20

I am using a Plugin in Wordpress which has a Button integrated. I want to schedule the function of the button into a Cron Job, which will execute the function of the button automatically so i dont have to click that button.

For that, im trying to use another Plugin (WP Cron Jobs or something). Through this Plugin, im able to add a new task via Hook or PHP. But I cant find the right snippet of code to enter.

The code I have of the function i want to automate:

    namespace SabaiApps\Directories\Component\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;
        }
    }

and

namespace SabaiApps\Directories\Component\System\Helper;

use SabaiApps\Directories\Application;

class CronHelper
{
    public function help(Application $application, array &$logs = null, $force)
    {
        // Init progress
        if (!isset($logs)) {
            $logs = ['success' => [], 'error' => [], 'warning' => [], 'info' => []];
            $log = true;
        }
        $logs['info'][] = __('Running cron...', 'directories');
        // Get timestamp of last cron
        $last_run = $application->getPlatform()->getOption('system_cron_last');
        if (!is_array($last_run)) {
            $last_run = ['' => time()];
        } else {
            $logs['info'][] = sprintf(
                __('Cron was last run at %s', 'directories'),
                $application->System_Date_datetime($last_run[''])
            );
        }
        // Invoke cron
        $application->Action('system_cron', [&$logs, &$last_run, $force]);
        // Save timestamp
        $application->getPlatform()->setOption('system_cron_last', $last_run);
        // Log
        if (!empty($log)) {
            foreach (array_keys($logs) as $level) {
                foreach ((array)$logs[$level] as $log) {
                    switch ($level) {
                        case 'success':
                            $application->logDebug(strip_tags($log));
                            break;
                        case 'info':
                        case 'notice':
                            $application->logNotice(strip_tags($log));
                            break;
                        case 'warning':
                            $application->logWarning(strip_tags($log));
                            break;
                        case 'error':
                            $application->logError(strip_tags($log));
                            break;
                        default:
                    }
                }
            }
        }
    }
}

More background information: That Button force runs a Cron Job. And I want to Create a Cron Job, which runs the 'force Cron Job' function.

Let me know if you need more information.

Many thanks

0 Answers
Related