Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/initiali...
File: migration-runner.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Initializers;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use Yoast\WP\Lib\Migrations\Adapter;
[5] Fix | Delete
use Yoast\WP\Lib\Migrations\Migration;
[6] Fix | Delete
use Yoast\WP\SEO\Conditionals\No_Conditionals;
[7] Fix | Delete
use Yoast\WP\SEO\Config\Migration_Status;
[8] Fix | Delete
use Yoast\WP\SEO\Loader;
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Triggers database migrations and handles results.
[12] Fix | Delete
*/
[13] Fix | Delete
class Migration_Runner implements Initializer_Interface {
[14] Fix | Delete
[15] Fix | Delete
use No_Conditionals;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* The migrations adapter.
[19] Fix | Delete
*
[20] Fix | Delete
* @var Adapter
[21] Fix | Delete
*/
[22] Fix | Delete
protected $adapter;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* The loader.
[26] Fix | Delete
*
[27] Fix | Delete
* @var Loader
[28] Fix | Delete
*/
[29] Fix | Delete
protected $loader;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* The migration status.
[33] Fix | Delete
*
[34] Fix | Delete
* @var Migration_Status
[35] Fix | Delete
*/
[36] Fix | Delete
protected $migration_status;
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Retrieves the conditionals for the migrations.
[40] Fix | Delete
*
[41] Fix | Delete
* @return array The conditionals.
[42] Fix | Delete
*/
[43] Fix | Delete
public static function get_conditionals() {
[44] Fix | Delete
return [];
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Migrations constructor.
[49] Fix | Delete
*
[50] Fix | Delete
* @param Migration_Status $migration_status The migration status.
[51] Fix | Delete
* @param Loader $loader The loader.
[52] Fix | Delete
* @param Adapter $adapter The migrations adapter.
[53] Fix | Delete
*/
[54] Fix | Delete
public function __construct(
[55] Fix | Delete
Migration_Status $migration_status,
[56] Fix | Delete
Loader $loader,
[57] Fix | Delete
Adapter $adapter
[58] Fix | Delete
) {
[59] Fix | Delete
$this->migration_status = $migration_status;
[60] Fix | Delete
$this->loader = $loader;
[61] Fix | Delete
$this->adapter = $adapter;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Runs this initializer.
[66] Fix | Delete
*
[67] Fix | Delete
* @return void
[68] Fix | Delete
*
[69] Fix | Delete
* @throws Exception When a migration errored.
[70] Fix | Delete
*/
[71] Fix | Delete
public function initialize() {
[72] Fix | Delete
$this->run_free_migrations();
[73] Fix | Delete
// The below actions is used for when queries fail, this may happen in a multisite environment when switch_to_blog is used.
[74] Fix | Delete
\add_action( '_yoast_run_migrations', [ $this, 'run_free_migrations' ] );
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* Runs the free migrations.
[79] Fix | Delete
*
[80] Fix | Delete
* @return void
[81] Fix | Delete
*
[82] Fix | Delete
* @throws Exception When a migration errored.
[83] Fix | Delete
*/
[84] Fix | Delete
public function run_free_migrations() {
[85] Fix | Delete
$this->run_migrations( 'free' );
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Initializes the migrations.
[90] Fix | Delete
*
[91] Fix | Delete
* @param string $name The name of the migration.
[92] Fix | Delete
* @param string $version The current version.
[93] Fix | Delete
*
[94] Fix | Delete
* @return bool True on success, false on failure.
[95] Fix | Delete
*
[96] Fix | Delete
* @throws Exception If the migration fails and YOAST_ENVIRONMENT is not production.
[97] Fix | Delete
*/
[98] Fix | Delete
public function run_migrations( $name, $version = \WPSEO_VERSION ) {
[99] Fix | Delete
if ( ! $this->migration_status->should_run_migration( $name, $version ) ) {
[100] Fix | Delete
return true;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
if ( ! $this->migration_status->lock_migration( $name ) ) {
[104] Fix | Delete
return false;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$migrations = $this->loader->get_migrations( $name );
[108] Fix | Delete
[109] Fix | Delete
if ( $migrations === false ) {
[110] Fix | Delete
$this->migration_status->set_error( $name, "Could not perform $name migrations. No migrations found.", $version );
[111] Fix | Delete
return false;
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
try {
[115] Fix | Delete
$this->adapter->create_schema_version_table();
[116] Fix | Delete
$all_versions = \array_keys( $migrations );
[117] Fix | Delete
$migrated_versions = $this->adapter->get_migrated_versions();
[118] Fix | Delete
$to_do_versions = \array_diff( $all_versions, $migrated_versions );
[119] Fix | Delete
[120] Fix | Delete
\sort( $to_do_versions, \SORT_STRING );
[121] Fix | Delete
[122] Fix | Delete
foreach ( $to_do_versions as $to_do_version ) {
[123] Fix | Delete
$class = $migrations[ $to_do_version ];
[124] Fix | Delete
$this->run_migration( $to_do_version, $class );
[125] Fix | Delete
}
[126] Fix | Delete
} catch ( Exception $exception ) {
[127] Fix | Delete
// Something went wrong...
[128] Fix | Delete
$this->migration_status->set_error( $name, $exception->getMessage(), $version );
[129] Fix | Delete
[130] Fix | Delete
if ( \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT !== 'production' ) {
[131] Fix | Delete
throw $exception;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
return false;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
$this->migration_status->set_success( $name, $version );
[138] Fix | Delete
[139] Fix | Delete
return true;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Runs a single migration.
[144] Fix | Delete
*
[145] Fix | Delete
* @param string $version The version.
[146] Fix | Delete
* @param string $class The migration class.
[147] Fix | Delete
*
[148] Fix | Delete
* @return void
[149] Fix | Delete
*
[150] Fix | Delete
* @throws Exception If the migration failed. Caught by the run_migrations function.
[151] Fix | Delete
*/
[152] Fix | Delete
protected function run_migration( $version, $class ) {
[153] Fix | Delete
/**
[154] Fix | Delete
* The migration to run.
[155] Fix | Delete
*
[156] Fix | Delete
* @var Migration
[157] Fix | Delete
*/
[158] Fix | Delete
$migration = new $class( $this->adapter );
[159] Fix | Delete
try {
[160] Fix | Delete
$this->adapter->start_transaction();
[161] Fix | Delete
$migration->up();
[162] Fix | Delete
$this->adapter->add_version( $version );
[163] Fix | Delete
$this->adapter->commit_transaction();
[164] Fix | Delete
} catch ( Exception $e ) {
[165] Fix | Delete
$this->adapter->rollback_transaction();
[166] Fix | Delete
throw new Exception( \sprintf( '%s - %s', $class, $e->getMessage() ), 0, $e );
[167] Fix | Delete
}
[168] Fix | Delete
}
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function