| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | require_once __DIR__ . '/dashboard-layout.php'; |
| 11 | require_once __DIR__ . '/dashboard-grammar.php'; |
| 12 | require_once __DIR__ . '/class-dashboard-section.php'; |
| 13 | require_once __DIR__ . '/class-dashboard-section-registry.php'; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | const WOOCOMMERCE_DASHBOARD_SECTION_AVAILABLE_FILTER = 'jetpack_premium_analytics_woocommerce_dashboard_section_available'; |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | function register_dashboard_section( $dashboard_name, $id, $args = array() ) { |
| 29 | return Dashboard_Section_Registry::get_instance()->register( $dashboard_name, $id, $args ); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | function get_registered_dashboard_section( $dashboard_name, $id ) { |
| 40 | return Dashboard_Section_Registry::get_instance()->get_registered( $dashboard_name, $id ); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | function get_available_dashboard_sections( $dashboard_name ) { |
| 50 | return Dashboard_Section_Registry::get_instance()->get_available_sections( $dashboard_name ); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | function is_woocommerce_dashboard_section_available() { |
| 59 | $is_available = class_exists( 'WooCommerce' ) || function_exists( 'WC' ); |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | return (bool) apply_filters( WOOCOMMERCE_DASHBOARD_SECTION_AVAILABLE_FILTER, $is_available ); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | function get_woocommerce_dashboard_section_default_layout() { |
| 75 | return get_dashboard_default_layout_for( 'woocommerce/store' ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | function register_default_dashboard_sections() { |
| 84 | $registry = Dashboard_Section_Registry::get_instance(); |
| 85 | |
| 86 | $sections = array( |
| 87 | 'analytics/traffic' => array( |
| 88 | 'label' => __( 'Traffic', 'jetpack-premium-analytics' ), |
| 89 | 'order' => 10, |
| 90 | 'default_layout' => static function () { |
| 91 | return get_dashboard_default_layout_for( 'analytics/traffic' ); |
| 92 | }, |
| 93 | ), |
| 94 | 'analytics/insights' => array( |
| 95 | 'label' => __( 'Insights', 'jetpack-premium-analytics' ), |
| 96 | 'order' => 20, |
| 97 | 'default_layout' => static function () { |
| 98 | return get_dashboard_default_layout_for( 'analytics/insights' ); |
| 99 | }, |
| 100 | ), |
| 101 | 'analytics/subscribers' => array( |
| 102 | 'label' => __( 'Subscribers', 'jetpack-premium-analytics' ), |
| 103 | 'order' => 30, |
| 104 | 'default_layout' => static function () { |
| 105 | return get_dashboard_default_layout_for( 'analytics/subscribers' ); |
| 106 | }, |
| 107 | ), |
| 108 | 'woocommerce/store' => array( |
| 109 | 'label' => __( 'WooCommerce', 'jetpack-premium-analytics' ), |
| 110 | 'order' => 40, |
| 111 | 'is_available' => __NAMESPACE__ . '\\is_woocommerce_dashboard_section_available', |
| 112 | 'default_layout' => __NAMESPACE__ . '\\get_woocommerce_dashboard_section_default_layout', |
| 113 | ), |
| 114 | ); |
| 115 | |
| 116 | foreach ( $sections as $id => $args ) { |
| 117 | if ( ! $registry->is_registered( DASHBOARD_NAME, $id ) ) { |
| 118 | register_dashboard_section( DASHBOARD_NAME, $id, $args ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | function bootstrap_dashboard_sections() { |
| 129 | if ( did_action( 'init' ) ) { |
| 130 | register_default_dashboard_sections(); |
| 131 | } else { |
| 132 | add_action( 'init', __NAMESPACE__ . '\\register_default_dashboard_sections' ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | function check_dashboard_sections_permission() { |
| 142 | return current_user_can( 'manage_options' ); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | function get_persisted_preferences_meta_key() { |
| 153 | global $wpdb; |
| 154 | |
| 155 | return $wpdb->get_blog_prefix() . 'persisted_preferences'; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | function get_stored_persisted_preferences_for_user( $user_id ) { |
| 169 | $default_layout_filter = __NAMESPACE__ . '\\inject_dashboard_default_layout'; |
| 170 | $removed_filter = remove_filter( 'get_user_metadata', $default_layout_filter, 99 ); |
| 171 | |
| 172 | $preferences = get_user_meta( $user_id, get_persisted_preferences_meta_key(), true ); |
| 173 | |
| 174 | if ( $removed_filter ) { |
| 175 | add_filter( 'get_user_metadata', $default_layout_filter, 99, 3 ); |
| 176 | } |
| 177 | |
| 178 | return is_array( $preferences ) ? $preferences : array(); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | function get_dashboard_section_layouts_for_user( $user_id ) { |
| 191 | $preferences = get_stored_persisted_preferences_for_user( $user_id ); |
| 192 | $scope = $preferences[ DASHBOARD_LAYOUT_SCOPE ] ?? array(); |
| 193 | |
| 194 | if ( ! is_array( $scope ) ) { |
| 195 | return array(); |
| 196 | } |
| 197 | |
| 198 | $layouts = $scope[ DASHBOARD_SECTION_LAYOUTS_KEY ] ?? array(); |
| 199 | |
| 200 | if ( ! is_array( $layouts ) ) { |
| 201 | return array(); |
| 202 | } |
| 203 | |
| 204 | $normalized = array(); |
| 205 | |
| 206 | foreach ( $layouts as $section_id => $layout ) { |
| 207 | if ( |
| 208 | is_string( $section_id ) |
| 209 | && 1 === preg_match( '/^' . get_dashboard_section_id_pattern() . '$/', $section_id ) |
| 210 | && is_dashboard_section_layout( $layout ) |
| 211 | ) { |
| 212 | $normalized[ $section_id ] = array_values( $layout ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return $normalized; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | function update_dashboard_section_layout_for_user( $user_id, $section_id, $layout ) { |
| 228 | $preferences = get_stored_persisted_preferences_for_user( $user_id ); |
| 229 | $original = $preferences; |
| 230 | |
| 231 | if ( ! isset( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) || ! is_array( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) ) { |
| 232 | $preferences[ DASHBOARD_LAYOUT_SCOPE ] = array(); |
| 233 | } |
| 234 | |
| 235 | if ( |
| 236 | ! isset( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) |
| 237 | || ! is_array( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) |
| 238 | ) { |
| 239 | $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] = array(); |
| 240 | } |
| 241 | |
| 242 | $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ][ $section_id ] = array_values( $layout ); |
| 243 | |
| 244 | if ( $preferences === $original ) { |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | $updated = update_user_meta( $user_id, get_persisted_preferences_meta_key(), $preferences ); |
| 249 | |
| 250 | return false !== $updated; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | function delete_dashboard_section_layout_for_user( $user_id, $section_id ) { |
| 261 | $preferences = get_stored_persisted_preferences_for_user( $user_id ); |
| 262 | |
| 263 | if ( |
| 264 | ! isset( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) |
| 265 | || ! is_array( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) |
| 266 | || ! isset( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) |
| 267 | || ! is_array( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) |
| 268 | ) { |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | if ( ! array_key_exists( $section_id, $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) ) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | unset( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ][ $section_id ] ); |
| 277 | |
| 278 | if ( empty( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) ) { |
| 279 | unset( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ); |
| 280 | } |
| 281 | |
| 282 | $updated = update_user_meta( $user_id, get_persisted_preferences_meta_key(), $preferences ); |
| 283 | |
| 284 | return false !== $updated; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | function delete_dashboard_section_layouts_for_user( $user_id ) { |
| 294 | $preferences = get_stored_persisted_preferences_for_user( $user_id ); |
| 295 | |
| 296 | if ( |
| 297 | ! isset( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) |
| 298 | || ! is_array( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) |
| 299 | || ! isset( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ) |
| 300 | ) { |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | unset( $preferences[ DASHBOARD_LAYOUT_SCOPE ][ DASHBOARD_SECTION_LAYOUTS_KEY ] ); |
| 305 | |
| 306 | if ( empty( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ) ) { |
| 307 | unset( $preferences[ DASHBOARD_LAYOUT_SCOPE ] ); |
| 308 | } |
| 309 | |
| 310 | $updated = update_user_meta( $user_id, get_persisted_preferences_meta_key(), $preferences ); |
| 311 | |
| 312 | return false !== $updated; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | function is_dashboard_section_layout( $layout ) { |
| 322 | if ( ! is_array( $layout ) ) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | $expected_key = 0; |
| 327 | |
| 328 | foreach ( $layout as $key => $widget ) { |
| 329 | if ( $key !== $expected_key ) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | ++$expected_key; |
| 334 | |
| 335 | if ( ! is_array( $widget ) ) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | if ( ! isset( $widget['uuid'] ) || ! is_string( $widget['uuid'] ) || '' === $widget['uuid'] ) { |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | if ( ! isset( $widget['type'] ) || ! is_string( $widget['type'] ) || '' === $widget['type'] ) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if ( isset( $widget['placement'] ) && ! is_array( $widget['placement'] ) ) { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | if ( isset( $widget['attributes'] ) && ! is_array( $widget['attributes'] ) ) { |
| 352 | return false; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | function validate_dashboard_section_layout( $value ) { |
| 366 | if ( is_dashboard_section_layout( $value ) ) { |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | return new \WP_Error( |
| 371 | 'invalid_dashboard_section_layout', |
| 372 | __( 'Dashboard section layout must be an array of widget instances.', 'jetpack-premium-analytics' ) |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | function sanitize_dashboard_section_layout( $value ) { |
| 383 | return is_array( $value ) ? array_values( $value ) : array(); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | function get_available_dashboard_section_for_route( $dashboard_name, $section_id ) { |
| 394 | $section = get_registered_dashboard_section( $dashboard_name, $section_id ); |
| 395 | |
| 396 | if ( ! $section ) { |
| 397 | return new \WP_Error( |
| 398 | 'dashboard_section_not_found', |
| 399 | __( 'Dashboard section not found.', 'jetpack-premium-analytics' ), |
| 400 | array( 'status' => 404 ) |
| 401 | ); |
| 402 | } |
| 403 | |
| 404 | if ( ! $section->is_available() ) { |
| 405 | return new \WP_Error( |
| 406 | 'dashboard_section_unavailable', |
| 407 | __( 'Dashboard section is not available.', 'jetpack-premium-analytics' ), |
| 408 | array( 'status' => 404 ) |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | return $section; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | function get_dashboard_section_response_data( Dashboard_Section $section, $section_layouts ) { |
| 423 | $has_custom_layout = array_key_exists( $section->id, $section_layouts ); |
| 424 | $data = $section->to_array(); |
| 425 | |
| 426 | $data['layout'] = $has_custom_layout ? $section_layouts[ $section->id ] : $section->get_default_layout(); |
| 427 | $data['hasCustomLayout'] = $has_custom_layout; |
| 428 | |
| 429 | return $data; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | function get_dashboard_sections_response( $request ) { |
| 439 | $section_layouts = get_dashboard_section_layouts_for_user( get_current_user_id() ); |
| 440 | $sections = array_map( |
| 441 | static function ( Dashboard_Section $section ) use ( $section_layouts ) { |
| 442 | return get_dashboard_section_response_data( $section, $section_layouts ); |
| 443 | }, |
| 444 | get_available_dashboard_sections( $request['name'] ) |
| 445 | ); |
| 446 | |
| 447 | return rest_ensure_response( $sections ); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | function get_dashboard_section_default_layout_response( $request ) { |
| 457 | $section = get_available_dashboard_section_for_route( $request['name'], $request['section'] ); |
| 458 | |
| 459 | if ( is_wp_error( $section ) ) { |
| 460 | return $section; |
| 461 | } |
| 462 | |
| 463 | return rest_ensure_response( $section->get_default_layout() ); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | function update_dashboard_section_layout_response( $request ) { |
| 473 | $section = get_available_dashboard_section_for_route( $request['name'], $request['section'] ); |
| 474 | |
| 475 | if ( is_wp_error( $section ) ) { |
| 476 | return $section; |
| 477 | } |
| 478 | |
| 479 | $layout = sanitize_dashboard_section_layout( $request['layout'] ); |
| 480 | $user = get_current_user_id(); |
| 481 | |
| 482 | if ( ! update_dashboard_section_layout_for_user( $user, $section->id, $layout ) ) { |
| 483 | return new \WP_Error( |
| 484 | 'dashboard_section_layout_update_failed', |
| 485 | __( 'Could not update dashboard section layout.', 'jetpack-premium-analytics' ), |
| 486 | array( 'status' => 500 ) |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | return rest_ensure_response( |
| 491 | get_dashboard_section_response_data( |
| 492 | $section, |
| 493 | array( $section->id => $layout ) |
| 494 | ) |
| 495 | ); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | function delete_dashboard_section_layout_response( $request ) { |
| 505 | $section = get_available_dashboard_section_for_route( $request['name'], $request['section'] ); |
| 506 | |
| 507 | if ( is_wp_error( $section ) ) { |
| 508 | return $section; |
| 509 | } |
| 510 | |
| 511 | $user = get_current_user_id(); |
| 512 | |
| 513 | if ( ! delete_dashboard_section_layout_for_user( $user, $section->id ) ) { |
| 514 | return new \WP_Error( |
| 515 | 'dashboard_section_layout_delete_failed', |
| 516 | __( 'Could not reset dashboard section layout.', 'jetpack-premium-analytics' ), |
| 517 | array( 'status' => 500 ) |
| 518 | ); |
| 519 | } |
| 520 | |
| 521 | return rest_ensure_response( |
| 522 | get_dashboard_section_response_data( |
| 523 | $section, |
| 524 | get_dashboard_section_layouts_for_user( $user ) |
| 525 | ) |
| 526 | ); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | function delete_dashboard_sections_layouts_response( $request ) { |
| 536 | if ( ! delete_dashboard_section_layouts_for_user( get_current_user_id() ) ) { |
| 537 | return new \WP_Error( |
| 538 | 'dashboard_section_layout_delete_failed', |
| 539 | __( 'Could not reset dashboard section layouts.', 'jetpack-premium-analytics' ), |
| 540 | array( 'status' => 500 ) |
| 541 | ); |
| 542 | } |
| 543 | |
| 544 | return get_dashboard_sections_response( $request ); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | function register_dashboard_sections_rest_routes() { |
| 553 | register_rest_route( |
| 554 | DASHBOARD_REST_NAMESPACE, |
| 555 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections', |
| 556 | array( |
| 557 | 'methods' => \WP_REST_Server::READABLE, |
| 558 | 'callback' => __NAMESPACE__ . '\\get_dashboard_sections_response', |
| 559 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 560 | 'args' => array( |
| 561 | 'name' => array( |
| 562 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 563 | 'type' => 'string', |
| 564 | ), |
| 565 | ), |
| 566 | ) |
| 567 | ); |
| 568 | |
| 569 | register_rest_route( |
| 570 | DASHBOARD_REST_NAMESPACE, |
| 571 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections', |
| 572 | array( |
| 573 | 'methods' => \WP_REST_Server::DELETABLE, |
| 574 | 'callback' => __NAMESPACE__ . '\\delete_dashboard_sections_layouts_response', |
| 575 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 576 | 'args' => array( |
| 577 | 'name' => array( |
| 578 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 579 | 'type' => 'string', |
| 580 | ), |
| 581 | ), |
| 582 | ) |
| 583 | ); |
| 584 | |
| 585 | register_rest_route( |
| 586 | DASHBOARD_REST_NAMESPACE, |
| 587 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections/(?P<section>' . get_dashboard_section_id_pattern() . ')/default-layout', |
| 588 | array( |
| 589 | 'methods' => \WP_REST_Server::READABLE, |
| 590 | 'callback' => __NAMESPACE__ . '\\get_dashboard_section_default_layout_response', |
| 591 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 592 | 'args' => array( |
| 593 | 'name' => array( |
| 594 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 595 | 'type' => 'string', |
| 596 | ), |
| 597 | 'section' => array( |
| 598 | 'description' => __( 'Dashboard section identifier.', 'jetpack-premium-analytics' ), |
| 599 | 'type' => 'string', |
| 600 | ), |
| 601 | ), |
| 602 | ) |
| 603 | ); |
| 604 | |
| 605 | register_rest_route( |
| 606 | DASHBOARD_REST_NAMESPACE, |
| 607 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections/(?P<section>' . get_dashboard_section_id_pattern() . ')/layout', |
| 608 | array( |
| 609 | 'methods' => 'PUT', |
| 610 | 'callback' => __NAMESPACE__ . '\\update_dashboard_section_layout_response', |
| 611 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 612 | 'args' => array( |
| 613 | 'name' => array( |
| 614 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 615 | 'type' => 'string', |
| 616 | ), |
| 617 | 'section' => array( |
| 618 | 'description' => __( 'Dashboard section identifier.', 'jetpack-premium-analytics' ), |
| 619 | 'type' => 'string', |
| 620 | ), |
| 621 | 'layout' => array( |
| 622 | 'description' => __( 'Widget layout to persist for the dashboard section.', 'jetpack-premium-analytics' ), |
| 623 | 'type' => 'array', |
| 624 | 'required' => true, |
| 625 | 'validate_callback' => __NAMESPACE__ . '\\validate_dashboard_section_layout', |
| 626 | 'sanitize_callback' => __NAMESPACE__ . '\\sanitize_dashboard_section_layout', |
| 627 | ), |
| 628 | ), |
| 629 | ) |
| 630 | ); |
| 631 | |
| 632 | register_rest_route( |
| 633 | DASHBOARD_REST_NAMESPACE, |
| 634 | '/dashboards/(?P<name>' . get_dashboard_name_pattern() . ')/sections/(?P<section>' . get_dashboard_section_id_pattern() . ')/layout', |
| 635 | array( |
| 636 | 'methods' => \WP_REST_Server::DELETABLE, |
| 637 | 'callback' => __NAMESPACE__ . '\\delete_dashboard_section_layout_response', |
| 638 | 'permission_callback' => __NAMESPACE__ . '\\check_dashboard_sections_permission', |
| 639 | 'args' => array( |
| 640 | 'name' => array( |
| 641 | 'description' => __( 'Dashboard identifier as produced by the build pipeline.', 'jetpack-premium-analytics' ), |
| 642 | 'type' => 'string', |
| 643 | ), |
| 644 | 'section' => array( |
| 645 | 'description' => __( 'Dashboard section identifier.', 'jetpack-premium-analytics' ), |
| 646 | 'type' => 'string', |
| 647 | ), |
| 648 | ), |
| 649 | ) |
| 650 | ); |
| 651 | } |
| 652 | |
| 653 | bootstrap_dashboard_sections(); |
| 654 | add_action( 'rest_api_init', __NAMESPACE__ . '\\register_dashboard_sections_rest_routes' ); |