| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
abstract class MWP_Progress_Abstract implements MWP_Progress_CurlCallbackInterface |
| 12 |
{ |
| 13 |
private $threshold; |
| 14 |
|
| 15 |
private $lastYield; |
| 16 |
|
| 17 |
protected function getThreshold() |
| 18 |
{ |
| 19 |
return $this->threshold; |
| 20 |
} |
| 21 |
|
| 22 |
protected function setThreshold($threshold) |
| 23 |
{ |
| 24 |
$this->threshold = $threshold; |
| 25 |
} |
| 26 |
|
| 27 |
protected function yieldCallback() |
| 28 |
{ |
| 29 |
if (time() - $this->lastYield >= $this->threshold) { |
| 30 |
$this->lastYield = time(); |
| 31 |
|
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
protected function formatBytes($bytes) |
| 39 |
{ |
| 40 |
$bytes = (int) $bytes; |
| 41 |
|
| 42 |
if ($bytes > 1024 * 1024 * 1024) { |
| 43 |
return round($bytes / 1024 / 1024 / 1024, 2).' GB'; |
| 44 |
} elseif ($bytes > 1024 * 1024) { |
| 45 |
return round($bytes / 1024 / 1024, 2).' MB'; |
| 46 |
} elseif ($bytes > 1024) { |
| 47 |
return round($bytes / 1024, 2).' KB'; |
| 48 |
} |
| 49 |
|
| 50 |
return $bytes.' B'; |
| 51 |
} |
| 52 |
|
| 53 |
protected function calculateOffset($curl) |
| 54 |
{ |
| 55 |
/** @handled function */ |
| 56 |
$info = curl_getinfo($curl); |
| 57 |
|
| 58 |
$url = parse_url($info['url']); |
| 59 |
|
| 60 |
if (!isset($url['query'])) { |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
parse_str($url['query'], $query); |
| 65 |
|
| 66 |
if (!isset($query['offset'])) { |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
return (int) $query['offset']; |
| 71 |
} |
| 72 |
|
| 73 |
public function getCallback() |
| 74 |
{ |
| 75 |
return array($this, 'callback'); |
| 76 |
} |
| 77 |
} |
| 78 |
|