Last active
August 23, 2024 19:04
-
-
Save bibicadotnet/cf9212949ece7079c1d76f172ba767ec to your computer and use it in GitHub Desktop.
Simply Static WP-CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| class Static_Command extends WP_CLI_Command { | |
| private $output_dir = '/var/www/html/vercel-simply-static'; | |
| private $check_interval = 5; // Thời gian chờ giữa các lần kiểm tra (giây) | |
| private $stable_time = 5; // Thời gian xác nhận không có file mới (giây) | |
| private $initial_delay = 10; // Thời gian chờ ban đầu để Simply Static bắt đầu (giây) | |
| public function run() { | |
| // Đảm bảo rằng plugin Simply Static đã được nạp | |
| if ( class_exists( 'Simply_Static\Plugin' ) ) { | |
| $simply_static = Simply_Static\Plugin::instance(); | |
| $simply_static->run_static_export(); | |
| WP_CLI::success( 'Static export build started. Waiting for completion...' ); | |
| // Chờ một khoảng thời gian ban đầu để quá trình sao chép file bắt đầu | |
| sleep($this->initial_delay); | |
| // Chờ đợi quá trình xuất tĩnh hoàn thành bằng cách kiểm tra thư mục | |
| $this->wait_for_completion(); | |
| } else { | |
| WP_CLI::error( 'Simply Static plugin not found.' ); | |
| } | |
| } | |
| private function wait_for_completion() { | |
| $previous_file_count = 0; | |
| $stable_check_time = 0; | |
| while (true) { | |
| $current_file_count = $this->get_file_count($this->output_dir); | |
| // Kiểm tra nếu có sự thay đổi về số lượng file trong thư mục | |
| if ($current_file_count > $previous_file_count) { | |
| $previous_file_count = $current_file_count; | |
| $stable_check_time = 0; // Reset thời gian kiểm tra ổn định | |
| } else { | |
| // Nếu số lượng file không thay đổi, tăng thời gian ổn định | |
| $stable_check_time += $this->check_interval; | |
| } | |
| // Nếu đã có ít nhất 1 file và thời gian ổn định đã đạt đến mức cần thiết, quá trình hoàn thành | |
| if ($current_file_count > 0 && $stable_check_time >= $this->stable_time) { | |
| break; | |
| } | |
| sleep($this->check_interval); | |
| } | |
| WP_CLI::success( 'Static export files have been successfully created in ' . $this->output_dir ); | |
| } | |
| private function get_file_count($dir) { | |
| if (!is_readable($dir)) return 0; | |
| $files = array_diff(scandir($dir), array('.', '..')); | |
| return count($files); | |
| } | |
| } | |
| WP_CLI::add_command( 'static', 'Static_Command' ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment