Skip to content

Commit 540cc4a

Browse files
Merge pull request #40 from wcreateweb/task/resolve-formatting
Resolve formatting issues
2 parents b1198b1 + 75901c4 commit 540cc4a

8 files changed

Lines changed: 279 additions & 22 deletions

File tree

.github/workflows/check-test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Check and Test
33
on:
44
push:
55
branches:
6-
- '**'
6+
- 'master'
77
pull_request:
88
branches:
99
- '**'
@@ -12,7 +12,6 @@ jobs:
1212
check:
1313
runs-on: ubuntu-latest
1414
timeout-minutes: 10
15-
continue-on-error: true # We haven't solved all formatting issues yet.
1615
strategy:
1716
matrix:
1817
php: ['7.4']

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# TinyPNG - JPEG, PNG & WebP image compression for WordPress
22

3+
[![Integration Tests](https://github.com/tinify/wordpress-plugin/actions/workflows/integration-tests.yml/badge.svg)](https://github.com/tinify/wordpress-plugin/actions/workflows/integration-tests.yml)
4+
[![Check and Test](https://github.com/tinify/wordpress-plugin/actions/workflows/check-test.yml/badge.svg)](https://github.com/tinify/wordpress-plugin/actions/workflows/check-test.yml)
5+
36
Make your website faster by optimizing your JPEG, PNG, and WebP images.
47

58
This plugin automatically optimizes your images by integrating with the

phpcs.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?xml version="1.0"?>
22
<ruleset>
3-
<config name="installed_paths" value="vendor/wp-coding-standards/wpcs"/>
3+
<config name="installed_paths" value="vendor/wp-coding-standards/wpcs" />
44

5-
<rule ref="WordPress-Core"/>
5+
<rule ref="WordPress-Core" />
66
<rule ref="Generic.Files.LineLength">
77
<properties>
8-
<property name="lineLimit" value="100"/>
9-
<property name="absoluteLineLimit" value="100"/>
8+
<property name="lineLimit" value="100" />
9+
<property name="absoluteLineLimit" value="100" />
10+
<property name="ignoreComments" value="true" />
1011
</properties>
1112
<exclude-pattern>src/views</exclude-pattern>
1213
</rule>
1314
<rule ref="WordPress.WP.I18n.UnorderedPlaceholders">
1415
<severity>0</severity>
1516
</rule>
1617

17-
<file>tiny-compress-images.php</file>
1818
<file>src</file>
1919
<file>test</file>
2020
<exclude-pattern>src/css</exclude-pattern>
2121
<exclude-pattern>src/data</exclude-pattern>
2222
<exclude-pattern>src/js</exclude-pattern>
2323
<exclude-pattern>vendor</exclude-pattern>
2424
<exclude-pattern>test/</exclude-pattern>
25-
</ruleset>
25+
</ruleset>

src/class-tiny-helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Tiny_Helpers {
2424
* truncate_text will truncate a string to a given length.
2525
* When text is longer than the given length, the string will be truncated and
2626
* the last characters will be replaced with an ellipsis.
27-
*
28-
* We can use mb_strlen & mb_substr as WordPress provides a compat function for
27+
*
28+
* We can use mb_strlen & mb_substr as WordPress provides a compat function for
2929
* it if mbstring php module is not installed.
3030
*
3131
* @param string $text the text

src/class-tiny-notices.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ public function show( $name, $message, $klass = 'error', $dismissible = true ) {
147147
}
148148

149149
$css = implode( ' ', $css );
150-
$plugin_name = esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
150+
$plugin_name = esc_html__(
151+
'TinyPNG - JPEG, PNG & WebP image compression',
152+
'tiny-compress-images'
153+
);
151154

152155
add_action( 'admin_notices',
153156
function() use ( $css, $name, $plugin_name, $message, $add ) {
@@ -268,7 +271,10 @@ public function incompatible_plugins_notice() {
268271
private function show_incompatible_plugins( $incompatible_plugins ) {
269272
$notice = '<div class="error notice tiny-notice incompatible-plugins">';
270273
$notice .= '<h3>';
271-
$notice .= esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
274+
$notice .= esc_html__(
275+
'TinyPNG - JPEG, PNG & WebP image compression',
276+
'tiny-compress-images'
277+
);
272278
$notice .= '</h3>';
273279
$notice .= '<p>';
274280
$notice .= esc_html__(
@@ -278,7 +284,10 @@ private function show_incompatible_plugins( $incompatible_plugins ) {
278284
$notice .= '</p>';
279285
$notice .= '<table>';
280286
$notice .= '<tr><td class="bullet">•</td><td class="name">';
281-
$notice .= esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
287+
$notice .= esc_html__(
288+
'TinyPNG - JPEG, PNG & WebP image compression',
289+
'tiny-compress-images'
290+
);
282291
$notice .= '</td><td></td></tr>';
283292
foreach ( $incompatible_plugins as $name => $file ) {
284293
$notice .= '<tr><td class="bullet">•</td><td class="name">';

src/class-tiny-plugin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ public function process_rpc_request() {
365365
}
366366

367367
public function compress_on_upload() {
368-
if (!wp_verify_nonce($_POST['_ajax_nonce'], 'new_media-' . $_POST['attachment_id'])) {
369-
exit;
370-
}
368+
if ( ! wp_verify_nonce( $_POST['_ajax_nonce'], 'new_media-' . $_POST['attachment_id'] ) ) {
369+
exit;
370+
}
371371
if ( current_user_can( 'upload_files' ) ) {
372372
$attachment_id = intval( $_POST['attachment_id'] );
373373
$metadata = $_POST['metadata'];

src/class-tiny-settings.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,9 @@ public function render_pending_status() {
859859
}
860860

861861
public function create_api_key() {
862-
if (!$this->check_ajax_referer()) {
863-
exit;
864-
}
862+
if ( ! $this->check_ajax_referer() ) {
863+
exit;
864+
}
865865
$compressor = $this->get_compressor();
866866
if ( ! current_user_can( 'manage_options' ) ) {
867867
$status = (object) array(
@@ -929,9 +929,9 @@ public function create_api_key() {
929929

930930
public function update_api_key() {
931931
$key = $_POST['key'];
932-
if (!$this->check_ajax_referer()) {
933-
exit;
934-
}
932+
if ( ! $this->check_ajax_referer() ) {
933+
exit;
934+
}
935935
if ( ! current_user_can( 'manage_options' ) ) {
936936
$status = (object) array(
937937
'ok' => false,

test/helpers/wordpress.php

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<?php
2+
3+
define( 'ABSPATH', dirname( dirname( __FILE__ ) ) . '/' );
4+
define( 'WPINC', 'wp-includes-for-tests' );
5+
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\content\LargeFileContent;
8+
9+
class WordPressOptions {
10+
private $values;
11+
12+
public function __construct() {
13+
$this->values = array(
14+
'thumbnail_size_w' => 150,
15+
'thumbnail_size_h' => 150,
16+
'medium_size_w' => 300,
17+
'medium_size_h' => 300,
18+
'medium_large_size_w' => 768,
19+
'medium_large_size_h' => 0,
20+
'large_size_w' => 1024,
21+
'large_size_h' => 1024,
22+
);
23+
}
24+
25+
public function set( $key, $value ) {
26+
if ( preg_match( '#^(.+)\[(.+)\]$#', $key, $match ) ) {
27+
if ( ! isset( $this->values[ $match[1] ] ) ) {
28+
$this->values[ $match[1] ] = array();
29+
}
30+
$this->values[ $match[1] ][ $match[2] ] = $value;
31+
} else {
32+
$this->values[ $key ] = $value;
33+
}
34+
}
35+
36+
public function get( $key, $default = null ) {
37+
return isset( $this->values[ $key ] ) ? $this->values[ $key ] : $default;
38+
}
39+
}
40+
41+
class WordPressStubs {
42+
const UPLOAD_DIR = 'wp-content/uploads';
43+
44+
private $vfs;
45+
private $initFunctions;
46+
private $admin_initFunctions;
47+
private $options;
48+
private $metadata;
49+
private $calls;
50+
private $stubs;
51+
52+
public function __construct( $vfs ) {
53+
$GLOBALS['wp'] = $this;
54+
$this->vfs = $vfs;
55+
$this->addMethod( 'add_action' );
56+
$this->addMethod( 'do_action' );
57+
$this->addMethod( 'add_filter' );
58+
$this->addMethod( 'register_setting' );
59+
$this->addMethod( 'add_settings_section' );
60+
$this->addMethod( 'add_settings_field' );
61+
$this->addMethod( 'get_option' );
62+
$this->addMethod( 'get_site_option' );
63+
$this->addMethod( 'update_site_option' );
64+
$this->addMethod( 'get_post_meta' );
65+
$this->addMethod( 'update_post_meta' );
66+
$this->addMethod( 'get_intermediate_image_sizes' );
67+
$this->addMethod( 'add_image_size' );
68+
$this->addMethod( 'translate' );
69+
$this->addMethod( 'load_plugin_textdomain' );
70+
$this->addMethod( 'get_post_mime_type' );
71+
$this->addMethod( 'get_plugin_data' );
72+
$this->addMethod( 'wp_upload_dir' );
73+
$this->addMethod( 'plugin_basename' );
74+
$this->addMethod( 'is_multisite' );
75+
$this->addMethod( 'current_user_can' );
76+
$this->addMethod( 'wp_get_attachment_metadata' );
77+
$this->addMethod( 'is_admin' );
78+
$this->defaults();
79+
$this->create_filesystem();
80+
}
81+
82+
public function create_filesystem() {
83+
vfsStream::newDirectory( self::UPLOAD_DIR )
84+
->at( $this->vfs );
85+
}
86+
87+
public function defaults() {
88+
$this->initFunctions = array();
89+
$this->admin_initFunctions = array();
90+
$this->options = new WordPressOptions();
91+
$this->metadata = array();
92+
$GLOBALS['_wp_additional_image_sizes'] = array();
93+
}
94+
95+
public function call( $method, $args ) {
96+
$this->calls[ $method ][] = $args;
97+
if ( 'add_action' === $method ) {
98+
if ( 'init' === $args[0] ) {
99+
$this->initFunctions[] = $args[1];
100+
} elseif ( 'admin_init' === $args[0] ) {
101+
$this->admin_initFunctions[] = $args[1];
102+
}
103+
}
104+
if ( 'translate' === $method ) {
105+
return $args[0];
106+
} elseif ( 'get_option' === $method ) {
107+
return call_user_func_array( array( $this->options, 'get' ), $args );
108+
} elseif ( 'get_post_meta' === $method ) {
109+
return call_user_func_array( array( $this, 'getMetadata' ), $args );
110+
} elseif ( 'add_image_size' === $method ) {
111+
return call_user_func_array( array( $this, 'addImageSize' ), $args );
112+
} elseif ( 'update_post_meta' === $method ) {
113+
return call_user_func_array( array( $this, 'updateMetadata' ), $args );
114+
} elseif ( 'get_intermediate_image_sizes' === $method ) {
115+
return array_merge( array( 'thumbnail', 'medium', 'medium_large', 'large' ), array_keys( $GLOBALS['_wp_additional_image_sizes'] ) );
116+
} elseif ( 'get_plugin_data' === $method ) {
117+
return array( 'Version' => '1.7.2' );
118+
} elseif ( 'wp_upload_dir' === $method ) {
119+
return array( 'basedir' => $this->vfs->url() . '/' . self::UPLOAD_DIR, 'baseurl' => '/' . self::UPLOAD_DIR );
120+
} elseif ( 'is_admin' === $method ) {
121+
return true;
122+
} elseif ( $this->stubs[ $method ] ) {
123+
return call_user_func_array( $this->stubs[ $method ], $args );
124+
}
125+
}
126+
127+
public function addMethod( $method ) {
128+
$this->calls[ $method ] = array();
129+
$this->stubs[ $method ] = array();
130+
if ( ! function_exists( $method ) ) {
131+
eval( "function $method() { return \$GLOBALS['wp']->call('$method', func_get_args()); }" );
132+
}
133+
}
134+
135+
public function addOption( $key, $value ) {
136+
$this->options->set( $key, $value );
137+
}
138+
139+
public function addImageSize( $size, $values ) {
140+
$GLOBALS['_wp_additional_image_sizes'][ $size ] = $values;
141+
}
142+
143+
public function getMetadata( $id, $key, $single = false ) {
144+
$values = isset( $this->metadata[ $id ] ) ? $this->metadata[ $id ] : array();
145+
$value = isset( $values[ $key ] ) ? $values[ $key ] : '';
146+
return $single ? $value : array( $value );
147+
}
148+
149+
public function updateMetadata( $id, $key, $values ) {
150+
$this->metadata[ $id ][ $key ] = $values;
151+
}
152+
153+
public function setTinyMetadata( $id, $values ) {
154+
$this->metadata[ $id ] = array( Tiny_Config::META_KEY => $values );
155+
}
156+
157+
public function getCalls( $method ) {
158+
return $this->calls[ $method ];
159+
}
160+
161+
public function init() {
162+
foreach ( $this->initFunctions as $func ) {
163+
call_user_func( $func );
164+
}
165+
}
166+
167+
public function admin_init() {
168+
foreach ( $this->admin_initFunctions as $func ) {
169+
call_user_func( $func );
170+
}
171+
}
172+
173+
public function stub( $method, $func ) {
174+
$this->stubs[ $method ] = $func;
175+
}
176+
177+
public function createImage( $file_size, $path, $name ) {
178+
if ( ! $this->vfs->hasChild( self::UPLOAD_DIR . "/$path" ) ) {
179+
vfsStream::newDirectory( self::UPLOAD_DIR . "/$path" )->at( $this->vfs );
180+
}
181+
$dir = $this->vfs->getChild( self::UPLOAD_DIR . "/$path" );
182+
183+
vfsStream::newFile( $name )
184+
->withContent( new LargeFileContent( $file_size ) )
185+
->at( $dir );
186+
}
187+
188+
public function createImages( $sizes = null, $original_size = 12345, $path = '14/01', $name = 'test' ) {
189+
vfsStream::newDirectory( self::UPLOAD_DIR . "/$path" )->at( $this->vfs );
190+
$dir = $this->vfs->getChild( self::UPLOAD_DIR . '/' . $path );
191+
192+
vfsStream::newFile( "$name.png" )
193+
->withContent( new LargeFileContent( $original_size ) )
194+
->at( $dir );
195+
196+
if ( is_null( $sizes ) ) {
197+
$sizes = array( 'thumbnail' => 100, 'medium' => 1000 , 'large' => 10000, 'post-thumbnail' => 1234 );
198+
}
199+
200+
foreach ( $sizes as $key => $size ) {
201+
vfsStream::newFile( "$name-$key.png" )
202+
->withContent( new LargeFileContent( $size ) )
203+
->at( $dir );
204+
}
205+
}
206+
207+
public function createImagesFromJSON( $virtual_images ) {
208+
foreach ( $virtual_images['images'] as $image ) {
209+
self::createImage( $image['size'], $virtual_images['path'], $image['file'] );
210+
}
211+
}
212+
213+
public function getTestMetadata( $path = '14/01', $name = 'test' ) {
214+
$metadata = array(
215+
'file' => "$path/$name.png",
216+
'width' => 4000,
217+
'height' => 3000,
218+
'sizes' => array(),
219+
);
220+
221+
$regex = '#^' . preg_quote( $name ) . '-([^.]+)[.](png|jpe?g)$#';
222+
$dir = $this->vfs->getChild( self::UPLOAD_DIR . "/$path" );
223+
foreach ( $dir->getChildren() as $child ) {
224+
$file = $child->getName();
225+
if ( preg_match( $regex, $file, $match ) ) {
226+
$metadata['sizes'][ $match[1] ] = array( 'file' => $file );
227+
}
228+
}
229+
230+
return $metadata;
231+
}
232+
}
233+
234+
class WP_HTTP_Proxy {
235+
public function is_enabled() {
236+
return false;
237+
}
238+
}
239+
240+
function __( $text, $domain = 'default' ) {
241+
return translate( $text, $domain );
242+
}
243+
244+
function esc_html__( $text, $domain = 'default' ) {
245+
return translate( $text, $domain );
246+
}

0 commit comments

Comments
 (0)