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 notice when updating a page from the foundation pages list #39876

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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { isFatalError } from '../lib/critical-css-errors';
export default function CriticalCssMeta() {
const [ cssState ] = useCriticalCssState();
const [ hasRetried, retry ] = useRetryRegenerate();
const [ regenerateReason ] = useRegenerationReason();
const [ { data: regenerateReason } ] = useRegenerationReason();
const { progress } = useLocalCriticalCssGenerator();
const showFatalError = isFatalError( cssState );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ const allowedSuggestions = [
'post_saved',
'switched_theme',
'plugin_change',
'foundation_page_saved',
'foundation_pages_list_updated',
] as const;

export type RegenerationReason = ( typeof allowedSuggestions )[ number ];
export type RegenerationReason = ( typeof allowedSuggestions )[ number ] | null;

/**
* Hook to get the reason why (if any) we should recommend users regenerate their Critical CSS.
*/
export function useRegenerationReason(): [ RegenerationReason | null, () => void ] {
const [ { data }, { mutate } ] = useDataSync(
export function useRegenerationReason(): [
{ data: RegenerationReason; refetch: () => void },
() => void,
] {
const [ { data, refetch }, { mutate } ] = useDataSync(
'jetpack_boost_ds',
'critical_css_suggest_regenerate',
z.enum( allowedSuggestions ).nullable()
Expand All @@ -25,5 +30,5 @@ export function useRegenerationReason(): [ RegenerationReason | null, () => void
mutate( null );
}

return [ data || null, reset ];
return [ { data: data || null, refetch }, reset ];
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const suggestionMap: { [ key: string ]: string } = {
"We noticed you've recently updated a plugin that may affect your site's HTML/CSS structure.",
'jetpack-boost'
),
foundation_page_saved: __( 'A Foundation page was updated.', 'jetpack-boost' ),
foundation_pages_list_updated: __( 'The list of Foundation pages was updated.', 'jetpack-boost' ),
};

const getSuggestionMessage = ( type: RegenerationReason | null ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { z } from 'zod';
/**
* Hook to get the Foundation Pages.
*/
export function useFoundationPages(): [ string[], ( newValue: string[] ) => void ] {
export function useFoundationPages(): [
string[],
( newValue: string[], onSuccessCallback?: () => void ) => void,
] {
const [ { data }, { mutate } ] = useDataSync(
'jetpack_boost_ds',
'foundation_pages_list',
z.array( z.string() )
);

function updatePages( newValue: string[] ) {
mutate( newValue );
function updatePages( newValue: string[], onSuccessCallback?: () => void ) {
mutate( newValue, {
onSuccess: onSuccessCallback,
} );
}

return [ data || [], updatePages ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import { useFoundationPages, useFoundationPagesProperties } from '../lib/stores/
import { createInterpolateElement } from '@wordpress/element';
import { recordBoostEvent } from '$lib/utils/analytics';
import getSupportLink from '$lib/utils/get-support-link';
import { useRegenerationReason } from '$features/critical-css/lib/stores/suggest-regenerate';

const Meta = () => {
const [ isExpanded, setIsExpanded ] = useState( false );
const [ foundationPages, setFoundationPages ] = useFoundationPages();
const foundationPagesProperties = useFoundationPagesProperties();
const [ { refetch: refetchRegenerationReason } ] = useRegenerationReason();

const updateFoundationPages = ( newValue: string ) => {
const newItems = newValue.split( '\n' ).map( line => line.trim() );

setFoundationPages( newItems );
setFoundationPages( newItems, () => {
refetchRegenerationReason();
} );
};

let content = null;
Expand All @@ -33,7 +37,10 @@ const Meta = () => {
foundationPagesProperties.blog_url &&
sprintf(
/* translators: %s is the blog URL. */
__( 'No need to include the blog URL (%s), it is already included.', 'jetpack-boost' ),
__(
'No need to add the blog URL (%s) to the list, it is automatically kept up to date.',
'jetpack-boost'
),
foundationPagesProperties.blog_url
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get;
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Set;
use Automattic\Jetpack_Boost\Lib\Environment_Change_Detector;

class Foundation_Pages_Entry implements Entry_Can_Get, Entry_Can_Set {

Expand All @@ -26,7 +27,10 @@ public function get( $fallback_value = false ) {
public function set( $value ) {
$value = $this->sanitize_value( $value );

update_option( $this->option_key, $value );
$updated = update_option( $this->option_key, $value );
if ( $updated ) {
( new Environment_Change_Detector() )->handle_foundation_pages_list_update();
}
}

private function sanitize_value( $value ) {
Expand Down
49 changes: 37 additions & 12 deletions projects/plugins/boost/app/lib/Environment_Change_Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
*/
class Environment_Change_Detector {

const ENV_CHANGE_LEGACY = '1';
const ENV_CHANGE_PAGE_SAVED = 'page_saved';
const ENV_CHANGE_POST_SAVED = 'post_saved';
const ENV_CHANGE_SWITCHED_THEME = 'switched_theme';
const ENV_CHANGE_PLUGIN_CHANGE = 'plugin_change';
const ENV_CHANGE_LEGACY = '1';
const ENV_CHANGE_PAGE_SAVED = 'page_saved';
const ENV_CHANGE_POST_SAVED = 'post_saved';
const ENV_CHANGE_SWITCHED_THEME = 'switched_theme';
const ENV_CHANGE_PLUGIN_CHANGE = 'plugin_change';
const ENV_CHANGE_FOUNDATION_PAGE_SAVED = 'foundation_page_saved';
const ENV_CHANGE_FOUNDATION_PAGES_LIST_UPDATED = 'foundation_pages_list_updated';

/**
* Initialize the change detection hooks.
Expand Down Expand Up @@ -46,13 +48,7 @@ public function handle_post_change( $post_id, $post ) {
return;
}

if ( 'page' === $post->post_type ) {
$change_type = $this::ENV_CHANGE_PAGE_SAVED;
} else {
$change_type = $this::ENV_CHANGE_POST_SAVED;
}

$this->do_action( false, $change_type );
$this->do_action( false, $this->get_post_change_type( $post ) );
}

public function handle_theme_change() {
Expand All @@ -63,6 +59,10 @@ public function handle_plugin_change() {
$this->do_action( false, $this::ENV_CHANGE_PLUGIN_CHANGE );
}

public function handle_foundation_pages_list_update() {
$this->do_action( false, $this::ENV_CHANGE_FOUNDATION_PAGES_LIST_UPDATED );
}

/**
* Fire the environment change action.
*
Expand Down Expand Up @@ -99,4 +99,29 @@ private function is_post_type_invalidating( $post_type ) {
return true;
}
}

/**
* Get the type of change for a specific post.
*
* @param \WP_Post $post The post object.
* @return string The change type.
*/
private function get_post_change_type( $post ) {
$foundation_pages = ( new Foundation_Pages() )->get_pages();
if ( $foundation_pages ) {
$foundation_pages_sanitized = array_map( 'untrailingslashit', $foundation_pages );
$current_url = untrailingslashit( get_permalink( $post ) );
if ( in_array( $current_url, $foundation_pages_sanitized, true ) ) {
return $this::ENV_CHANGE_FOUNDATION_PAGE_SAVED;
}
}

if ( 'page' === $post->post_type ) {
$change_type = $this::ENV_CHANGE_PAGE_SAVED;
} else {
$change_type = $this::ENV_CHANGE_POST_SAVED;
}

return $change_type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public static function get_critical_source_urls( $context_posts = array() ) {
$urls['posts_page'] = array( $permalink );
}
}
} elseif ( ! $front_page ) {
}

if ( ! $front_page && ! isset( $urls['posts_page'] ) ) {
$urls['posts_page'] = array( home_url( '/' ) );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Add a notice prompting critical css regen after updating foundation pages list.


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Add notice to prompt user to do critical css regeneration when they update a foundation page.


2 changes: 2 additions & 0 deletions projects/plugins/boost/wp-js-data-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ function jetpack_boost_initialize_datasync() {
'post_saved',
'switched_theme',
'plugin_change',
'foundation_page_saved',
'foundation_pages_list_updated',
)
)->nullable();

Expand Down
Loading