Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
27 / 30 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Speculation_Rules | |
90.00% |
27 / 30 |
|
60.00% |
3 / 5 |
7.05 | |
0.00% |
0 / 1 |
| get_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_cornerstone_rules | |
95.65% |
22 / 23 |
|
0.00% |
0 / 1 |
2 | |||
| get_cornerstone_urls | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Speculation Rules implementation for cornerstone pages |
| 4 | * |
| 5 | * @package Boost |
| 6 | * @since 3.13.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack_Boost\Modules\Optimizations\Speculation_Rules; |
| 10 | |
| 11 | use Automattic\Jetpack_Boost\Contracts\Changes_Output_On_Activation; |
| 12 | use Automattic\Jetpack_Boost\Contracts\Feature; |
| 13 | use Automattic\Jetpack_Boost\Contracts\Optimization; |
| 14 | use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils; |
| 15 | /** |
| 16 | * Class to handle speculation rules for cornerstone pages |
| 17 | */ |
| 18 | class Speculation_Rules implements Feature, Changes_Output_On_Activation, Optimization { |
| 19 | |
| 20 | /** |
| 21 | * Get the slug for this module. |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | public static function get_slug() { |
| 26 | return 'speculation_rules'; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Check if the feature is available |
| 31 | * |
| 32 | * @return bool |
| 33 | */ |
| 34 | public static function is_available() { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Initialize the speculation rules |
| 40 | * |
| 41 | * @since 3.13.0 |
| 42 | * @return void |
| 43 | */ |
| 44 | public function setup() { |
| 45 | // Use WP core action to add speculation rules |
| 46 | add_action( 'wp_load_speculation_rules', array( $this, 'add_cornerstone_rules' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Add speculation rules for cornerstone pages |
| 51 | * |
| 52 | * @param \WP_Speculation_Rules $speculation_rules The speculation rules instance. |
| 53 | * @since 3.13.0 |
| 54 | * @return void |
| 55 | */ |
| 56 | public function add_cornerstone_rules( $speculation_rules ) { |
| 57 | $cornerstone_urls = $this->get_cornerstone_urls(); |
| 58 | if ( empty( $cornerstone_urls ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // remove the protocol and domain from the list of cornerstone urls |
| 63 | $home_url = wp_parse_url( home_url() ); |
| 64 | $domain = $home_url['host']; |
| 65 | $protocol = $home_url['scheme']; |
| 66 | $cornerstone_urls = array_map( |
| 67 | function ( $url ) use ( $protocol, $domain ) { |
| 68 | return trailingslashit( str_replace( $protocol . '://' . $domain, '', $url ) ); |
| 69 | }, |
| 70 | $cornerstone_urls |
| 71 | ); |
| 72 | |
| 73 | // Add prerender rule for cornerstone pages with moderate eagerness |
| 74 | $speculation_rules->add_rule( |
| 75 | 'prerender', |
| 76 | 'cornerstone-pages-prerender', |
| 77 | array( |
| 78 | 'source' => 'document', |
| 79 | 'where' => array( |
| 80 | 'href_matches' => $cornerstone_urls, |
| 81 | ), |
| 82 | 'eagerness' => 'moderate', |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the list of cornerstone page URLs |
| 89 | * |
| 90 | * @since 3.13.0 |
| 91 | * @return array Array of cornerstone page URLs |
| 92 | */ |
| 93 | private function get_cornerstone_urls() { |
| 94 | $cornerstone_urls = Cornerstone_Utils::get_list(); |
| 95 | if ( empty( $cornerstone_urls ) ) { |
| 96 | return array(); |
| 97 | } |
| 98 | |
| 99 | return $cornerstone_urls; |
| 100 | } |
| 101 | } |