PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 All 32 releases
image-optimization / modules / core / components / migrations.php

migrations.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.6, at modules/core/components/migrations.php

73 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Core\Components;
4
5 use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook;
6 use ImageOptimization\Classes\Logger;
7 use ImageOptimization\Classes\Migration\{
8 Migration_Manager,
9 Migration_Meta,
10 };
11
12 use Throwable;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 class Migrations {
19 /**
20 * @async
21 * @param string $migration_name
22 *
23 * @return bool
24 */
25 public function run_migration( string $migration_name ): bool {
26 $migration_class = Migration_Manager::get_migration( $migration_name );
27
28 if ( ! $migration_class ) {
29 Logger::log(
30 Logger::LEVEL_ERROR,
31 "Migration class for `$migration_name` does not exist."
32 );
33
34 return false;
35 }
36
37 if ( ! method_exists( $migration_class, 'run' ) || ! is_callable( [ $migration_class, 'run' ] ) ) {
38 Logger::log(
39 Logger::LEVEL_ERROR,
40 "The run method does not exist or is not static in the class `$migration_class`."
41 );
42
43 return false;
44 }
45
46 try {
47 $migration_class::run();
48
49 ( new Migration_Meta() )
50 ->add_migration_passed( $migration_name )
51 ->save();
52
53 Logger::log(
54 Logger::LEVEL_INFO,
55 "The migration `$migration_name` successfully executed."
56 );
57 } catch ( Throwable $t ) {
58 Logger::log(
59 Logger::LEVEL_ERROR,
60 "Error while running the migration `$migration_name`: " . $t->getMessage()
61 );
62
63 return false;
64 }
65
66 return true;
67 }
68
69 public function __construct() {
70 add_action( Async_Operation_Hook::DB_MIGRATION, [ $this, 'run_migration' ] );
71 }
72 }
73