<commit_before><?php
/**
 * Main image resize handling through 404 template redirect
 * included in main plugin file
 * @var Plugin $plugin
 *
 * Created by PhpStorm.
 * User: alpipego
 * Date: 26.06.2017
 * Time: 16:53
 */

use Alpipego\Resizefly\Image\Handler;
use Alpipego\Resizefly\Image\Image;
use Alpipego\Resizefly\Plugin;
use Alpipego\Resizefly\Upload\DuplicateOriginal;

add_action( 'template_redirect', function () use ( $plugin ) {
	if ( ! is_404() ) {
		return;
	}

	$requested = urldecode( $_SERVER['REQUEST_URI'] );
	if ( strpos( $plugin->get( 'options.cache.url' ), $requested ) === 0 ) {
		$requested = str_replace( $plugin->get( 'config.cache.url' ), $plugin->get( 'uploads' )->getBaseUrl(), $requested );
	}

	$regex = '/(?<file>.*?)-(?<width>[0-9]+)x(?<height>[0-9]+)@(?<density>[0-3])\.(?<ext>jpe?g|png|gif)/i';
	if ( preg_match( $regex, $requested, $matches ) ) {
		$plugin['request.file'] = $matches;
		$plugin->addDefiniton( $plugin->get( 'config.path' ) . '/app/config/image.php' );

		/** @var Image $image */
		$image = $plugin->get( Image::class );
		$image->setImage( $plugin['request.file'] );

		if ( ! file_exists( $image->getOriginalPath() ) ) {
			\Alpipego\Resizefly\throw404();
		}

		// check if to resize from duplicate
		if ( (bool) apply_filters( 'resizefly/duplicate', true ) ) {
			if ( ! file_exists( $image->getDuplicatePath() ) ) {
				$plugin->get( DuplicateOriginal::class )->rebuild( $image->getOriginalPath() );
			}

			// get wp image editor and handle errors
			$plugin['wp_image_editor'] = wp_get_image_editor( $image->getDuplicatePath() );
		} else {
			$plugin['wp_image_editor'] = wp_get_image_editor( $image->getOriginalPath() );
		}

		if ( is_wp_error( $plugin['wp_image_editor'] ) ) {
			\Alpipego\Resizefly\throw404();
		}

		// check if image size is allowed
		if ( (bool) get_option( 'resizefly_restrict_sizes', apply_filters( 'resizefly/restrict_sizes', true ) ) ) {
			if ( ! $plugin->get( Handler::class )->allowedImageSize( get_option( 'resizefly_sizes', [] ) ) ) {
				\Alpipego\Resizefly\throw404();
			}
		}

		$plugin->run();
	}<commit_msg>get original if requested larger than duplicate<commit_after><?php
/**
 * Main image resize handling through 404 template redirect
 * included in main plugin file
 * @var Plugin $plugin
 *
 * Created by PhpStorm.
 * User: alpipego
 * Date: 26.06.2017
 * Time: 16:53
 */

use Alpipego\Resizefly\Image\Handler;
use Alpipego\Resizefly\Image\Image;
use Alpipego\Resizefly\Plugin;
use Alpipego\Resizefly\Upload\DuplicateOriginal;

add_action( 'template_redirect', function () use ( $plugin ) {
	if ( ! is_404() ) {
		return;
	}

	$requested = urldecode( $_SERVER['REQUEST_URI'] );
	if ( strpos( $plugin->get( 'options.cache.url' ), $requested ) === 0 ) {
		$requested = str_replace( $plugin->get( 'config.cache.url' ), $plugin->get( 'uploads' )->getBaseUrl(), $requested );
	}

	$regex = '/(?<file>.*?)-(?<width>[0-9]+)x(?<height>[0-9]+)@(?<density>[0-3])\.(?<ext>jpe?g|png|gif)/i';
	if ( preg_match( $regex, $requested, $matches ) ) {
		$plugin['request.file'] = $matches;
		$plugin->addDefiniton( $plugin->get( 'config.path' ) . '/app/config/image.php' );

		/** @var Image $image */
		$image = $plugin->get( Image::class );
		$image->setImage( $plugin['request.file'] );

		if ( ! file_exists( $image->getOriginalPath() ) ) {
			\Alpipego\Resizefly\throw404();
		}

		// check if to resize from duplicate
		if ( (bool) apply_filters( 'resizefly/duplicate', true ) && $matches['width'] <= (int) apply_filters( 'resizefly/duplicate/long_edge', 2560 ) ) {
			if ( ! file_exists( $image->getDuplicatePath() ) ) {
				$plugin->get( DuplicateOriginal::class )->rebuild( $image->getOriginalPath() );
			}

			// get wp image editor and handle errors
			$plugin['wp_image_editor'] = wp_get_image_editor( $image->getDuplicatePath() );
		} else {
			$plugin['wp_image_editor'] = wp_get_image_editor( $image->getOriginalPath() );
		}

		if ( is_wp_error( $plugin['wp_image_editor'] ) ) {
			\Alpipego\Resizefly\throw404();
		}

		// check if image size is allowed
		if ( (bool) get_option( 'resizefly_restrict_sizes', apply_filters( 'resizefly/restrict_sizes', true ) ) ) {
			if ( ! $plugin->get( Handler::class )->allowedImageSize( get_option( 'resizefly_sizes', [] ) ) ) {
				\Alpipego\Resizefly\throw404();
			}
		}

		$plugin->run();
	}
{'fraction_non_alphanumeric': 0.19082737958054852, 'fraction_numerical': 0.01659368518091726, 'mean_word_length': 4.222623345367028, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3496911', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1591, 'n_words': 293}