Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/config
File: migration-status.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Config;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Migration_Status class.
[5] Fix | Delete
*
[6] Fix | Delete
* Used to validate whether or not migrations have been run and whether or not they should be run again.
[7] Fix | Delete
*/
[8] Fix | Delete
class Migration_Status {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* The migration option key.
[12] Fix | Delete
*
[13] Fix | Delete
* @var string
[14] Fix | Delete
*/
[15] Fix | Delete
const MIGRATION_OPTION_KEY = 'yoast_migrations_';
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* The migration options.
[19] Fix | Delete
*
[20] Fix | Delete
* @var array
[21] Fix | Delete
*/
[22] Fix | Delete
protected $migration_options = [];
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Checks if a given migration should be run.
[26] Fix | Delete
*
[27] Fix | Delete
* @param string $name The name of the migration.
[28] Fix | Delete
* @param string $version The current version.
[29] Fix | Delete
*
[30] Fix | Delete
* @return bool Whether or not the migration should be run.
[31] Fix | Delete
*/
[32] Fix | Delete
public function should_run_migration( $name, $version = \WPSEO_VERSION ) {
[33] Fix | Delete
$migration_status = $this->get_migration_status( $name );
[34] Fix | Delete
[35] Fix | Delete
// Check if we've attempted to run this migration in the past 10 minutes. If so, it may still be running.
[36] Fix | Delete
if ( \array_key_exists( 'lock', $migration_status ) ) {
[37] Fix | Delete
$timestamp = \strtotime( '-10 minutes' );
[38] Fix | Delete
[39] Fix | Delete
return $timestamp > $migration_status['lock'];
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
// Is the migration version less than the current version.
[43] Fix | Delete
return \version_compare( $migration_status['version'], $version, '<' );
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Checks whether or not the given migration is at least the given version, defaults to checking for the latest version.
[48] Fix | Delete
*
[49] Fix | Delete
* @param string $name The name of the migration.
[50] Fix | Delete
* @param string $version The version to check, defaults to the latest version.
[51] Fix | Delete
*
[52] Fix | Delete
* @return bool Whether or not the requested migration is at least the requested version.
[53] Fix | Delete
*/
[54] Fix | Delete
public function is_version( $name, $version = \WPSEO_VERSION ) {
[55] Fix | Delete
$migration_status = $this->get_migration_status( $name );
[56] Fix | Delete
[57] Fix | Delete
return \version_compare( $version, $migration_status['version'], '<=' );
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Gets the error of a given migration if it exists.
[62] Fix | Delete
*
[63] Fix | Delete
* @param string $name The name of the migration.
[64] Fix | Delete
*
[65] Fix | Delete
* @return bool|array False if there is no error, otherwise the error.
[66] Fix | Delete
*/
[67] Fix | Delete
public function get_error( $name ) {
[68] Fix | Delete
$migration_status = $this->get_migration_status( $name );
[69] Fix | Delete
[70] Fix | Delete
if ( ! isset( $migration_status['error'] ) ) {
[71] Fix | Delete
return false;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
return $migration_status['error'];
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* Sets an error for the migration.
[79] Fix | Delete
*
[80] Fix | Delete
* @param string $name The name of the migration.
[81] Fix | Delete
* @param string $message Message explaining the reason for the error.
[82] Fix | Delete
* @param string $version The current version.
[83] Fix | Delete
*
[84] Fix | Delete
* @return void
[85] Fix | Delete
*/
[86] Fix | Delete
public function set_error( $name, $message, $version = \WPSEO_VERSION ) {
[87] Fix | Delete
$migration_status = $this->get_migration_status( $name );
[88] Fix | Delete
[89] Fix | Delete
$migration_status['error'] = [
[90] Fix | Delete
'time' => \strtotime( 'now' ),
[91] Fix | Delete
'version' => $version,
[92] Fix | Delete
'message' => $message,
[93] Fix | Delete
];
[94] Fix | Delete
[95] Fix | Delete
$this->set_migration_status( $name, $migration_status );
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Updates the migration version to the latest version.
[100] Fix | Delete
*
[101] Fix | Delete
* @param string $name The name of the migration.
[102] Fix | Delete
* @param string $version The current version.
[103] Fix | Delete
*
[104] Fix | Delete
* @return void
[105] Fix | Delete
*/
[106] Fix | Delete
public function set_success( $name, $version = \WPSEO_VERSION ) {
[107] Fix | Delete
$migration_status = $this->get_migration_status( $name );
[108] Fix | Delete
unset( $migration_status['lock'] );
[109] Fix | Delete
unset( $migration_status['error'] );
[110] Fix | Delete
$migration_status['version'] = $version;
[111] Fix | Delete
$this->set_migration_status( $name, $migration_status );
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
/**
[115] Fix | Delete
* Locks the migration status.
[116] Fix | Delete
*
[117] Fix | Delete
* @param string $name The name of the migration.
[118] Fix | Delete
*
[119] Fix | Delete
* @return bool Whether or not the migration was succesfully locked.
[120] Fix | Delete
*/
[121] Fix | Delete
public function lock_migration( $name ) {
[122] Fix | Delete
$migration_status = $this->get_migration_status( $name );
[123] Fix | Delete
$migration_status['lock'] = \strtotime( 'now' );
[124] Fix | Delete
[125] Fix | Delete
return $this->set_migration_status( $name, $migration_status );
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Retrieves the migration option.
[130] Fix | Delete
*
[131] Fix | Delete
* @param string $name The name of the migration.
[132] Fix | Delete
*
[133] Fix | Delete
* @return bool|array The status of the migration, false if no status exists.
[134] Fix | Delete
*/
[135] Fix | Delete
protected function get_migration_status( $name ) {
[136] Fix | Delete
$current_blog_id = \get_current_blog_id();
[137] Fix | Delete
if ( ! isset( $this->migration_options[ $current_blog_id ][ $name ] ) ) {
[138] Fix | Delete
$migration_status = \get_option( self::MIGRATION_OPTION_KEY . $name );
[139] Fix | Delete
[140] Fix | Delete
if ( ! \is_array( $migration_status ) || ! isset( $migration_status['version'] ) ) {
[141] Fix | Delete
$migration_status = [ 'version' => '0.0' ];
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
if ( ! isset( $this->migration_options[ $current_blog_id ] ) ) {
[145] Fix | Delete
$this->migration_options[ $current_blog_id ] = [];
[146] Fix | Delete
}
[147] Fix | Delete
$this->migration_options[ $current_blog_id ][ $name ] = $migration_status;
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
return $this->migration_options[ $current_blog_id ][ $name ];
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Retrieves the migration option.
[155] Fix | Delete
*
[156] Fix | Delete
* @param string $name The name of the migration.
[157] Fix | Delete
* @param array $migration_status The migration status.
[158] Fix | Delete
*
[159] Fix | Delete
* @return bool True if the status was succesfully updated, false otherwise.
[160] Fix | Delete
*/
[161] Fix | Delete
protected function set_migration_status( $name, $migration_status ) {
[162] Fix | Delete
if ( ! \is_array( $migration_status ) || ! isset( $migration_status['version'] ) ) {
[163] Fix | Delete
return false;
[164] Fix | Delete
}
[165] Fix | Delete
$current_blog_id = \get_current_blog_id();
[166] Fix | Delete
[167] Fix | Delete
if ( ! isset( $this->migration_options[ $current_blog_id ] ) ) {
[168] Fix | Delete
$this->migration_options[ $current_blog_id ] = [];
[169] Fix | Delete
}
[170] Fix | Delete
$this->migration_options[ $current_blog_id ][ $name ] = $migration_status;
[171] Fix | Delete
[172] Fix | Delete
return \update_option( self::MIGRATION_OPTION_KEY . $name, $migration_status );
[173] Fix | Delete
}
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function