| @@ -49,12 +49,20 @@ | ||
| 49 | 49 | |
| 50 | 50 | $container = apply_filters( 'rocket_container', null ); |
| 51 | 51 | |
| 52 | 52 | if ( is_object( $container ) && method_exists( $container, 'get' ) ) { |
| 53 | - $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' ); | |
| 54 | 57 | |
| 55 | - if ( $cdn && method_exists( $cdn, 'get_cdn_urls' ) ) { | |
| 56 | - $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; | |
| 57 | 65 | } |
| 58 | 66 | } |
| 59 | 67 | |
| 60 | 68 | if ( ! isset( $url ) && function_exists( 'get_rocket_cdn_cnames' ) ) { |