| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dashboard policy: the dashboard feature flags and the composition flag's script-data bridge. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-premium-analytics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Feature_Flags\Feature_Flags; |
| 11 |
|
| 12 |
/** |
| 13 |
* Name of the feature flag that offers adding, removing and resetting widgets. |
| 14 |
*/ |
| 15 |
const DASHBOARD_COMPOSITION_FLAG = 'premium-analytics-dashboard-composition'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Name of the feature flag that shows the Store section. |
| 19 |
*/ |
| 20 |
const DASHBOARD_STORE_SECTION_FLAG = 'premium-analytics-store-section'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Registers the dashboard feature flags. |
| 24 |
* |
| 25 |
* Runs on every request so the flag stays discoverable wherever flags are read or |
| 26 |
* toggled: REST, WP-CLI and the WordPress.com control screen included. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function register_dashboard_feature_flags() { |
| 31 |
Feature_Flags::register( |
| 32 |
DASHBOARD_COMPOSITION_FLAG, |
| 33 |
array( |
| 34 |
'default' => true, |
| 35 |
'description' => 'Offer adding, removing and resetting widgets on the analytics dashboard, on top of moving and resizing them.', |
| 36 |
'owner' => 'jetpack-premium-analytics', |
| 37 |
) |
| 38 |
); |
| 39 |
|
| 40 |
Feature_Flags::register( |
| 41 |
DASHBOARD_STORE_SECTION_FLAG, |
| 42 |
array( |
| 43 |
'default' => false, |
| 44 |
'description' => 'Show the Store tab on the analytics dashboard of sites running WooCommerce.', |
| 45 |
'owner' => 'jetpack-premium-analytics', |
| 46 |
) |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Whether the dashboard offers adding, removing and resetting widgets. |
| 52 |
* |
| 53 |
* @return bool |
| 54 |
*/ |
| 55 |
function is_dashboard_composition_enabled() { |
| 56 |
return Feature_Flags::is_enabled( DASHBOARD_COMPOSITION_FLAG ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Whether the dashboard shows the Store section. |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
function is_dashboard_store_section_enabled() { |
| 65 |
return Feature_Flags::is_enabled( DASHBOARD_STORE_SECTION_FLAG ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* No-op kept for older copies of the package, whose dashboard-sections.php calls it after |
| 70 |
* skipping its include of this file. |
| 71 |
* |
| 72 |
* @deprecated 0.10.0 The preview scope it opened is gone. |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
function is_dashboard_unlocked_for_a11n() { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Configures the dashboard policy script data. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
function configure_dashboard_policy() { |
| 86 |
add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_dashboard_policy_script_data', 20 ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Injects the flag's answer into JetpackScriptData for the dashboard policy. |
| 91 |
* |
| 92 |
* @param array $data The script data passed by the assets package. |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
function inject_dashboard_policy_script_data( array $data ): array { |
| 96 |
if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 97 |
$data['premium_analytics'] = array(); |
| 98 |
} |
| 99 |
|
| 100 |
$data['premium_analytics']['dashboard_composition_enabled'] = is_dashboard_composition_enabled(); |
| 101 |
|
| 102 |
return $data; |
| 103 |
} |
| 104 |
|