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

Closes #504 AVIF support #801

Merged
merged 33 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f3c600a
Closes #763: AVIF support: UI (#769)
Miraeld Jan 3, 2024
fc76130
Closes #772: Changed webp to avif and slight modification to UI (#780)
Khadreal Jan 15, 2024
ec0bf15
Closes #766: Add avif format to picture element (#781)
jeawhanlee Jan 16, 2024
188264f
Add league/container, event-manager to composer and mozart (#782)
Khadreal Jan 18, 2024
1dc3a71
Add AVIF Rewrite rules (#774)
Miraeld Jan 18, 2024
0e8a3b1
AVIF settings update (#775)
Miraeld Jan 19, 2024
0a8f547
Add filter for nextgen image format #777 (#783)
Khadreal Jan 19, 2024
994c4eb
Feature/778 avif bulk generate (#784)
Khadreal Jan 23, 2024
de6dc98
Change methods name (#785)
Khadreal Jan 25, 2024
b396de2
Update WP background processing and Action Scheduler (#786)
Tabrisrp Jan 29, 2024
19f6e4f
Fixes #791 Update texts related to WebP and Next-Gen (#800)
Tabrisrp Feb 9, 2024
5d431cf
Merge branch 'develop' into feature/avif
Tabrisrp Feb 14, 2024
fc23358
Fixes Container, Event Manager, CLI & Rewrite rules for AVIF (#797)
Tabrisrp Feb 19, 2024
29f4bfb
Fixes #789 Fix error displayed when using generate missing next-gen v…
Tabrisrp Feb 22, 2024
6c286ea
Fixes #788 Generate next-gen formats on image upload (#809)
Tabrisrp Feb 22, 2024
2d98bbc
Closes #796: Auto generation of AVIF on save (#806)
Miraeld Feb 22, 2024
4502844
Closes #790, #792, #803: Delete/Generate next-gen in media not workin…
jeawhanlee Feb 22, 2024
6a64753
coding standards fixes
Tabrisrp Feb 22, 2024
770c25f
update some occurences of webp to next-gen
Tabrisrp Feb 22, 2024
0103815
update text
Tabrisrp Feb 24, 2024
a62dc23
update minified file
Tabrisrp Feb 26, 2024
e357b67
Fixes #815 Skip image deletion if returned path value is false (#819)
Tabrisrp Feb 27, 2024
324c4fa
Closes #796: Auto AVIF Convertion on settings save (#811)
Miraeld Feb 28, 2024
4677279
Closes #822: The filter to enable webP along with AVIF is not working…
jeawhanlee Feb 28, 2024
85edeba
Fixes #826 Only generate missing next-gen when enabling AVIF option (…
Tabrisrp Feb 29, 2024
f673ed2
Apply nginx rules for next gen images (#824)
wordpressfan Mar 1, 2024
6a7f2f3
Fix data for optimized medias without next-gen versions (#820)
Tabrisrp Mar 1, 2024
22e543c
Fix/830 bulk optimisation (#832)
Khadreal Mar 4, 2024
610acc3
Fix generation of next gen images with first settings save of avif op…
wordpressfan Mar 4, 2024
088230c
update version to 2.2-alpha1
Tabrisrp Mar 4, 2024
20b61ca
update author
Tabrisrp Mar 4, 2024
2ff1bfc
update readme
Tabrisrp Mar 4, 2024
77598bf
change version to 2.2
Tabrisrp Mar 6, 2024
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
12 changes: 6 additions & 6 deletions assets/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ window.imagify = window.imagify || {};
/**
* Fade CDN URL field.
*/
$( '[name="imagify_settings[display_webp_method]"]' ).on( 'change.imagify init.imagify', function( e ) {
$( '[name="imagify_settings[display_nextgen_method]"]' ).on( 'change.imagify init.imagify', function( e ) {
if ( 'picture' === e.target.value ) {
$( e.target ).closest( '.imagify-radio-group' ).next( '.imagify-options-line' ).removeClass( 'imagify-faded' );
} else {
Expand Down Expand Up @@ -496,7 +496,7 @@ window.imagify = window.imagify || {};
.on( 'imagifybeat-send', this.addRequirementsImagifybeat )
.on( 'imagifybeat-tick', { imagifyOptionsBulk: this }, this.processRequirementsImagifybeat );

if ( false !== imagifyOptions.bulk.progress_webp.total && false !== imagifyOptions.bulk.progress_webp.remaining ) {
if ( false !== imagifyOptions.bulk.progress_next_gen.total && false !== imagifyOptions.bulk.progress_next_gen.remaining ) {
// Reset properties.
w.imagify.optionsBulk.error = false;
w.imagify.optionsBulk.working = true;
Expand All @@ -511,10 +511,10 @@ window.imagify = window.imagify || {};

this.$missingWebpMessage.hide().attr('aria-hidden', 'true');

processed = imagifyOptions.bulk.progress_webp.total - imagifyOptions.bulk.progress_webp.remaining;
progress = Math.floor( processed / imagifyOptions.bulk.progress_webp.total * 100 );
processed = imagifyOptions.bulk.progress_next_gen.total - imagifyOptions.bulk.progress_next_gen.remaining;
progress = Math.floor( processed / imagifyOptions.bulk.progress_next_gen.total * 100 );
this.$progressBar.css( 'width', progress + '%' );
this.$progressText.text( processed + '/' + imagifyOptions.bulk.progress_webp.total );
this.$progressText.text( processed + '/' + imagifyOptions.bulk.progress_next_gen.total );

this.$progressWrap.slideDown().attr( 'aria-hidden', 'false' ).removeClass( 'hidden' );
}
Expand Down Expand Up @@ -655,7 +655,7 @@ window.imagify = window.imagify || {};

_this = this;

$.get( this.getAjaxUrl( 'MissingWebp', imagifyOptions.bulk.contexts ) )
$.get( this.getAjaxUrl( 'MissingNextGen', imagifyOptions.bulk.contexts ) )
.done( function( response ) {
var errorMessage;

Expand Down
2 changes: 1 addition & 1 deletion assets/js/options.min.js

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions classes/Avif/Apache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Imagify\Avif;

use Imagify\WriteFile\AbstractApacheDirConfFile;

/**
* Add and remove contents to the .htaccess file to display AVIF images on the site.
*/
class Apache extends AbstractApacheDirConfFile {

/**
* Name of the tag used as block delemiter.
*
* @var string
*/
const TAG_NAME = 'Imagify: avif file type';

/**
* Get unfiltered new contents to write into the file.
*
* @return string
*/
protected function get_raw_new_contents() {
return trim( '
<IfModule mod_mime.c>
AddType image/avif .avif
</IfModule>' );
}
}
169 changes: 169 additions & 0 deletions classes/Avif/Display.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
declare(strict_types=1);

namespace Imagify\Avif;

use Imagify\EventManagement\SubscriberInterface;
use Imagify\Notices\Notices;
use Imagify\WriteFile\WriteFileInterface;

/**
* Display AVIF images on the site using picture tag.
*/
class Display implements SubscriberInterface {
/**
* Server conf object.
*
* @var WriteFileInterface|null
* @since 1.9
*/
protected $server_conf = null;

/**
* Returns an array of events this subscriber listens to
*
* @return array
*/
public static function get_subscribed_events() {
return [
'imagify_settings_on_save' => [ 'maybe_add_rewrite_rules', 12 ],
'imagify_activation' => 'activate',
'imagify_deactivation' => 'deactivate',
];
}

/**
* If display Next-Gen images, add the AVIF type to the .htaccess/etc file.
*
* @since 1.9
*
* @param array $values The option values.
*
* @return array
*/
public function maybe_add_rewrite_rules( $values ) {
if ( ! $this->get_server_conf() ) {
return $values;
}

$enabled = isset( $values['display_nextgen'] ) ? true : false;
$result = false;

if ( $enabled ) {
// Add the AVIF file type.
$result = $this->get_server_conf()->add();
} elseif ( ! $enabled ) {
// Remove the AVIF file type.
$result = $this->get_server_conf()->remove();
}

if ( ! is_wp_error( $result ) ) {
return $values;
}

// Display an error message.
if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) {
Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() );

return $values;
}

Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() );

return $values;
}

/**
* Add rules on plugin activation.
*
* @since 1.9
*/
public function activate() {
$conf = $this->get_server_conf();

if ( ! $conf ) {
return;
}

if ( ! get_imagify_option( 'display_nextgen' ) ) {
return;
}

if ( is_wp_error( $conf->is_file_writable() ) ) {
return;
}

$conf->add();
}

/**
* Remove rules on plugin deactivation.
*
* @since 1.9
*/
public function deactivate() {
$conf = $this->get_server_conf();

if ( ! $conf ) {
return;
}

$file_path = $conf->get_file_path();
$filesystem = \Imagify_Filesystem::get_instance();

if ( ! $filesystem->exists( $file_path ) ) {
return;
}
if ( ! $filesystem->is_writable( $file_path ) ) {
return;
}

$conf->remove();
}

/**
* Get the path to the directory conf file.
*
* @since 1.9
*
* @param bool $relative True to get a path relative to the site’s root.
* @return string|bool The file path. False on failure.
*/
public function get_file_path( $relative = false ) {
if ( ! $this->get_server_conf() ) {
return false;
}

$file_path = $this->get_server_conf()->get_file_path();

if ( $relative ) {
return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path );
}

return $file_path;
}

/**
* Get the server conf instance.
* Note: nothing needed for nginx.
*
* @since 1.9
*
* @return WriteFileInterface
*/
protected function get_server_conf() {
global $is_apache, $is_iis7;

if ( isset( $this->server_conf ) ) {
return $this->server_conf;
}

if ( $is_apache ) {
$this->server_conf = new Apache();
} elseif ( $is_iis7 ) {
$this->server_conf = new IIS();
}

return $this->server_conf;
}
}
32 changes: 32 additions & 0 deletions classes/Avif/IIS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Imagify\Avif;

use Imagify\WriteFile\AbstractIISDirConfFile;

/**
* Add and remove contents to the web.config file to display AVIF images on the site.
*/
class IIS extends AbstractIISDirConfFile {

/**
* Name of the tag used as block delemiter.
*
* @var string
*/
const TAG_NAME = 'Imagify: avif file type';

/**
* Get unfiltered new contents to write into the file.
*
* @return array
*/
protected function get_raw_new_contents() {
return trim( '
<!-- @parent /configuration/system.webServer -->
<staticContent name="' . esc_attr( static::TAG_NAME ) . ' 1">
<mimeMap fileExtension=".avif" mimeType="image/avif" />
</staticContent>' );
}
}
59 changes: 59 additions & 0 deletions classes/Avif/RewriteRules/Apache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

namespace Imagify\Avif\RewriteRules;

use Imagify\WriteFile\AbstractApacheDirConfFile;

/**
* Add and remove rewrite rules to the .htaccess file to display AVIF images on the site.
*/
class Apache extends AbstractApacheDirConfFile {

/**
* Name of the tag used as block delimiter.
*
* @var string
*/
const TAG_NAME = 'Imagify: rewrite rules for avif';

/**
* Get unfiltered new contents to write into the file.
*
* @access protected
*
* @return string
*/
protected function get_raw_new_contents() {
$extensions = $this->get_extensions_pattern();
$extensions = str_replace( '|avif', '', $extensions );
$home_root = wp_parse_url( home_url( '/' ) );
$home_root = $home_root['path'];

return trim( '
<IfModule mod_setenvif.c>
# Vary: Accept for all the requests to jpeg, png, and gif.
SetEnvIf Request_URI "\.(' . $extensions . ')$" REQUEST_image
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase ' . $home_root . '

# Check if browser supports AVIF images.
# Update the MIME type accordingly.
RewriteCond %{HTTP_ACCEPT} image/avif

# Check if AVIF replacement image exists.
RewriteCond %{REQUEST_FILENAME}.avif -f

# Serve AVIF image instead.
RewriteRule (.+)\.(' . $extensions . ')$ $1.$2.avif [T=image/avif,NC]
</IfModule>

<IfModule mod_headers.c>
# Update the MIME type accordingly.
Header append Vary Accept env=REQUEST_image
</IfModule>' );
}
}
Loading
Loading