PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / 3rd-party / wp-rocket / classes / Main.php

Main.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at inc/3rd-party/wp-rocket/classes/Main.php

96 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 try {
54 // WP Rocket does not register the 'cdn' service in every context.
55 if ( ! method_exists( $container, 'has' ) || $container->has( 'cdn' ) ) {
56 $cdn = $container->get( 'cdn' );
57
58 if ( $cdn && method_exists( $cdn, 'get_cdn_urls' ) ) {
59 $url = $cdn->get_cdn_urls( [ 'all', 'images' ] );
60 }
61 }
62 } catch ( \Throwable $e ) {
63 // Container could not resolve or build the service: fall back to get_rocket_cdn_cnames().
64 $url = null;
65 }
66 }
67
68 if ( ! isset( $url ) && function_exists( 'get_rocket_cdn_cnames' ) ) {
69 $url = get_rocket_cdn_cnames( [ 'all', 'images' ] );
70 }
71
72 if ( empty( $url ) ) {
73 return $source;
74 }
75
76 $url = reset( $url );
77
78 if ( ! $url ) {
79 return $source;
80 }
81
82 if ( ! preg_match( '@^(https?:)?//@i', $url ) ) {
83 $url = '//' . $url;
84 }
85
86 $scheme = wp_parse_url( \Imagify_Filesystem::get_instance()->get_site_root_url() );
87 $scheme = ! empty( $scheme['scheme'] ) ? $scheme['scheme'] : null;
88 $url = set_url_scheme( $url, $scheme );
89
90 $source['name'] = 'WP Rocket';
91 $source['url'] = $url;
92
93 return $source;
94 }
95 }
96