[Launch Your Store] Dynamically create Coming Soon page content (#46101)

This commit is contained in:
Paul Sealock 2024-04-05 14:56:39 +13:00 committed by GitHub
parent 34b48892e0
commit 04aab95452
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 115 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Comment: Update unreleased feature

View File

@ -37,8 +37,11 @@ class LaunchYourStore {
'woocommerce_private_link' => array( 'yes', 'no' ),
);
$at_least_one_saved = false;
if ( isset( $_POST['woocommerce_store_pages_only'] ) ) {
$this->possibly_update_coming_soon_page_content( wc_clean( wp_unslash( $_POST['woocommerce_store_pages_only'] ) ) );
}
$at_least_one_saved = false;
foreach ( $options as $name => $option ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $_POST[ $name ] ) && in_array( $_POST[ $name ], $option, true ) ) {
@ -53,6 +56,108 @@ class LaunchYourStore {
}
}
/**
* Update the contents of the coming soon page on settings change. Do not update if the post request
* doesn't change the store pages only setting, if the setting is unchanged, or if the page has been edited.
*
* @param string $next_store_pages_only The next store pages only setting.
* @return void
*/
public function possibly_update_coming_soon_page_content( $next_store_pages_only ) {
$option_name = 'woocommerce_store_pages_only';
$current_store_pages_only = get_option( $option_name, null );
// If the current and next store pages only values are the same, return.
if ( $current_store_pages_only && $current_store_pages_only === $next_store_pages_only ) {
return;
}
$page_id = get_option( 'woocommerce_coming_soon_page_id' );
$page = get_post( $page_id );
$original_page_content = 'yes' === $current_store_pages_only
? $this->get_store_only_coming_soon_content()
: $this->get_entire_site_coming_soon_content();
// If the page exists and the content is not the same as the original content, its been edited from its original state. Return early to respect any changes.
if ( $page && $page->post_content !== $original_page_content ) {
return;
}
if ( $page_id ) {
$next_page_content = 'yes' === $next_store_pages_only
? $this->get_store_only_coming_soon_content()
: $this->get_entire_site_coming_soon_content();
wp_update_post(
array(
'ID' => $page_id,
'post_content' => $next_page_content,
)
);
}
}
/**
* Create a pattern for the store only coming soon page.
*
* @return string
*/
public function get_store_only_coming_soon_content() {
$heading = __( 'Great things coming soon', 'woocommerce' );
$subheading = __( 'Something big is brewing! Our store is in the works - Launching shortly!', 'woocommerce' );
return sprintf(
'<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->
<!-- wp:heading {"textAlign":"center","level":1} -->
<h1 class="wp-block-heading has-text-align-center">%s</h1>
<!-- /wp:heading -->
<!-- wp:spacer {"height":"10px"} -->
<div style="height:10px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->
<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center">%s</p>
<!-- /wp:paragraph -->
<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer --></div>
<!-- /wp:group -->',
$heading,
$subheading
);
}
/**
* Create a pattern for the entire site coming soon page.
*
* @return string
*/
public function get_entire_site_coming_soon_content() {
$heading = __( 'Pardon our dust! We\'re working on something amazing -- check back soon!', 'woocommerce' );
return sprintf(
'<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->
<!-- wp:heading {"textAlign":"center","level":1} -->
<h1 class="wp-block-heading has-text-align-center">%s</h1>
<!-- /wp:heading -->
<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer --></div>
<!-- /wp:group -->',
$heading
);
}
/**
* Add `coming soon` page when it hasn't been created yet.
*
@ -66,12 +171,15 @@ class LaunchYourStore {
$is_home = isset( $current_page['id'] ) && 'woocommerce-home' === $current_page['id'];
$page_id_option = get_option( $option_name, false );
if ( $current_screen && 'woocommerce_page_wc-admin' === $current_screen->id && $is_home && ! $page_id_option ) {
wc_create_page(
$store_pages_only = 'yes' === get_option( 'woocommerce_store_pages_only', 'no' );
$page_id = wc_create_page(
esc_sql( _x( 'Coming Soon', 'Page slug', 'woocommerce' ) ),
$option_name,
_x( 'Coming Soon', 'Page title', 'woocommerce' ),
'tbd',
$store_pages_only ? $this->get_store_only_coming_soon_content() : $this->get_entire_site_coming_soon_content(),
);
// Make sure the page uses the no-title template. This only works for Twenty twenty-four and is temporary.
update_post_meta( $page_id, '_wp_page_template', 'page-no-title' );
// wc_create_page doesn't create options with autoload = yes.
// Since we'll querying the option on WooCommerce home,
// we should update the option to set autoload to yes.