-
Notifications
You must be signed in to change notification settings - Fork 800
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66c11b3
Add default values
haqadn 7aaf631
Merge branch 'add/foundation-pages' into fp/default-urls
dilirity ec3945d
Make sure URLs are valid before adding them to the list
dilirity ca71f08
Use global function reference instead of namespaced one
dilirity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ) ); | ||
} | ||
|
||
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, | ||
) | ||
); | ||
Comment on lines
+60
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.