✏️ 正在编辑: autoloader.php
路径:
/home/h359620/public_html/wp-content/themes/datis/autoloader.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php /** * ----------------------------------------------------------------------------- * Datis Theme Autoloader (Optimized + Cached + Safe Boot) * ----------------------------------------------------------------------------- * - Scans /inc directory ONLY when cache is missing/expired * - Caches PHP file list in a transient * - Requires cached file list on every request (fast) * - Boots singleton-style classes ONLY if: * 1) class file is inside theme directory (NOT plugins) * 2) boot method requires 0 required parameters * - Skips /widgets and excluded panel templates * - Runs at after_setup_theme with priority 0 * ----------------------------------------------------------------------------- */ if (!defined('ABSPATH')) { exit; } if (!function_exists('datis_autoload_register_optimized')) { function datis_autoload_get_inc_dir(): string { return defined('DATIS_INC') ? rtrim(DATIS_INC, '/\\') : ''; } function datis_autoload_get_theme_dir(): string { return defined('DATIS_DIR') ? rtrim(DATIS_DIR, '/\\') : rtrim(get_template_directory(), '/\\'); } function datis_autoload_cache_key(): string { $ver = defined('DATIS_VER') ? DATIS_VER : wp_get_theme()->get('Version'); return 'datis_autoload_files_v3_' . md5($ver . '|' . get_stylesheet()); } function datis_autoload_cache_ttl(): int { if (defined('WP_DEBUG') && WP_DEBUG) { return 5 * MINUTE_IN_SECONDS; } return 12 * HOUR_IN_SECONDS; } function datis_autoload_build_file_list(string $base_dir): array { $base_dir = rtrim($base_dir, '/\\'); if ($base_dir === '' || !is_dir($base_dir)) return []; $files = []; $iterator = new RecursiveIteratorIterator( new RecursiveCallbackFilterIterator( new RecursiveDirectoryIterator($base_dir, RecursiveDirectoryIterator::SKIP_DOTS), function ($file, $key, $iterator) { // Skip widgets folder completely if ($iterator->hasChildren() && $file->getFilename() === 'widgets') { return false; } return true; } ), RecursiveIteratorIterator::SELF_FIRST ); foreach ($iterator as $file) { if (!$file->isFile()) continue; if (strtolower($file->getExtension()) !== 'php') continue; $path = str_replace('\\', '/', $file->getPathname()); // Skip panel shell & partials if (strpos($path, '/inc/includes/panel/panel-shell.php') !== false) { continue; } if (strpos($path, '/inc/includes/panel/partials/') !== false) { continue; } $files[] = $file->getPathname(); } // Deterministic order sort($files, SORT_STRING); return $files; } function datis_autoload_get_files(): array { $key = datis_autoload_cache_key(); $cached = get_transient($key); if (is_array($cached) && !empty($cached)) { return $cached; } $base = datis_autoload_get_inc_dir(); $files = datis_autoload_build_file_list($base); if (!empty($files)) { set_transient($key, $files, datis_autoload_cache_ttl()); } return $files; } /** * Check class file belongs to theme (avoid plugins like Redux). */ function datis_autoload_is_theme_class(string $class, string $theme_dir): bool { try { $ref = new ReflectionClass($class); $file = $ref->getFileName(); if (!$file) return false; $file = str_replace('\\', '/', $file); $theme_dir = str_replace('\\', '/', $theme_dir); return (strpos($file, $theme_dir) === 0); } catch (Throwable $e) { return false; } } /** * Check boot method has zero REQUIRED params. */ function datis_autoload_method_requires_no_args(string $class, string $method): bool { try { $rm = new ReflectionMethod($class, $method); return ($rm->getNumberOfRequiredParameters() === 0); } catch (Throwable $e) { return false; } } /** * Boot only safe singleton-like classes in theme. */ function datis_autoload_boot_singletons(array $before, array $after): void { $new = array_diff($after, $before); if (empty($new)) return; $theme_dir = datis_autoload_get_theme_dir(); if ($theme_dir === '') return; foreach ($new as $class) { if (!is_string($class) || $class === '') continue; if (!class_exists($class)) continue; // IMPORTANT: Only boot classes that are defined in THEME files (not plugins/core) if (!datis_autoload_is_theme_class($class, $theme_dir)) { continue; } // Boot patterns (only if method needs 0 args) if (is_callable([$class, 'get_instance']) && datis_autoload_method_requires_no_args($class, 'get_instance')) { $class::get_instance(); continue; } if (is_callable([$class, 'instance']) && datis_autoload_method_requires_no_args($class, 'instance')) { $class::instance(); continue; } if (is_callable([$class, 'boot']) && datis_autoload_method_requires_no_args($class, 'boot')) { $class::boot(); continue; } } } function datis_autoload_register_optimized(): void { if (!defined('DATIS_INC')) return; $files = datis_autoload_get_files(); if (empty($files)) return; $before = get_declared_classes(); foreach ($files as $file) { if (is_string($file) && $file !== '' && file_exists($file)) { require_once $file; } } $after = get_declared_classes(); // Boot only newly declared theme classes safely datis_autoload_boot_singletons($before, $after); } add_action('after_setup_theme', 'datis_autoload_register_optimized', 0); // Admin flush: wp-admin/?datis_flush_autoload=1 add_action('admin_init', function () { if (!current_user_can('manage_options')) return; if (empty($_GET['datis_flush_autoload'])) return; delete_transient(datis_autoload_cache_key()); }); }
💾 保存文件
← 返回文件管理器