photon.php
54 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Compatibility for Photon |
| 4 | * |
| 5 | * Replace photon with image-cdn. |
| 6 | * |
| 7 | * @package automattic/jetpack-image-cdn |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Image_CDN\Compatibility; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Disable the unpackaged photon version living in Jetpack. |
| 18 | * |
| 19 | * Before the package, Jetpack had the photon functionality within the plugin. To avoid double |
| 20 | * activation, we need to disable the photon functionality in Jetpack to avoid potential conflicts. |
| 21 | * |
| 22 | * If this package is installed via a different plugin, e.g. Boost, Image CDN should be served instead |
| 23 | * of the photon module in Jetpack. |
| 24 | */ |
| 25 | function jetpack_image_cdn_photon_compat() { |
| 26 | /* |
| 27 | * Photon used have different functions names. They are later replaced by methods in |
| 28 | * Image_CDN_Core class. And the filters are now handled by the Image_CDN_Core class itself. |
| 29 | */ |
| 30 | remove_filter( 'jetpack_photon_url', 'jetpack_photon_url', 10 ); |
| 31 | remove_filter( 'jetpack_photon_pre_args', 'jetpack_photon_parse_wpcom_query_args', 10 ); |
| 32 | remove_filter( 'jetpack_photon_skip_for_url', 'jetpack_photon_banned_domains', 9 ); |
| 33 | remove_filter( 'widget_text', 'jetpack_photon_support_text_widgets' ); |
| 34 | |
| 35 | /* |
| 36 | * If photon is active in an old version of Jetpack which is using the unpackaged photon |
| 37 | * fake loading the module in Jetpack itself. This will prevent jetpack from loading |
| 38 | * the photon module, and we will use the image CDN package instead. |
| 39 | * |
| 40 | * When this is happening, the package is likely being loaded from a different plugin, e.g. Boost. |
| 41 | */ |
| 42 | if ( class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( JETPACK__VERSION, '12.1', '<=' ) ) ) { |
| 43 | do_action( 'jetpack_module_loaded_photon' ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Remove the hooks that Jetpack uses to load photon and pretend that the module is already loaded. |
| 49 | * |
| 50 | * Jetpack hooks the filters as it loads. So, we can remove the filters when `plugins_loaded` fires. |
| 51 | * This would make sure Jetpack has already loaded and hooked the filters. |
| 52 | */ |
| 53 | add_action( 'plugins_loaded', __NAMESPACE__ . '\jetpack_image_cdn_photon_compat' ); |
| 54 |