• Resolved devellopah

    (@dagman25)


    Hi Mateusz! Thanks for the great plugin. I really appreciate you made it possible to use for free.

    I have encountered an issue. First of all, i have uncheked option

    Automatic removal of WebP files larger than original option

    So i have webp versions for all images. But i can see a few images hasn’t been replaced with their versions(webp_network.png), though their webp versions are available on server(webp_ftp.png).

    Please, help spot an issue.

    p.s.
    Below I have provided the folder with screens that should help to better understand my issue.
    issue screens

    • This topic was modified 4 years, 11 months ago by devellopah.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Hello @dagman25,

    Thanks for your message.

    Please use the via .htaccess method. Pass Thru mode cannot modify all URLs to images. It modifies only those found in the HTML code.

    Best,
    Mateusz

    Thread Starter devellopah

    (@dagman25)

    Hi @mateuszgbiorczyk!
    According to webp_network.png (from screens folder) screen, four .jpeg images not replaced with .webp (at the bottom) were initiated by document and only two by js files.So why those four image hasn’t be replaced?

    • This reply was modified 4 years, 11 months ago by devellopah.
    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @dagman25 Can you replace the following file in the plugin code?
    /webp-converter-for-media/src/Loader/PassthruLoader.php

    Paste the following content there:

    
    <?php
    
    namespace WebpConverter\Loader;
    
    /**
     * Supports method of loading images using .php file as Pass Thru.
     */
    class PassthruLoader extends LoaderAbstract implements LoaderInterface {
    
    	const LOADER_TYPE   = 'passthru';
    	const PATH_LOADER   = '/webpc-passthru.php';
    	const LOADER_SOURCE = '/includes/passthru.php';
    
    	/**
    	 * Integrates with WordPress hooks.
    	 *
    	 * @return void
    	 */
    	public function init_hooks() {
    		add_action( 'get_header', [ $this, 'start_buffer' ] );
    	}
    
    	/**
    	 * Returns status if loader is active.
    	 *
    	 * @return bool Is loader active?
    	 */
    	public function is_active_loader(): bool {
    		$settings = $this->get_plugin()->get_settings();
    		return ( isset( $settings['loader_type'] ) && ( $settings['loader_type'] === self::LOADER_TYPE ) );
    	}
    
    	/**
    	 * Initializes actions for activating loader.
    	 *
    	 * @param bool $is_debug Is debugging?
    	 *
    	 * @return void
    	 */
    	public function activate_loader( bool $is_debug = false ) {
    		$path_source = WEBPC_PATH . self::LOADER_SOURCE;
    		$source_code = ( is_readable( $path_source ) ) ? file_get_contents( $path_source ) ?: '' : '';
    		if ( ! $source_code ) {
    			return;
    		}
    
    		$path_dir_uploads = apply_filters( 'webpc_dir_name', '', 'uploads' );
    		$path_dir_webp    = apply_filters( 'webpc_dir_name', '', 'webp' );
    		$upload_suffix    = implode( '/', array_diff( explode( '/', $path_dir_uploads ), explode( '/', $path_dir_webp ) ) );
    
    		$source_code = preg_replace(
    			'/(PATH_UPLOADS(?:\s+)= \')(\')/',
    			'$1' . $path_dir_uploads . '$2',
    			$source_code
    		);
    		$source_code = preg_replace(
    			'/(PATH_UPLOADS_WEBP(?:\s+)= \')(\')/',
    			'$1' . $path_dir_webp . '/' . $upload_suffix . '$2',
    			$source_code ?: ''
    		);
    		$source_code = preg_replace(
    			'/(MIME_TYPES(?:\s+)= \')(\')/',
    			'$1' . json_encode( $this->get_mime_types() ) . '$2',
    			$source_code ?: ''
    		);
    
    		$dir_output = dirname( apply_filters( 'webpc_dir_path', '', 'uploads' ) );
    		if ( is_writable( $dir_output ) ) {
    			file_put_contents( $dir_output . self::PATH_LOADER, $source_code );
    		}
    	}
    
    	/**
    	 * Initializes actions for deactivating loader.
    	 *
    	 * @return void
    	 */
    	public function deactivate_loader() {
    		$dir_output = dirname( apply_filters( 'webpc_dir_path', '', 'uploads' ) ) . self::PATH_LOADER;
    		if ( is_writable( $dir_output ) ) {
    			unlink( $dir_output );
    		}
    	}
    
    	/**
    	 * Opens buffer in which all output is stored.
    	 *
    	 * @return void
    	 * @internal
    	 */
    	public function start_buffer() {
    		ob_start( function( $buffer ) {
    			return $this->update_image_urls( $buffer );
    		} );
    	}
    
    	/**
    	 * Replaces URLs to source images in output buffer.
    	 *
    	 * @param string $buffer   Contents of output buffer.
    	 * @param bool   $is_debug Is debugging?
    	 *
    	 * @return string Contents of output buffer.
    	 * @internal
    	 */
    	public function update_image_urls( string $buffer, bool $is_debug = false ): string {
    		if ( ! $this->is_active_loader() ) {
    			return $buffer;
    		}
    
    		$settings   = ( ! $is_debug ) ? $this->get_plugin()->get_settings() : $this->get_plugin()->get_settings_debug();
    		$extensions = implode( '|', $settings['extensions'] ?? [] );
    		if ( ! $extensions || ( ! $source_dir = self::get_loader_url() )
    			|| ( ! $allowed_dirs = $this->get_allowed_dirs( $settings ) ) ) {
    			return $buffer;
    		}
    
    		$dir_paths = str_replace( '/', '\\/', implode( '|', $allowed_dirs ) );
    		return preg_replace(
    			'/(https?:\/\/(?:[^\s()"\']+)(?:' . $dir_paths . ')(?:[^\s()"\']+)\.(?:' . $extensions . '))/',
    			$source_dir . '?src=$1&nocache=1',
    			$buffer
    		) ?: '';
    	}
    
    	/**
    	 * Returns URL for Passthru loader.
    	 *
    	 * @return string|null URL of source PHP file.
    	 */
    	public static function get_loader_url() {
    		if ( ! $source_dir = dirname( apply_filters( 'webpc_dir_url', '', 'uploads' ) ) ) {
    			return null;
    		}
    		return $source_dir . self::PATH_LOADER;
    	}
    
    	/**
    	 * Returns list of directories for which redirection from source images to output images.
    	 *
    	 * @param mixed[] $settings Plugin settings.
    	 *
    	 * @return string[] List of directories names.
    	 */
    	private function get_allowed_dirs( array $settings ): array {
    		$dirs = [];
    		foreach ( $settings['dirs'] as $dir ) {
    			$dirs[] = apply_filters( 'webpc_dir_name', '', $dir );
    		}
    		return array_filter( $dirs );
    	}
    }
    

    Did this fix your problem?

    Thread Starter devellopah

    (@dagman25)

    Hi @mateuszgbiorczyk!
    Thanks for this chunk of code, it fixed the issue!
    Would you, please, provide this code with next plugin update?

    I have got a new issue, though. It’s about images loaded with Pass Thru mode has nocache=1. Pagespeed complaines these images has no cache ttl.
    What if to remove nocache=1 to satisfy pagespeed or you added the flag for the purpose? If it’s ok to remove, how can i do it?

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    I am very pleased. Yes @dagman25, I added this code to the latest update.

    I also added the filter you requested. If you want to remove this parameter, use the following filter:
    add_filter( 'webpc_passthru_url_nocache', '__return_false' );

    Thread Starter devellopah

    (@dagman25)

    HI @mateuszgbiorczyk! I have added filter, but then removed. Adding filter doesnt give any help, the cache ttl still None and the site became slower, to be honest.
    Anyway, thanks for you help!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘A bunch of images hasn’t been replaced with their webp versions.’ is closed to new replies.