?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin/Crons/



?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin

?? Viewing: DeleteTempAttachments.php

<?php

namespace KS_PAC_DCFH\Admin\Crons;

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

class DeleteTempAttachments
{
    public const EXPIRY_TIME = 7200;

    private string $hook = 'dcfh_delete_tmp_files';

    private string $recurrence = 'twicedaily';

    public function load(): void
    {
        add_action('init', [$this, 'maybe_next_scheduled']);
        add_action($this->hook, [$this, 'maybe_schedule_event']);
    }

    public function maybe_next_scheduled(): void
    {
        if (!wp_next_scheduled($this->hook)) {
            wp_schedule_event(time(), $this->recurrence, $this->hook);
        }
    }

    public function maybe_schedule_event(): void
    {
        if (!function_exists('list_files')) {
            require_once(ABSPATH.'wp-admin/includes/file.php');
        }
        $upload_tmp_dir = ks_pac_dcfh_app()->file_utils()::get_tmp_upload_dir();
        $files = list_files($upload_tmp_dir, 100, ['index.php', '.htaccess']);
        if (!empty($files)) {
            usort($files, static function ($a, $b) {
                return filemtime($b) - filemtime($a);
            });
            $now = time();
            foreach ($files as $file) {
                if (is_file($file) && file_exists($file)) {
                    $file_explode = explode('-', pathinfo($file, PATHINFO_FILENAME));
                    if (is_array($file_explode)) {
                        $file_timestamp = end($file_explode);
                        if ((1 === preg_match('~^[1-9][0-9]*$~', $file_timestamp)) && ($now - $file_timestamp) > self::EXPIRY_TIME) {
                            wp_delete_file($file);
                        }
                    }
                }
            }
        }
    }
}


??

??