HEX
Server: Apache/2.4.65 (Debian)
System: Linux 88f31f35b0b8 6.1.0-38-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.147-1 (2025-08-02) x86_64
User: www-data (33)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/adnkronos/src/Plugin.php
<?php

namespace AdnKronos;

use AdnKronos\Admin\AccountPage;
use AdnKronos\Admin\AdminNotice;
use AdnKronos\Admin\SettingsPage;
use AdnKronos\Api\AdnkApiClient;
use AdnKronos\Api\WpHttpClient;
use AdnKronos\Cron\CronManager;
use AdnKronos\Cron\CronSchedules;
use AdnKronos\Feed\ArticleFeedParser;
use AdnKronos\Feed\FeedFetcher;
use AdnKronos\Feed\VideoFeedParser;
use AdnKronos\Import\ImageImporter;
use AdnKronos\Import\ImportMutex;
use AdnKronos\Import\Importer;
use AdnKronos\Import\PostCreator;
use AdnKronos\Katzgrau\KLogger\Logger;
use AdnKronos\Options\AccountOptions;
use AdnKronos\Options\ImporterOptions;
use AdnKronos\Stats\StatsCollector;
use AdnKronos\Stats\StatsReporter;

/**
 * Main plugin class.
 *
 * Wires up all services (constructor) and registers all WordPress hooks (register()).
 * Keeping construction and hook registration separate makes the wiring testable
 * without triggering any WordPress side-effects.
 */
class Plugin {

    /** @var Importer */
    private $importer;

    /** @var CronManager */
    private $cronManager;

    /** @var StatsReporter */
    private $statsReporter;

    /** @var string  Absolute path to the main plugin file (adnk-importer.php) */
    private $pluginFile;

    public function __construct($pluginFile) {
	    $this->pluginFile = $pluginFile;

		$logger = new Logger(ADNK_LOG_FOLDER);

	    $http         = new WpHttpClient();
        $apiClient    = new AdnkApiClient($http, ADNK_API_BASE_URL, ADNK_API_STATISTICS, $logger);
        $importerOpts = new ImporterOptions();
        $accountOpts  = new AccountOptions();

        $feedFetcher   = new FeedFetcher($http, $logger);
        $articleParser = new ArticleFeedParser();
        $videoParser   = new VideoFeedParser();
        $imageImporter = new ImageImporter($logger);
        $postCreator   = new PostCreator($imageImporter, $logger);

        $mutex             = new ImportMutex($logger);
        $this->importer    = new Importer($apiClient, $feedFetcher, $articleParser, $videoParser, $postCreator, $importerOpts, $mutex, $logger);
        $this->cronManager = new CronManager();

        $statsCollector      = new StatsCollector();
        $this->statsReporter = new StatsReporter($apiClient, $statsCollector, $accountOpts, $logger);

        if (is_admin()) {
            $settingsPage = new SettingsPage($importerOpts, $accountOpts, $apiClient, $this->cronManager);
            $accountPage  = new AccountPage($accountOpts, $apiClient);
            $adminNotice  = new AdminNotice();

            $settingsPage->register();
            $accountPage->register();
            $adminNotice->register();
        }
    }

    /**
     * Registers all WordPress hooks for cron, admin actions, and activation.
     */
    public function register() {
        register_activation_hook($this->pluginFile, array($this->cronManager, 'scheduleAll'));

        add_filter('cron_schedules', array(new CronSchedules(), 'addCustomSchedules'));
        add_action(CronManager::IMPORT_HOOK, array($this->importer, 'run'));
        add_action(CronManager::STATS_HOOK,  array($this->statsReporter, 'report'));

        if (is_admin()) {
            add_action('admin_action_adnk_import_now', array($this, 'importByButton'));
            add_action('admin_action_adnkinvio',       array($this->statsReporter, 'report'));
        }
    }

    /**
     * Triggered by the "Importa Ora" admin button.
     * Runs the import then redirects to the post list.
     */
    public function importByButton() {
        $this->importer->run();
        wp_redirect(site_url() . '/wp-admin/edit.php');
        exit;
    }

}