Skip to content

Commit d2f12eb

Browse files
Merge pull request #90 from wcreateweb/feat/skip-wc-gallery-imgs
Skip WooCommerce gallery images
2 parents b24f0e6 + 0f61937 commit d2f12eb

File tree

12 files changed

+139
-100
lines changed

12 files changed

+139
-100
lines changed

.github/workflows/integration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
run: npm run test:playwright
7474
env:
7575
WORDPRESS_PORT: 80${{ matrix.wp }}
76+
PHP_VERSION: ${{ matrix.php }}
7677

7778
- uses: actions/upload-artifact@v4
7879
if: ${{ !cancelled() }}

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://tinypng.com/
44
Tags: compress images, compression, image size, page speed, performance
55
Requires at least: 4.0
66
Tested up to: 6.9
7-
Stable tag: 3.6.8
7+
Stable tag: 3.6.9
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -174,6 +174,9 @@ A: You can upgrade to a paid account by adding your *Payment details* on your [a
174174
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.
175175

176176
== Changelog ==
177+
= 3.6.9 =
178+
* fix: prevent picture element on product pages (WooCommerce)
179+
177180
= 3.6.8 =
178181
* feat: download logs and diagnostics
179182
* chore: updated phpcs rules and formatting

src/class-tiny-picture.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ class Tiny_Picture extends Tiny_WP_Base {
3838
/** @var array */
3939
private $allowed_domains = array();
4040

41+
/** @var Tiny_Settings */
42+
private $settings;
43+
4144
/**
4245
* Initialize the plugin.
4346
*
44-
* @param string $base_dir Absolute path (e.g. ABSPATH)
45-
* @param array $domains List of allowed domain URLs
47+
* @param Tiny_Settings $settings Tinify Settings
48+
* @param string $base_dir Absolute path (e.g. ABSPATH)
49+
* @param array $domains List of allowed domain URLs
4650
*/
47-
public function __construct( $base_dir = ABSPATH, $domains = array() ) {
51+
public function __construct( $settings, $base_dir = ABSPATH, $domains = array() ) {
52+
$this->settings = $settings;
4853
$this->base_dir = $base_dir;
4954
$this->allowed_domains = $domains;
5055

@@ -64,12 +69,20 @@ public function __construct( $base_dir = ABSPATH, $domains = array() ) {
6469
return;
6570
}
6671

67-
add_action(
68-
'template_redirect',
69-
function () {
70-
ob_start( array( $this, 'replace_sources' ), 1000 );
71-
}
72-
);
72+
add_action( 'template_redirect', array( $this, 'on_template_redirect' ) );
73+
}
74+
75+
public function on_template_redirect() {
76+
$conversion_enabled = $this->settings->get_conversion_enabled();
77+
if ( apply_filters( 'tiny_replace_with_picture', $conversion_enabled ) ) {
78+
/**
79+
* Controls wether the page should replace <img> with <picture> elements
80+
* converted sources.
81+
*
82+
* @since 3.6.9
83+
*/
84+
ob_start( array( $this, 'replace_sources' ), 1000 );
85+
}
7386
}
7487

7588
public function replace_sources( $content ) {

src/class-tiny-plugin.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,8 @@ public function init() {
7272
dirname( plugin_basename( __FILE__ ) ) . '/languages'
7373
);
7474

75-
if ( $this->settings->get_conversion_enabled() ) {
76-
/**
77-
* Controls wether the page should replace <img> with <picture> elements
78-
* converted sources.
79-
*
80-
* @since 3.7.0
81-
*/
82-
$should_replace = apply_filters( 'tiny_replace_with_picture', true );
83-
if ( $should_replace ) {
84-
new Tiny_Picture( ABSPATH, array( get_site_url() ) );
85-
}
86-
}
75+
new Tiny_Picture( $this->settings, ABSPATH, array( get_site_url() ) );
76+
$this->tiny_compatibility();
8777
}
8878

8979
public function cli_init() {
@@ -236,12 +226,14 @@ public function add_plugin_links( $current_links ) {
236226

237227
public function tiny_compatibility() {
238228
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
239-
$tiny_wpml_compatibility = new Tiny_WPML();
229+
new Tiny_WPML();
240230
}
241231

242232
if ( Tiny_AS3CF::is_active() ) {
243-
$tiny_as3cf = new Tiny_AS3CF( $this->settings );
233+
new Tiny_AS3CF( $this->settings );
244234
}
235+
236+
new Tiny_WooCommerce();
245237
}
246238

247239
public function compress_original_retina_image( $attachment_id, $path ) {

src/compatibility/as3cf/class-tiny-as3cf.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ class Tiny_AS3CF {
2525
* Checks wether the lite version is active
2626
*/
2727
public static function lite_is_active() {
28-
$lite_name = 'amazon-s3-and-cloudfront/wordpress-s3.php';
29-
30-
return is_plugin_active( $lite_name );
28+
return class_exists( 'Amazon_S3_And_CloudFront' );
3129
}
3230

3331
/**
3432
* Checks wether the pro version is active
3533
*/
3634
public static function pro_is_active() {
37-
$pro_name = 'amazon-s3-and-cloudfront-pro/amazon-s3-and-cloudfront-pro.php';
38-
39-
return is_plugin_active( $pro_name );
35+
return class_exists( 'Amazon_S3_And_CloudFront_Pro' );
4036
}
4137

4238
public function __construct( $settings ) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/*
3+
* Tiny Compress Images - WordPress plugin.
4+
* Copyright (C) 2015-2018 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 WooCommerce compatibility
23+
*
24+
* @since 3.6.9
25+
*/
26+
class Tiny_WooCommerce {
27+
28+
public function __construct() {
29+
if ( ! class_exists( 'WooCommerce' ) ) {
30+
return;
31+
}
32+
33+
$this->add_hooks();
34+
}
35+
36+
private function add_hooks() {
37+
add_filter( 'tiny_replace_with_picture', array( $this, 'skip_on_product_pages' ), 10, 1 );
38+
}
39+
40+
/**
41+
* We are skipping single product pages for now.
42+
* Variation images in the product gallery are injected through JavaScript but might never
43+
* display because the sourceset takes priority over the root img. The replacement is on the image and not
44+
* on the srcset.
45+
*
46+
* @since 3.6.9
47+
*
48+
* @param bool $should_replace Whether to replace images with picture elements.
49+
* @return bool False on product pages, otherwise unchanged.
50+
*/
51+
public function skip_on_product_pages( $should_replace ) {
52+
if ( is_product() ) {
53+
return false;
54+
}
55+
56+
return $should_replace;
57+
}
58+
}

test/integration/compatibility.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Page, expect, test } from '@playwright/test';
2-
import { activatePlugin, clearMediaLibrary, deactivatePlugin, enableCompressionSizes, getWPVersion, setAPIKey, setCompressionTiming, uploadMedia } from './utils';
2+
import { activatePlugin, clearMediaLibrary, deactivatePlugin, enableCompressionSizes, getPHPVersion, getWPVersion, setAPIKey, setCompressionTiming, uploadMedia } from './utils';
33

44
test.describe.configure({ mode: 'serial' });
55

@@ -36,9 +36,16 @@ test.describe('as3cf', () => {
3636
test.beforeAll(async ({ browser }) => {
3737
page = await browser.newPage();
3838
WPVersion = await getWPVersion(page);
39+
const phpVersion = getPHPVersion();
3940

40-
if (WPVersion < 5.5) {
41-
// Skipping test as it WP Offload does not support WordPress < 5.5
41+
if (WPVersion < 5.9) {
42+
// Skipping test as it WP Offload does not support WordPress < 5.9
43+
test.skip();
44+
return;
45+
}
46+
47+
if (phpVersion < 81) {
48+
// Skipping test as WP Offload Media requires PHP 8.1+
4249
test.skip();
4350
return;
4451
}

test/integration/utils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,26 @@ export async function getWPVersion(page: Page): Promise<number> {
160160
return parsedText;
161161
}
162162

163+
/**
164+
* @returns {number} retrieves the current PHP version from environment variable
165+
*/
166+
export function getPHPVersion(): number {
167+
const { PHP_VERSION } = process.env;
168+
if (!PHP_VERSION) {
169+
throw Error('PHP_VERSION is not set');
170+
}
171+
172+
return +PHP_VERSION;
173+
}
174+
163175
/**
164176
* @param {Page} page context
165177
* @param {string} pluginSlug slug of the plugin, ex 'tiny-compress-images'
166178
*/
167179
export async function activatePlugin(page: Page, pluginSlug: string) {
168180
await page.goto('/wp-admin/plugins.php');
169181

170-
const plugin = await page.locator('tr[data-slug="' + pluginSlug + '"]');
182+
const plugin = page.locator('tr[data-slug="' + pluginSlug + '"]').first();
171183
if (!plugin) {
172184
throw Error(`Plug-in ${pluginSlug} not found. Are you sure it is installed?`);
173185
}
@@ -192,7 +204,7 @@ export async function deactivatePlugin(page: Page, pluginSlug: string) {
192204
return;
193205
}
194206

195-
const plugin = await page.locator('tr[data-slug="' + pluginSlug + '"]');
207+
const plugin = await page.locator('tr[data-slug="' + pluginSlug + '"]').first();
196208
const className = await plugin.getAttribute('class');
197209
const pluginActivated = className === 'active';
198210
if (!pluginActivated) {

test/unit/TinyPictureTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public function set_up()
1212
{
1313
parent::set_up();
1414

15-
$this->tiny_picture = new Tiny_Picture($this->vfs->url(), array('https://www.tinifytest.com'));
15+
$settings = new Tiny_Settings();
16+
$this->tiny_picture = new Tiny_Picture($settings, $this->vfs->url(), array('https://www.tinifytest.com'));
1617
}
1718

1819
/**
@@ -335,7 +336,8 @@ public function test_does_not_register_hooks_when_pagebuilder_request()
335336
return false;
336337
});
337338

338-
$tiny_picture = new Tiny_Picture($this->vfs->url(), array('https://www.tinifytest.com'));
339+
$settings = new Tiny_Settings();
340+
$tiny_picture = new Tiny_Picture($settings, $this->vfs->url(), array('https://www.tinifytest.com'));
339341

340342
$template_redirect_registered = false;
341343
foreach ($this->wp->getCalls('add_action') as $call) {

test/unit/TinyTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
require_once dirname( __FILE__ ) . '/../helpers/wordpress.php';
66
require_once dirname( __FILE__ ) . '/../helpers/wordpress-cli.php';
77
require_once dirname( __FILE__ ) . '/../../src/config/class-tiny-config.php';
8+
require_once dirname( __FILE__ ) . '/../../src/compatibility/wpml/class-tiny-wpml.php';
9+
require_once dirname( __FILE__ ) . '/../../src/compatibility/as3cf/class-tiny-as3cf.php';
10+
require_once dirname( __FILE__ ) . '/../../src/compatibility/woocommerce/class-tiny-woocommerce.php';
811
require_once 'vendor/autoload.php';
912

1013
use org\bovigo\vfs\vfsStream;

0 commit comments

Comments
 (0)