| 1 |
<?php |
| 2 |
namespace Imagify\ThirdParty\WPRocket; |
| 3 |
|
| 4 |
use Imagify\Traits\InstanceGetterTrait; |
| 5 |
|
| 6 |
/** |
| 7 |
* Compat class for WP Rocket plugin. |
| 8 |
* |
| 9 |
* @since 1.9.3 |
| 10 |
*/ |
| 11 |
class Main { |
| 12 |
use InstanceGetterTrait; |
| 13 |
|
| 14 |
/** |
| 15 |
* Launch the hooks. |
| 16 |
* |
| 17 |
* @since 1.9.3 |
| 18 |
*/ |
| 19 |
public function init() { |
| 20 |
add_filter( 'imagify_cdn_source', [ $this, 'set_cdn_source' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** ----------------------------------------------------------------------------------------- */ |
| 25 |
/** HOOKS =================================================================================== */ |
| 26 |
/** ----------------------------------------------------------------------------------------- */ |
| 27 |
|
| 28 |
/** |
| 29 |
* Provide a custom CDN source. |
| 30 |
* |
| 31 |
* @since 1.9.3 |
| 32 |
* |
| 33 |
* @param array $source { |
| 34 |
* An array of arguments. |
| 35 |
* |
| 36 |
* @type $name string The name of which provides the URL (plugin name, etc). |
| 37 |
* @type $url string The CDN URL. |
| 38 |
* } |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public function set_cdn_source( $source ) { |
| 42 |
if ( ! function_exists( 'get_rocket_option' ) ) { |
| 43 |
return $source; |
| 44 |
} |
| 45 |
|
| 46 |
if ( ! get_rocket_option( 'cdn' ) ) { |
| 47 |
return $source; |
| 48 |
} |
| 49 |
|
| 50 |
$container = apply_filters( 'rocket_container', null ); |
| 51 |
|
| 52 |
if ( is_object( $container ) && method_exists( $container, 'get' ) ) { |
| 53 |
$cdn = $container->get( 'cdn' ); |
| 54 |
|
| 55 |
if ( $cdn && method_exists( $cdn, 'get_cdn_urls' ) ) { |
| 56 |
$url = $cdn->get_cdn_urls( [ 'all', 'images' ] ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! isset( $url ) && function_exists( 'get_rocket_cdn_cnames' ) ) { |
| 61 |
$url = get_rocket_cdn_cnames( [ 'all', 'images' ] ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( empty( $url ) ) { |
| 65 |
return $source; |
| 66 |
} |
| 67 |
|
| 68 |
$url = reset( $url ); |
| 69 |
|
| 70 |
if ( ! $url ) { |
| 71 |
return $source; |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! preg_match( '@^(https?:)?//@i', $url ) ) { |
| 75 |
$url = '//' . $url; |
| 76 |
} |
| 77 |
|
| 78 |
$scheme = wp_parse_url( \Imagify_Filesystem::get_instance()->get_site_root_url() ); |
| 79 |
$scheme = ! empty( $scheme['scheme'] ) ? $scheme['scheme'] : null; |
| 80 |
$url = set_url_scheme( $url, $scheme ); |
| 81 |
|
| 82 |
$source['name'] = 'WP Rocket'; |
| 83 |
$source['url'] = $url; |
| 84 |
|
| 85 |
return $source; |
| 86 |
} |
| 87 |
} |
| 88 |
|