Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.71% |
44 / 68 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Cornerstone_Pages | |
64.71% |
44 / 68 |
|
63.64% |
7 / 11 |
55.72 | |
0.00% |
0 / 1 |
| setup | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| set_default_pages | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| register_ds_stores | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| remove_ccss_front_page_provider | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| default_pages | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| get_yoast_cornerstone_pages | |
16.67% |
3 / 18 |
|
0.00% |
0 / 1 |
13.26 | |||
| get_woocommerce_pages | |
54.55% |
6 / 11 |
|
0.00% |
0 / 1 |
7.35 | |||
| make_relative_url | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| get_properties | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| add_display_post_states | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get_max_pages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib\Cornerstone; |
| 4 | |
| 5 | use Automattic\Jetpack\Schema\Schema; |
| 6 | use Automattic\Jetpack_Boost\Contracts\Has_Setup; |
| 7 | use Automattic\Jetpack_Boost\Data_Sync\Cornerstone_Pages_Entry; |
| 8 | use Automattic\Jetpack_Boost\Lib\Premium_Features; |
| 9 | class Cornerstone_Pages implements Has_Setup { |
| 10 | |
| 11 | const PREMIUM_MAX_PAGES = 10; |
| 12 | const FREE_MAX_PAGES = 1; |
| 13 | |
| 14 | public function setup() { |
| 15 | $this->register_ds_stores(); |
| 16 | |
| 17 | add_filter( 'jetpack_boost_critical_css_providers', array( $this, 'remove_ccss_front_page_provider' ), 10, 2 ); |
| 18 | add_filter( 'display_post_states', array( $this, 'add_display_post_states' ), 10, 2 ); |
| 19 | add_action( 'init', array( $this, 'set_default_pages' ), 0 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Set the default pages for the first time Boost is running on the website. |
| 24 | */ |
| 25 | public function set_default_pages() { |
| 26 | // Since the DS store always returns an empty array, we can't know if the user |
| 27 | // wants an empty list of pages or this is the first time Boost is running on the website |
| 28 | // and we need to actually load the default pages. |
| 29 | $raw_pages = \get_option( 'jetpack_boost_ds_cornerstone_pages_list' ); |
| 30 | if ( $raw_pages === false ) { |
| 31 | jetpack_boost_ds_set( 'cornerstone_pages_list', $this->default_pages() ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private function register_ds_stores() { |
| 36 | $schema = Schema::as_array( Schema::as_string() )->fallback( array() ); |
| 37 | jetpack_boost_register_option( 'cornerstone_pages_list', $schema, new Cornerstone_Pages_Entry( 'cornerstone_pages_list' ) ); |
| 38 | jetpack_boost_register_readonly_option( 'cornerstone_pages_properties', array( $this, 'get_properties' ) ); |
| 39 | } |
| 40 | |
| 41 | public function remove_ccss_front_page_provider( $providers ) { |
| 42 | $filtered_providers = array(); |
| 43 | |
| 44 | foreach ( $providers as $provider ) { |
| 45 | if ( $provider['key'] !== 'core_front_page' ) { |
| 46 | $filtered_providers[] = $provider; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return $filtered_providers; |
| 51 | } |
| 52 | |
| 53 | private function default_pages() { |
| 54 | $max_pages = $this->get_max_pages(); |
| 55 | $yoast_cornerstone_pages = $this->get_yoast_cornerstone_pages(); |
| 56 | $woocommerce_pages = $this->get_woocommerce_pages(); |
| 57 | |
| 58 | $urls = array_unique( array_merge( $woocommerce_pages, $yoast_cornerstone_pages ) ); |
| 59 | $urls = array_map( 'untrailingslashit', $urls ); |
| 60 | |
| 61 | // An empty string represents the home page. |
| 62 | // Remove it if it's in the list since the home page is a predefined page. |
| 63 | $urls = array_filter( $urls ); |
| 64 | |
| 65 | return array_slice( $urls, 0, $max_pages ); |
| 66 | } |
| 67 | |
| 68 | private function get_yoast_cornerstone_pages() { |
| 69 | $urls = array(); |
| 70 | if ( ! function_exists( 'wpseo_init' ) ) { |
| 71 | return $urls; |
| 72 | } |
| 73 | |
| 74 | $max_pages = $this->get_max_pages(); |
| 75 | $yoast_cornerstone_content = get_posts( |
| 76 | array( |
| 77 | 'meta_key' => '_yoast_wpseo_is_cornerstone', |
| 78 | 'meta_value' => '1', |
| 79 | 'post_type' => 'any', |
| 80 | 'posts_per_page' => $max_pages, |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | foreach ( $yoast_cornerstone_content as $post ) { |
| 85 | $permalink = get_permalink( $post->ID ); |
| 86 | if ( $permalink ) { |
| 87 | $relative_permalink = $this->make_relative_url( $permalink ); |
| 88 | $urls[] = $relative_permalink; |
| 89 | } |
| 90 | } |
| 91 | return $urls; |
| 92 | } |
| 93 | |
| 94 | private function get_woocommerce_pages() { |
| 95 | $urls = array(); |
| 96 | if ( ! function_exists( 'wc_get_page_id' ) ) { |
| 97 | return $urls; |
| 98 | } |
| 99 | |
| 100 | $shop_page_id = \wc_get_page_id( 'shop' ); |
| 101 | if ( $shop_page_id ) { |
| 102 | $url = get_permalink( $shop_page_id ); |
| 103 | if ( $url ) { |
| 104 | $relative_url = $this->make_relative_url( $url ); |
| 105 | |
| 106 | if ( $relative_url ) { |
| 107 | $urls[] = $relative_url; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $urls; |
| 113 | } |
| 114 | |
| 115 | private function make_relative_url( $url ) { |
| 116 | if ( is_string( $url ) && strpos( $url, home_url() ) === 0 ) { |
| 117 | $url = substr( $url, strlen( home_url() ) ); |
| 118 | } |
| 119 | |
| 120 | return $url; |
| 121 | } |
| 122 | |
| 123 | public function get_properties() { |
| 124 | $properties = array( |
| 125 | 'max_pages' => $this->get_max_pages(), |
| 126 | 'max_pages_premium' => static::PREMIUM_MAX_PAGES, |
| 127 | 'default_pages' => array(), |
| 128 | 'predefined_pages' => Cornerstone_Utils::get_predefined_list(), |
| 129 | ); |
| 130 | |
| 131 | // We need this to ensure we don't include the home page in the default pages since an empty array is returned when no default pages are found. |
| 132 | $default_pages = $this->default_pages(); |
| 133 | |
| 134 | if ( ! empty( $default_pages ) ) { |
| 135 | $properties['default_pages'] = array_map( 'home_url', $default_pages ); |
| 136 | } |
| 137 | return $properties; |
| 138 | } |
| 139 | |
| 140 | public function add_display_post_states( $post_states, $post ) { |
| 141 | if ( Cornerstone_Utils::is_cornerstone_page( $post->ID ) ) { |
| 142 | $post_states[] = __( 'Cornerstone Page', 'jetpack-boost' ); |
| 143 | } |
| 144 | |
| 145 | return $post_states; |
| 146 | } |
| 147 | |
| 148 | private function get_max_pages() { |
| 149 | return Premium_Features::has_feature( Premium_Features::CORNERSTONE_TEN_PAGES ) ? static::PREMIUM_MAX_PAGES : static::FREE_MAX_PAGES; |
| 150 | } |
| 151 | } |