<commit_before><?php
/**
 * Created by PhpStorm.
 * User: alpipego
 * Date: 16/09/16
 * Time: 10:15
 */

namespace Alpipego\Resizefly\Upload;


/**
 * Class Dir
 * @package Alpipego\Resizefly\Upload
 */
class Dir {
	/**
	 * @var
	 */
	private $uploads;

	/**
	 * Dir constructor.
	 */
	public function __construct() {
		\add_filter( 'upload_dir', [ $this, 'resolvePath' ] );
		\add_filter( 'upload_dir', [ $this, 'resolveUrl' ] );
	}

	public function run() {
		\add_filter( 'resizefly_filter_url', [ $this, 'filterImageUrl' ] );
		\add_filter( 'wp_prepare_attachment_for_js', function ( $response ) {
			$response['url'] = $this->filterImageUrl( $response['url'] );

			return $response;
		} );

		\add_filter( 'wp_get_attachment_image_src', function ( $image ) {
			$image[0] = $this->filterImageUrl( $image[0] );

			return $image;
		} );
	}

	public function filterImageUrl( $url ) {
		$resizeUrl = \trailingslashit( $this->uploads['baseurl'] ) . trim( get_option( 'resizefly_resized_path', 'resizefly' ), DIRECTORY_SEPARATOR );
		if ( strpos( $url, $resizeUrl ) === false ) {
			$url = str_replace( $this->uploads['baseurl'], $resizeUrl, $url );
		}

		return $url;
	}

	/**
	 * @return array wp_uploads_dir()
	 */
	public function getUploads() {
		return $this->uploads;
	}

	/**
	 * Wrap `wp_uploads_dir`
	 *
	 * @param $uploads array wp_uploads_dir
	 *
	 * @return array wp_uploads_dir<commit_msg>bail when not supported file type<commit_after><?php
/**
 * Created by PhpStorm.
 * User: alpipego
 * Date: 16/09/16
 * Time: 10:15
 */

namespace Alpipego\Resizefly\Upload;


/**
 * Class Dir
 * @package Alpipego\Resizefly\Upload
 */
class Dir {
	/**
	 * @var
	 */
	private $uploads;

	/**
	 * Dir constructor.
	 */
	public function __construct() {
		\add_filter( 'upload_dir', [ $this, 'resolvePath' ] );
		\add_filter( 'upload_dir', [ $this, 'resolveUrl' ] );
	}

	public function run() {
		\add_filter( 'resizefly_filter_url', [ $this, 'filterImageUrl' ] );
		\add_filter( 'wp_prepare_attachment_for_js', function ( $response ) {
			$response['url'] = $this->filterImageUrl( $response['url'] );

			return $response;
		} );

		\add_filter( 'wp_get_attachment_image_src', function ( $image ) {
			$image[0] = $this->filterImageUrl( $image[0] );

			return $image;
		} );
	}

	public function filterImageUrl( $url ) {
		if (!preg_match('%(png|jpe?g|gif)%', $url)) {
			return $url;
		}
		$resizeUrl = \trailingslashit( $this->uploads['baseurl'] ) . trim( get_option( 'resizefly_resized_path', 'resizefly' ), DIRECTORY_SEPARATOR );
		if ( strpos( $url, $resizeUrl ) === false ) {
			$url = str_replace( $this->uploads['baseurl'], $resizeUrl, $url );
		}

		return $url;
	}

	/**
	 * @return array wp_uploads_dir()
	 */
	public function getUploads() {
		return $this->uploads;
	}

	/**
	 * Wrap `wp_uploads_dir`
	 *
	 * @param $uploads array wp_uploads_dir
	 *
	 * @return array wp_uploads_dir
{'fraction_non_alphanumeric': 0.17598343685300208, 'fraction_numerical': 0.008281573498964804, 'mean_word_length': 3.7524590163934426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '2926603', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1062, 'n_words': 209}