Skip to content

Commit 66dc440

Browse files
committed
Add preferred image format option
1 parent dbac684 commit 66dc440

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

includes/classes/Admin/Dashboard.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use const ImageOptimizerPro\Constants\LICENSE_ENDPOINT;
2020
use const ImageOptimizerPro\Constants\LICENSE_INFO_TRANSIENT;
2121
use const ImageOptimizerPro\Constants\LICENSE_KEY_OPTION;
22+
use const ImageOptimizerPro\Constants\SETTING_OPTION;
2223

2324
/**
2425
* Class Dashboard
@@ -116,6 +117,7 @@ public function add_menu() {
116117
public function render_dashboard() {
117118
$license_key = get_license_key();
118119
$license_info = get_license_info();
120+
$settings = \ImageOptimizerPro\Utils\get_settings();
119121

120122
if ( is_network_admin() ) {
121123
settings_errors();
@@ -140,6 +142,24 @@ public function render_dashboard() {
140142
<span class="description"><?php echo esc_html( get_license_status_message() ); ?></span>
141143
</td>
142144
</tr>
145+
<tr>
146+
<th scope="row">
147+
</th>
148+
<td>
149+
<fieldset>
150+
<legend class="screen-reader-text">
151+
<span><?php esc_html_e( 'Use WebP over AVIF', 'image-optimizer-pro' ); ?></span>
152+
</legend>
153+
<label for="preferred_format">
154+
<input name="preferred_format" type="checkbox" id="preferred_format" value="webp" <?php checked( 'webp', $settings['preferred_format'] ); ?>>
155+
<?php esc_html_e( 'Use WebP over AVIF', 'image-optimizer-pro' ); ?>
156+
</label>
157+
</fieldset>
158+
<span class="description">
159+
<?php esc_html_e( 'Activate this option to prioritize the WebP format over AVIF for image optimization.', 'image-optimizer-pro' ); ?>
160+
</span>
161+
</td>
162+
</tr>
143163
</tbody>
144164
</table>
145165
<?php submit_button( esc_html__( 'Save Changes', 'image-optimizer-pro' ), 'submit primary' ); ?>
@@ -243,6 +263,9 @@ public function save_settings() {
243263
return;
244264
}
245265

266+
$settings = [];
267+
$settings['preferred_format'] = sanitize_text_field( wp_unslash( $_POST['preferred_format'] ?? '' ) );
268+
246269
$license_key = sanitize_text_field( filter_input( INPUT_POST, 'license_key' ) );
247270
$current_license_key = mask_string( $license_key, 3 );
248271
$old_license_key = mask_string( get_license_key(), 3 );
@@ -255,8 +278,10 @@ public function save_settings() {
255278
$encrypted_license_key = $encryption->encrypt( $license_key );
256279

257280
if ( IMAGE_OPTIMIZER_PRO_IS_NETWORK ) {
281+
update_site_option( SETTING_OPTION, $settings );
258282
update_site_option( LICENSE_KEY_OPTION, $encrypted_license_key );
259283
} else {
284+
update_option( SETTING_OPTION, $settings, false );
260285
update_option( LICENSE_KEY_OPTION, $encrypted_license_key, false );
261286
}
262287

includes/classes/Optimizer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class Optimizer {
4949
*/
5050
protected static $image_sizes = null;
5151

52+
/**
53+
* Preferred image format.
54+
* It is used to determine the preferred image format for the current request.
55+
*
56+
* @var null
57+
*/
58+
protected static $preferred_image_formats = null;
59+
5260
/**
5361
* Singleton implementation
5462
*
@@ -107,6 +115,10 @@ private function setup() {
107115
return true;
108116
}
109117

118+
$settings = \ImageOptimizerPro\Utils\get_settings();
119+
// set preferred image format
120+
self::$preferred_image_formats = $settings['preferred_format'];
121+
110122
// skip photonized urls when image optimizer active
111123
add_filter( 'jetpack_photon_skip_for_url', '__return_true' );
112124
add_filter( 'wp_resource_hints', [ $this, 'add_dns_prefetch' ], 10, 2 );
@@ -1492,6 +1504,10 @@ public static function image_optimizer_url( $image_url, $args = array(), $scheme
14921504
$image_optimizer_url = add_query_arg( array( 'ssl' => 1 ), $image_optimizer_url );
14931505
}
14941506

1507+
if ( ! empty( self::$preferred_image_formats ) && 'webp' === self::$preferred_image_formats ) {
1508+
$image_optimizer_url = add_query_arg( array( 'format' => 'webp' ), $image_optimizer_url );
1509+
}
1510+
14951511
return self::url_schema( $image_optimizer_url, $scheme );
14961512
}
14971513

includes/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace ImageOptimizerPro\Constants;
99

1010
const MENU_SLUG = 'image-optimizer-pro';
11+
const SETTING_OPTION = 'image_optimizer_pro_settings';
1112
const LICENSE_KEY_OPTION = 'image_optimizer_pro_license_key';
1213

1314
// transient keys

includes/utils.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use const ImageOptimizerPro\Constants\LICENSE_ENDPOINT;
1212
use const ImageOptimizerPro\Constants\LICENSE_INFO_TRANSIENT;
1313
use const ImageOptimizerPro\Constants\LICENSE_KEY_OPTION;
14+
use const ImageOptimizerPro\Constants\SETTING_OPTION;
1415

1516
/**
1617
* Is plugin activated network wide?
@@ -297,3 +298,25 @@ function is_license_active() {
297298

298299
return false;
299300
}
301+
302+
/**
303+
* Get settings with defaults
304+
*
305+
* @return array
306+
* @since 1.0.1
307+
*/
308+
function get_settings() {
309+
$defaults = [
310+
'preferred_format' => '',
311+
];
312+
313+
if ( IMAGE_OPTIMIZER_PRO_IS_NETWORK ) {
314+
$settings = get_site_option( SETTING_OPTION, [] );
315+
} else {
316+
$settings = get_option( SETTING_OPTION, [] );
317+
}
318+
319+
$settings = wp_parse_args( $settings, $defaults );
320+
321+
return $settings;
322+
}

0 commit comments

Comments
 (0)