← All changes
|
jetpack_vendor/automattic/jetpack-premium-analytics/src/dashboard-sections.php
+43
-65
16.3-a.3
→
16.3
View file →
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Dashboard Sections API: the registry helpers, the preview scope, and the REST routes. | |
| 3 | + * Dashboard Sections API: the registry helpers, the section script data, and the REST routes. | |
| 4 | 4 | * |
| 5 | 5 | * The package's own sections register through this API from default-dashboard-sections.php, |
| 6 | 6 | * the same way a plugin extending the dashboard does. |
| 7 | 7 | * |
| @@ -15,21 +15,14 @@ | ||
| 15 | 15 | require_once __DIR__ . '/rest-namespace.php'; |
| 16 | 16 | require_once __DIR__ . '/class-dashboard-section.php'; |
| 17 | 17 | require_once __DIR__ . '/class-dashboard-section-registry.php'; |
| 18 | 18 | |
| 19 | -/** | |
| 20 | - * Filter through which the preview's section scope is resolved. | |
| 21 | - */ | |
| 22 | -const DASHBOARD_PREVIEW_SCOPE_FILTER = 'jetpack_premium_analytics_dashboard_preview_scope'; | |
| 19 | +// Guarded on a symbol the file declares, so a second copy of the package can't redeclare it. | |
| 20 | +if ( ! function_exists( __NAMESPACE__ . '\\register_dashboard_feature_flags' ) ) { | |
| 21 | + require_once __DIR__ . '/dashboard-policy.php'; | |
| 22 | +} | |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | - * Section slugs the customer preview exposes as tabs. A section still rolling out to some | |
| 26 | - * sites is opened through the filter instead. Widget types are registered independently of | |
| 27 | - * this, as they are of the per-section availability checks. | |
| 28 | - */ | |
| 29 | -const PREVIEW_SECTIONS = array( 'traffic', 'insights' ); | |
| 30 | - | |
| 31 | -/** | |
| 32 | 25 | * Registers a dashboard section. |
| 33 | 26 | * |
| 34 | 27 | * @param string $dashboard_name Dashboard identifier. |
| 35 | 28 | * @param string $id Section identifier. |
| @@ -61,62 +54,19 @@ | ||
| 61 | 54 | return Dashboard_Section_Registry::get_instance()->get_available_sections( $dashboard_name ); |
| 62 | 55 | } |
| 63 | 56 | |
| 64 | 57 | /** |
| 65 | - * Whether the dashboard is running as the customer-facing preview. | |
| 66 | - * | |
| 67 | - * The site's own opt-in means the preview. Anything else that switches the dashboard on, the | |
| 68 | - * WordPress.com blog sticker or the `jetpack_premium_analytics_enabled` filter, means us. | |
| 69 | - * | |
| 70 | - * @since 0.6.0 | |
| 71 | - * | |
| 72 | - * @return bool | |
| 73 | - */ | |
| 74 | -function is_dashboard_preview_scoped() { | |
| 75 | - return (bool) get_option( Enablement_Setting::ENABLED_OPTION ); | |
| 76 | -} | |
| 77 | - | |
| 78 | -/** | |
| 79 | - * Whether the preview exposes a dashboard section. | |
| 80 | - * | |
| 81 | - * @since 0.6.0 | |
| 82 | - * | |
| 83 | - * @param string $dashboard_name Dashboard identifier. Only this package's own dashboard is scoped. | |
| 84 | - * @param string $slug URL-facing section slug. | |
| 85 | - * @return bool | |
| 86 | - */ | |
| 87 | -function is_dashboard_section_in_preview_scope( $dashboard_name, $slug ) { | |
| 88 | - $in_scope = DASHBOARD_NAME !== $dashboard_name | |
| 89 | - || ! is_dashboard_preview_scoped() | |
| 90 | - || in_array( $slug, PREVIEW_SECTIONS, true ); | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Filters whether the preview exposes a dashboard section. | |
| 94 | - * | |
| 95 | - * `__return_true` restores the whole dashboard, which is how a development or test site | |
| 96 | - * sees every tab. | |
| 97 | - * | |
| 98 | - * @since 0.6.0 | |
| 99 | - * | |
| 100 | - * @param bool $in_scope Whether the preview exposes the section. | |
| 101 | - * @param string $slug URL-facing section slug. | |
| 102 | - * @param string $dashboard_name Dashboard the section belongs to. | |
| 103 | - */ | |
| 104 | - return (bool) apply_filters( DASHBOARD_PREVIEW_SCOPE_FILTER, $in_scope, $slug, $dashboard_name ); | |
| 105 | -} | |
| 106 | - | |
| 107 | -/** | |
| 108 | 58 | * Slugs of the tabs the dashboard exposes, for the client's report routes. |
| 109 | 59 | * |
| 110 | 60 | * Reads the same sections the tab list does, so a report cannot outlive the tab it sits |
| 111 | - * behind. Null, never `array()`, before the registry is hydrated: an empty array is a | |
| 112 | - * published scope that exposes nothing. | |
| 61 | + * behind. Null, never `array()`, while nothing is registered: an empty array means no | |
| 62 | + * tab is available. | |
| 113 | 63 | * |
| 114 | 64 | * @since 0.6.0 |
| 115 | 65 | * |
| 116 | 66 | * @return string[]|null |
| 117 | 67 | */ |
| 118 | -function get_dashboard_preview_scope_sections() { | |
| 68 | +function get_available_dashboard_section_slugs() { | |
| 119 | 69 | $registry = Dashboard_Section_Registry::get_instance(); |
| 120 | 70 | |
| 121 | 71 | if ( empty( $registry->get_all_registered( DASHBOARD_NAME ) ) ) { |
| 122 | 72 | return null; |
| @@ -130,23 +80,51 @@ | ||
| 130 | 80 | ); |
| 131 | 81 | } |
| 132 | 82 | |
| 133 | 83 | /** |
| 134 | - * Configures the preview scope script data. | |
| 84 | + * Configures the section script data. | |
| 135 | 85 | * |
| 136 | 86 | * @since 0.6.0 |
| 137 | 87 | * |
| 138 | 88 | * @return void |
| 139 | 89 | */ |
| 90 | +function configure_dashboard_sections_script_data() { | |
| 91 | + add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_dashboard_sections_script_data', 20 ); | |
| 92 | +} | |
| 93 | + | |
| 94 | +/** | |
| 95 | + * Kept for older copies of the package, whose Dashboard_Section::is_available() calls it on every | |
| 96 | + * availability check. Every section is in scope now, so the section's own rule decides. | |
| 97 | + * | |
| 98 | + * @since 0.6.0 | |
| 99 | + * @deprecated 0.10.0 The preview scope is gone. | |
| 100 | + * | |
| 101 | + * @param string $dashboard_name Dashboard identifier. | |
| 102 | + * @param string $slug URL-facing section slug. | |
| 103 | + * @return bool Always true. | |
| 104 | + */ | |
| 105 | +function is_dashboard_section_in_preview_scope( $dashboard_name, $slug ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Kept for the signature older copies call. | |
| 106 | + return true; | |
| 107 | +} | |
| 108 | + | |
| 109 | +/** | |
| 110 | + * Kept for older copies of the package: they guard their include of this file on another | |
| 111 | + * symbol and call this, so a newer copy loading first must still define it. | |
| 112 | + * | |
| 113 | + * @since 0.6.0 | |
| 114 | + * @deprecated 0.10.0 Use configure_dashboard_sections_script_data(). | |
| 115 | + * | |
| 116 | + * @return void | |
| 117 | + */ | |
| 140 | 118 | function configure_dashboard_preview_scope() { |
| 141 | - add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_dashboard_preview_scope_script_data', 20 ); | |
| 119 | + configure_dashboard_sections_script_data(); | |
| 142 | 120 | } |
| 143 | 121 | |
| 144 | 122 | /** |
| 145 | - * Injects the preview's section scope into JetpackScriptData. | |
| 123 | + * Injects the available section slugs into JetpackScriptData. | |
| 146 | 124 | * |
| 147 | 125 | * The same list travels over REST for the tab bar, but a report route reads no REST before |
| 148 | - * choosing its redirect, so it reads the scope from boot data instead. | |
| 126 | + * choosing its redirect, so it reads the slugs from boot data instead. | |
| 149 | 127 | * |
| 150 | 128 | * @since 0.6.0 |
| 151 | 129 | * |
| 152 | 130 | * @param array $data The script data passed by the assets package. |
| @@ -151,10 +129,10 @@ | ||
| 151 | 129 | * |
| 152 | 130 | * @param array $data The script data passed by the assets package. |
| 153 | 131 | * @return array |
| 154 | 132 | */ |
| 155 | -function inject_dashboard_preview_scope_script_data( array $data ): array { | |
| 156 | - $sections = get_dashboard_preview_scope_sections(); | |
| 133 | +function inject_dashboard_sections_script_data( array $data ): array { | |
| 134 | + $sections = get_available_dashboard_section_slugs(); | |
| 157 | 135 | |
| 158 | 136 | if ( null === $sections ) { |
| 159 | 137 | return $data; |
| 160 | 138 | } |
| @@ -162,9 +140,9 @@ | ||
| 162 | 140 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 163 | 141 | $data['premium_analytics'] = array(); |
| 164 | 142 | } |
| 165 | 143 | |
| 166 | - $data['premium_analytics']['preview_sections'] = $sections; | |
| 144 | + $data['premium_analytics']['sections'] = $sections; | |
| 167 | 145 | |
| 168 | 146 | return $data; |
| 169 | 147 | } |
| 170 | 148 | |