| 1 |
<?php |
| 2 |
namespace Imagify\ThirdParty\WPRocket; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Compat class for WP Rocket plugin. |
| 8 |
* |
| 9 |
* @since 1.9.3 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
class Main { |
| 13 |
use \Imagify\Traits\InstanceGetterTrait; |
| 14 |
|
| 15 |
/** |
| 16 |
* Launch the hooks. |
| 17 |
* |
| 18 |
* @since 1.9.3 |
| 19 |
* @access public |
| 20 |
* @author Grégory Viguier |
| 21 |
*/ |
| 22 |
public function init() { |
| 23 |
add_action( 'admin_init', [ $this, 'dequeue_sweetalert' ] ); |
| 24 |
add_filter( 'imagify_cdn_source', [ $this, 'set_cdn_source' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
/** ----------------------------------------------------------------------------------------- */ |
| 29 |
/** HOOKS =================================================================================== */ |
| 30 |
/** ----------------------------------------------------------------------------------------- */ |
| 31 |
|
| 32 |
/** |
| 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 |
* Provide a custom CDN source. |
| 58 |
* |
| 59 |
* @since 1.9.3 |
| 60 |
* @author Grégory Viguier |
| 61 |
* |
| 62 |
* @param array $source { |
| 63 |
* An array of arguments. |
| 64 |
* |
| 65 |
* @type $name string The name of which provides the URL (plugin name, etc). |
| 66 |
* @type $url string The CDN URL. |
| 67 |
* } |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
public function set_cdn_source( $source ) { |
| 71 |
if ( ! function_exists( 'get_rocket_option' ) ) { |
| 72 |
return $source; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! get_rocket_option( 'cdn' ) ) { |
| 76 |
return $source; |
| 77 |
} |
| 78 |
|
| 79 |
$container = apply_filters( 'rocket_container', null ); |
| 80 |
|
| 81 |
if ( is_object( $container ) && method_exists( $container, 'get' ) ) { |
| 82 |
$cdn = $container->get( 'cdn' ); |
| 83 |
|
| 84 |
if ( $cdn && method_exists( $cdn, 'get_cdn_urls' ) ) { |
| 85 |
$url = $cdn->get_cdn_urls( [ 'all', 'images' ] ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! isset( $url ) && function_exists( 'get_rocket_cdn_cnames' ) ) { |
| 90 |
$url = get_rocket_cdn_cnames( [ 'all', 'images' ] ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( empty( $url ) ) { |
| 94 |
return $source; |
| 95 |
} |
| 96 |
|
| 97 |
$url = reset( $url ); |
| 98 |
|
| 99 |
if ( ! $url ) { |
| 100 |
return $source; |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! preg_match( '@^(https?:)?//@i', $url ) ) { |
| 104 |
$url = '//' . $url; |
| 105 |
} |
| 106 |
|
| 107 |
$scheme = wp_parse_url( \Imagify_Filesystem::get_instance()->get_site_root_url() ); |
| 108 |
$scheme = ! empty( $scheme['scheme'] ) ? $scheme['scheme'] : null; |
| 109 |
$url = set_url_scheme( $url, $scheme ); |
| 110 |
|
| 111 |
$source['name'] = 'WP Rocket'; |
| 112 |
$source['url'] = $url; |
| 113 |
|
| 114 |
return $source; |
| 115 |
} |
| 116 |
} |
| 117 |
|