← All changes
|
jetpack_vendor/automattic/jetpack-premium-analytics/src/default-dashboard-sections.php
+33
-117
16.3-a.3
→
16.3
View file →
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * The package's own dashboard sections: the built-in tabs, their availability gates and default |
| 4 | 4 | * layouts, and the filters over those gates. They register through the section API in |
| 5 | - * dashboard-sections.php, the same way a plugin extending the dashboard would. | |
| 5 | + * dashboard-sections.php, the same way a plugin extending the dashboard does; the Ads tab is | |
| 6 | + * one such plugin section, registered by the WordAds module and by WordPress.com. | |
| 6 | 7 | * |
| 7 | 8 | * @package automattic/jetpack-premium-analytics |
| 8 | 9 | */ |
| 9 | 10 | |
| @@ -9,9 +10,8 @@ | ||
| 9 | 10 | |
| 10 | 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 11 | 12 | |
| 12 | 13 | use Automattic\Jetpack\Modules; |
| 13 | -use Automattic\Jetpack\Status\Host; | |
| 14 | 14 | |
| 15 | 15 | // Guarded on a symbol the file declares, so a second copy of the package can't |
| 16 | 16 | // redeclare it. See the include block in Analytics::load_dashboard_components(). |
| 17 | 17 | if ( ! function_exists( __NAMESPACE__ . '\\register_dashboard_section' ) ) { |
| @@ -28,13 +28,8 @@ | ||
| 28 | 28 | */ |
| 29 | 29 | const SUBSCRIBERS_DASHBOARD_SECTION_AVAILABLE_FILTER = 'jetpack_premium_analytics_subscribers_dashboard_section_available'; |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | - * Filter for Ads section availability. | |
| 33 | - */ | |
| 34 | -const ADS_DASHBOARD_SECTION_AVAILABLE_FILTER = 'jetpack_premium_analytics_ads_dashboard_section_available'; | |
| 35 | - | |
| 36 | -/** | |
| 37 | 32 | * Whether the WooCommerce dashboard section should be exposed. |
| 38 | 33 | * |
| 39 | 34 | * @return bool True when WooCommerce is active. |
| 40 | 35 | */ |
| @@ -63,8 +58,26 @@ | ||
| 63 | 58 | return is_woocommerce_dashboard_section_available() && Capabilities::current_user_can_view_store_reports(); |
| 64 | 59 | } |
| 65 | 60 | |
| 66 | 61 | /** |
| 62 | + * Whether the Store dashboard section should be exposed. | |
| 63 | + * | |
| 64 | + * The site's own opt-in needs the Store flag; the blog sticker and the | |
| 65 | + * `jetpack_premium_analytics_enabled` filter leave the option off and keep every section. | |
| 66 | + * | |
| 67 | + * @since 0.10.0 | |
| 68 | + * | |
| 69 | + * @return bool | |
| 70 | + */ | |
| 71 | +function is_store_dashboard_section_available() { | |
| 72 | + // An older copy of the package may have loaded dashboard-policy.php without the flag. | |
| 73 | + $is_enabled = ! get_option( Enablement_Setting::ENABLED_OPTION ) | |
| 74 | + || ( function_exists( __NAMESPACE__ . '\\is_dashboard_store_section_enabled' ) && is_dashboard_store_section_enabled() ); | |
| 75 | + | |
| 76 | + return $is_enabled && is_woocommerce_dashboard_section_available_to_current_user(); | |
| 77 | +} | |
| 78 | + | |
| 79 | +/** | |
| 67 | 80 | * Whether the Subscribers dashboard section should be exposed. |
| 68 | 81 | * |
| 69 | 82 | * Sites without Jetpack have no module state to check, so the section remains |
| 70 | 83 | * available. Modules::is_active() also returns true on WPCOM Simple. |
| @@ -86,46 +99,8 @@ | ||
| 86 | 99 | return (bool) apply_filters( SUBSCRIBERS_DASHBOARD_SECTION_AVAILABLE_FILTER, $is_available ); |
| 87 | 100 | } |
| 88 | 101 | |
| 89 | 102 | /** |
| 90 | - * Whether the Ads dashboard section is available. | |
| 91 | - * | |
| 92 | - * WPCOM reads the plan feature rather than the module, which is a false negative | |
| 93 | - * on Atomic and meaningless on Simple. Mirrors is_videopress_available(). | |
| 94 | - * | |
| 95 | - * @since 0.4.0 | |
| 96 | - * | |
| 97 | - * @return bool True when the site can produce WordAds earnings. | |
| 98 | - */ | |
| 99 | -function is_ads_dashboard_section_available() { | |
| 100 | - if ( ( new Host() )->is_wpcom_platform() ) { | |
| 101 | - $is_available = function_exists( 'wpcom_site_has_feature' ) && \wpcom_site_has_feature( 'wordads' ); | |
| 102 | - } else { | |
| 103 | - $is_available = ! class_exists( 'Jetpack' ) || ( new Modules() )->is_active( 'wordads' ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * Filters whether the Ads dashboard section is available. | |
| 108 | - * | |
| 109 | - * @since 0.4.0 | |
| 110 | - * | |
| 111 | - * @param bool $is_available Whether WordAds was detected in the current request. | |
| 112 | - */ | |
| 113 | - return (bool) apply_filters( ADS_DASHBOARD_SECTION_AVAILABLE_FILTER, $is_available ); | |
| 114 | -} | |
| 115 | - | |
| 116 | -/** | |
| 117 | - * Whether the current user can access the Ads dashboard section. | |
| 118 | - * | |
| 119 | - * @since 0.4.0 | |
| 120 | - * | |
| 121 | - * @return bool | |
| 122 | - */ | |
| 123 | -function is_ads_dashboard_section_available_to_current_user() { | |
| 124 | - return is_ads_dashboard_section_available() && Capabilities::current_user_can_view_ad_reports(); | |
| 125 | -} | |
| 126 | - | |
| 127 | -/** | |
| 128 | 103 | * The Traffic tab's default widget layout. |
| 129 | 104 | * |
| 130 | 105 | * @return array Widget instances. |
| 131 | 106 | */ |
| @@ -178,9 +153,9 @@ | ||
| 178 | 153 | 5, |
| 179 | 154 | 1, |
| 180 | 155 | 2 |
| 181 | 156 | ), |
| 182 | - // Row 4: UTM insights + clicks + VideoPress (sites running VideoPress only). | |
| 157 | + // Row 4: UTM insights + clicks; the VideoPress package seeds Top videos at order 8. | |
| 183 | 158 | get_dashboard_default_widget_instance( |
| 184 | 159 | 'default-utm-insights-widget-instance', |
| 185 | 160 | 'jpa/utm-insights', |
| 186 | 161 | 6, |
| @@ -196,15 +171,8 @@ | ||
| 196 | 171 | 7, |
| 197 | 172 | 1, |
| 198 | 173 | 2 |
| 199 | 174 | ), |
| 200 | - get_dashboard_default_widget_instance( | |
| 201 | - 'default-videopress-widget-instance', | |
| 202 | - 'jpa/videopress', | |
| 203 | - 8, | |
| 204 | - 1, | |
| 205 | - 2 | |
| 206 | - ), | |
| 207 | 175 | // Row 5: authors + search terms + file downloads (Simple only). |
| 208 | 176 | get_dashboard_default_widget_instance( |
| 209 | 177 | 'default-authors-widget-instance', |
| 210 | 178 | 'jpa/authors', |
| @@ -449,48 +417,20 @@ | ||
| 449 | 417 | ); |
| 450 | 418 | } |
| 451 | 419 | |
| 452 | 420 | /** |
| 453 | - * The Ads tab's default widget layout. | |
| 421 | + * Registers the default Premium Analytics dashboard sections. | |
| 454 | 422 | * |
| 455 | - * @return array Widget instances. | |
| 456 | - */ | |
| 457 | -function get_ads_section_default_layout() { | |
| 458 | - return array( | |
| 459 | - // Row 1: WordAds chart. | |
| 460 | - get_dashboard_default_widget_instance( | |
| 461 | - 'default-wordads-chart-tabs-widget-instance', | |
| 462 | - 'jpa/wordads-chart-tabs', | |
| 463 | - 0, | |
| 464 | - 3, | |
| 465 | - 2 | |
| 466 | - ), | |
| 467 | - // Row 2: all-time balance. | |
| 468 | - get_dashboard_default_widget_instance( | |
| 469 | - 'default-wordads-highlights-widget-instance', | |
| 470 | - 'jpa/wordads-highlights', | |
| 471 | - 1, | |
| 472 | - 3, | |
| 473 | - 1 | |
| 474 | - ), | |
| 475 | - // Row 3: earnings history. | |
| 476 | - get_dashboard_default_widget_instance( | |
| 477 | - 'default-wordads-earnings-history-widget-instance', | |
| 478 | - 'jpa/wordads-earnings-history', | |
| 479 | - 2, | |
| 480 | - 1, | |
| 481 | - 2 | |
| 482 | - ), | |
| 483 | - ); | |
| 484 | -} | |
| 485 | - | |
| 486 | -/** | |
| 487 | - * Registers the default Premium Analytics dashboard sections. | |
| 423 | + * Hooked on the registration action and safe to call directly: a section already registered | |
| 424 | + * is skipped. | |
| 488 | 425 | * |
| 426 | + * @param Dashboard_Section_Registry|null $registry Optional. The registry being hydrated. Defaults to the main instance. | |
| 489 | 427 | * @return void |
| 490 | 428 | */ |
| 491 | -function register_default_dashboard_sections() { | |
| 492 | - $registry = Dashboard_Section_Registry::get_instance(); | |
| 429 | +function register_default_dashboard_sections( $registry = null ) { | |
| 430 | + if ( ! $registry instanceof Dashboard_Section_Registry ) { | |
| 431 | + $registry = Dashboard_Section_Registry::get_instance(); | |
| 432 | + } | |
| 493 | 433 | |
| 494 | 434 | $sections = array( |
| 495 | 435 | 'analytics/traffic' => array( |
| 496 | 436 | 'label' => __( 'Traffic', 'jetpack-premium-analytics-pkg' ), |
| @@ -528,45 +468,21 @@ | ||
| 528 | 468 | // Store registers no heading of its own, so it falls back to the label. |
| 529 | 469 | 'woocommerce/store' => array( |
| 530 | 470 | 'label' => __( 'Store', 'jetpack-premium-analytics-pkg' ), |
| 531 | 471 | 'order' => 40, |
| 532 | - 'is_available' => __NAMESPACE__ . '\\is_woocommerce_dashboard_section_available_to_current_user', | |
| 472 | + 'is_available' => __NAMESPACE__ . '\\is_store_dashboard_section_available', | |
| 533 | 473 | // Nothing backfills historical orders to WordPress.com but the analytics |
| 534 | 474 | // full sync. The site sections above read data it already holds. |
| 535 | 475 | 'requires_sync' => true, |
| 536 | 476 | 'default_layout' => __NAMESPACE__ . '\\get_store_section_default_layout', |
| 537 | 477 | ), |
| 538 | - 'analytics/ads' => array( | |
| 539 | - 'label' => __( 'Ads', 'jetpack-premium-analytics-pkg' ), | |
| 540 | - 'order' => 50, | |
| 541 | - 'is_available' => __NAMESPACE__ . '\\is_ads_dashboard_section_available_to_current_user', | |
| 542 | - // Only the chart supports dates, so it owns the control. No Ads widget | |
| 543 | - // supports comparison. | |
| 544 | - 'date_filter_options' => array( | |
| 545 | - 'with_date_comparison' => false, | |
| 546 | - 'with_header_date_control' => false, | |
| 547 | - ), | |
| 548 | - 'default_layout' => __NAMESPACE__ . '\\get_ads_section_default_layout', | |
| 549 | - ), | |
| 550 | 478 | ); |
| 551 | 479 | |
| 552 | 480 | foreach ( $sections as $id => $args ) { |
| 553 | 481 | if ( ! $registry->is_registered( DASHBOARD_NAME, $id ) ) { |
| 554 | - register_dashboard_section( DASHBOARD_NAME, $id, $args ); | |
| 482 | + $registry->register( DASHBOARD_NAME, $id, $args ); | |
| 555 | 483 | } |
| 556 | 484 | } |
| 557 | 485 | } |
| 558 | 486 | |
| 559 | -/** | |
| 560 | - * Hydrates the dashboard section registry with the package's own sections. | |
| 561 | - * | |
| 562 | - * @return void | |
| 563 | - */ | |
| 564 | -function bootstrap_dashboard_sections() { | |
| 565 | - if ( did_action( 'init' ) ) { | |
| 566 | - register_default_dashboard_sections(); | |
| 567 | - } else { | |
| 568 | - add_action( 'init', __NAMESPACE__ . '\\register_default_dashboard_sections' ); | |
| 569 | - } | |
| 570 | -} | |
| 571 | - | |
| 572 | -bootstrap_dashboard_sections(); | |
| 487 | +// Registered when the registry hydrates, through the same action a plugin extending the dashboard uses. | |
| 488 | +add_action( Dashboard_Section_Registry::REGISTER_ACTION, __NAMESPACE__ . '\\register_default_dashboard_sections' ); | |