| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Cdn { |
| 7 |
private static $instance = null; |
| 8 |
private $assets_url; |
| 9 |
private $version; |
| 10 |
private $token; |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin constructor. |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 18 |
$this->version = WPMCS_VERSION; |
| 19 |
$this->token = WPMCS_TOKEN; |
| 20 |
|
| 21 |
// Initialize setup |
| 22 |
// $this->init(); |
| 23 |
} |
| 24 |
/** |
| 25 |
* |
| 26 |
* CDN url generate |
| 27 |
* @since 1.0.0 |
| 28 |
* |
| 29 |
*/ |
| 30 |
public static function may_generate_cdn_url($url, $key) { |
| 31 |
$settings = Utils::get_settings(); |
| 32 |
|
| 33 |
if ( |
| 34 |
isset($settings['enable_cdn'], $settings['cdn_url']) && |
| 35 |
$settings['enable_cdn'] && !empty($settings['cdn_url']) && |
| 36 |
isset($url, $key) && !Utils::is_empty($url) && !Utils::is_empty($key) |
| 37 |
) { |
| 38 |
$url_parts = explode($key, $url); |
| 39 |
if (!empty($url_parts[0])) { |
| 40 |
$cdn_url = 'https://' . preg_replace('#^https?://#i', '', trim($settings['cdn_url'])); |
| 41 |
$new_url = str_replace(trailingslashit($url_parts[0]), trailingslashit($cdn_url), $url); |
| 42 |
$new_url = apply_filters('wpmcs_cdn_url', $new_url, $url, $cdn_url); |
| 43 |
return preg_replace('/([^:])(\/{2,})/', '$1/', $new_url); // remove double slashes |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return $url; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Is CDN URL ? |
| 53 |
* @since 1.3.6 |
| 54 |
*/ |
| 55 |
public static function is_cdn_url($url) { |
| 56 |
$settings = Utils::get_settings(); |
| 57 |
|
| 58 |
if ( |
| 59 |
isset($settings['enable_cdn'], $settings['cdn_url']) && |
| 60 |
$settings['enable_cdn'] && !empty($settings['cdn_url']) && |
| 61 |
isset($url) && !Utils::is_empty($url) |
| 62 |
) { |
| 63 |
$cdn_url = 'https://' . preg_replace('#^https?://#i', '', trim($settings['cdn_url'])); |
| 64 |
return (strpos($url, $cdn_url . '/') !== false); |
| 65 |
} |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Get the CDN domain. |
| 73 |
* |
| 74 |
* @since 1.3.6 |
| 75 |
* @return string The CDN domain URL if enabled, otherwise an empty string. |
| 76 |
*/ |
| 77 |
public static function get_domain() { |
| 78 |
$settings = Utils::get_settings(); |
| 79 |
|
| 80 |
if ( |
| 81 |
isset($settings['enable_cdn'], $settings['cdn_url']) && |
| 82 |
$settings['enable_cdn'] && !empty($settings['cdn_url']) |
| 83 |
) { |
| 84 |
return 'https://' . preg_replace('#^https?://#i', '', trim($settings['cdn_url'])); |
| 85 |
} |
| 86 |
|
| 87 |
return ''; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public static function instance(){ |
| 92 |
if (is_null(self::$instance)) { |
| 93 |
self::$instance = new self(); |
| 94 |
} |
| 95 |
return self::$instance; |
| 96 |
} |
| 97 |
} |