Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/MWP/Migratio...
File: Migration.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Base component for ManageWP Worker migrations.
[11] Fix | Delete
* Renamed options and those that changed their format from one plugin version to another should be handled here.
[12] Fix | Delete
*/
[13] Fix | Delete
class MWP_Migration_Migration
[14] Fix | Delete
{
[15] Fix | Delete
/**
[16] Fix | Delete
* Upon plugin activation this version is persisted to the database.
[17] Fix | Delete
* It is also checked against on every master request.
[18] Fix | Delete
*/
[19] Fix | Delete
const VERSION = 2;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* @var MWP_WordPress_Context
[23] Fix | Delete
*/
[24] Fix | Delete
private $context;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Migration version => migration name (method).
[28] Fix | Delete
* The last migration index MUST be the same as the VERSION constant above.
[29] Fix | Delete
*
[30] Fix | Delete
* @var array
[31] Fix | Delete
*/
[32] Fix | Delete
private static $migrations = array(
[33] Fix | Delete
1 => 'migrateBackupFileNames',
[34] Fix | Delete
2 => 'setDefaultOptionValues',
[35] Fix | Delete
);
[36] Fix | Delete
[37] Fix | Delete
public function __construct(MWP_WordPress_Context $context)
[38] Fix | Delete
{
[39] Fix | Delete
$this->context = $context;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
private function acquireLock($lockName, $ttl = 3600)
[43] Fix | Delete
{
[44] Fix | Delete
$wpdb = $this->context->getDb();
[45] Fix | Delete
$lockRow = $wpdb->get_row("SELECT option_value FROM {$wpdb->prefix}options WHERE option_name = '$lockName'");
[46] Fix | Delete
$currentTimestamp = $this->context->getCurrentTime()->format('U');
[47] Fix | Delete
[48] Fix | Delete
if ($lockRow) {
[49] Fix | Delete
/** @noinspection PhpUndefinedFieldInspection */
[50] Fix | Delete
if ((int) $lockRow->option_value + $ttl > $currentTimestamp) {
[51] Fix | Delete
return false;
[52] Fix | Delete
} else {
[53] Fix | Delete
$this->releaseLock($lockName);
[54] Fix | Delete
}
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
$locked = $wpdb->query("INSERT INTO {$wpdb->prefix}options SET option_name = '$lockName', option_value = '$currentTimestamp'");
[58] Fix | Delete
[59] Fix | Delete
return (bool) $locked;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
private function releaseLock($lockName)
[63] Fix | Delete
{
[64] Fix | Delete
$wpdb = $this->context->getDb();
[65] Fix | Delete
$released = $wpdb->query("DELETE FROM {$wpdb->prefix}options WHERE option_name = '$lockName'");
[66] Fix | Delete
[67] Fix | Delete
return (bool) $released;
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
public function migrate()
[71] Fix | Delete
{
[72] Fix | Delete
$wpdb = $this->context->getDb();
[73] Fix | Delete
$lockName = 'worker_migration_lock';
[74] Fix | Delete
[75] Fix | Delete
$version = (int) $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'worker_migration_version'");
[76] Fix | Delete
$currentVersion = self::VERSION;
[77] Fix | Delete
[78] Fix | Delete
if ($version >= $currentVersion) {
[79] Fix | Delete
return;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
if (!$this->acquireLock($lockName)) {
[83] Fix | Delete
return;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
foreach (self::$migrations as $migrationVersion => $migrationName) {
[87] Fix | Delete
if ($version < $migrationVersion) {
[88] Fix | Delete
$this->$migrationName();
[89] Fix | Delete
$wpdb->query("INSERT INTO {$wpdb->prefix}options SET option_name = 'worker_migration_version', option_value = '$migrationVersion' ON DUPLICATE KEY UPDATE option_value = '$migrationVersion'");
[90] Fix | Delete
}
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
$this->releaseLock($lockName);
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
private function setDefaultOptionValues()
[97] Fix | Delete
{
[98] Fix | Delete
if (!$this->context->optionGet('mwp_recovering')) {
[99] Fix | Delete
$this->context->optionSet('mwp_recovering', '');
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
if (!$this->context->optionGet('mwp_incremental_update_active')) {
[103] Fix | Delete
$this->context->optionSet('mwp_incremental_update_active', '');
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
if (!$this->context->optionGet('mwp_core_autoupdate')) {
[107] Fix | Delete
$this->context->optionSet('mwp_core_autoupdate', '');
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
if (!$this->context->optionGet('mwp_container_parameters')) {
[111] Fix | Delete
$this->context->optionSet('mwp_container_parameters', array());
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
if (!$this->context->optionGet('mwp_container_site_parameters')) {
[115] Fix | Delete
$this->context->optionSet('mwp_container_site_parameters', array());
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
if (!$this->context->optionGet('_worker_nossl_key')) {
[119] Fix | Delete
$this->context->optionSet('_worker_nossl_key', '');
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
if (!$this->context->optionGet('_worker_public_key')) {
[123] Fix | Delete
$this->context->optionSet('_worker_public_key', '');
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
if (!$this->context->optionGet('mwp_maintenace_mode')) {
[127] Fix | Delete
$this->context->optionSet('mwp_maintenace_mode', array());
[128] Fix | Delete
}
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
private function migrateBackupFileNames()
[132] Fix | Delete
{
[133] Fix | Delete
$tasks = (array) $this->context->optionGet('mwp_backup_tasks');
[134] Fix | Delete
$changed = false;
[135] Fix | Delete
[136] Fix | Delete
foreach ($tasks as $taskName => $taskInfo) {
[137] Fix | Delete
if (!isset($taskInfo['task_results']) || !is_array($taskInfo['task_results'])) {
[138] Fix | Delete
continue;
[139] Fix | Delete
}
[140] Fix | Delete
foreach ($taskInfo['task_results'] as $resultId => $taskResult) {
[141] Fix | Delete
if (!isset($taskResult['server']['file_path']) || !file_exists($taskResult['server']['file_path'])) {
[142] Fix | Delete
continue;
[143] Fix | Delete
}
[144] Fix | Delete
$filePath = $taskResult['server']['file_path'];
[145] Fix | Delete
$hash = $this->generateRandomHash(filesize($filePath));
[146] Fix | Delete
$newFilePath = preg_replace('{_[a-z0-9]{32}\.zip$}', sprintf('_%s.zip', $hash), $filePath);
[147] Fix | Delete
$renamed = rename($filePath, $newFilePath);
[148] Fix | Delete
if (!$renamed) {
[149] Fix | Delete
continue;
[150] Fix | Delete
}
[151] Fix | Delete
$changed = true;
[152] Fix | Delete
$oldUrl = $tasks[$taskName]['task_results'][$resultId]['server']['file_url'];
[153] Fix | Delete
$newUrl = preg_replace('{_[a-z0-9]{32}\.zip$}', sprintf('_%s.zip', $hash), $oldUrl);
[154] Fix | Delete
[155] Fix | Delete
$tasks[$taskName]['task_results'][$resultId]['server']['file_path'] = $newFilePath;
[156] Fix | Delete
$tasks[$taskName]['task_results'][$resultId]['server']['file_url'] = $newUrl;
[157] Fix | Delete
}
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
if (!$changed) {
[161] Fix | Delete
return;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$this->context->optionSet('mwp_backup_tasks', $tasks);
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
private function generateRandomHash($seed)
[168] Fix | Delete
{
[169] Fix | Delete
return str_shuffle(md5(mt_rand().$seed));
[170] Fix | Delete
}
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function