Skip to content

Commit 70c1df1

Browse files
Format
1 parent 96504f4 commit 70c1df1

File tree

3 files changed

+61
-68
lines changed

3 files changed

+61
-68
lines changed

src/class-tiny-picture.php

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,37 @@
1818
* with this program; if not, write to the Free Software Foundation, Inc., 51
1919
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
*/
21-
class Tiny_Picture
22-
{
23-
public static function init()
24-
{
25-
if (is_admin() || is_customize_preview()) {
21+
class Tiny_Picture {
22+
23+
public static function init() {
24+
if ( is_admin() || is_customize_preview() ) {
2625
return;
2726
}
2827

29-
if (defined('DOING_AJAX') && DOING_AJAX) {
28+
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
3029
return;
3130
}
3231

33-
if (defined('DOING_CRON') && DOING_CRON) {
32+
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
3433
return;
3534
}
3635

3736
add_action('template_redirect', function () {
38-
ob_start([self::class, 'replace_img_with_picture_tag']);
37+
ob_start( [ self::class, 'replace_img_with_picture_tag' ] );
3938
}, 1000);
4039
}
4140

42-
public static function replace_img_with_picture_tag($content)
43-
{
44-
$images = Tiny_Picture::filter_images($content);
41+
public static function replace_img_with_picture_tag( $content ) {
42+
$images = Tiny_Picture::filter_images( $content );
4543

46-
foreach ($images as $image) {
47-
$content = Tiny_Picture::replace_image($content, $image);
44+
foreach ( $images as $image ) {
45+
$content = Tiny_Picture::replace_image( $content, $image );
4846
}
4947
return $content;
5048
}
5149

52-
private static function replace_image($content, $image)
53-
{
54-
$content = str_replace($image->img_element, $image->get_picture_element(), $content);
50+
private static function replace_image( $content, $image ) {
51+
$content = str_replace( $image->img_element, $image->get_picture_element(), $content );
5552
return $content;
5653
}
5754

@@ -60,35 +57,34 @@ private static function replace_image($content, $image)
6057
*
6158
* @return Tiny_Image[]
6259
*/
63-
private static function filter_images($content)
64-
{
65-
if (preg_match('/(?=<body).*<\/body>/is', $content, $body)) {
60+
private static function filter_images( $content ) {
61+
if ( preg_match( '/(?=<body).*<\/body>/is', $content, $body ) ) {
6662
$content = $body[0];
6763
}
6864

69-
$content = preg_replace('/<!--(.*)-->/Uis', '', $content);
65+
$content = preg_replace( '/<!--(.*)-->/Uis', '', $content );
7066

71-
$content = preg_replace('#<noscript(.*?)>(.*?)</noscript>#is', '', $content);
67+
$content = preg_replace( '#<noscript(.*?)>(.*?)</noscript>#is', '', $content );
7268

73-
if (! preg_match_all('/<img\s.*>/isU', $content, $matches)) {
69+
if ( ! preg_match_all( '/<img\s.*>/isU', $content, $matches ) ) {
7470
return array();
7571
}
7672

77-
$images = array_map(function ($img) {
78-
return new Tiny_Picture_Element($img);
73+
$images = array_map(function ( $img ) {
74+
return new Tiny_Picture_Element( $img );
7975
}, $matches[0]);
80-
$images = array_filter($images);
76+
$images = array_filter( $images );
8177

82-
if (! $images || ! is_array($images)) {
78+
if ( ! $images || ! is_array( $images ) ) {
8379
return array();
8480
}
8581

8682
return $images;
8783
}
8884
}
8985

90-
class Tiny_Picture_Element
91-
{
86+
class Tiny_Picture_Element {
87+
9288

9389

9490
/**
@@ -104,15 +100,14 @@ class Tiny_Picture_Element
104100
private $img_element_node;
105101

106102
private $base_dir;
107-
103+
108104
private $site_url;
109105

110-
public function __construct($img_element)
111-
{
106+
public function __construct( $img_element ) {
112107
$this->img_element = $img_element;
113108
$dom = new \DOMDocument();
114-
$dom->loadHTML($img_element);
115-
$this->img_element_node = $dom->getElementsByTagName('img')->item(0);
109+
$dom->loadHTML( $img_element );
110+
$this->img_element_node = $dom->getElementsByTagName( 'img' )->item( 0 );
116111

117112
$this->base_dir = get_home_path();
118113
$this->site_url = get_site_url();
@@ -123,48 +118,46 @@ public function __construct($img_element)
123118
*
124119
* @return array{path: string, size: string}[] The image sources
125120
*/
126-
private function get_image_srcsets()
127-
{
121+
private function get_image_srcsets() {
128122
$result = array();
129-
$srcset = $this->img_element_node->getAttribute('srcset');
123+
$srcset = $this->img_element_node->getAttribute( 'srcset' );
130124

131-
if ($srcset) {
125+
if ( $srcset ) {
132126
// Split the srcset by commas to get individual entries
133-
$srcset_entries = explode(',', $srcset);
127+
$srcset_entries = explode( ',', $srcset );
134128

135-
foreach ($srcset_entries as $entry) {
129+
foreach ( $srcset_entries as $entry ) {
136130
// Trim whitespace
137-
$entry = trim($entry);
131+
$entry = trim( $entry );
138132

139133
// Split by whitespace to separate path and size descriptor
140-
$parts = preg_split('/\s+/', $entry, 2);
134+
$parts = preg_split( '/\s+/', $entry, 2 );
141135

142-
if (count($parts) === 2) {
136+
if ( count( $parts ) === 2 ) {
143137
// We have both path and size
144138
$result[] = array(
145139
'path' => $parts[0],
146140
'size' => $parts[1],
147141
);
148-
} elseif (count($parts) === 1) {
142+
} elseif ( count( $parts ) === 1 ) {
149143
// We only have a path (unusual in srcset)
150144
$result[] = array(
151145
'path' => $parts[0],
152146
'size' => '',
153147
);
154148
}
155149
}
156-
} elseif ($this->img_element_node->hasAttribute('src')) {
150+
} elseif ( $this->img_element_node->hasAttribute( 'src' ) ) {
157151
// No srcset, but we have a src attribute
158152
$result[] = array(
159-
'path' => $this->img_element_node->getAttribute('src'),
153+
'path' => $this->img_element_node->getAttribute( 'src' ),
160154
'size' => '',
161155
);
162156
}
163157
return $result;
164158
}
165159

166-
private function get_local_path($url)
167-
{
160+
private function get_local_path( $url ) {
168161
if ( strpos( $url, 'http' ) === 0 ) {
169162
if ( strpos( $url, $this->site_url ) !== 0 ) {
170163
// url is not the current site
@@ -179,44 +172,42 @@ private function get_local_path($url)
179172
return $url;
180173
}
181174

182-
private function get_formatted_source($imgsrcs, $mimetype)
183-
{
175+
private function get_formatted_source( $imgsrcs, $mimetype ) {
184176
$formatted_src_set = array();
185-
foreach ($imgsrcs as $imgsrc) {
186-
$format_url = Tiny_Helpers::replace_file_extension($mimetype, $imgsrc['path']);
187-
$local_path = $this->get_local_path($format_url);
188-
if (empty($local_path)) {
177+
foreach ( $imgsrcs as $imgsrc ) {
178+
$format_url = Tiny_Helpers::replace_file_extension( $mimetype, $imgsrc['path'] );
179+
$local_path = $this->get_local_path( $format_url );
180+
if ( empty( $local_path ) ) {
189181
continue;
190182
}
191-
$exists_local = file_exists($local_path);
192-
if ($exists_local) {
183+
$exists_local = file_exists( $local_path );
184+
if ( $exists_local ) {
193185
$formatted_src_set[] = $format_url . ' ' . $imgsrc['size'];
194186
}
195187
}
196188

197-
if (empty($formatted_src_set)) {
189+
if ( empty( $formatted_src_set ) ) {
198190
// no alternative sources found
199191
return '';
200192
}
201193

202-
$source_set = implode(', ', $formatted_src_set);
203-
$trimmed_source_set = trim($source_set);
194+
$source_set = implode( ', ', $formatted_src_set );
195+
$trimmed_source_set = trim( $source_set );
204196
return '<source type="' . $mimetype . '" srcset="' . $trimmed_source_set . '">';
205197
}
206198

207-
public function get_picture_element()
208-
{
199+
public function get_picture_element() {
209200
$srcsets = $this->get_image_srcsets();
210201

211-
$avif = $this->get_formatted_source($srcsets, 'image/avif');
212-
$webp = $this->get_formatted_source($srcsets, 'image/webp');
213-
if (empty($avif) && empty($webp)) {
202+
$avif = $this->get_formatted_source( $srcsets, 'image/avif' );
203+
$webp = $this->get_formatted_source( $srcsets, 'image/webp' );
204+
if ( empty( $avif ) && empty( $webp ) ) {
214205
return $this->img_element;
215206
}
216207

217208
$picture = '<picture>';
218-
$picture .= $this->get_formatted_source($srcsets, 'image/avif');
219-
$picture .= $this->get_formatted_source($srcsets, 'image/webp');
209+
$picture .= $this->get_formatted_source( $srcsets, 'image/avif' );
210+
$picture .= $this->get_formatted_source( $srcsets, 'image/webp' );
220211
$picture .= $this->img_element;
221212

222213
$picture .= '</picture>';

src/class-tiny-plugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function version() {
4040
public function __construct() {
4141
parent::__construct();
4242
$this->settings = new Tiny_Settings();
43-
if ($this->settings->get_conversion_enabled()) {
43+
if ( $this->settings->get_conversion_enabled() ) {
4444
Tiny_Picture::init();
4545
}
4646
}
@@ -638,7 +638,7 @@ public function render_bulk_optimization_page() {
638638
$images_to_convert = $stats['available-unoptimised-sizes'];
639639

640640
$conversion_enabled = $this->settings->get_conversion_enabled();
641-
if ($conversion_enabled) {
641+
if ( $conversion_enabled ) {
642642
$images_to_convert *= 2;
643643
}
644644
$estimated_costs = Tiny_Compress::estimate_cost(

src/class-tiny-settings.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,9 @@ public function render_format_conversion() {
967967
echo '<p class="tiny-check">';
968968
echo '<input type="checkbox" id="' . $convertopts_convert_id . '" name="' . $convertopts_convert . '" value="on"' . $convertopts_convert_checked . '/>';
969969
echo '<label for="' . $convertopts_convert_id . '">' . esc_html__( 'Generate optimized image formats', 'tiny-compress-images' ) . '</label>';
970-
echo '<span class="description">' . wp_kses(__( 'Creating an optimized image will take <strong>1 additional compression</strong> for each image size.', 'tiny-compress-images' ), array('strong' => array())) . '</span>'; // WPCS: Needed for proper translation.
970+
echo '<span class="description">' . wp_kses( __( 'Creating an optimized image will take <strong>1 additional compression</strong> for each image size.', 'tiny-compress-images' ), array(
971+
'strong' => array(),
972+
) ) . '</span>'; // WPCS: Needed for proper translation.
971973
echo '</p>';
972974

973975
echo '</div>';

0 commit comments

Comments
 (0)