Skip to content

Commit 4aa6131

Browse files
Merge pull request #42 from wcreateweb/task/as3cf-compatibility
AS3CF (WP Offload) compatibility
2 parents cf89f81 + c2c57f7 commit 4aa6131

18 files changed

Lines changed: 371 additions & 169 deletions

.wp-env.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
{
2-
"plugins": [ "." ]
3-
}
2+
"plugins": ["."],
3+
"config": {
4+
"AWS_ENDPOINT": "http://host.docker.internal:4566",
5+
"AWS_USE_PATH_STYLE_ENDPOINT": "true",
6+
"AWS_ACCESS_KEY_ID": "test_key_id",
7+
"AWS_SECRET_ACCESS_KEY": "test_secret_access_key",
8+
"AWS_REGION": "eu-central-1",
9+
"AWS_DEFAULT_REGION": "eu-central-1"
10+
}
11+
}

bin/integration-tests

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,23 @@ function prepare_test_config {
5555
}
5656

5757
function restore_config {
58-
mv src/config/class-tiny-config.php.bak src/config/class-tiny-config.php
59-
mv src/vendor/tinify/Tinify/Client.php.bak src/vendor/tinify/Tinify/Client.php
58+
mv src/vendor/tinify/Tinify/Client.php.bak src/vendor/tinify/Tinify/Client.php
59+
mv src/config/class-tiny-config.php.bak src/config/class-tiny-config.php
6060
}
6161

6262
function start_services {
63-
docker build -t mock-webservice -f config/Dockerfile-mock-webservice . || { trap - EXIT; exit 1; }
64-
docker run -d --name tinify-mock-api -p ${MOCK_PORT}:80 -v $(pwd)/test/mock-tinypng-webservice:/var/www/html mock-webservice || { trap - EXIT; exit 1; }
63+
echo "Starting Mocks.."
64+
docker compose -f config/mocks.docker-compose.yml up -d
6565

66+
echo "Starting WordPress.."
6667
npx wp-env start
68+
69+
echo "Installing compatible plugins.."
70+
npx wp-env run tests-cli wp plugin install amazon-s3-and-cloudfront
6771
}
6872

6973
function stop_services {
70-
docker stop $(docker ps -q --filter "ancestor=mock-webservice") || { trap - EXIT; exit 1; }
71-
docker rm $(docker ps -a -q --filter "ancestor=mock-webservice") || { trap - EXIT; exit 1; }
74+
docker compose -f config/mocks.docker-compose.yml down
7275

7376
npx wp-env stop
7477
}

bin/run-mocks

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
port="$1"
44

5-
MOCK_PORT=${port:-8100}
5+
docker compose -f config/mocks.docker-compose.yml up -d
66

7-
docker build -t mock-webservice -f config/Dockerfile-mock-webservice .
8-
docker run -d --name tinify-mock-api -p ${MOCK_PORT}:80 -v $(pwd)/test/mock-tinypng-webservice:/var/www/html mock-webservice
7+
mv src/vendor/tinify/Tinify/Client.php src/vendor/tinify/Tinify/Client.php.bak
8+
cp test/fixtures/Client.php src/vendor/tinify/Tinify/Client.php
9+
10+
mv src/config/class-tiny-config.php src/config/class-tiny-config.php.bak
11+
cp test/fixtures/class-tiny-config.php src/config/class-tiny-config.php
912

1013
echo "Replacing configuration files..."
1114
mv src/vendor/tinify/Tinify/Client.php src/vendor/tinify/Tinify/Client.php.bak

bin/run-wordpress

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ export WP_ENV_PHP_VERSION=${PHP_VERSION_FULL}
3434

3535
npx wp-env start
3636

37+
echo "Installing compatible plugins.."
38+
npx wp-env run tests-cli wp plugin install amazon-s3-and-cloudfront
39+
3740
echo "To stop, run: bin/stop-wordpress"

bin/stop-mocks

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/bash
22

3-
echo "Putting back configuration files..."
4-
mv src/config/class-tiny-config.php.bak src/config/class-tiny-config.php
3+
echo "Restoring config.."
54
mv src/vendor/tinify/Tinify/Client.php.bak src/vendor/tinify/Tinify/Client.php
5+
mv src/config/class-tiny-config.php.bak src/config/class-tiny-config.php
66

7-
echo "Shutting down containers..."
8-
docker stop $(docker ps -q --filter "ancestor=mock-webservice")
9-
docker rm $(docker ps -a -q --filter "ancestor=mock-webservice")
7+
echo "Shutting down mocks.."
8+
docker compose -f config/mocks.docker-compose.yml down

config/localstack-init.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
awslocal s3 mb s3://tinytest
3+

