Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Cornerstone_Provider | |
0.00% |
0 / 31 |
|
0.00% |
0 / 9 |
210 | |
0.00% |
0 / 1 |
| get_critical_source_urls | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_provider_key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_current_storage_keys | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_request_url | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_keys | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_hash_for_url | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| describe_key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_edit_url | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| get_success_ratio | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Provider for the cornerstone pages |
| 4 | * |
| 5 | * @package automattic/jetpack-boost |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers; |
| 9 | |
| 10 | use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils; |
| 11 | |
| 12 | /** |
| 13 | * Class Cornerstone_Provider |
| 14 | * |
| 15 | * @package Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Providers |
| 16 | */ |
| 17 | class Cornerstone_Provider extends Provider { |
| 18 | |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | protected static $name = 'cornerstone'; |
| 23 | |
| 24 | /** |
| 25 | * Get the providers for cornerstone pages. |
| 26 | * |
| 27 | * @param array $_context_posts Context posts, not used. Cornerstone pages are always available. |
| 28 | * @return array |
| 29 | */ |
| 30 | public static function get_critical_source_urls( $_context_posts = array() ) { |
| 31 | $urls = Cornerstone_Utils::get_list(); |
| 32 | |
| 33 | $groups = array(); |
| 34 | foreach ( $urls as $url ) { |
| 35 | $groups[ self::get_hash_for_url( $url ) ] = array( $url ); |
| 36 | } |
| 37 | |
| 38 | return $groups; |
| 39 | } |
| 40 | |
| 41 | public static function get_provider_key( $url ) { |
| 42 | return self::$name . '_' . self::get_hash_for_url( $url ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the current storage keys for cornerstone pages. |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | public static function get_current_storage_keys() { |
| 51 | $current_url = self::get_request_url(); |
| 52 | return array( self::get_provider_key( $current_url ) ); |
| 53 | } |
| 54 | |
| 55 | public static function get_request_url() { |
| 56 | global $wp; |
| 57 | |
| 58 | // If pretty parmalinks are enabled, use the request. Otherwise, use the query vars. |
| 59 | if ( get_option( 'permalink_structure' ) ) { |
| 60 | return home_url( $wp->request ); |
| 61 | } |
| 62 | |
| 63 | return add_query_arg( $wp->query_vars, home_url() ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the keys for cornerstone pages. |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | public static function get_keys() { |
| 72 | $urls = Cornerstone_Utils::get_list(); |
| 73 | |
| 74 | return array_map( array( __CLASS__, 'get_hash_for_url' ), $urls ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @inheritdoc |
| 79 | */ |
| 80 | public static function get_hash_for_url( $url ) { |
| 81 | // Remove the home_url from the beginning of the URL. |
| 82 | $home_url = home_url(); |
| 83 | if ( stripos( $url, $home_url ) === 0 ) { |
| 84 | $url = substr( $url, strlen( $home_url ) ); |
| 85 | } |
| 86 | |
| 87 | $url = ltrim( $url, '/' ); |
| 88 | $url = untrailingslashit( $url ); |
| 89 | |
| 90 | $hash = hash( 'md5', $url ); |
| 91 | |
| 92 | return substr( $hash, 0, 8 ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @inheritdoc |
| 97 | */ |
| 98 | public static function describe_key( $_key ) { |
| 99 | return __( 'Cornerstone page', 'jetpack-boost' ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @inheritdoc |
| 104 | */ |
| 105 | public static function get_edit_url( $key ) { |
| 106 | $hash = substr( $key, strlen( self::$name ) + 1 ); |
| 107 | |
| 108 | $source_urls = self::get_critical_source_urls(); |
| 109 | |
| 110 | if ( ! isset( $source_urls[ $hash ] ) ) { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | $post_id = url_to_postid( $source_urls[ $hash ][0] ); |
| 115 | |
| 116 | if ( ! $post_id ) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | return get_edit_post_link( $post_id ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @inheritdoc |
| 125 | */ |
| 126 | public static function get_success_ratio() { |
| 127 | return 1; |
| 128 | } |
| 129 | } |