| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 12 | |
| 13 | require_once __DIR__ . '/dashboard-layout.php'; |
| 14 | require_once __DIR__ . '/dashboard-grammar.php'; |
| 15 | require_once __DIR__ . '/rest-namespace.php'; |
| 16 | require_once __DIR__ . '/class-dashboard-section.php'; |
| 17 | require_once __DIR__ . '/class-dashboard-section-registry.php'; |
| 18 | |
| 19 | |
| 20 | if ( ! function_exists( __NAMESPACE__ . '\\register_dashboard_feature_flags' ) ) { |
| 21 | require_once __DIR__ . '/dashboard-policy.php'; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | function register_dashboard_section( $dashboard_name, $id, $args = array() ) { |
| 33 | return Dashboard_Section_Registry::get_instance()->register( $dashboard_name, $id, $args ); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | function get_registered_dashboard_section( $dashboard_name, $id ) { |
| 44 | return Dashboard_Section_Registry::get_instance()->get_registered( $dashboard_name, $id ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | function get_available_dashboard_sections( $dashboard_name ) { |
| 54 | return Dashboard_Section_Registry::get_instance()->get_available_sections( $dashboard_name ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | function get_available_dashboard_section_slugs() { |
| 69 | $registry = Dashboard_Section_Registry::get_instance(); |
| 70 | |
| 71 | if ( empty( $registry->get_all_registered( DASHBOARD_NAME ) ) ) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | return array_map( |
| 76 | static function ( Dashboard_Section $section ) { |
| 77 | return $section->slug; |
| 78 | }, |
| 79 | $registry->get_available_sections( DASHBOARD_NAME ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 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 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | function is_dashboard_section_in_preview_scope( $dashboard_name, $slug ) { |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | function configure_dashboard_preview_scope() { |
| 119 | configure_dashboard_sections_script_data(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | function inject_dashboard_sections_script_data( array $data ): array { |
| 134 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 135 | $data['premium_analytics'] = array(); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | $data['premium_analytics']['can_view_stats'] = Capabilities::current_user_can_view_stats(); |
| 140 | |
| 141 | $sections = get_available_dashboard_section_slugs(); |
| 142 | |
| 143 | if ( null !== $sections ) { |
| 144 | $data['premium_analytics']['sections'] = $sections; |
| 145 | } |
| 146 | |
| 147 | return $data; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | function check_dashboard_sections_permission() { |
| 156 | return Capabilities::current_user_can_view_analytics(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | function get_available_dashboard_section_for_route( $dashboard_name, $section_id ) { |
| 167 | $section = get_registered_dashboard_section( $dashboard_name, $section_id ); |
| 168 | |
| 169 | if ( ! $section ) { |
| 170 | return new \WP_Error( |
| 171 | 'dashboard_section_not_found', |
| 172 | __( 'Dashboard section not found.', 'jetpack-premium-analytics-pkg' ), |
| 173 | array( 'status' => 404 ) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | if ( ! $section->is_available() ) { |
| 178 | return new \WP_Error( |
| 179 | 'dashboard_section_unavailable', |
| 180 | __( 'Dashboard section is not available.', 'jetpack-premium-analytics-pkg' ), |
| 181 | array( 'status' => 404 ) |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | return $section; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | function get_dashboard_section_schema() { |
| 198 | return array( |
| 199 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 200 | 'title' => 'jetpack-premium-analytics-dashboard-section', |
| 201 | 'type' => 'object', |
| 202 | 'properties' => array( |
| 203 | 'id' => array( |
| 204 | 'description' => __( 'Namespaced section identifier.', 'jetpack-premium-analytics-pkg' ), |
| 205 | 'type' => 'string', |
| 206 | 'readonly' => true, |
| 207 | ), |
| 208 | 'slug' => array( |
| 209 | 'description' => __( 'URL-facing section slug, derived from the identifier.', 'jetpack-premium-analytics-pkg' ), |
| 210 | 'type' => 'string', |
| 211 | 'readonly' => true, |
| 212 | ), |
| 213 | 'label' => array( |
| 214 | 'description' => __( 'Translated display label, naming the section tab.', 'jetpack-premium-analytics-pkg' ), |
| 215 | 'type' => 'string', |
| 216 | 'readonly' => true, |
| 217 | ), |
| 218 | 'title' => array( |
| 219 | 'description' => __( 'Translated section heading, distinct from the tab label. Null falls back to the label.', 'jetpack-premium-analytics-pkg' ), |
| 220 | 'type' => array( 'string', 'null' ), |
| 221 | 'readonly' => true, |
| 222 | ), |
| 223 | 'order' => array( |
| 224 | 'description' => __( 'Sort order, ascending.', 'jetpack-premium-analytics-pkg' ), |
| 225 | 'type' => 'integer', |
| 226 | 'readonly' => true, |
| 227 | ), |
| 228 | 'date_filter' => array( |
| 229 | 'description' => __( 'Which shape the section date filter takes: the rolling date range, or all time plus single years.', 'jetpack-premium-analytics-pkg' ), |
| 230 | 'type' => 'string', |
| 231 | 'enum' => Dashboard_Section::DATE_FILTERS, |
| 232 | 'default' => Dashboard_Section::DATE_FILTER_RANGE, |
| 233 | 'readonly' => true, |
| 234 | ), |
| 235 | 'date_filter_options' => array( |
| 236 | 'description' => __( 'What the section date filter supports, and where it renders.', 'jetpack-premium-analytics-pkg' ), |
| 237 | 'type' => 'object', |
| 238 | 'properties' => array( |
| 239 | 'with_date_comparison' => array( |
| 240 | 'description' => __( 'Whether the section supports period-over-period comparison at all. When false, no widget in the section receives comparison parameters.', 'jetpack-premium-analytics-pkg' ), |
| 241 | 'type' => 'boolean', |
| 242 | 'default' => true, |
| 243 | ), |
| 244 | 'with_header_date_control' => array( |
| 245 | 'description' => __( 'Whether the section header renders the date control. When false, the section widgets host their own.', 'jetpack-premium-analytics-pkg' ), |
| 246 | 'type' => 'boolean', |
| 247 | 'default' => true, |
| 248 | ), |
| 249 | 'with_header_interval_control' => array( |
| 250 | 'description' => __( 'Whether the section header renders the chart interval control. When false, the section charts host their own.', 'jetpack-premium-analytics-pkg' ), |
| 251 | 'type' => 'boolean', |
| 252 | 'default' => true, |
| 253 | ), |
| 254 | ), |
| 255 | 'readonly' => true, |
| 256 | ), |
| 257 | 'requires_sync' => array( |
| 258 | 'description' => __( 'Whether the section\'s numbers stay incomplete until the analytics initial full sync has finished.', 'jetpack-premium-analytics-pkg' ), |
| 259 | 'type' => 'boolean', |
| 260 | 'default' => false, |
| 261 | 'readonly' => true, |
| 262 | ), |
| 263 | 'default_layout' => array( |
| 264 | 'description' => __( 'Bundled default widget layout.', 'jetpack-premium-analytics-pkg' ), |
| 265 | 'type' => 'array', |
| 266 | 'items' => array( 'type' => 'object' ), |
| 267 | 'readonly' => true, |
| 268 | ), |
| 269 | ), |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | function get_dashboard_sections_response( $request ) { |
| 280 | $sections = array_map( |
| 281 | static function ( Dashboard_Section $section ) { |
| 282 | return $section->to_array(); |
| 283 | }, |
| 284 | get_available_dashboard_sections( $request['name'] ) |
| 285 | ); |
| 286 | |
| 287 | return rest_ensure_response( $sections ); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | function get_dashboard_section_default_layout_response( $request ) { |
| 297 | $section = get_available_dashboard_section_for_route( $request['name'], $request['section'] ); |
| 298 | |
| 299 | if ( is_wp_error( $section ) ) { |
| 300 | return $section; |
| 301 | } |
| 302 | |
| 303 | return rest_ensure_response( $section->get_default_layout() ); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | function register_dashboard_sections_rest_routes() { |
| 312 | register_rest_route( |
| 313 | DASHBOARD_REST_NAMESPACE, |
| 314 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections', |
| 315 | array( |
| 316 | array( |
| 317 | |
| 318 | 'methods' => \WP_REST_Server::READABLE, |
| 319 | 'callback' => __NAMESPACE__ . '\\get_dashboard_sections_response', |
| 320 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 321 | 'args' => array( |
| 322 | 'name' => array( |
| 323 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics-pkg' ), |
| 324 | 'type' => 'string', |
| 325 | ), |
| 326 | ), |
| 327 | ), |
| 328 | 'schema' => __NAMESPACE__ . '\\get_dashboard_section_schema', |
| 329 | ) |
| 330 | ); |
| 331 | |
| 332 | register_rest_route( |
| 333 | DASHBOARD_REST_NAMESPACE, |
| 334 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections/(?P<section>' . get_dashboard_section_id_pattern() . ')/default-layout', |
| 335 | array( |
| 336 | 'methods' => \WP_REST_Server::READABLE, |
| 337 | 'callback' => __NAMESPACE__ . '\\get_dashboard_section_default_layout_response', |
| 338 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 339 | 'args' => array( |
| 340 | 'name' => array( |
| 341 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics-pkg' ), |
| 342 | 'type' => 'string', |
| 343 | ), |
| 344 | 'section' => array( |
| 345 | 'description' => __( 'Dashboard section identifier.', 'jetpack-premium-analytics-pkg' ), |
| 346 | 'type' => 'string', |
| 347 | ), |
| 348 | ), |
| 349 | ) |
| 350 | ); |
| 351 | } |