lue']
&&
! in_array( __FUNCTION__, (array) $boxes, true )
) {
rocket_notice_html(
[
'status' => 'warning',
'message' => sprintf(
// Translators: %1$s = strong opening tag, %2$s = strong closing tag.
__( '%1$sWP Rocket:%2$s You have "Cache by Device Type" enabled on Cloudflare APO. If you judge it necessary for the website to have a different cache on mobile and desktop, we suggest you enable our “Separate Cache Files for Mobiles Devices” to ensure the generated cache is accurate.', 'rocket' ),
'',
''
),
'dismiss_button' => __FUNCTION__,
'dismissible' => '',
'action' => 'enable_separate_mobile_cache',
]
);
}
}
/**
* Checks if APO notices should be displayed
*
* @return bool
*/
private function can_display_notice(): bool {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return false;
}
$screen = get_current_screen();
if (
isset( $screen->id )
&&
'settings_page_wprocket' !== $screen->id
) {
return false;
}
if ( ! $this->is_plugin_active() ) {
return false;
}
return $this->is_apo_enabled();
}
/**
* Purge everything on Cloudflare
*
* @return void
*/
public function purge_cloudflare() {
if ( ! $this->is_plugin_active() ) {
return;
}
$this->facade->purge_everything();
}
/**
* Purges posts when using purge this URL button
*
* @param array $urls Array of URLs.
*
* @return void
*/
public function purge_cloudflare_partial( $urls ) {
if ( ! $this->is_plugin_active() ) {
return;
}
$post_ids = array_filter( array_map( 'url_to_postid', $urls ) );
$this->facade->purge_urls( $post_ids );
}
/**
* Purges CF after Used CSS generation or clean
*
* @param string $url URL to purge.
*
* @return void
*/
public function purge_cloudflare_after_usedcss( $url ) {
if ( ! $this->is_plugin_active() ) {
return;
}
$post_id = url_to_postid( $url );
if ( empty( $post_id ) ) {
return;
}
$this->facade->purge_urls( $post_id );
}
/**
* Enable separate cache for mobile option
*
* @return void
*/
public function enable_separate_mobile_cache() {
check_admin_referer( 'rocket_enable_separate_mobile_cache' );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$this->options->set( 'cache_mobile', 1 );
$this->options->set( 'do_caching_mobile_files', 1 );
$this->options_api->set( 'settings', $this->options->get_options() );
wp_safe_redirect( wp_get_referer() );
rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ? wp_die() : exit;
}
/**
* Checks if CF plugin is enabled & credentials saved
*
* @return bool
*/
private function is_plugin_active(): bool {
if ( ! is_plugin_active( 'cloudflare/cloudflare.php' ) ) {
return false;
}
if (
empty( get_option( 'cloudflare_api_email', '' ) )
||
empty( get_option( 'cloudflare_api_key', '' ) )
||
empty( get_option( 'cloudflare_cached_domain_name', '' ) )
) {
return false;
}
return true;
}
/**
* Checks if CF APO is enabled
*
* @return bool
*/
private function is_apo_enabled(): bool {
$is_apo_enabled = get_option( 'automatic_platform_optimization', [] );
if ( ! key_exists( 'value', $is_apo_enabled ) ) {
return false;
}
return (bool) $is_apo_enabled['value'];
}
/**
* Add the helper message on the CDN settings.
*
* @param string[] $addons Name from the addon that requires the helper message.
* @return string[]
*/
public function add_cdn_helper_message( array $addons ): array {
if ( ! $this->is_plugin_active() ) {
return $addons;
}
$addons[] = 'Cloudflare';
return $addons;
}
/**
* Purge Cloudflare on deactivate.
*
* @return void
*/
public function deactivate() {
$this->purge_cloudflare();
}
/**
* Unregister Call on clean posts.
*
* @return void
*/
public function unregister_cloudflare_clean_on_post() {
$this->unregister_callback( 'deleted_post', 'purgeCacheByRelevantURLs' );
$this->unregister_callback( 'transition_post_status', 'purgeCacheOnPostStatusChange', PHP_INT_MAX );
}
/**
* Unregister a callback.
*
* @param string $hook Hook on which to unregister.
* @param string $method The callback to unregister.
* @param int $priority the priority from the callback.
* @return void
*/
protected function unregister_callback( string $hook, string $method, int $priority = 10 ) {
global $wp_filter;
if ( ! key_exists( $hook, $wp_filter ) ) {
return;
}
$original_wp_filter = $wp_filter[ $hook ]->callbacks;
if ( ! key_exists( $priority, $original_wp_filter ) ) {
return;
}
foreach ( $original_wp_filter[ $priority ] as $key => $config ) {
if ( substr( $key, - strlen( $method ) ) !== $method ) {
continue;
}
unset( $wp_filter[ $hook ]->callbacks[ $priority ][ $key ] );
}
}
}