diff --git a/camo-image-proxy.php b/camo-image-proxy.php index 5bfce911edf4230042ec967543caf8b4995d9386..35251e02dd967538d7ef36296b5a8124d4be9e1d 100755 --- a/camo-image-proxy.php +++ b/camo-image-proxy.php @@ -34,7 +34,7 @@ require_once PLUGIN_PATH . '/inc/class-options-page.php'; /** * URL Building */ -require_once PLUGIN_PATH . '/inc/class-urls.php'; +require_once PLUGIN_PATH . '/inc/class-url.php'; /** * Rewrite WordPress-generated URLs @@ -60,5 +60,7 @@ function init() { if ( is_admin() ) { Options_Page::instance(); } + + Rewrite_URLs::instance(); } add_action( 'init', __NAMESPACE__ . '\init' ); diff --git a/inc/class-rewrite-urls.php b/inc/class-rewrite-urls.php index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..cd68b690bb0f07dc10f95e4201f315547181bfe2 100644 --- a/inc/class-rewrite-urls.php +++ b/inc/class-rewrite-urls.php @@ -0,0 +1,38 @@ +<?php +/** + * Force Core's image functions to use Camo + * + * @package Camo_Image_Proxy + */ + +namespace Camo_Image_Proxy; + +/** + * Class Rewrite_URLs + */ +class Rewrite_URLs { + use Singleton; + + /** + * Hooks + */ + public function setup() { + add_filter( 'wp_get_attachment_image_src', [ $this, 'encode_image' ] ); + } + + /** + * Camouflage attachment URL + * + * @param array $image Image data. + * @return array + */ + public function encode_image( array $image ) : array { + $url = URL::instance()->encode( $image[0] ); + + if ( is_string( $url ) ) { + $image[0] = $url; + } + + return $image; + } +} diff --git a/inc/class-urls.php b/inc/class-url.php similarity index 86% rename from inc/class-urls.php rename to inc/class-url.php index 00f51cd3d8ec80a8259805c01ca131fba7c1ab69..dc2475e5137220650b3eb88e39b537cc96807c17 100644 --- a/inc/class-urls.php +++ b/inc/class-url.php @@ -25,7 +25,7 @@ class URL { $can_rewrite = true; // Validate host. - if ( $this->is_valid_url( $host ) ) { + if ( ! $this->is_valid_url( $host ) ) { $can_rewrite = false; } @@ -75,6 +75,14 @@ class URL { * @return bool */ private function is_valid_url( string $url ) : bool { - return empty( $url ) || ( ! filter_var( $url, FILTER_VALIDATE_URL ) && ! filter_var( $url, FILTER_VALIDATE_IP ) ); + if ( empty( $url ) ) { + return false; + } + + if ( false === filter_var( $url, FILTER_VALIDATE_URL ) && false === filter_var( $url, FILTER_VALIDATE_IP ) ) { + return false; + } + + return true; } }