Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortpixel Image Optimizer Incompatibility #11837

Merged
merged 19 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'integrations.cfi' => \Google\Web_Stories\Integrations\Conditional_Featured_Image::class,
'integrations.sitekit' => \Google\Web_Stories\Integrations\Site_Kit::class,
'integrations.themes_support' => \Google\Web_Stories\Integrations\Core_Themes_Support::class,
'integrations.shortpixel' => \Google\Web_Stories\Integrations\ShortPixel::class,
timarney marked this conversation as resolved.
Show resolved Hide resolved
'imgareaselect_patch' => \Google\Web_Stories\Admin\ImgAreaSelect_Patch::class,
'kses' => \Google\Web_Stories\KSES::class,
'media.base_color' => \Google\Web_Stories\Media\Base_Color::class,
Expand Down
59 changes: 59 additions & 0 deletions includes/Integrations/ShortPixel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Class ShortPixel.
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2022 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Web_Stories\Integrations;

use Google\Web_Stories\Service_Base;

/**
* Class ShortPixel
*/
class ShortPixel extends Service_Base {

/**
* Runs on instantiation.
*
* @since 1.23.0
*/
public function register(): void {
add_filter( 'shortpixel_image_urls', [ $this, 'image_urls' ], 10, 1 );
timarney marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Ensures page template urls bypass optimisation.
*
* @since 1.23.0
*
* @param string[] $urls Urls that will be sent to optimisation.
* @return string[] The filtered Urls.
*/
public function image_urls( $urls ): array {
if ( false !== strpos( $urls[0], 'web-stories-page-template' ) ) {
timarney marked this conversation as resolved.
Show resolved Hide resolved
return [];
}
return $urls;
}
}
1 change: 1 addition & 0 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Plugin extends ServiceBasedPlugin {
'integrations.cfi' => Integrations\Conditional_Featured_Image::class,
'integrations.sitekit' => Integrations\Site_Kit::class,
'integrations.themes_support' => Integrations\Core_Themes_Support::class,
'integrations.shortpixel' => Integrations\ShortPixel::class,
'imgareaselect_patch' => Admin\ImgAreaSelect_Patch::class,
'kses' => KSES::class,
'font_post_type' => Font_Post_Type::class,
Expand Down
55 changes: 55 additions & 0 deletions tests/phpunit/integration/tests/Integrations/ShortPixel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Web_Stories\Tests\Integration\Integrations;

use Google\Web_Stories\Tests\Integration\TestCase;
timarney marked this conversation as resolved.
Show resolved Hide resolved

/**
* @coversDefaultClass \Google\Web_Stories\Integrations\ShortPixel
*/
class ShortPixel extends TestCase {
timarney marked this conversation as resolved.
Show resolved Hide resolved
/**
* @covers ::register
*/
public function test_register(): void {
$short_pixel = new \Google\Web_Stories\Integrations\ShortPixel();
$short_pixel->register();
$this->assertSame( 10, has_filter( 'shortpixel_image_urls', [ $short_pixel, 'image_urls' ] ) );
timarney marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @covers ::image_urls
*/
public function test_image_urls(): void {
$short_pixel = new \Google\Web_Stories\Integrations\ShortPixel();
$urls = [ 'http://localhost:8899/wp-content/uploads/2022/03/web-stories-page-template-768.jpg' ];
$result = $short_pixel->image_urls( $urls, 10 );
timarney marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEqualSets( [], $result );
}

/**
* @covers ::image_urls
*/
public function test_image_urls_non_page_template_image(): void {
$short_pixel = new \Google\Web_Stories\Integrations\ShortPixel();
/* given a non "page-template" image the urls should pass thru */
$urls = [ 'http://localhost:8899/wp-content/uploads/2022/03/example-750.jpg' ];
$result = $short_pixel->image_urls( $urls, 10 );
timarney marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEqualSets( $urls, $result );
}
}