| @@ -1,27 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Imagify\ThirdParty\WPRocket; |
| 3 | 3 | |
| 4 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 4 | +use Imagify\Traits\InstanceGetterTrait; | |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | 7 | * Compat class for WP Rocket plugin. |
| 8 | 8 | * |
| 9 | - * @since 1.9.3 | |
| 10 | - * @author Grégory Viguier | |
| 9 | + * @since 1.9.3 | |
| 11 | 10 | */ |
| 12 | 11 | class Main { |
| 13 | - use \Imagify\Traits\InstanceGetterTrait; | |
| 12 | + use InstanceGetterTrait; | |
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * Launch the hooks. |
| 17 | 16 | * |
| 18 | - * @since 1.9.3 | |
| 19 | - * @access public | |
| 20 | - * @author Grégory Viguier | |
| 17 | + * @since 1.9.3 | |
| 21 | 18 | */ |
| 22 | 19 | public function init() { |
| 23 | - add_action( 'admin_init', [ $this, 'dequeue_sweetalert' ] ); | |
| 24 | 20 | add_filter( 'imagify_cdn_source', [ $this, 'set_cdn_source' ] ); |
| 25 | 21 | } |
| 26 | 22 | |
| 27 | 23 | |
| @@ -29,36 +25,11 @@ | ||
| 29 | 25 | /** HOOKS =================================================================================== */ |
| 30 | 26 | /** ----------------------------------------------------------------------------------------- */ |
| 31 | 27 | |
| 32 | 28 | /** |
| 33 | - * Remove all Imagify admin notices + CSS & JS files on WP Rocket (< 3.0) options screen to avoid conflict with older version of SweetAlert. | |
| 34 | - * | |
| 35 | - * @since 1.9.3 | |
| 36 | - * @access public | |
| 37 | - * @author Grégory Viguier | |
| 38 | - */ | |
| 39 | - public function dequeue_sweetalert() { | |
| 40 | - if ( ! defined( 'WP_ROCKET_VERSION' ) || ! defined( 'WP_ROCKET_PLUGIN_SLUG' ) ) { | |
| 41 | - return; | |
| 42 | - } | |
| 43 | - | |
| 44 | - if ( version_compare( WP_ROCKET_VERSION, '3.0' ) >= 0 ) { | |
| 45 | - return; | |
| 46 | - } | |
| 47 | - | |
| 48 | - if ( ! imagify_is_screen( 'settings_page_' . WP_ROCKET_PLUGIN_SLUG ) && ! imagify_is_screen( 'settings_page_' . WP_ROCKET_PLUGIN_SLUG . '-network' ) ) { | |
| 49 | - return; | |
| 50 | - } | |
| 51 | - | |
| 52 | - remove_action( 'all_admin_notices', [ \Imagify_Notices::get_instance(), 'render_notices' ] ); | |
| 53 | - remove_action( 'admin_enqueue_scripts', [ \Imagify_Assets::get_instance(), 'enqueue_styles_and_scripts' ], IMAGIFY_INT_MAX ); | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | 29 | * Provide a custom CDN source. |
| 58 | 30 | * |
| 59 | - * @since 1.9.3 | |
| 60 | - * @author Grégory Viguier | |
| 31 | + * @since 1.9.3 | |
| 61 | 32 | * |
| 62 | 33 | * @param array $source { |
| 63 | 34 | * An array of arguments. |
| 64 | 35 | * |
| @@ -78,12 +49,20 @@ | ||
| 78 | 49 | |
| 79 | 50 | $container = apply_filters( 'rocket_container', null ); |
| 80 | 51 | |
| 81 | 52 | if ( is_object( $container ) && method_exists( $container, 'get' ) ) { |
| 82 | - $cdn = $container->get( 'cdn' ); | |
| 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' ); | |
| 83 | 57 | |
| 84 | - if ( $cdn && method_exists( $cdn, 'get_cdn_urls' ) ) { | |
| 85 | - $url = $cdn->get_cdn_urls( [ 'all', 'images' ] ); | |
| 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; | |
| 86 | 65 | } |
| 87 | 66 | } |
| 88 | 67 | |
| 89 | 68 | if ( ! isset( $url ) && function_exists( 'get_rocket_cdn_cnames' ) ) { |