config/mocks.docker-compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3"
2+
3+
name: tinify-mocks
4+
5+
services:
6+
localstack:
7+
image: localstack/localstack
8+
volumes:
9+
- "./localstack-init.sh:/etc/localstack/init/ready.d/init-aws.sh"
10+
ports:
11+
- 4566:4566
12+
tinify-mock-api:
13+
image: mock-webservice
14+
build:
15+
dockerfile: ./Dockerfile-mock-webservice
16+
ports:
17+
- 8100:80
18+
volumes:
19+
- ../test/mock-tinypng-webservice:/var/www/html

src/class-tiny-image.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -171,39 +171,11 @@ public function get_mime_type() {
171171
return get_post_mime_type( $this->id );
172172
}
173173

174-
public function download_missing_image_sizes() {
175-
global $as3cf;
176-
177-
if ( ! $as3cf || ! $as3cf->is_plugin_setup() ) {
178-
error_log( 'offload s3 plugin not configured..' );
179-
return;
180-
}
181-
182-
$s3_data = get_post_meta( $this->id, 'amazonS3_info', true );
183-
if ( ! $s3_data ) {
184-
return;
185-
}
186-
187-
$path = dirname( $s3_data['key'] );
188-
189-
foreach ( $this->sizes as $size_name => $size ) {
190-
$local_file_path = get_home_path() . $s3_data['key'];
191-
192-
$s3_data['key'] = wp_normalize_path( $path . '/' . basename( $size->filename ) );
193-
$as3cf->plugin_compat->copy_s3_file_to_server( $s3_data, $size->filename );
194-
}
195-
}
196-
197174
public function compress() {
198175
if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) {
199176
return;
200177
}
201178

202-
/* Integration tests need to be written before this can be enabled. */
203-
// if ( $this->settings->has_offload_s3_installed() ) {
204-
// $this->download_missing_image_sizes();
205-
// }
206-
207179
$success = 0;
208180
$failed = 0;
209181

@@ -447,24 +419,6 @@ public function get_statistics( $active_sizes, $active_tinify_sizes ) {
447419
}
448420
}// End foreach().
449421

450-
/*
451-
When an image hasn't yet been optimized but only exists on S3, we still need to
452-
know the total size of the image sizes for the bulk optimization tool.
453-
TODO: First write integration tests before enabling this again.
454-
455-
if (
456-
0 === $this->statistics['initial_total_size'] &&
457-
0 === $this->statistics['optimized_total_size'] &&
458-
$this->settings->has_offload_s3_installed()
459-
) {
460-
$s3_data = get_post_meta( $this->id, 'wpos3_filesize_total', true );
461-
if ( $s3_data ) {
462-
$this->statistics['initial_total_size'] = $s3_data;
463-
$this->statistics['optimized_total_size'] = $s3_data;
464-
}
465-
}
466-
*/
467-
468422
return $this->statistics;
469423
}
470424

src/class-tiny-notices.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,6 @@ public function outdated_platform_notice() {
245245
} // End if().
246246
}
247247

248-
public function show_offload_s3_notice() {
249-
$message = esc_html__(
250-
'Removing files from the server is incompatible with background compressions. Images will still be automatically compressed, but no longer in the background.', // WPCS: Needed for proper translation.
251-
'tiny-compress-images'
252-
);
253-
$this->show( 'offload-s3', $message, 'notice-error', false );
254-
}
255-
256-
public function old_offload_s3_version_notice() {
257-
$message = esc_html__(
258-
'Background compressions are not compatible with the version of WP Offload S3 you have installed. Please update to version 0.7.2 at least.', // WPCS: Needed for proper translation.
259-
'tiny-compress-images'
260-
);
261-
$this->show( 'old-offload-s3-version', $message, 'notice-error', false );
262-
}
263-
264248
public function incompatible_plugins_notice() {
265249
$incompatible_plugins = array_filter( self::$incompatible_plugins, 'is_plugin_active' );
266250
if ( count( $incompatible_plugins ) > 0 ) {

src/class-tiny-plugin.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ public function tiny_compatibility() {
181181
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
182182
$tiny_wpml_compatibility = new Tiny_WPML();
183183
}
184+
185+
if ( Tiny_AS3CF::is_active() ) {
186+
$tiny_as3cf = new Tiny_AS3CF( $this->settings );
187+
}
184188
}
185189

186190
public function compress_original_retina_image( $attachment_id, $path ) {
@@ -266,8 +270,7 @@ public function enqueue_scripts( $hook ) {
266270
public function process_attachment( $metadata, $attachment_id ) {
267271
if ( $this->settings->auto_compress_enabled() ) {
268272
if (
269-
$this->settings->background_compress_enabled() &&
270-
! $this->settings->remove_local_files_setting_enabled()
273+
$this->settings->background_compress_enabled()
271274
) {
272275
$this->async_compress_on_upload( $metadata, $attachment_id );
273276
} else {

0 commit comments

Comments
 (0)