Cleaners
5 years ago
Exceptions
5 years ago
Cancel.php
3 years ago
CancelUpdate.php
4 years ago
Cloning.php
3 years ago
CloningProcess.php
3 years ago
Data.php
3 years ago
Database.php
3 years ago
Delete.php
3 years ago
Directories.php
2 years ago
Files.php
2 years ago
Finish.php
2 years ago
Job.php
2 years ago
JobExecutable.php
3 years ago
Logs.php
3 years ago
PreserveDataFirstStep.php
3 years ago
PreserveDataSecondStep.php
3 years ago
ProcessLock.php
3 years ago
Scan.php
2 years ago
SearchReplace.php
3 years ago
TotalStepsAreNumberOfTables.php
5 years ago
Updating.php
3 years ago
Verify.php
3 years ago
JobExecutable.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Modules\Jobs; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Utils\Strings; |
| 7 | use WPStaging\Framework\Utils\ServerVars; |
| 8 | |
| 9 | // No Direct Access |
| 10 | if (!defined("WPINC")) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Class JobExecutable |
| 17 | * I'm sorry for such mess, we need to support PHP 5.3 |
| 18 | * @package WPStaging\Backend\Modules\Jobs |
| 19 | */ |
| 20 | abstract class JobExecutable extends Job |
| 21 | { |
| 22 | /** |
| 23 | * @var Strings |
| 24 | */ |
| 25 | protected $strUtil; |
| 26 | |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | protected $response = [ |
| 31 | "status" => false, |
| 32 | "percentage" => 0, |
| 33 | "total" => 0, |
| 34 | "step" => 0, |
| 35 | "last_msg" => '', |
| 36 | ]; |
| 37 | |
| 38 | /** |
| 39 | * JobExecutable constructor. |
| 40 | */ |
| 41 | public function __construct() |
| 42 | { |
| 43 | parent::__construct(); |
| 44 | |
| 45 | // Calculate total steps |
| 46 | $this->calculateTotalSteps(); |
| 47 | |
| 48 | // Set server settings for jobs (Do not set this globally as this will affect other plugins and even wp core then as well. Helpscout Ticket #9061) |
| 49 | // Set whether a client disconnect should abort script execution |
| 50 | @ignore_user_abort(true); |
| 51 | |
| 52 | // Set maximum execution time |
| 53 | WPStaging::make(ServerVars::class)->setTimeLimit(0); |
| 54 | |
| 55 | // Set maximum time in seconds a script is allowed to parse input data |
| 56 | @ini_set('max_input_time', '-1'); |
| 57 | |
| 58 | // Set maximum backtracking steps |
| 59 | @ini_set('pcre.backtrack_limit', PHP_INT_MAX); |
| 60 | |
| 61 | $this->strUtil = WPStaging::make(Strings::class); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Prepare Response Array |
| 66 | * @param bool $status false if not finished |
| 67 | * @param bool $incrementCurrentStep |
| 68 | * @return array |
| 69 | */ |
| 70 | protected function prepareResponse($status = false, $incrementCurrentStep = true) |
| 71 | { |
| 72 | if ($incrementCurrentStep) { |
| 73 | $this->options->currentStep++; |
| 74 | } |
| 75 | |
| 76 | $percentage = 0; |
| 77 | if (isset($this->options->currentStep) && isset($this->options->totalSteps) && $this->options->totalSteps > 0) { |
| 78 | $percentage = round(($this->options->currentStep / $this->options->totalSteps) * 100); |
| 79 | $percentage = ($percentage > 100) ? 100 : $percentage; |
| 80 | } |
| 81 | |
| 82 | return $this->response = [ |
| 83 | "status" => $status, |
| 84 | "percentage" => $percentage, |
| 85 | "total" => $this->options->totalSteps, |
| 86 | "step" => $this->options->currentStep, |
| 87 | "job" => $this->options->currentJob, |
| 88 | "last_msg" => $this->logger->getLastLogMsg(), |
| 89 | "job_done" => $status |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Start Module |
| 95 | * @return object |
| 96 | */ |
| 97 | public function start() |
| 98 | { |
| 99 | // Execute steps |
| 100 | $this->run(); |
| 101 | |
| 102 | // Save option, progress |
| 103 | $this->saveOptions(); |
| 104 | |
| 105 | return (object) $this->response; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Run Steps |
| 110 | */ |
| 111 | protected function run() |
| 112 | { |
| 113 | // Execute steps |
| 114 | for ($i = 0; $i < $this->options->totalSteps; $i++) { |
| 115 | // Job is finished or over threshold limits was hit |
| 116 | if (!$this->execute()) { |
| 117 | break; |
| 118 | } |
| 119 | // Return after every step to create lower batches |
| 120 | // This also gets a smoother progress bar and to a less consumptive php cpu load |
| 121 | // This decrease performance tremendous but also lowers memory consumption |
| 122 | if ($this->settings->cpuLoad === 'low') { |
| 123 | return (object) $this->response; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Calculate Total Steps in This Job and Assign It to $this->options->totalSteps |
| 130 | * @return void |
| 131 | */ |
| 132 | abstract protected function calculateTotalSteps(); |
| 133 | |
| 134 | /** |
| 135 | * Execute the Current Step |
| 136 | * Returns false when over threshold limits are hit or when the job is done, true otherwise |
| 137 | * @return bool |
| 138 | */ |
| 139 | abstract protected function execute(); |
| 140 | } |
| 141 |