Repository navigation
Expand file tree
/
Copy pathclass-tiny-migrate.php
More file actions
147 lines (130 loc) · 4.09 KB
/
Copy pathclass-tiny-migrate.php
File metadata and controls
147 lines (130 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2026 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* Handles sequential database migrations for the TinyPNG plugin.
*
* Each migration method targets a specific version and is only executed
* once per site, tracked via the `DB_VERSION_OPTION` constant.
*
* @since 3.7.0
*/
class Tiny_Migrate {
/**
* The current database schema version.
*
* Increment this integer by 1 each time a new migration is added.
*
* @since 3.7.0
* @var int
*/
const DB_VERSION = 1;
/**
* WordPress option key used to track the applied database version.
*
* @since 3.7.0
* @var string
*/
const DB_VERSION_OPTION = 'tinypng_db_version';
/**
* When migration fails, will pause migration for an hour
* as long as the key exists in transient
*
* @since 3.7.0
* @var string
*/
const MIGRATION_BACKOFF_KEY = 'tinypng_migration_backoff';
/**
* Returns an ordered map of migrations keyed by version number.
*
* Each entry maps a version integer to a callable that performs the
* corresponding migration. Add new entries in ascending version order.
* Increment `DB_VERSION` when adding a new migration.
*
* @since 3.7.0
*
* @return array<int, callable> Ordered map of version to migration callable.
*/
private static function migrations() {
return array(
1 => array( self::class, 'migrate_meta_key_to_private' ),
);
}
/**
* Runs all pending migrations in version order.
*
* Compares the stored database version against each known migration
* and executes any that have not yet been applied. Updates the stored
* version upon completion.
*
* @since 3.7.0
*
* @return void
*/
public static function run() {
$stored_version = (int) get_option( self::DB_VERSION_OPTION, 0 );
if ( $stored_version >= self::DB_VERSION ) {
return;
}
foreach ( self::migrations() as $version => $migration ) {
if ( $stored_version >= $version ) {
continue;
}
if ( get_transient( self::MIGRATION_BACKOFF_KEY ) ) {
// transient key to hold migrations exists so exit early
return;
}
if ( ! call_user_func( $migration ) ) {
set_transient( self::MIGRATION_BACKOFF_KEY, 1, HOUR_IN_SECONDS );
return;
}
}
update_option( self::DB_VERSION_OPTION, self::DB_VERSION );
}
/**
* Migrates the tiny meta key from public to private.
*
* Renames all `tiny_compress_images` post meta entries to
* `_tiny_compress_images`.
*
* @since 3.7.0
*
* @return bool True on success or when there is nothing to migrate, false on DB error.
*/
private static function migrate_meta_key_to_private() {
global $wpdb;
$batch_size = 2500;
do {
$query =
"UPDATE $wpdb->postmeta
SET meta_key = '_tiny_compress_images'
WHERE meta_key = 'tiny_compress_images'
LIMIT " . $batch_size;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Renames a fixed internal meta key in fixed-size batches; only internal constants are interpolated.
$result = $wpdb->query( $query );
if ( false === $result ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( 'Tinify: failed to migrate meta key. DB error: ' . $wpdb->last_error );
return false;
}
} while ( $batch_size === (int) $result );
wp_cache_flush();
return true;
}
}