| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\CDN; |
| 5 |
|
| 6 |
use Imagify\EventManagement\SubscriberInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* CDN subscriber |
| 10 |
*/ |
| 11 |
class CDN implements SubscriberInterface { |
| 12 |
/** |
| 13 |
* Array of events this subscriber listens to |
| 14 |
* |
| 15 |
* @return array |
| 16 |
*/ |
| 17 |
public static function get_subscribed_events() { |
| 18 |
return [ |
| 19 |
'imagify_cdn_source_url' => 'get_cdn_source', |
| 20 |
]; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get the CDN "source". |
| 25 |
* |
| 26 |
* @since 1.9.3 |
| 27 |
* |
| 28 |
* @param string $option_url An URL to use instead of the one stored in the option. It is used only if no constant/filter. |
| 29 |
* |
| 30 |
* @return array { |
| 31 |
* @type string $source Where does it come from? Possible values are 'constant', 'filter', or 'option'. |
| 32 |
* @type string $name Who? Can be a constant name, a plugin name, or an empty string. |
| 33 |
* @type string $url The CDN URL, with a trailing slash. An empty string if no URL is set. |
| 34 |
* } |
| 35 |
*/ |
| 36 |
public function get_cdn_source( $option_url = '' ) { |
| 37 |
if ( defined( 'IMAGIFY_CDN_URL' ) && IMAGIFY_CDN_URL && is_string( IMAGIFY_CDN_URL ) ) { |
| 38 |
// Use a constant. |
| 39 |
$source = [ |
| 40 |
'source' => 'constant', |
| 41 |
'name' => 'IMAGIFY_CDN_URL', |
| 42 |
'url' => IMAGIFY_CDN_URL, |
| 43 |
]; |
| 44 |
} else { |
| 45 |
// Maybe use a filter. |
| 46 |
$filter_source = [ |
| 47 |
'name' => null, |
| 48 |
'url' => null, |
| 49 |
]; |
| 50 |
|
| 51 |
/** |
| 52 |
* Provide a custom CDN source. |
| 53 |
* |
| 54 |
* @since 1.9.3 |
| 55 |
* |
| 56 |
* @param array $filter_source { |
| 57 |
* @type $name string The name of which provides the URL (plugin name, etc). |
| 58 |
* @type $url string The CDN URL. |
| 59 |
* } |
| 60 |
*/ |
| 61 |
$filter_source = apply_filters( 'imagify_cdn_source', $filter_source ); |
| 62 |
|
| 63 |
if ( ! empty( $filter_source['url'] ) ) { |
| 64 |
$source = [ |
| 65 |
'source' => 'filter', |
| 66 |
'name' => ! empty( $filter_source['name'] ) ? $filter_source['name'] : '', |
| 67 |
'url' => $filter_source['url'], |
| 68 |
]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if ( empty( $source['url'] ) ) { |
| 73 |
// No constant, no filter: use the option. |
| 74 |
$source = [ |
| 75 |
'source' => 'option', |
| 76 |
'name' => '', |
| 77 |
'url' => $option_url && is_string( $option_url ) ? $option_url : get_imagify_option( 'cdn_url' ), |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
if ( empty( $source['url'] ) ) { |
| 82 |
// Nothing set. |
| 83 |
return [ |
| 84 |
'source' => 'option', |
| 85 |
'name' => '', |
| 86 |
'url' => '', |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
$source['url'] = $this->sanitize_cdn_url( $source['url'] ); |
| 91 |
|
| 92 |
if ( empty( $source['url'] ) ) { |
| 93 |
// Not an URL. |
| 94 |
return [ |
| 95 |
'source' => 'option', |
| 96 |
'name' => '', |
| 97 |
'url' => '', |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
return $source; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Sanitize the CDN URL value. |
| 106 |
* |
| 107 |
* @since 1.9.3 |
| 108 |
* |
| 109 |
* @param string $url The URL to sanitize. |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function sanitize_cdn_url( $url ) { |
| 114 |
$url = sanitize_text_field( $url ); |
| 115 |
|
| 116 |
if ( ! $url || ! preg_match( '@^https?://.+\.[^.]+@i', $url ) ) { |
| 117 |
// Not an URL. |
| 118 |
return ''; |
| 119 |
} |
| 120 |
|
| 121 |
return trailingslashit( $url ); |
| 122 |
} |
| 123 |
} |
| 124 |
|