rosell-dk / webp-express

Wordpress plugin for serving autogenerated WebP images instead of jpeg/png to browsers that supports WebP
GNU General Public License v3.0
227 stars 64 forks source link

Exclude images / folders #238

Open rosell-dk opened 5 years ago

rosell-dk commented 5 years ago

1: Make it possible to select which general areas that WebP Express should be active in. This should apply to .htaccess rules, Alter HTML and Bulk Convert. Options could be:

2: Make it possible to add exceptions. For example these types:

rosell-dk commented 5 years ago

General options could be:

rosell-dk commented 5 years ago

Requested here: https://wordpress.org/support/topic/add-image-exception/#post-11645032

acicovic commented 3 years ago

Hello there,

@rosell-dk thanks for all the work you've been putting in this plugin. This feature is really needed in my case.

I have a client that accepts png/jpg images from an Elementor form (path is /wp-content/uploads/elementor/forms/) for printing purposes, and they can't access the original images because WebP Express runs on everything. So right now the only options I can give them is to download pictures by FTP (probably wont' happen) or disable the plugin altogether.

As Elementor forms are popular, this might become a problem for the plugin in general. I think that there should be at least a basic folder exclude feature, so in cases like that we don't have to disable the plugin altogether.

Do you know if this feature planned for now or the future?

Thanks!

rosell-dk commented 3 years ago

Hi,

I'm currently focused on a file manager interface. And then there is avif support. So I suppose the feature is further down the line.

However, there are a few tweaks available, as described here: https://wordpress.org/plugins/webp-express/#can%20i%20make%20an%20exceptions%20for%20some%20images%3F

acicovic commented 3 years ago

Hey, thanks for the input.

For any others who may face the same issue, the code to be added to /wp-content/uploads/.htaccess is:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^elementor/forms - [L]

This must be before WebP Express rules.

Related thread: https://wordpress.org/support/topic/can-i-make-an-exception-for-specific-post-image/

jhotadhari commented 2 years ago

Hi, I want to exclude woocommerce digital downloadable products. All files in wp-content/uploads/woocommerce_downloads/.

Could hide the converted images from the public with .htaccess, but would be better to exclude them entirely from being converted.

Some ideas:

Would be nice to have a filter hook just before the image gets converted. For example in WebPExpress\Convert::convertFile in the try block:

$checking = 'custom';
if ( $exclude_file_msg = apply_filters( 'webpexpress/exclude_file', false, $source, $config, $convertOptions, $converter ) ) {
    throw new \Exception( is_string( $exclude_file_msg )
        ? $exclude_file_msg
        : 'file is excluded'
    );
}

And it would be nice to exclude this files from bulk converter as well. For example a filter hook for WebPExpress\BulkConvert\defaultListOptions would allow to pass a _regexPattern to the filter.

public static function defaultListOptions($config)
{
    return apply_filters( 'webpexpress/BulkConvert/defaultListOptions', [
        //'root' => Paths::getUploadDirAbs(),
        'ext' => $config['destination-extension'],
        'destination-folder' => $config['destination-folder'],  /* hm, "destination-folder" is a bad name... */
        'webExpressContentDirAbs' => Paths::getWebPExpressContentDirAbs(),
        'uploadDirAbs' => Paths::getUploadDirAbs(),
        'useDocRootForStructuringCacheDir' => (($config['destination-structure'] == 'doc-root') && (Paths::canUseDocRootForStructuringCacheDir())),
        'imageRoots' => new ImageRoots(Paths::getImageRootsDefForSelectedIds($config['scope'])),   // (Paths::getImageRootsDef()
        'filter' => [
            'only-converted' => false,
            'only-unconverted' => true,
            'image-types' => $config['image-types'],
            'max-depth' => 100,
        ],
        'flattenList' => true,
    ], $config );
}

And the regular expression match in WebPExpress\BulkConvert\getListRecursively should include the entire file path: if (preg_match($filter['_regexPattern'], $dir . "/" . $filename)).

The custom filter function for my case would be:

add_filter( 'webpexpress/BulkConvert/defaultListOptions', function( $list_options, $config ) {
    if (!isset($list_options['filter']['_regexPattern'])) {
        $imageTypes = $list_options['filter']['image-types'];
        $fileExtensions = [];
        if ($imageTypes & 1) {
          $fileExtensions[] = 'jpe?g';
        }
        if ($imageTypes & 2) {
          $fileExtensions[] = 'png';
        }
        // $list_options['filter']['_regexPattern'] = '#\.(' . implode('|', $fileExtensions) . ')$#';
        $list_options['filter']['_regexPattern'] = '#^(?!.*\bwoocommerce_uploads\/\b).*\.(' . implode('|', $fileExtensions) . ')$#';
    }
    return $list_options;
}, 10, 2 );

Well, would be better to allow multiple regex-patterns and not overwrite the default one that checks the file extension.

IonTulbure commented 1 year ago

Upvote : Please add this feature.

fgdaniel commented 1 year ago

Pls add this feature and maybe support for animated webp and animated pngs, now they dont work..

mrsjaved commented 1 year ago

Please add this feature.

scaphandroid commented 5 months ago

Please add this feature.