Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boost: Add default URLs for foundation pages #39884

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion projects/plugins/boost/app/lib/Foundation_Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@

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;
}

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' ) );
Comment on lines +24 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I really like seeing this here.

}

public function remove_ccss_front_page_provider( $providers ) {
Expand All @@ -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,
)
);
Comment on lines +60 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked if Yoast has a way to do this for us, but sadly it doesn't ):


$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();
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 0 additions & 6 deletions projects/plugins/boost/wp-js-data-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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' ) );
Loading