| 1 |
<?php |
| 2 |
namespace YayMail\Migrations; |
| 3 |
|
| 4 |
/** |
| 5 |
* Migation Helper |
| 6 |
*/ |
| 7 |
class MigrationHelper { |
| 8 |
|
| 9 |
/** |
| 10 |
* Append third number to version if it does not exist yet |
| 11 |
* |
| 12 |
* @param string $version The version string to format. |
| 13 |
* @return string|null |
| 14 |
* @Example: 4.0 => 4.0.0 |
| 15 |
*/ |
| 16 |
public static function format_version_number( $version ) { |
| 17 |
if ( empty( $version ) ) { |
| 18 |
return null; |
| 19 |
} |
| 20 |
$parts = explode( '.', $version ); |
| 21 |
$version_parts_count = count( $parts ); |
| 22 |
while ( $version_parts_count < 3 ) { |
| 23 |
$parts[] = '0'; |
| 24 |
++$version_parts_count; |
| 25 |
} |
| 26 |
return implode( '.', $parts ); |
| 27 |
} |
| 28 |
|
| 29 |
public static function filter_migrations( array $migrations, $old_version, $new_version ): array { |
| 30 |
$filtered_migrations = array_filter( |
| 31 |
$migrations, |
| 32 |
function( $version ) use ( $old_version, $new_version ) { |
| 33 |
return version_compare( empty( $old_version ) ? '0.0.0' : $old_version, $version, '<' ) && version_compare( $new_version, $version, '>=' ); |
| 34 |
}, |
| 35 |
ARRAY_FILTER_USE_KEY |
| 36 |
); |
| 37 |
|
| 38 |
// Sort migrations by version |
| 39 |
uksort( $filtered_migrations, 'version_compare' ); |
| 40 |
|
| 41 |
return $filtered_migrations; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Check if the created date is within the specified time window from the current time. |
| 46 |
* |
| 47 |
* @param string $created_date The date to check, formatted as 'Y-m-d H:i:s'. |
| 48 |
* @param int $time_window The time window in seconds to check against. |
| 49 |
* @param string|null $reference_time Optional reference time to check against instead of current time. |
| 50 |
* @return bool True if the created date is within the time window, false otherwise. |
| 51 |
*/ |
| 52 |
public static function is_within_time_window( $created_date, $time_window, $reference_time = null ) { |
| 53 |
try { |
| 54 |
$compared_time = $reference_time ? new \DateTime( $reference_time, wp_timezone() ) : current_datetime(); |
| 55 |
$created = new \DateTime( $created_date, wp_timezone() ); |
| 56 |
return abs( $compared_time->getTimestamp() - $created->getTimestamp() ) <= $time_window; |
| 57 |
} catch ( \Exception $e ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Perform migrations for specified versions. |
| 65 |
* |
| 66 |
* @param array $migrations An associative array where keys are version numbers (e.g., '4.0.0') and values are fully qualified class names of the migration classes. |
| 67 |
* @param bool $skip_check_migration Whether to skip migration checks. |
| 68 |
* |
| 69 |
* @throws \Exception If the class for a version does not exist, or if the class or its instance does not have a callable `get_instance()` or `perform_migration()` method. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public static function perform_migrations( $migrations, $skip_check_migration = false ) { |
| 74 |
foreach ( $migrations as $version => $class ) { |
| 75 |
if ( ! class_exists( $class ) ) { |
| 76 |
throw new \Exception( esc_html( "Class $class for version $version does not exist." ), 500 ); |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! is_callable( [ $class, 'get_instance' ] ) ) { |
| 80 |
throw new \Exception( esc_html( "Class $class for version $version does not have a callable get_instance method." ), 500 ); |
| 81 |
} |
| 82 |
|
| 83 |
$migration_instance = $class::get_instance(); |
| 84 |
|
| 85 |
if ( ! is_callable( [ $migration_instance, 'perform_migration' ] ) ) { |
| 86 |
throw new \Exception( esc_html( "Instance of class $class for version $version does not have a callable perform_migration method." ), 500 ); |
| 87 |
} |
| 88 |
|
| 89 |
$migration_instance->perform_migration( $skip_check_migration ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|