Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.83% covered (danger)
47.83%
11 / 23
50.00% covered (danger)
50.00%
5 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Cornerstone_Utils
47.83% covered (danger)
47.83%
11 / 23
50.00% covered (danger)
50.00%
5 / 10
41.84
0.00% covered (danger)
0.00%
0 / 1
 get_list
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_custom_list
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_predefined_list
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_cornerstone_page_by_url
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 sanitize_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_provider_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepare_provider_data
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 is_current_page_cornerstone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_cornerstone_page
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 maybe_trailing_slash_urls
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Automattic\Jetpack_Boost\Lib\Cornerstone;
4
5use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Cornerstone_Provider;
6
7class Cornerstone_Utils {
8
9    /**
10     * Get the list of cornerstone pages.
11     *
12     * @return string[] The relative URLs of all the cornerstone pages.
13     */
14    public static function get_list() {
15        /**
16         * Filters the list of cornerstone pages. This list includes the predefined and custom pages.
17         * If you want to change the list of custom pages, use `jetpack_boost_cornerstone_pages_list` instead.
18         *
19         * @since 4.4.0-beta1
20         *
21         * @param string[] $urls The absolute URLs of all the cornerstone pages.
22         */
23        return apply_filters( 'jetpack_boost_cornerstone_pages_list_complete', array_merge( self::get_predefined_list(), self::get_custom_list() ) );
24    }
25
26    /**
27     * Gets the list of Cornerstone Pages that the user has added to the custom list.
28     *
29     * @return string[] The absolute URLs of the cornerstone pages.
30     *
31     * @since 4.2.0
32     */
33    public static function get_custom_list() {
34        $pages = jetpack_boost_ds_get( 'cornerstone_pages_list' );
35
36        // Bail early if no pages are found.
37        if ( empty( $pages ) ) {
38            return array();
39        }
40
41        return self::maybe_trailing_slash_urls( $pages );
42    }
43
44    /**
45     * Gets the list of Cornerstone Pages that the user cannot remove.
46     *
47     * @return string[] The absolute URLs of the cornerstone pages.
48     *
49     * @since 4.2.0
50     */
51    public static function get_predefined_list() {
52        return self::maybe_trailing_slash_urls( array( home_url() ) );
53    }
54
55    /**
56     * Checks if a URL is a cornerstone page.
57     *
58     * @param string $url The URL to check.
59     * @return bool True if the URL is a cornerstone page, false otherwise.
60     */
61    public static function is_cornerstone_page_by_url( $url ) {
62        $cornerstone_pages = self::get_list();
63        if ( empty( $cornerstone_pages ) ) {
64            return false;
65        }
66
67        $cornerstone_pages = array_map( 'untrailingslashit', $cornerstone_pages );
68        return in_array( self::sanitize_url( $url ), $cornerstone_pages, true );
69    }
70
71    /**
72     * Sanitize a URL to make it a compatible cornerstone page URL.
73     *
74     * @param string $url The URL to sanitize.
75     * @return string The sanitized URL.
76     */
77    public static function sanitize_url( $url ) {
78        return untrailingslashit( $url );
79    }
80
81    /**
82     * Get the provider key for a given URL.
83     *
84     * @param string $url The URL to get the provider key for.
85     * @return string The provider key.
86     */
87    public static function get_provider_key( $url ) {
88        return Cornerstone_Provider::get_provider_key( self::sanitize_url( $url ) );
89    }
90
91    /**
92     * Prepare provider data for a given URL.
93     * This is usually sent to the Cloud API.
94     *
95     * @param string $url The URL to prepare provider data for.
96     * @return array The provider data.
97     */
98    public static function prepare_provider_data( $url ) {
99        return array(
100            'key' => self::get_provider_key( $url ),
101            // Send the URL verbatim (do not sanitize/untrailingslashit it). The cloud analyzer
102            // fetches this exact URL, so it must keep the site's canonical trailing-slash form to
103            // avoid edge/WAF 301s or 403s that break analysis. The `key` is intentionally still
104            // stripped so storage/retrieval stays slash-agnostic. Do not "simplify" url back to
105            // self::sanitize_url( $url ).
106            'url' => $url,
107        );
108    }
109
110    /**
111     * Checks if the current page is a cornerstone page.
112     *
113     * @return bool True if the current page is a cornerstone page, false otherwise.
114     *
115     * @since 3.13.1
116     */
117    public static function is_current_page_cornerstone() {
118        return self::is_cornerstone_page_by_url( Cornerstone_Provider::get_request_url() );
119    }
120
121    /**
122     * Check if a post ID is a cornerstone page.
123     *
124     * @param int $post_id The ID of the post to check.
125     * @return bool True if the post is a cornerstone page, false otherwise.
126     */
127    public static function is_cornerstone_page( $post_id ) {
128        return self::is_cornerstone_page_by_url( get_permalink( $post_id ) );
129    }
130
131    /**
132     * Adds trailing slashes to URLs if the current permalink structure requires it.
133     *
134     * @param string[] $urls The URLs to process.
135     * @return string[] The processed URLs.
136     */
137    public static function maybe_trailing_slash_urls( $urls ) {
138        $permalink_structure = \get_option( 'permalink_structure' );
139
140        // If permalink structure ends with slash, add trailing slashes.
141        if ( $permalink_structure && substr( $permalink_structure, -1 ) === '/' ) {
142            $urls = array_map( 'trailingslashit', $urls );
143        }
144
145        return $urls;
146    }
147}