Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
wpcomsh_activate_blaze_module
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_force_activate_blaze_module
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_rm_blaze_module_list
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
wpcomsh_activate_blaze_module_on_launching
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_blaze_purge_transient_cache
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
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
9use 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 */
16function 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
28 */
29function wpcomsh_force_activate_blaze_module() {
30    if ( 'wp-admin' === get_option( 'wpcom_admin_interface' ) ) {
31        return;
32    }
33
34    wpcomsh_activate_blaze_module();
35}
36add_action( 'init', 'wpcomsh_force_activate_blaze_module', 0, 0 );
37
38/**
39 * Remove Blaze from the old Module list.
40 * Available at wp-admin/admin.php?page=jetpack_modules
41 *
42 * @param array $items Array of Jetpack modules.
43 * @return array
44 */
45function wpcomsh_rm_blaze_module_list( $items ) {
46    if ( isset( $items['blaze'] ) && get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) {
47        unset( $items['blaze'] );
48    }
49    return $items;
50}
51add_filter( 'jetpack_modules_list_table_items', 'wpcomsh_rm_blaze_module_list' );
52
53/**
54 * The Blaze module may not be auto-activated when the site is not public,
55 * so we have to activate the module when it's public manually.
56 *
57 * @param int $old_value of blog_public option.
58 * @param int $new_value of blog_public option.
59 */
60function wpcomsh_activate_blaze_module_on_launching( $old_value, $new_value ) {
61    $blog_public = (int) $new_value;
62    // 'blog_public' is set to '1' when a site is launched.
63    if ( $blog_public === 1 ) {
64        wpcomsh_activate_blaze_module();
65    }
66
67    return $new_value;
68}
69add_filter( 'update_option_blog_public', 'wpcomsh_activate_blaze_module_on_launching', 10, 2 );
70
71/**
72 * Delete the transient for the given site id.
73 *
74 * @return void
75 */
76function wpcomsh_blaze_purge_transient_cache() {
77    $site_id = Jetpack_Connection::get_site_id();
78
79    if ( is_wp_error( $site_id ) ) {
80        return;
81    }
82
83    $transient = 'jetpack_blaze_site_supports_blaze_' . $site_id;
84    delete_transient( $transient );
85}
86
87/**
88 * Delete the caching transient when coming soon is changed.
89 */
90add_action(
91    'pre_update_option_wpcom_public_coming_soon',
92    function ( $option ) {
93        wpcomsh_blaze_purge_transient_cache();
94        return $option;
95    }
96);
97
98/**
99 * Delete the caching transient when the blog visibility option changes.
100 */
101add_action(
102    'pre_update_option_blog_public',
103    function ( $option ) {
104        wpcomsh_blaze_purge_transient_cache();
105        return $option;
106    }
107);
108
109/**
110 * On Atomic sites the Promote with Blaze option is enabled.
111 */
112add_action(
113    'jetpack_modules_loaded',
114    function () {
115        remove_filter( 'jetpack_blaze_post_row_actions_enable', 'jetpack_blaze_post_row_actions_disable' );
116    }
117);