Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3d81f3e
rename meta key to private meta key
tijmenbruggeman May 8, 2026
5d99e3a
add migration 370
tijmenbruggeman May 8, 2026
159d5e7
run migration on plugin load
tijmenbruggeman May 8, 2026
7cc6dde
add migration tests
tijmenbruggeman May 15, 2026
662a526
Change version to plain counter
tijmenbruggeman May 15, 2026
ab4f772
Rename migration
tijmenbruggeman May 15, 2026
ace767f
exit if migration fails
tijmenbruggeman May 15, 2026
d70b46c
consistent return value
tijmenbruggeman May 15, 2026
27bfcc7
add changelog entry
tijmenbruggeman May 15, 2026
5a5faac
cancel migration if it errored
tijmenbruggeman May 15, 2026
bde6368
remove redundent set
tijmenbruggeman May 15, 2026
48f2c24
move migration to plugins_loaded hook
tijmenbruggeman May 15, 2026
669853e
refer to the const instead of option
tijmenbruggeman May 15, 2026
da54abb
comment for clarity where nothing to migration is positive
tijmenbruggeman May 15, 2026
c535958
add test to validate migration failure
tijmenbruggeman May 15, 2026
044ea9a
create an order list of migrations
tijmenbruggeman May 15, 2026
9a75b4b
log error when migration failed
tijmenbruggeman May 15, 2026
454ad77
add backoff mechanism to retry migration each hour
tijmenbruggeman May 15, 2026
54027e0
format
tijmenbruggeman May 15, 2026
1029b73
add tests for backoff
tijmenbruggeman May 15, 2026
4c0f7fa
only add callable methods to stubs, swallow error logs in test
tijmenbruggeman May 15, 2026
d2641ac
change docs
tijmenbruggeman Jun 9, 2026
c5073f3
add wp_cache_flush to prevent stale meta data keys
tijmenbruggeman Jun 9, 2026
bc3c83d
migrate in batches of 2500
tijmenbruggeman Jun 9, 2026
3404849
fall back to legacy key when migrating
tijmenbruggeman Jun 22, 2026
dbf6aea
require php 5.6
tijmenbruggeman Jun 22, 2026
2c94ac8
define when undefined
tijmenbruggeman Jun 22, 2026
b602bdc
format
tijmenbruggeman Jun 22, 2026
7e2551d
stop swallwing errs
tijmenbruggeman Jun 22, 2026
4786c2e
use self as it is within class
tijmenbruggeman Jun 22, 2026
7538b01
revert php require
tijmenbruggeman Jun 22, 2026
60415a8
fix tests after moving func
tijmenbruggeman Jun 22, 2026
e514714
add legacy key to tests
tijmenbruggeman Jun 22, 2026
022e581
run migration on admin_init
tijmenbruggeman Jun 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ A: You can upgrade to a paid account by adding your *Payment details* on your [a
A: When the conversion feature is enabled (to convert images to AVIF or WebP), each image will use double the number of credits: one for compression and one for format conversion.

== Changelog ==
= 3.7.0 =
* chore: migrated meta key from `tiny_compress_images` to `_tiny_compress_images`
Comment thread
tijmenbruggeman marked this conversation as resolved.

= 3.6.14 =
* fix: added check for valid path before deleting converted image
* fix: use hook uninstall_plugin instead of uninstall.php to prevent dependency deletion
Expand Down
23 changes: 22 additions & 1 deletion src/class-tiny-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,30 @@ private function duplicate_check( $filenames, $file, $size_name ) {
return $filenames;
}

/**
* Will retrieve compression meta data for the given post_id.
*
* As migrations on large libraries can take longer, we will fall back on
* the legacy key on migrating. We can remove the LEGACY_META_KEY on 3.8.0.
*
* @since 3.7.0
*
* @param int $post_id Attachment ID.
* @return mixed The stored tiny metadata, or '' when none exists.
*/
public static function get_tiny_metadata( $post_id ) {
$tiny_metadata = get_post_meta( $post_id, Tiny_Config::META_KEY, true );

if ( empty( $tiny_metadata ) ) {
$tiny_metadata = get_post_meta( $post_id, Tiny_Config::LEGACY_META_KEY, true );
}
Comment thread
tijmenbruggeman marked this conversation as resolved.

return $tiny_metadata;
}

private function parse_tiny_metadata( $tiny_metadata = null ) {
if ( is_null( $tiny_metadata ) ) {
$tiny_metadata = get_post_meta( $this->id, Tiny_Config::META_KEY, true );
$tiny_metadata = self::get_tiny_metadata( $this->id );
}
if ( $tiny_metadata ) {
foreach ( $tiny_metadata as $size => $meta ) {
Expand Down
147 changes: 147 additions & 0 deletions src/class-tiny-migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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';
Comment thread
tijmenbruggeman marked this conversation as resolved.

/**
* 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' ),
);
}
Comment thread
tijmenbruggeman marked this conversation as resolved.

/**
* 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 );
}
Comment thread
tijmenbruggeman marked this conversation as resolved.
Comment thread
tijmenbruggeman marked this conversation as resolved.

/**
* 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;
}
}
3 changes: 2 additions & 1 deletion src/config/class-tiny-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class Tiny_Config {
const SHRINK_URL = 'https://api.tinify.com/shrink';
const KEYS_URL = 'https://api.tinify.com/keys';
const MONTHLY_FREE_COMPRESSIONS = 500;
const META_KEY = 'tiny_compress_images';
const META_KEY = '_tiny_compress_images';
const LEGACY_META_KEY = 'tiny_compress_images';
}
15 changes: 8 additions & 7 deletions test/fixtures/class-tiny-config.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

if (! defined('TINY_DEBUG')) {
define('TINY_DEBUG', null);
if ( ! defined( 'TINY_DEBUG' ) ) {
define( 'TINY_DEBUG', null );
}

class Tiny_Config
{
class Tiny_Config {
/* URL is only used by fopen driver. */
const SHRINK_URL = 'http://tinify-mock-api/shrink';
const KEYS_URL = 'http://tinify-mock-api/keys';
const SHRINK_URL = 'http://tinify-mock-api/shrink';
const KEYS_URL = 'http://tinify-mock-api/keys';
const MONTHLY_FREE_COMPRESSIONS = 500;
const META_KEY = 'tiny_compress_images';
const META_KEY = '_tiny_compress_images';
const LEGACY_META_KEY = 'tiny_compress_images';
}


// ajax hook to delete all attachments as doing it via UI is flaky
add_action( 'wp_ajax_clear_media_library', 'clear_media_library' );
function clear_media_library() {
Expand Down
30 changes: 30 additions & 0 deletions test/helpers/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

define('ABSPATH', dirname(__FILE__) . '/../');
define('WPINC', 'wp-includes-for-tests');
if (! defined('HOUR_IN_SECONDS')) {
define('HOUR_IN_SECONDS', 3600);
}
require_once dirname(__FILE__) . '/../' . WPINC . '/file.php';

use org\bovigo\vfs\vfsStream;
Expand Down Expand Up @@ -53,9 +56,12 @@ class WordPressStubs
private $admin_initFunctions;
private $options;
private $metadata;
private $transients;
private $calls;
private $stubs;
private $filters;
public $postmeta = 'wp_postmeta';
public $last_error = '';

public function __construct($vfs)
{
Expand Down Expand Up @@ -98,6 +104,12 @@ public function __construct($vfs)
$this->addMethod('get_locale');
$this->addMethod('wp_timezone_string');
$this->addMethod('update_option');
$this->addMethod('update');
$this->addMethod('query');
$this->addMethod('wp_cache_flush');
$this->addMethod('get_transient');
$this->addMethod('set_transient');
$this->addMethod('delete_transient');
$this->addMethod('check_ajax_referer');
$this->addMethod('wp_json_encode');
$this->addMethod('wp_send_json_error');
Expand All @@ -122,6 +134,7 @@ public function defaults()
$this->admin_initFunctions = array();
$this->options = new WordPressOptions();
$this->metadata = array();
$this->transients = array();
$this->filters = array();
$GLOBALS['_wp_additional_image_sizes'] = array();
}
Expand Down Expand Up @@ -185,6 +198,18 @@ public function call($method, $args)
}
if ('translate' === $method) {
return $args[0];
} elseif ('get_transient' === $method) {
$key = isset($args[0]) ? $args[0] : '';
return isset($this->transients[$key]) ? $this->transients[$key] : false;
} elseif ('set_transient' === $method) {
$key = isset($args[0]) ? $args[0] : '';
$value = isset($args[1]) ? $args[1] : '';
$this->transients[$key] = $value;
return true;
} elseif ('delete_transient' === $method) {
$key = isset($args[0]) ? $args[0] : '';
unset($this->transients[$key]);
return true;
} elseif ('get_option' === $method) {
return call_user_func_array(array($this->options, 'get'), $args);
} elseif ('get_post_meta' === $method) {
Expand Down Expand Up @@ -224,6 +249,11 @@ public function addOption($key, $value)
$this->options->set($key, $value);
}

public function addTransient($key, $value)
{
$this->transients[$key] = $value;
}

public function addImageSize($size, $values)
{
$GLOBALS['_wp_additional_image_sizes'][$size] = $values;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/TinyImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function set_up() {
}

public function test_tiny_post_meta_key_may_never_change() {
$this->assertEquals( '61b16225f107e6f0a836bf19d47aa0fd912f8925', sha1( Tiny_Config::META_KEY ) );
$this->assertEquals( '438fc52ce17b9aedf0cf70dea52d5551affba59a', sha1( Tiny_Config::META_KEY ) );
}

public function test_update_wp_metadata_should_not_update_with_no_resized_original() {
Expand Down
Loading
Loading