diff --git a/projects/plugins/boost/app/lib/Foundation_Pages.php b/projects/plugins/boost/app/lib/Foundation_Pages.php index 5eb7c01e880c6..491999f47d0d6 100644 --- a/projects/plugins/boost/app/lib/Foundation_Pages.php +++ b/projects/plugins/boost/app/lib/Foundation_Pages.php @@ -2,10 +2,15 @@ namespace Automattic\Jetpack_Boost\Lib; +use Automattic\Jetpack\Schema\Schema; use Automattic\Jetpack_Boost\Contracts\Has_Setup; +use Automattic\Jetpack_Boost\Data_Sync\Foundation_Pages_Entry; class Foundation_Pages implements Has_Setup { + const PREMIUM_MAX_PAGES = 10; + const FREE_MAX_PAGES = 1; + public function setup() { if ( ! $this->is_development_features_enabled() ) { return; @@ -13,6 +18,13 @@ public function setup() { add_filter( 'jetpack_boost_critical_css_providers', array( $this, 'remove_ccss_front_page_provider' ), 10, 2 ); add_filter( 'display_post_states', array( $this, 'add_display_post_states' ), 10, 2 ); + add_action( 'init', array( $this, 'register_ds_stores' ) ); + } + + public function register_ds_stores() { + $schema = Schema::as_array( Schema::as_string() )->fallback( self::default_pages() ); + jetpack_boost_register_option( 'foundation_pages_list', $schema, new Foundation_Pages_Entry( 'foundation_pages_list' ) ); + jetpack_boost_register_readonly_option( 'foundation_pages_properties', array( $this, 'get_properties' ) ); } public function remove_ccss_front_page_provider( $providers ) { @@ -27,6 +39,74 @@ public function remove_ccss_front_page_provider( $providers ) { return $filtered_providers; } + private function default_pages() { + if ( $this->get_max_pages() === static::FREE_MAX_PAGES ) { + return array( '/' ); + } + + $max_pages = $this->get_max_pages(); + $yoast_cornerstone_pages = $this->get_yoast_cornerstone_pages(); + $woocommerce_pages = $this->get_woocommerce_pages(); + + $homepage = array( '/' ); + + $urls = array_unique( array_merge( $homepage, $woocommerce_pages, $yoast_cornerstone_pages ) ); + + return array_slice( $urls, 0, $max_pages ); + } + + private function get_yoast_cornerstone_pages() { + $max_pages = $this->get_max_pages(); + $yoast_cornerstone_content = get_posts( + array( + 'meta_key' => '_yoast_wpseo_is_cornerstone', + 'meta_value' => '1', + 'post_type' => 'any', + 'posts_per_page' => $max_pages, + ) + ); + + $urls = array(); + foreach ( $yoast_cornerstone_content as $post ) { + $permalink = get_permalink( $post->ID ); + if ( $permalink ) { + $relative_permalink = $this->make_relative_url( $permalink ); + $urls[] = $relative_permalink; + } + } + + return $urls; + } + + private function get_woocommerce_pages() { + $urls = array(); + if ( ! function_exists( 'wc_get_page_id' ) ) { + return $urls; + } + + $shop_page_id = \wc_get_page_id( 'shop' ); + if ( $shop_page_id ) { + $url = get_permalink( $shop_page_id ); + if ( $url ) { + $relative_url = $this->make_relative_url( $url ); + + if ( $relative_url ) { + $urls[] = $relative_url; + } + } + } + + return $urls; + } + + private function make_relative_url( $url ) { + if ( is_string( $url ) && strpos( $url, home_url() ) === 0 ) { + $url = substr( $url, strlen( home_url() ) ); + } + + return $url; + } + public function get_pages() { if ( ! $this->is_development_features_enabled() ) { return array(); @@ -56,7 +136,7 @@ public function add_display_post_states( $post_states, $post ) { } private function get_max_pages() { - return Premium_Features::has_any() ? 10 : 1; + return Premium_Features::has_any() ? static::PREMIUM_MAX_PAGES : static::FREE_MAX_PAGES; } private function is_development_features_enabled() { diff --git a/projects/plugins/boost/wp-js-data-sync.php b/projects/plugins/boost/wp-js-data-sync.php index 5cf6c09bcc402..6c5f24c0ad3d7 100644 --- a/projects/plugins/boost/wp-js-data-sync.php +++ b/projects/plugins/boost/wp-js-data-sync.php @@ -7,7 +7,6 @@ use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync; use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync_Readonly; use Automattic\Jetpack_Boost\Data_Sync\Critical_CSS_Meta_Entry; -use Automattic\Jetpack_Boost\Data_Sync\Foundation_Pages_Entry; use Automattic\Jetpack_Boost\Data_Sync\Getting_Started_Entry; use Automattic\Jetpack_Boost\Data_Sync\Mergeable_Array_Entry; use Automattic\Jetpack_Boost\Data_Sync\Minify_Excludes_State_Entry; @@ -17,7 +16,6 @@ use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Set_Provider_CSS; use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Set_Provider_Error_Dismissed; use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Set_Provider_Errors; -use Automattic\Jetpack_Boost\Lib\Foundation_Pages; use Automattic\Jetpack_Boost\Lib\My_Jetpack; use Automattic\Jetpack_Boost\Lib\Premium_Features; use Automattic\Jetpack_Boost\Lib\Premium_Pricing; @@ -387,7 +385,3 @@ function jetpack_boost_initialize_datasync() { jetpack_boost_register_action( 'page_cache', 'deactivate-wpsc', Schema::as_void(), new Deactivate_WPSC() ); jetpack_boost_register_option( 'image_cdn_liar', Schema::as_boolean()->fallback( false ), new Status( Liar::get_slug() ) ); - -jetpack_boost_register_option( 'foundation_pages_list', Schema::as_array( Schema::as_string() )->fallback( array( '/' ) ), new Foundation_Pages_Entry( 'foundation_pages_list' ) ); - -jetpack_boost_register_readonly_option( 'foundation_pages_properties', array( new Foundation_Pages(), 'get_properties' ) );