Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.74% covered (success)
92.74%
115 / 124
76.47% covered (warning)
76.47%
13 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
Analytics_Dashboard
92.74% covered (success)
92.74%
115 / 124
76.47% covered (warning)
76.47%
13 / 17
48.88
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 register_section
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 get_no_orders_notice
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 get_default_layout
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 enqueue_store_performance_bookings
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
6.10
 remove_unavailable_widget_types
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 register_widget_types
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
5.01
 add_error_reporting_origin
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 is_section_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_woocommerce_available
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 current_user_can_view_store_reports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_bookings_plugin_active
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 supports_widget_contract
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 load_build
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
4.94
 build_dir
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 section_slug_is_taken
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 dashboard_has_section_slug
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
8.74
1<?php
2/**
3 * The WooCommerce section of the Premium Analytics dashboard.
4 *
5 * @package automattic/jetpack-woocommerce-stats
6 */
7
8namespace Automattic\Jetpack\WooCommerceStats;
9
10use Automattic\Jetpack\PremiumAnalytics\Sync\Sync_Status_Tracker;
11use function Automattic\Jetpack\PremiumAnalytics\get_dashboard_default_widget_instance;
12use function Automattic\Jetpack\PremiumAnalytics\register_dashboard_section;
13use function Automattic\Jetpack\PremiumAnalytics\register_widget_types_from_manifest;
14use const Automattic\Jetpack\PremiumAnalytics\DASHBOARD_NAME;
15use const Automattic\Jetpack\PremiumAnalytics\DASHBOARD_TILE_CARD;
16use const Automattic\Jetpack\PremiumAnalytics\DASHBOARD_TILE_LIST;
17
18/**
19 * Registers the WooCommerce section and the package's widget types.
20 *
21 * The plugin that bundles it calls `init()`; both register when the dashboard's registries hydrate,
22 * gated here on WooCommerce and on the reader's store reports capability.
23 *
24 * @since 0.1.0-alpha
25 */
26class Analytics_Dashboard {
27
28    const PACKAGE_VERSION = '0.5.0';
29
30    /**
31     * Namespaced section identifier. Its slug, `woocommerce`, keys the section's URL and stored layouts.
32     */
33    const SECTION_ID = 'woocommerce-analytics/woocommerce';
34
35    /**
36     * Widget type names the package builds, from `widgets/*\/widget.json`.
37     */
38    const STORE_PERFORMANCE_TYPE         = 'woocommerce-analytics/store-performance';
39    const NET_SALES_OVER_TIME_TYPE       = 'woocommerce-analytics/net-sales-over-time';
40    const TOTAL_SALES_OVER_TIME_TYPE     = 'woocommerce-analytics/total-sales-over-time';
41    const GROSS_SALES_OVER_TIME_TYPE     = 'woocommerce-analytics/gross-sales-over-time';
42    const ORDERS_OVER_TIME_TYPE          = 'woocommerce-analytics/orders-over-time';
43    const AVERAGE_ORDER_VALUE_TYPE       = 'woocommerce-analytics/average-order-value';
44    const AVERAGE_ITEMS_PER_ORDER_TYPE   = 'woocommerce-analytics/average-items-per-order';
45    const ITEMS_SOLD_TYPE                = 'woocommerce-analytics/items-sold';
46    const BOOKINGS_OVER_TIME_TYPE        = 'woocommerce-analytics/bookings-over-time';
47    const VISITORS_OVER_TIME_TYPE        = 'woocommerce-analytics/visitors-over-time';
48    const NEW_VS_RETURNING_CUSTOMER_TYPE = 'woocommerce-analytics/new-vs-returning-customer';
49    const PAYMENT_STATUS_TYPE            = 'woocommerce-analytics/payment-status';
50    const ORDERS_FULFILLMENT_TYPE        = 'woocommerce-analytics/orders-fulfillment';
51    const COUPON_USAGE_OVER_TIME_TYPE    = 'woocommerce-analytics/coupon-usage-over-time';
52    const BOOKINGS_BY_STATUS_TYPE        = 'woocommerce-analytics/bookings-by-status';
53    const SESSIONS_BY_DEVICE_TYPE        = 'woocommerce-analytics/sessions-by-device';
54
55    /**
56     * The columns family, with the bookings variants of the first two.
57     */
58    const REVENUE_BY_CUSTOMER_TYPE_TYPE          = 'woocommerce-analytics/revenue-by-customer-type';
59    const BOOKINGS_REVENUE_BY_CUSTOMER_TYPE_TYPE = 'woocommerce-analytics/bookings-revenue-by-customer-type';
60    const SALES_BY_DEVICE_TYPE                   = 'woocommerce-analytics/sales-by-device';
61    const BOOKINGS_BY_DEVICE_TYPE                = 'woocommerce-analytics/bookings-by-device';
62    const TOTAL_RETURNS_TYPE                     = 'woocommerce-analytics/total-returns';
63    const SALES_BY_COUPON_TYPE                   = 'woocommerce-analytics/sales-by-coupon';
64    const TOP_PERFORMING_PRODUCTS_TYPE           = 'woocommerce-analytics/top-performing-products';
65    const TOP_PERFORMING_BOOKINGS_TYPE           = 'woocommerce-analytics/top-performing-bookings';
66    const SALES_BY_UTM_SOURCE_TYPE               = 'woocommerce-analytics/sales-by-utm-source';
67    const SALES_BY_UTM_CHANNEL_TYPE              = 'woocommerce-analytics/sales-by-utm-channel';
68    const SALES_BY_UTM_CAMPAIGN_TYPE             = 'woocommerce-analytics/sales-by-utm-campaign';
69    const VISITORS_BY_LOCATION_TYPE              = 'woocommerce-analytics/visitors-by-location';
70
71    /**
72     * The conversion funnel, for every product and for bookings.
73     */
74    const CONVERSION_RATE_TYPE                = 'woocommerce-analytics/conversion-rate';
75    const STORE_CONVERSION_RATE_BOOKINGS_TYPE = 'woocommerce-analytics/store-conversion-rate-bookings';
76
77    /**
78     * Registry actions of the dashboard package.
79     */
80    const REGISTER_SECTIONS_ACTION     = 'jetpack_premium_analytics_register_dashboard_sections';
81    const REGISTER_WIDGET_TYPES_ACTION = 'jetpack_premium_analytics_register_widget_types';
82
83    /**
84     * Text domain of the widget metadata and of the built widget bundles.
85     */
86    const TEXTDOMAIN = 'jetpack-woocommerce-stats-pkg';
87
88    /**
89     * Lowest widget contract the build works against: 2.4.0, the first to export `useOpenChartBucket`.
90     */
91    const MIN_WIDGET_API_VERSION = '2.4.0';
92
93    /**
94     * Widget categories the package builds but holds back from the tab for now: the bookings
95     * reports answer 500 on a store without a synced booking (WOOA7S-2287).
96     */
97    const HELD_BACK_WIDGET_CATEGORIES = array( 'bookings' );
98
99    /**
100     * Ahead of Traffic (10), so a store's dashboard opens on this tab.
101     */
102    const SECTION_ORDER = 5;
103
104    /**
105     * Widget types the tab offers in the inserter on top of its default layout: the over-time
106     * charts a store owner may prefer over Store performance.
107     */
108    const INSERTABLE_WIDGET_TYPES = array(
109        self::NET_SALES_OVER_TIME_TYPE,
110        self::TOTAL_SALES_OVER_TIME_TYPE,
111        self::GROSS_SALES_OVER_TIME_TYPE,
112        self::ORDERS_OVER_TIME_TYPE,
113        self::VISITORS_OVER_TIME_TYPE,
114    );
115
116    /**
117     * Filter through which WooCommerce detection is resolved, for hosts and tests without the plugin.
118     */
119    const WOOCOMMERCE_AVAILABLE_FILTER = 'jetpack_woocommerce_stats_woocommerce_available';
120
121    /**
122     * Widget categories that also need the WooCommerce Bookings extension.
123     */
124    const BOOKINGS_WIDGET_CATEGORIES = array( 'bookings' );
125
126    /**
127     * The module that offers the Bookings metric to Store performance.
128     */
129    const STORE_PERFORMANCE_BOOKINGS_MODULE = '@jetpack-woocommerce-stats/store-performance-bookings';
130
131    /**
132     * Action the dashboard page fires as it enqueues its scripts: its menu slug,
133     * `Analytics::MENU_PAGE_SLUG`, under wp-build's `_init` suffix.
134     */
135    const DASHBOARD_PAGE_INIT_ACTION = 'jetpack-premium-analytics-wp-admin_init';
136
137    /**
138     * Register the sync opt-in, then hook both registrants on the dashboard's registry actions, the Bookings metric on the dashboard
139     * page, the reports proxy on REST requests and its cache reset on the end of the initial sync,
140     * and the store currency on the script data.
141     *
142     * Priority 20, after the dashboard package's own registrants: an older package that still
143     * registers the section itself is found by slug and left alone.
144     *
145     * Call it before plugins_loaded completes, for the sync opt-in ({@see Sync_Configuration::register()}).
146     *
147     * @return void
148     */
149    public static function init() {
150        Sync_Configuration::register();
151
152        add_action( self::REGISTER_SECTIONS_ACTION, array( __CLASS__, 'register_section' ), 20 );
153        add_action( self::REGISTER_WIDGET_TYPES_ACTION, array( __CLASS__, 'register_widget_types' ), 20 );
154        add_action( self::DASHBOARD_PAGE_INIT_ACTION, array( __CLASS__, 'enqueue_store_performance_bookings' ) );
155
156        add_action( 'rest_api_init', array( Api_Proxy_Controller::class, 'init' ) );
157        add_action( Report_Execution\Report_Registry::REGISTER_ACTION, array( Report_Execution\Default_Reports::class, 'register' ) );
158        add_filter( 'jetpack_stats_transient_cleanup_prefixes', array( Api_Proxy_Controller::class, 'register_transient_cleanup_prefix' ) );
159        add_action( Sync_Status_Tracker::MILESTONE_ACTION, array( Api_Proxy_Controller::class, 'clear_cache' ) );
160
161        Store_Currency::init();
162    }
163
164    /**
165     * Register the WooCommerce section unless `woocommerce` or the older `store` slug is taken.
166     *
167     * @param object $registry The section registry being hydrated.
168     * @return void
169     */
170    public static function register_section( $registry ) {
171        if ( self::section_slug_is_taken( $registry ) ) {
172            return;
173        }
174
175        register_dashboard_section(
176            DASHBOARD_NAME,
177            self::SECTION_ID,
178            array(
179                'label'                   => __( 'WooCommerce', 'jetpack-woocommerce-stats-pkg' ),
180                'order'                   => self::SECTION_ORDER,
181                'is_available'            => array( __CLASS__, 'is_section_available' ),
182                // Nothing backfills historical orders to WordPress.com but the analytics full sync.
183                'requires_sync'           => true,
184                'notice'                  => array( __CLASS__, 'get_no_orders_notice' ),
185                'default_layout'          => array( __CLASS__, 'get_default_layout' ),
186                'insertable_widget_types' => self::INSERTABLE_WIDGET_TYPES,
187            )
188        );
189    }
190
191    /**
192     * The message above the tab while the store has never had an order.
193     *
194     * @return string|null The message, or null once the store has an order or without WooCommerce.
195     */
196    public static function get_no_orders_notice() {
197        if ( ! function_exists( 'wc_get_orders' ) ) {
198            return null;
199        }
200
201        // The registered statuses, so an abandoned checkout draft is not mistaken for a first order.
202        $orders = wc_get_orders(
203            array(
204                'type'   => 'shop_order',
205                'status' => array_keys( wc_get_order_statuses() ),
206                'limit'  => 1,
207                'return' => 'ids',
208            )
209        );
210
211        if ( ! empty( $orders ) ) {
212            return null;
213        }
214
215        return __( 'Your store has no orders yet. Your sales, orders and customers will show here after your first order.', 'jetpack-woocommerce-stats-pkg' );
216    }
217
218    /**
219     * The default layout of the WooCommerce tab; the inserter offers its types plus
220     * `INSERTABLE_WIDGET_TYPES`.
221     *
222     * Each group below fills one row of the dashboard grid.
223     *
224     * @return array[] Widget instances, as `get_dashboard_default_widget_instance()` builds them.
225     */
226    public static function get_default_layout() {
227        return array(
228            get_dashboard_default_widget_instance( 'default-store-performance-widget-instance', self::STORE_PERFORMANCE_TYPE, 0, 'full', DASHBOARD_TILE_CARD ),
229
230            get_dashboard_default_widget_instance( 'default-top-performing-products-widget-instance', self::TOP_PERFORMING_PRODUCTS_TYPE, 10, 1, DASHBOARD_TILE_LIST ),
231            get_dashboard_default_widget_instance( 'default-items-sold-widget-instance', self::ITEMS_SOLD_TYPE, 15, 1, DASHBOARD_TILE_CARD ),
232            get_dashboard_default_widget_instance( 'default-average-order-value-widget-instance', self::AVERAGE_ORDER_VALUE_TYPE, 20, 1, DASHBOARD_TILE_CARD ),
233            get_dashboard_default_widget_instance( 'default-average-items-per-order-widget-instance', self::AVERAGE_ITEMS_PER_ORDER_TYPE, 30, 1, DASHBOARD_TILE_CARD ),
234
235            get_dashboard_default_widget_instance( 'default-conversion-rate-widget-instance', self::CONVERSION_RATE_TYPE, 40, 1, DASHBOARD_TILE_CARD ),
236            get_dashboard_default_widget_instance( 'default-visitors-by-location-widget-instance', self::VISITORS_BY_LOCATION_TYPE, 50, 2, DASHBOARD_TILE_LIST ),
237
238            get_dashboard_default_widget_instance( 'default-new-vs-returning-customer-widget-instance', self::NEW_VS_RETURNING_CUSTOMER_TYPE, 60, 1, DASHBOARD_TILE_CARD ),
239            get_dashboard_default_widget_instance( 'default-revenue-by-customer-type-widget-instance', self::REVENUE_BY_CUSTOMER_TYPE_TYPE, 70, 1, DASHBOARD_TILE_CARD ),
240            get_dashboard_default_widget_instance( 'default-total-returns-widget-instance', self::TOTAL_RETURNS_TYPE, 80, 1, DASHBOARD_TILE_CARD ),
241
242            get_dashboard_default_widget_instance( 'default-sales-by-utm-channel-widget-instance', self::SALES_BY_UTM_CHANNEL_TYPE, 90, 1, DASHBOARD_TILE_LIST ),
243            get_dashboard_default_widget_instance( 'default-sales-by-utm-source-widget-instance', self::SALES_BY_UTM_SOURCE_TYPE, 100, 1, DASHBOARD_TILE_LIST ),
244            get_dashboard_default_widget_instance( 'default-sales-by-utm-campaign-widget-instance', self::SALES_BY_UTM_CAMPAIGN_TYPE, 110, 1, DASHBOARD_TILE_LIST ),
245
246            get_dashboard_default_widget_instance( 'default-orders-fulfillment-widget-instance', self::ORDERS_FULFILLMENT_TYPE, 120, 1, DASHBOARD_TILE_CARD ),
247            get_dashboard_default_widget_instance( 'default-sales-by-coupon-widget-instance', self::SALES_BY_COUPON_TYPE, 130, 1, DASHBOARD_TILE_LIST ),
248            get_dashboard_default_widget_instance( 'default-coupon-usage-over-time-widget-instance', self::COUPON_USAGE_OVER_TIME_TYPE, 140, 1, DASHBOARD_TILE_CARD ),
249
250            get_dashboard_default_widget_instance( 'default-payment-status-widget-instance', self::PAYMENT_STATUS_TYPE, 150, 1, DASHBOARD_TILE_CARD ),
251            get_dashboard_default_widget_instance( 'default-sessions-by-device-widget-instance', self::SESSIONS_BY_DEVICE_TYPE, 160, 1, DASHBOARD_TILE_CARD ),
252            get_dashboard_default_widget_instance( 'default-sales-by-device-widget-instance', self::SALES_BY_DEVICE_TYPE, 170, 1, DASHBOARD_TILE_CARD ),
253        );
254    }
255
256    /**
257     * Load the Bookings metric with the dashboard on a store running the Bookings extension.
258     *
259     * Enqueued as a module of its own: the page's boot runs only the init modules its build names,
260     * and a boot dependency is preloaded, not run. The build's own registrant hooks
261     * `wp_default_scripts`, already fired on this page hook, so it runs here.
262     *
263     * @return void
264     */
265    public static function enqueue_store_performance_bookings() {
266        if ( ! self::is_bookings_plugin_active() || ! self::supports_widget_contract() || ! self::is_woocommerce_available() || ! self::current_user_can_view_store_reports() ) {
267            return;
268        }
269
270        self::load_build();
271        if ( ! function_exists( 'jetpack_woocommerce_stats_register_script_modules' ) ) {
272            return;
273        }
274        jetpack_woocommerce_stats_register_script_modules();
275
276        wp_enqueue_script_module( self::STORE_PERFORMANCE_BOOKINGS_MODULE );
277    }
278
279    /**
280     * The manifest minus the categories held back from the tab and the bookings categories
281     * without their extension.
282     *
283     * @param array[] $widget_modules     Widget module records of the build manifest.
284     * @param bool    $bookings_available Whether the WooCommerce Bookings extension is active.
285     * @return array[] The records the package registers.
286     */
287    private static function remove_unavailable_widget_types( $widget_modules, $bookings_available ) {
288        return array_values(
289            array_filter(
290                $widget_modules,
291                static function ( $widget_module ) use ( $bookings_available ) {
292                    $category = $widget_module['category'] ?? '';
293
294                    if ( in_array( $category, self::HELD_BACK_WIDGET_CATEGORIES, true ) ) {
295                        return false;
296                    }
297
298                    return $bookings_available || ! in_array( $category, self::BOOKINGS_WIDGET_CATEGORIES, true );
299                }
300            )
301        );
302    }
303
304    /**
305     * Register the widget types from the package's build manifest.
306     *
307     * Loads the generated build first: after `init` it registers the widget script modules on the
308     * spot, so they reach the page import map the same request.
309     *
310     * Registers nothing without WooCommerce or for a reader who cannot see store reports: the types
311     * would only collect 403s from the proxy.
312     *
313     * @param object $registry The widget type registry being hydrated.
314     * @return void
315     */
316    public static function register_widget_types( $registry ) {
317        add_filter( 'jetpack_premium_analytics_error_reporting_script_origins', array( __CLASS__, 'add_error_reporting_origin' ) );
318
319        if ( ! self::supports_widget_contract() || ! self::is_woocommerce_available() || ! self::current_user_can_view_store_reports() ) {
320            return;
321        }
322
323        self::load_build();
324        if ( ! function_exists( 'jetpack_woocommerce_stats_get_registered_widget_modules' ) ) {
325            return;
326        }
327
328        register_widget_types_from_manifest(
329            self::remove_unavailable_widget_types( jetpack_woocommerce_stats_get_registered_widget_modules(), self::is_bookings_plugin_active() ),
330            array(
331                'textdomain'    => self::TEXTDOMAIN,
332                'i18n_manifest' => add_query_arg( 'ver', self::PACKAGE_VERSION, plugins_url( 'i18n-manifest.json', self::build_dir() . '/build.php' ) ),
333            ),
334            $registry
335        );
336    }
337
338    /**
339     * Append this package's dashboard build directories to the error reporting origins.
340     *
341     * The widget and module directories rather than the whole build, which also carries script that
342     * never runs on the dashboard. Every widget and module builds into one of these two, so a new
343     * one needs no change here.
344     *
345     * @param string[] $origins Absolute URL prefixes, each ending in a slash.
346     * @return string[]
347     */
348    public static function add_error_reporting_origin( $origins ) {
349        $origins = is_array( $origins ) ? $origins : array();
350        $build   = trailingslashit( plugins_url( '', self::build_dir() . '/build.php' ) );
351
352        $origins[] = $build . 'widgets/';
353        $origins[] = $build . 'modules/';
354
355        return $origins;
356    }
357
358    /**
359     * Whether the WooCommerce tab is available: WooCommerce is active and the reader may see store
360     * reports.
361     *
362     * @return bool
363     */
364    public static function is_section_available() {
365        return self::is_woocommerce_available() && self::current_user_can_view_store_reports();
366    }
367
368    /**
369     * Whether WooCommerce is active.
370     *
371     * @return bool
372     */
373    public static function is_woocommerce_available() {
374        $is_available = class_exists( 'WooCommerce' ) || function_exists( 'WC' );
375
376        /**
377         * Filters whether WooCommerce was detected.
378         *
379         * @since 0.2.0
380         *
381         * @param bool $is_available Whether WooCommerce was detected in the current request.
382         */
383        return (bool) apply_filters( self::WOOCOMMERCE_AVAILABLE_FILTER, $is_available );
384    }
385
386    /**
387     * Whether the current user may read the store reports, the rule the reports proxy enforces.
388     *
389     * @return bool
390     */
391    public static function current_user_can_view_store_reports() {
392        // phpcs:ignore WordPress.WP.Capabilities.Unknown -- WooCommerce registers this capability.
393        return current_user_can( 'manage_options' ) || current_user_can( 'view_woocommerce_reports' );
394    }
395
396    /**
397     * Whether the WooCommerce Bookings extension is active; `is_plugin_active()` only exists in
398     * admin contexts, hence the guard.
399     *
400     * @return bool
401     */
402    private static function is_bookings_plugin_active() {
403        return class_exists( 'WC_Bookings' )
404            || ( function_exists( 'is_plugin_active' ) && \is_plugin_active( 'woocommerce-bookings/woocommerce-bookings.php' ) );
405    }
406
407    /**
408     * Whether the dashboard's widget contract is one the widgets were built against.
409     *
410     * On an older contract the SDK module lacks a name the widgets import, and they fail to load.
411     *
412     * @return bool
413     */
414    private static function supports_widget_contract() {
415        // The dashboard loads the file naming its contract when its widget registry is first read,
416        // which has not happened yet when the dashboard page enqueues its scripts.
417        if (
418            ! defined( 'Automattic\\Jetpack\\PremiumAnalytics\\WIDGET_API_VERSION' )
419            && function_exists( 'Automattic\\Jetpack\\PremiumAnalytics\\ensure_widget_registry_ready' )
420        ) {
421            \Automattic\Jetpack\PremiumAnalytics\ensure_widget_registry_ready();
422        }
423
424        if ( ! defined( 'Automattic\\Jetpack\\PremiumAnalytics\\WIDGET_API_VERSION' ) ) {
425            return false;
426        }
427
428        $version = \Automattic\Jetpack\PremiumAnalytics\WIDGET_API_VERSION;
429
430        return version_compare( $version, self::MIN_WIDGET_API_VERSION, '>=' ) && version_compare( $version, '3', '<' );
431    }
432
433    /**
434     * Load the generated build once. Guarded by symbol, not by path: two copies of the package
435     * can share a request.
436     *
437     * @return void
438     */
439    private static function load_build() {
440        if ( function_exists( 'jetpack_woocommerce_stats_get_registered_widget_modules' ) ) {
441            return;
442        }
443        $loader = self::build_dir() . '/build.php';
444        if ( file_exists( $loader ) ) {
445            require_once $loader;
446        }
447    }
448
449    /**
450     * Directory of the wp-build output.
451     *
452     * @return string
453     */
454    private static function build_dir() {
455        return dirname( __DIR__ ) . '/build';
456    }
457
458    /**
459     * Whether this tab's slug, or the `store` slug an older package still registers, is taken.
460     *
461     * @param object $registry The section registry being hydrated.
462     * @return bool
463     */
464    private static function section_slug_is_taken( $registry ) {
465        foreach ( array( 'woocommerce', 'store' ) as $slug ) {
466            if ( self::dashboard_has_section_slug( $registry, DASHBOARD_NAME, $slug ) ) {
467                return true;
468            }
469        }
470
471        return false;
472    }
473
474    /**
475     * Whether a section with the slug is registered on the dashboard.
476     *
477     * Reads the registry through the slug lookup when the package offers it, and through the full
478     * list otherwise: the package and this one ship on different cadences.
479     *
480     * @param object $registry       The registry being hydrated.
481     * @param string $dashboard_name Dashboard identifier.
482     * @param string $slug           Section slug.
483     * @return bool
484     */
485    private static function dashboard_has_section_slug( $registry, $dashboard_name, $slug ) {
486        if ( method_exists( $registry, 'get_registered_by_slug' ) ) {
487            return null !== $registry->get_registered_by_slug( $dashboard_name, $slug );
488        }
489
490        foreach ( $registry->get_all_registered( $dashboard_name ) as $section ) {
491            if ( $section->slug === $slug ) {
492                return true;
493            }
494        }
495
496        return false;
497    }
498}