PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 19.1 All 128 releases
wordpress-seo / src / config / migration-status.php

migration-status.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/config/migration-status.php

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