Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
21.74% |
5 / 23 |
|
20.00% |
2 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Cornerstone_Utils | |
21.74% |
5 / 23 |
|
20.00% |
2 / 10 |
107.95 | |
0.00% |
0 / 1 |
| get_list | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_custom_list | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_predefined_list | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_cornerstone_page_by_url | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| sanitize_url | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_provider_key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| prepare_provider_data | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| is_current_page_cornerstone | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_cornerstone_page | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| maybe_trailing_slash_urls | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib\Cornerstone; |
| 4 | |
| 5 | use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers\Cornerstone_Provider; |
| 6 | |
| 7 | class 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 | 'url' => self::sanitize_url( $url ), |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Checks if the current page is a cornerstone page. |
| 107 | * |
| 108 | * @return bool True if the current page is a cornerstone page, false otherwise. |
| 109 | * |
| 110 | * @since 3.13.1 |
| 111 | */ |
| 112 | public static function is_current_page_cornerstone() { |
| 113 | return self::is_cornerstone_page_by_url( Cornerstone_Provider::get_request_url() ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Check if a post ID is a cornerstone page. |
| 118 | * |
| 119 | * @param int $post_id The ID of the post to check. |
| 120 | * @return bool True if the post is a cornerstone page, false otherwise. |
| 121 | */ |
| 122 | public static function is_cornerstone_page( $post_id ) { |
| 123 | return self::is_cornerstone_page_by_url( get_permalink( $post_id ) ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Adds trailing slashes to URLs if the current permalink structure requires it. |
| 128 | * |
| 129 | * @param string[] $urls The URLs to process. |
| 130 | * @return string[] The processed URLs. |
| 131 | */ |
| 132 | public static function maybe_trailing_slash_urls( $urls ) { |
| 133 | $permalink_structure = \get_option( 'permalink_structure' ); |
| 134 | |
| 135 | // If permalink structure ends with slash, add trailing slashes. |
| 136 | if ( $permalink_structure && substr( $permalink_structure, -1 ) === '/' ) { |
| 137 | $urls = array_map( 'trailingslashit', $urls ); |
| 138 | } |
| 139 | |
| 140 | return $urls; |
| 141 | } |
| 142 | } |