Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
10 / 16
75.00% covered (warning)
75.00%
3 / 4
CRAP
n/a
0 / 0
wpcom_premium_analytics_site_has_wordads
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
wpcom_premium_analytics_wordads_is_enabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
wpcom_premium_analytics_register_wordads_section
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
wpcom_premium_analytics_register_wordads_widget_types
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The Ads section of the Premium Analytics dashboard on the WordPress.com platform.
4 *
5 * Simple runs no Jetpack plugin, so the module registrant skips the platform and this file decides
6 * for both: the plan must include WordAds, and the site must have it on (Atomic: the WordAds module;
7 * Simple: the WordAds approval stickers), as classic Stats does.
8 * The section, its layout and the widgets come from the jetpack-ads package, which
9 * the Jetpack plugin bundles: WordPress.com loads that copy, so this package does not.
10 *
11 * @package automattic/jetpack-mu-wpcom
12 */
13
14use Automattic\Jetpack\Modules;
15use Automattic\Jetpack\Status\Host;
16use Automattic\Jetpack\WordAds\Analytics_Dashboard;
17
18/**
19 * Whether the site's plan includes WordAds, the site has it on, and the package that owns the
20 * section is here.
21 *
22 * @since $$next-version$$
23 *
24 * @return bool
25 */
26function wpcom_premium_analytics_site_has_wordads() {
27    return function_exists( 'wpcom_site_has_feature' )
28        && wpcom_site_has_feature( 'wordads' )
29        && wpcom_premium_analytics_wordads_is_enabled()
30        && class_exists( Analytics_Dashboard::class );
31}
32
33/**
34 * Whether the site is using WordAds, read as classic Stats in wp-admin reads it: the
35 * approval stickers on Simple, the WordAds module on Atomic. A plan that could use WordAds
36 * is not a site that does.
37 *
38 * @since $$next-version$$
39 *
40 * @return bool
41 */
42function wpcom_premium_analytics_wordads_is_enabled() {
43    if ( ( new Host() )->is_wpcom_simple() ) {
44        // The stickers the sites API's has_wordads() reads on Simple.
45        return function_exists( 'has_any_blog_stickers' )
46            && (bool) has_any_blog_stickers( array( 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
47    }
48
49    // Atomic runs the Jetpack plugin, and a site with the WordAds module off is not running ads,
50    // whatever its approval sticker says. Not `available_only`: the module list is not loaded on
51    // every request that hydrates the registry.
52    return ( new Modules() )->is_active( 'wordads', false );
53}
54
55/**
56 * Register the Ads section on a site whose plan includes WordAds and that has it on.
57 *
58 * @since $$next-version$$
59 *
60 * @param \Automattic\Jetpack\PremiumAnalytics\Dashboard_Section_Registry $registry The registry being hydrated.
61 * @return void
62 */
63function wpcom_premium_analytics_register_wordads_section( $registry ) {
64    if ( ! wpcom_premium_analytics_site_has_wordads() ) {
65        return;
66    }
67
68    Analytics_Dashboard::register_section( $registry );
69}
70
71/**
72 * Register the Ads widget types on a site whose plan includes WordAds and that has it on.
73 *
74 * @since $$next-version$$
75 *
76 * @param \Automattic\Jetpack\PremiumAnalytics\Widget_Type_Registry $registry The registry being hydrated.
77 * @return void
78 */
79function wpcom_premium_analytics_register_wordads_widget_types( $registry ) {
80    if ( ! wpcom_premium_analytics_site_has_wordads() ) {
81        return;
82    }
83
84    Analytics_Dashboard::register_widget_types( $registry );
85}
86
87// After the package's own registrants (priority 10), so an existing `ads` slug is found and left alone.
88add_action( 'jetpack_premium_analytics_register_dashboard_sections', 'wpcom_premium_analytics_register_wordads_section', 20 );
89add_action( 'jetpack_premium_analytics_register_widget_types', 'wpcom_premium_analytics_register_wordads_widget_types', 20 );