Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
31.11% |
14 / 45 |
|
33.33% |
2 / 6 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_activate_blaze_module | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| wpcomsh_force_activate_blaze_module | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| wpcomsh_report_forced_blaze_module | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| wpcomsh_rm_blaze_module_list | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| wpcomsh_activate_blaze_module_on_launching | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| wpcomsh_blaze_purge_transient_cache | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Customizations to the Blaze feature. |
| 4 | * We want that feature to always be available on Atomic sites. |
| 5 | * |
| 6 | * @package wpcomsh |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
| 10 | |
| 11 | /** |
| 12 | * Activate the Blaze module |
| 13 | * If you use a version of Jetpack that supports it, |
| 14 | * and if it is not already enabled. |
| 15 | */ |
| 16 | function wpcomsh_activate_blaze_module() { |
| 17 | if ( ! defined( 'JETPACK__VERSION' ) || ! class_exists( 'Jetpack' ) ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | if ( ! Jetpack::is_module_active( 'blaze' ) ) { |
| 22 | Jetpack::activate_module( 'blaze', false, false ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Force-enable the Blaze module, and report it as forced on. |
| 28 | */ |
| 29 | function wpcomsh_force_activate_blaze_module() { |
| 30 | if ( 'wp-admin' === get_option( 'wpcom_admin_interface' ) ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | wpcomsh_activate_blaze_module(); |
| 35 | |
| 36 | // Only after activating: the filter makes Blaze read as active, so activation would skip saving it. |
| 37 | if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'blaze' ) ) { |
| 38 | add_filter( 'jetpack_active_modules', 'wpcomsh_report_forced_blaze_module' ); |
| 39 | } |
| 40 | } |
| 41 | add_action( 'init', 'wpcomsh_force_activate_blaze_module', 0, 0 ); |
| 42 | |
| 43 | /** |
| 44 | * Add Blaze to the active modules, which Jetpack reads as forced on and locks its switch. |
| 45 | * |
| 46 | * @param array $modules Active module slugs. |
| 47 | * @return array |
| 48 | */ |
| 49 | function wpcomsh_report_forced_blaze_module( $modules ) { |
| 50 | return array_merge( (array) $modules, array( 'blaze' ) ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Remove Blaze from the old Module list. |
| 55 | * Available at wp-admin/admin.php?page=jetpack_modules |
| 56 | * |
| 57 | * @param array $items Array of Jetpack modules. |
| 58 | * @return array |
| 59 | */ |
| 60 | function wpcomsh_rm_blaze_module_list( $items ) { |
| 61 | if ( isset( $items['blaze'] ) && get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) { |
| 62 | unset( $items['blaze'] ); |
| 63 | } |
| 64 | return $items; |
| 65 | } |
| 66 | add_filter( 'jetpack_modules_list_table_items', 'wpcomsh_rm_blaze_module_list' ); |
| 67 | |
| 68 | /** |
| 69 | * The Blaze module may not be auto-activated when the site is not public, |
| 70 | * so we have to activate the module when it's public manually. |
| 71 | * |
| 72 | * @param int $old_value of blog_public option. |
| 73 | * @param int $new_value of blog_public option. |
| 74 | */ |
| 75 | function wpcomsh_activate_blaze_module_on_launching( $old_value, $new_value ) { |
| 76 | $blog_public = (int) $new_value; |
| 77 | // 'blog_public' is set to '1' when a site is launched. |
| 78 | if ( $blog_public === 1 ) { |
| 79 | wpcomsh_activate_blaze_module(); |
| 80 | } |
| 81 | |
| 82 | return $new_value; |
| 83 | } |
| 84 | add_filter( 'update_option_blog_public', 'wpcomsh_activate_blaze_module_on_launching', 10, 2 ); |
| 85 | |
| 86 | /** |
| 87 | * Delete the transient for the given site id. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | function wpcomsh_blaze_purge_transient_cache() { |
| 92 | $site_id = Jetpack_Connection::get_site_id(); |
| 93 | |
| 94 | if ( is_wp_error( $site_id ) ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $transient = 'jetpack_blaze_site_supports_blaze_' . $site_id; |
| 99 | delete_transient( $transient ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Delete the caching transient when coming soon is changed. |
| 104 | */ |
| 105 | add_action( |
| 106 | 'pre_update_option_wpcom_public_coming_soon', |
| 107 | function ( $option ) { |
| 108 | wpcomsh_blaze_purge_transient_cache(); |
| 109 | return $option; |
| 110 | } |
| 111 | ); |
| 112 | |
| 113 | /** |
| 114 | * Delete the caching transient when the blog visibility option changes. |
| 115 | */ |
| 116 | add_action( |
| 117 | 'pre_update_option_blog_public', |
| 118 | function ( $option ) { |
| 119 | wpcomsh_blaze_purge_transient_cache(); |
| 120 | return $option; |
| 121 | } |
| 122 | ); |
| 123 | |
| 124 | /** |
| 125 | * On Atomic sites the Promote with Blaze option is enabled. |
| 126 | */ |
| 127 | add_action( |
| 128 | 'jetpack_modules_loaded', |
| 129 | function () { |
| 130 | remove_filter( 'jetpack_blaze_post_row_actions_enable', 'jetpack_blaze_post_row_actions_disable' ); |
| 131 | } |
| 132 | ); |