Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.36% |
19 / 22 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Cornerstone_Pages_Entry | |
86.36% |
19 / 22 |
|
66.67% |
4 / 6 |
12.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| set | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| sanitize_value | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| transform_to_relative | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| transform_to_absolute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Data_Sync; |
| 4 | |
| 5 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get; |
| 6 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Set; |
| 7 | use Automattic\Jetpack_Boost\Lib\Environment_Change_Detector; |
| 8 | |
| 9 | class Cornerstone_Pages_Entry implements Entry_Can_Get, Entry_Can_Set { |
| 10 | |
| 11 | private $option_key; |
| 12 | |
| 13 | public function __construct( $option_key ) { |
| 14 | $this->option_key = 'jetpack_boost_ds_' . $option_key; |
| 15 | } |
| 16 | |
| 17 | public function get( $fallback_value = array() ) { |
| 18 | $urls = get_option( $this->option_key, array() ); |
| 19 | |
| 20 | if ( empty( $urls ) && ! empty( $fallback_value ) ) { |
| 21 | $urls = $fallback_value; |
| 22 | $this->set( $urls ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Filters the list of cornerstone pages. This list only includes the custom pages. |
| 27 | * If you want the full list, use `jetpack_boost_cornerstone_pages_list_complete` instead. |
| 28 | * |
| 29 | * @since 3.7.0 |
| 30 | * |
| 31 | * @param array $urls An array of absolute URLs. |
| 32 | */ |
| 33 | return apply_filters( 'jetpack_boost_cornerstone_pages_list', array_map( array( $this, 'transform_to_absolute' ), $urls ) ); |
| 34 | } |
| 35 | |
| 36 | public function set( $value ) { |
| 37 | $value = $this->sanitize_value( $value ); |
| 38 | |
| 39 | $updated = update_option( $this->option_key, $value ); |
| 40 | if ( $updated ) { |
| 41 | ( new Environment_Change_Detector() )->handle_cornerstone_pages_list_update(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private function sanitize_value( $value ) { |
| 46 | if ( is_array( $value ) ) { |
| 47 | $value = array_values( array_unique( array_map( 'untrailingslashit', array_map( array( $this, 'transform_to_relative' ), $value ) ) ) ); |
| 48 | } else { |
| 49 | $value = array(); |
| 50 | } |
| 51 | |
| 52 | return $value; |
| 53 | } |
| 54 | |
| 55 | private function transform_to_relative( $url ) { |
| 56 | $url = trim( $url ); |
| 57 | |
| 58 | // Remove the home_url from the beginning of the URL if it exists. |
| 59 | if ( strpos( $url, home_url() ) === 0 ) { |
| 60 | $url = substr( $url, strlen( home_url() ) ); |
| 61 | } |
| 62 | |
| 63 | // Ensure the URL starts with a slash. |
| 64 | if ( $url !== '' ) { |
| 65 | $url = ltrim( $url, '/' ); |
| 66 | $url = '/' . $url; |
| 67 | } |
| 68 | |
| 69 | return $url; |
| 70 | } |
| 71 | |
| 72 | private function transform_to_absolute( $url ) { |
| 73 | return home_url( $url ); |
| 74 | } |
| 75 | } |