Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
wpcom_forms_has_blog_sticker
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
wpcom_is_central_forms_management_enabled
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
wpcom_maybe_disable_central_forms_management
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcom_contact_form_set_editor_feature_flags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * WordPress.com Contact Form feature flags.
4 *
5 * Controls the Central Forms Management rollout and disables the integrations
6 * tab on WordPress.com sites. Works on both Simple and Atomic infrastructure.
7 *
8 * @package automattic/jetpack-mu-wpcom
9 */
10
11/**
12 * Check if a site has a given blog sticker.
13 *
14 * Uses the appropriate sticker API depending on whether the site is
15 * Simple (has_blog_sticker) or Atomic (wpcomsh_is_site_sticker_active).
16 *
17 * @param string $sticker The sticker name to check.
18 * @param int    $blog_id The blog ID to check.
19 * @return bool
20 */
21function wpcom_forms_has_blog_sticker( $sticker, $blog_id ) {
22    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC && function_exists( 'wpcomsh_is_site_sticker_active' ) ) {
23        return (bool) wpcomsh_is_site_sticker_active( $sticker );
24    } elseif ( function_exists( 'has_blog_sticker' ) ) {
25        return (bool) has_blog_sticker( $sticker, $blog_id );
26    }
27
28    return false;
29}
30
31/**
32 * Check if Central Forms Management is enabled for a given blog.
33 *
34 * Enabled for all WordPress.com sites except e2e test sites.
35 *
36 * @param int|null $blog_id Blog ID. Defaults to the current WP.com blog ID.
37 * @return bool
38 */
39function wpcom_is_central_forms_management_enabled( $blog_id = null ) {
40    if ( null === $blog_id ) {
41        $blog_id = function_exists( 'get_wpcom_blog_id' ) ? get_wpcom_blog_id() : get_current_blog_id();
42    }
43
44    // Exclude e2e test sites — their tests aren't ready for CFM yet.
45    if ( wpcom_forms_has_blog_sticker( 'a8c-e2e-test-blog', $blog_id ) ) {
46        return false;
47    }
48
49    // Allow disabling CFM for individual sites via blog sticker.
50    if ( wpcom_forms_has_blog_sticker( 'disable-central-forms-management', $blog_id ) ) {
51        return false;
52    }
53
54    return true;
55}
56
57/**
58 * Disable Central Forms Management for excluded WordPress.com sites.
59 *
60 * CFM is now enabled by default in the Forms package. This function
61 * explicitly disables it for sites that should be excluded (e2e test
62 * sites, sites with the disable-central-forms-management sticker).
63 *
64 * Called immediately on file load since this file is required during
65 * plugins_loaded (priority 10) inside load_wpcom_sites_features().
66 */
67function wpcom_maybe_disable_central_forms_management() {
68    if ( wpcom_is_central_forms_management_enabled() ) {
69        return;
70    }
71
72    add_filter( 'jetpack_forms_alpha', '__return_false', 999 );
73}
74wpcom_maybe_disable_central_forms_management();
75
76/**
77 * Set the 'central-form-management' block editor feature flag.
78 *
79 * @param array $flags Existing feature flags.
80 * @return array Modified feature flags.
81 */
82function wpcom_contact_form_set_editor_feature_flags( $flags ) {
83    $flags['central-form-management'] = wpcom_is_central_forms_management_enabled();
84    return $flags;
85}
86add_filter( 'jetpack_block_editor_feature_flags', 'wpcom_contact_form_set_editor_feature_flags', 1000 );