Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
12 / 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 / 2
0.00% covered (danger)
0.00%
0 / 1
6
wpcom_premium_analytics_wordads_is_enabled
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
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 site must have WordAds on (Simple: the WordAds approval stickers; Atomic: the WordAds
7 * module and the plan feature), as classic Stats does. Simple skips the plan: sites approved before
8 * WordAds needed a plan still run ads.
9 * The section, its layout and the widgets come from the jetpack-ads package, which
10 * the Jetpack plugin bundles: WordPress.com loads that copy, so this package does not.
11 *
12 * @package automattic/jetpack-mu-wpcom
13 */
14
15use Automattic\Jetpack\Modules;
16use Automattic\Jetpack\Status\Host;
17use Automattic\Jetpack\WordAds\Analytics_Dashboard;
18
19/**
20 * Whether the site has WordAds on and the package that owns the section is here.
21 *
22 * @since 6.11.0
23 *
24 * @return bool
25 */
26function wpcom_premium_analytics_site_has_wordads() {
27    return wpcom_premium_analytics_wordads_is_enabled()
28        && class_exists( Analytics_Dashboard::class );
29}
30
31/**
32 * Whether the site is using WordAds, read as classic Stats in wp-admin reads it: the
33 * approval stickers on Simple, the WordAds module and the plan feature on Atomic. A plan that
34 * could use WordAds is not a site that does.
35 *
36 * @since 6.11.0
37 *
38 * @return bool
39 */
40function wpcom_premium_analytics_wordads_is_enabled() {
41    if ( ( new Host() )->is_wpcom_simple() ) {
42        // The stickers the sites API's has_wordads() reads on Simple.
43        return function_exists( 'has_any_blog_stickers' )
44            && (bool) has_any_blog_stickers( array( 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
45    }
46
47    // Atomic runs the Jetpack plugin, and a site with the WordAds module off is not running ads,
48    // whatever its approval sticker says. Not `available_only`: the module list is not loaded on
49    // every request that hydrates the registry. The plan too: nothing turns the module off on a downgrade.
50    return ( new Modules() )->is_active( 'wordads', false )
51        && function_exists( 'wpcom_site_has_feature' )
52        && wpcom_site_has_feature( 'wordads' );
53}
54
55/**
56 * Register the Ads section on a site that has WordAds on.
57 *
58 * @since 6.11.0
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 that has WordAds on.
73 *
74 * @since 6.11.0
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 );