<?php
/*
    WordPressAdmin

    Interface to WordPress Admin functions

    Used for registering hooks, Admin UI components, ...
*/

namespace StaticDeploy;

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


class WordPressAdmin {

    /**
     * Build update checker
     *
     * @param string $bootstrap_file main plugin filepath
     */
    public static function buildUpdateChecker( string $bootstrap_file ): void
    {
    }

    /**
     * Register hooks for WordPress and plugin actions
     *
     * @param string $bootstrap_file main plugin filepath
     */
    public static function registerHooks(): void {
        add_filter(
            // phpcs:ignore WordPress.WP.CronInterval -- namespaces not yet fully supported
            'cron_schedules',
            WPCron::customCronSchedules( ... )
        );

        add_filter(
            'cron_request',
            WPCron::cronWithBasicAuth( ... )
        );

        add_action(
            'wp_ajax_static_deploy_run',
            Controller::ajaxRun( ... ),
            10,
            0
        );

        add_action(
            'wp_ajax_static_deploy_poll_log',
            Controller::ajaxPollLog( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_ui_save_options',
            Controller::UISaveOptions( ... ),
            10,
            0
        );

        add_action(
            'static_deploy_register_addon',
            Addons::registerAddon( ... ),
            10,
            5
        );

        add_action(
            'static_deploy_post_deploy_trigger',
            Controller::emailDeployNotification( ... ),
            10,
            0
        );

        add_action(
            'static_deploy_post_deploy_trigger',
            Controller::webhookDeployNotification( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_post_processed_site_delete',
            Controller::adminPostProcessedSiteDelete( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_post_processed_site_show',
            Controller::adminPostProcessedSiteShow( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_log_delete',
            Controller::adminLogDelete( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_delete_all_caches',
            Controller::adminDeleteAllCaches( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_delete_jobs_queue',
            Controller::adminDeleteJobsQueue( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_process_jobs_queue',
            Controller::adminProcessJobsQueue( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_process_queue',
            self::adminPostProcessQueue( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_detected_files_delete',
            Controller::adminDetectedFilesDelete( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_detected_files_show',
            Controller::adminDetectedFilesShow( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_deploy_cache_delete',
            Controller::adminDeployCacheDelete( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_deploy_cache_show',
            Controller::adminDeployCacheShow( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_crawled_files_delete',
            Controller::adminCrawledFilesDelete( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_crawled_files_show',
            Controller::adminCrawledFilesShow( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_ui_save_job_options',
            Controller::adminUISaveJobsOptions( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_ui_save_advanced_options',
            Controller::adminUISaveAdvancedOptions( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_manually_enqueue_jobs',
            Controller::adminManuallyEnqueueJobs( ... ),
            10,
            0
        );

        add_action(
            'admin_post_static_deploy_toggle_addon',
            Controller::adminToggleAddon( ... ),
            10,
            0
        );

        add_action(
            'static_deploy_process_queue',
            Controller::processQueue( ... ),
            10,
            0
        );

        add_action(
            'static_deploy_headless_hook',
            Controller::runHeadless( ... ),
            10,
            0
        );

        add_action(
            'static_deploy_crawl',
            [ Crawler::class, 'crawl' ],
            10,
            3
        );

        add_action(
            'save_post',
            Controller::savePostHandler( ... ),
            0
        );

        add_action(
            'trashed_post',
            Controller::trashedPostHandler( ... ),
            0
        );

        /*
         * Register actions for when we should invalidate cache for
         * a URL(s) or whole site
         *
         */
        $single_url_invalidation_events = [
            'save_post',
            'deleted_post',
        ];

        foreach ( $single_url_invalidation_events as $single_url_invalidation_event ) {
            add_action(
                $single_url_invalidation_event,
                Controller::invalidateSingleURLCache( ... ),
                10,
                2
            );
        }
    }

    /**
     * Add plugin elements to WordPress Admin UI
     */
    public static function addAdminUIElements(): void {
        if ( is_admin() ) {
            add_action(
                'admin_menu',
                Controller::registerOptionsPage( ... )
            );
            add_filter( 'custom_menu_order', '__return_true' );
            add_filter( 'menu_order', Controller::setMenuOrder( ... ) );
            AdminBar::registerHooks();
        }
    }

    /*
     * Do security checks before calling Controller::processQueue
     */
    public static function adminPostProcessQueue(): void {
        $method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_URL );
        if ( ! $method ) {
            $msg = 'Empty method in request to admin-post.php (adminPostProcessQueue)';
        } elseif ( 'POST' !== $method ) {
            $method = strval( $method );
            $msg = "Invalid method in request to admin-post.php (adminPostProcessQueue): {$method}";
        }
        $nonce_valid = isset( $_POST['_wpnonce'] ) && wp_verify_nonce(
            sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ),
            'static_deploy_process_queue'
        );
        if ( ! $nonce_valid ) {
            $msg = 'Invalid nonce in request to admin-post.php (adminPostProcessQueue)';
        }

        if ( isset( $msg ) ) {
            throw WsLog::ex( esc_html( $msg ) );
        }

        Controller::processQueue();
    }
}
