Commit e6c5b324 authored by Erick Hitter's avatar Erick Hitter

Camouflage attachment URLs

parent ddafdc42
......@@ -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' );
<?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;
}
}
......@@ -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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment