Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.86% covered (warning)
82.86%
29 / 35
66.67% covered (warning)
66.67%
2 / 3
CRAP
n/a
0 / 0
wpcom_custom_domain_get_purchases
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcom_custom_domain_is_wpcom_host
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
wpcom_site_has_mapped_custom_domain
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
11
1<?php
2/**
3 * Whether the current site has a custom (non-WordPress.com) domain.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Status\Host;
9
10// @codeCoverageIgnoreStart -- shadowed by the test stub in tests/lib/functions-wordpress.php.
11if ( ! function_exists( 'wpcom_custom_domain_get_purchases' ) ) {
12    /**
13     * Source of purchase data for wpcom_site_has_mapped_custom_domain(). Pre-definable
14     * by tests without redeclaring `wpcom_get_site_purchases()`, which has no
15     * `function_exists` guard upstream.
16     *
17     * @return array
18     *
19     * @phan-suppress PhanRedefineFunction -- phan sees both this and the test stub as definitions even though only one loads at runtime.
20     */
21    function wpcom_custom_domain_get_purchases() {
22        if ( function_exists( 'wpcom_get_site_purchases' ) ) {
23            return wpcom_get_site_purchases();
24        }
25        return array();
26    }
27}
28// @codeCoverageIgnoreEnd
29
30if ( ! function_exists( 'wpcom_custom_domain_is_wpcom_host' ) ) {
31    /**
32     * Whether a host is a WordPress.com address rather than a custom domain.
33     *
34     * Simple can ask each mapping's `is_wpcom_tld()`; Atomic has no such check, so
35     * this lists WordPress.com's own hosts and managed subdomains.
36     *
37     * @param string $host A host name, such as `example.wordpress.com`.
38     * @return bool
39     */
40    function wpcom_custom_domain_is_wpcom_host( $host ) {
41        $wpcom_domains = array(
42            'wordpress.com',
43            'wpcomstaging.com',
44            'w.link',
45            'videopress.tv',
46        );
47        $dotblog_names = array( 'art', 'business', 'car', 'code', 'data', 'design', 'family', 'fashion', 'finance', 'fitness', 'food', 'game', 'health', 'home', 'law', 'movie', 'music', 'news', 'p2', 'photo', 'poetry', 'politics', 'school', 'science', 'sport', 'tech', 'travel', 'video', 'water' );
48        foreach ( $dotblog_names as $name ) {
49            $wpcom_domains[] = $name . '.blog';
50        }
51
52        $host = strtolower( rtrim( (string) $host, '.' ) );
53        foreach ( $wpcom_domains as $wpcom_domain ) {
54            if ( $host === $wpcom_domain || str_ends_with( $host, '.' . $wpcom_domain ) ) {
55                return true;
56            }
57        }
58
59        return false;
60    }
61}
62
63if ( ! function_exists( 'wpcom_site_has_mapped_custom_domain' ) ) {
64    /**
65     * Whether the current site has a custom (non-WordPress.com) domain, primary or not.
66     *
67     * On Atomic it sees the home host and domain purchases only, so it misses a
68     * domain that is mapped but neither primary nor bought for this site.
69     *
70     * @return bool
71     */
72    function wpcom_site_has_mapped_custom_domain(): bool {
73        if ( ( new Host() )->is_wpcom_simple() ) {
74            if ( ! class_exists( '\Domain_Mapping' ) ) {
75                return false;
76            }
77
78            // @phan-suppress-next-line PhanUndeclaredClassMethod -- wpcom-only, guarded by class_exists().
79            $mappings = \Domain_Mapping::find_by_blog_id( get_current_blog_id() ) ?? array();
80            foreach ( $mappings as $mapping ) {
81                if ( ! $mapping->get_domain()->is_wpcom_tld() ) {
82                    return true;
83                }
84            }
85
86            return false;
87        }
88
89        $home_host = wp_parse_url( home_url(), PHP_URL_HOST );
90        if ( is_string( $home_host ) && '' !== $home_host && ! wpcom_custom_domain_is_wpcom_host( $home_host ) ) {
91            return true;
92        }
93
94        foreach ( wpcom_custom_domain_get_purchases() as $purchase ) {
95            if ( isset( $purchase->product_type ) && in_array( $purchase->product_type, array( 'domain_map', 'domain_reg' ), true ) ) {
96                return true;
97            }
98        }
99
100        return false;
101    }
102}