Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
102 changes: 102 additions & 0 deletions src/class-tiny-migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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 `tinypng_db_version` option.
Comment thread
tijmenbruggeman marked this conversation as resolved.
Outdated
*
* @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.

/**
* 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;
}

if ( $stored_version < 1 && ! self::migrate_meta_key_to_private() ) {
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.
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 boolean
*/
private static function migrate_meta_key_to_private() {
global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->update(
$wpdb->postmeta,
array( 'meta_key' => '_tiny_compress_images' ),
array( 'meta_key' => 'tiny_compress_images' ),
Comment thread
tijmenbruggeman marked this conversation as resolved.
Outdated
array( '%s' ),
array( '%s' )
);

if ( false === $result ) {
return false;
}
Comment thread
tijmenbruggeman marked this conversation as resolved.
Outdated

return true;
Comment thread
tijmenbruggeman marked this conversation as resolved.
Outdated
}
}
2 changes: 1 addition & 1 deletion src/config/class-tiny-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ 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';
}
7 changes: 6 additions & 1 deletion test/helpers/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class WordPressStubs
private $calls;
private $stubs;
private $filters;
public $postmeta = 'wp_postmeta';

public function __construct($vfs)
{
Expand Down Expand Up @@ -98,6 +99,7 @@ public function __construct($vfs)
$this->addMethod('get_locale');
$this->addMethod('wp_timezone_string');
$this->addMethod('update_option');
$this->addMethod('update');
$this->addMethod('check_ajax_referer');
$this->addMethod('wp_json_encode');
$this->addMethod('wp_send_json_error');
Expand Down Expand Up @@ -144,7 +146,10 @@ public function call($method, $args)
}
// Allow explicit stubs to override defaults/behaviors
if (isset($this->stubs[$method]) && $this->stubs[$method]) {
return call_user_func_array($this->stubs[$method], $args);
if (is_callable($this->stubs[$method])) {
return call_user_func_array($this->stubs[$method], $args);
}
return $this->stubs[$method];
Comment thread
tijmenbruggeman marked this conversation as resolved.
Outdated
}
if ('add_filter' === $method) {
$tag = isset($args[0]) ? $args[0] : '';
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
65 changes: 65 additions & 0 deletions test/unit/TinyMigrateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

require_once dirname(__FILE__) . '/TinyTestCase.php';
require_once dirname(__FILE__) . '/../../src/class-tiny-migrate.php';

class Tiny_Migrate_Test extends Tiny_TestCase
{

public function set_up()
{
parent::set_up();
$this->wp->stub('update', 1);
}
Comment thread
tijmenbruggeman marked this conversation as resolved.

Comment thread
tijmenbruggeman marked this conversation as resolved.
/**
* Helper to check if a specific option update occurred.
*/
private function assertOptionWasUpdated($option, $value)
{
$calls = $this->wp->getCalls('update_option');
foreach ($calls as $call) {
if (($call[0] ?? null) === $option && ($call[1] ?? null) === $value) {
return $this->assertTrue(true);
}
}
$this->fail("Failed asserting that option '$option' was updated to '$value'.");
}

public function test_run_skips_migration_when_db_version_is_current()
{
$this->wp->addOption(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION);

Tiny_Migrate::run();

$this->assertCount(0, $this->wp->getCalls('update'), 'Should not touch DB if version matches.');
}

public function test_run_performs_migration_and_updates_version()
{
Tiny_Migrate::run();

$update_calls = $this->wp->getCalls('update');
$this->assertCount(1, $update_calls);

list($table, $data, $where) = $update_calls[0];

$this->assertEquals('wp_postmeta', $table);
$this->assertEquals(['meta_key' => '_tiny_compress_images'], $data);
$this->assertEquals(['meta_key' => 'tiny_compress_images'], $where);

$this->assertOptionWasUpdated(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION);
}

Comment thread
tijmenbruggeman marked this conversation as resolved.
public function test_run_does_not_update_option_if_unnecessary()
{
$this->wp->addOption(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION);

Tiny_Migrate::run();

$option_calls = $this->wp->getCalls('update_option');
$version_updates = array_filter($option_calls, fn($call) => $call[0] === Tiny_Migrate::DB_VERSION_OPTION);

$this->assertEmpty($version_updates, 'Should not re-save the version if already current.');
}
}
3 changes: 3 additions & 0 deletions tiny-compress-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

require dirname( __FILE__ ) . '/src/config/class-tiny-config.php';
require dirname( __FILE__ ) . '/src/class-tiny-migrate.php';
require dirname( __FILE__ ) . '/src/class-tiny-helpers.php';
require dirname( __FILE__ ) . '/src/class-tiny-php.php';
require dirname( __FILE__ ) . '/src/class-tiny-wp-base.php';
Expand Down Expand Up @@ -37,6 +38,8 @@
require dirname( __FILE__ ) . '/src/class-tiny-compress-fopen.php';
}

Tiny_Migrate::run();
Comment thread
tijmenbruggeman marked this conversation as resolved.
Outdated

$tiny_plugin = new Tiny_Plugin();

register_uninstall_hook(
Expand Down
Loading