<?php namespace Tussendoor\Reloadify\Controllers;

use ActionScheduler;
use Tussendoor\Reloadify\Plugin;

class ScheduleController extends Controller
{

    public function register()
    {
        $this->addActions();
    }

    public function addActions()
    {
        add_action('init', [$this, 'scheduleRecurringPluginActions']);
    }

    /**
     * Get our recurring plugin tasks from the dedicated config file
     * Loop over these tasks to schedule them
     * 
     * @see /config/tasks.php
     */
    public function scheduleRecurringPluginActions() {

        if (!ActionScheduler::is_initialized()) return;

        $tasks = Plugin::tasks('recurring');

        foreach ($tasks as $group => $data) {
            if (false === as_has_scheduled_action($data['action'])) {
                ActionScheduler::factory()->recurring($data['action'], $data['args'], $data['timestamp'], $data['interval'], $group);
            }
        }
    }
}