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