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 #794 display the smaller size on image library data #810

Merged
merged 40 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
40 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)
remyperona Jan 29, 2024
19f6e4f
Fixes #791 Update texts related to WebP and Next-Gen (#800)
remyperona Feb 9, 2024
5d431cf
Merge branch 'develop' into feature/avif
remyperona Feb 14, 2024
fc23358
Fixes Container, Event Manager, CLI & Rewrite rules for AVIF (#797)
remyperona Feb 19, 2024
bde102e
:feat: Add check for avif image size to display on media page :closes…
Khadreal Feb 21, 2024
67bbc1b
Fixed lint error
Khadreal Feb 21, 2024
29f4bfb
Fixes #789 Fix error displayed when using generate missing next-gen v…
remyperona Feb 22, 2024
6c286ea
Fixes #788 Generate next-gen formats on image upload (#809)
remyperona 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
remyperona Feb 22, 2024
770c25f
update some occurences of webp to next-gen
remyperona Feb 22, 2024
2a14a1e
Modified the use of webp to nextgen close 794
Khadreal Feb 23, 2024
0103815
update text
remyperona Feb 24, 2024
a62dc23
update minified file
remyperona Feb 26, 2024
1cf2adc
remove dot from format
Khadreal Feb 27, 2024
e357b67
Fixes #815 Skip image deletion if returned path value is false (#819)
remyperona Feb 27, 2024
ead1038
Merge branch 'tester' into avif/media-library#794
Khadreal Feb 28, 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 (…
remyperona 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)
remyperona Mar 1, 2024
96138a8
Merge branch 'feature/avif' into avif/media-library#794
Khadreal Mar 4, 2024
07f5377
:feat: Changed the order of media size to avif -> webp -> optimised
Khadreal Mar 4, 2024
8976b4c
fixed lint error
Khadreal Mar 4, 2024
7153e87
Resolve merge conflict
Khadreal Mar 11, 2024
7d93d51
merge develop and fixed code conflict
Khadreal Mar 13, 2024
6fce2ad
Merge commit 'daec1aca7e12badfac89a0a0170836a0fe568f95' into avif/med…
MathieuLamiot Mar 14, 2024
855858d
Fixed undefined error
Khadreal Mar 15, 2024
96717a1
:feat: Fixed undefined array key for image without webp version
Khadreal Mar 18, 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
2 changes: 1 addition & 1 deletion 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
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