Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
Automattic\Jetpack_Boost\Compatibility\Super_Cache\migrate_from_super_cache
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Automatically enable Page Cache when migrating from WP Super Cache.
4 *
5 * @package automattic/jetpack-boost
6 */
7
8namespace Automattic\Jetpack_Boost\Compatibility\Super_Cache;
9
10use Automattic\Jetpack_Boost\Lib\Analytics;
11use Automattic\Jetpack_Boost\Lib\Status;
12use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Page_Cache;
13
14/**
15 * Accept migrations from WP Super Cache
16 */
17function migrate_from_super_cache() {
18
19    // Check if Jetpack Boost Page Cache module is enabled
20    if ( ! class_exists( Page_Cache::class ) ) {
21        return;
22    }
23
24    if ( ! Page_Cache::is_available() ) {
25        return;
26    }
27
28    // If Boost has already activated the Page Cache based on WPSC migration once, we don't want to do it again.
29    // For example - if the user manually de-activates Page Cache in Boost.
30    $migration_status = get_transient( 'jb_cache_moved_to_boost' );
31    // If the cache is already migrated, we don't need to do anything.
32    if ( get_transient( 'jb_boost_migration_complete' ) ) {
33        return;
34    }
35
36    // If Super Cache has set the transient, log it to tracks, but only once.
37    if ( $migration_status && ! get_transient( 'jb_boost_migration_tracked' ) ) {
38        set_transient( 'jb_boost_migration_tracked', true, 7 * DAY_IN_SECONDS );
39        Analytics::record_user_event( 'migrated_from_wpsc', array( 'source' => $migration_status ) );
40    }
41
42    // Only proceed to activate Page Cache in Jetpack Boost
43    // if the user clicked the admin notice.
44    if ( $migration_status !== 'notice' ) {
45        return;
46    }
47
48    // Check if Boost has Page Cache already enabled.
49    $status = new Status( Page_Cache::get_slug() );
50    if ( $status->get() === true ) {
51        return;
52    }
53
54    set_transient( 'jb_boost_migration_complete', true, 7 * DAY_IN_SECONDS );
55    $status->set( true );
56}
57
58add_action( 'admin_init', __NAMESPACE__ . '\migrate_from_super_cache' );