Skip to content

Commit

Permalink
Boost: Update foundation page list instructions (#39885)
Browse files Browse the repository at this point in the history
  • Loading branch information
haqadn authored Oct 29, 2024
1 parent bc0fd52 commit a420112
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function useFoundationPages(): [

const FoundationPagesProperties = z.object( {
max_pages: z.number(),
blog_url: z.string().nullable(),
} );
type FoundationPagesProperties = z.infer< typeof FoundationPagesProperties >;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ const Meta = () => {
items={ foundationPages.join( '\n' ) }
setItems={ updateFoundationPages }
maxItems={ foundationPagesProperties.max_pages }
description={
foundationPagesProperties.blog_url &&
description={ createInterpolateElement(
sprintf(
/* translators: %s is the blog URL. */
/* translators: %s is the site URL. */
__(
'No need to add the blog URL (%s) to the list, it is automatically kept up to date.',
'Add one URL per line. Only URLs starting with <b>%s</b> will be included. Relative URLs are automatically expanded.',
'jetpack-boost'
),
foundationPagesProperties.blog_url
)
}
Jetpack_Boost.site.url
),
{
b: <b />,
}
) }
/>
);
} else {
Expand Down Expand Up @@ -119,12 +121,20 @@ const List: React.FC< ListProps > = ( { items, setItems, maxItems, description }
const [ inputValue, setInputValue ] = useState( items );
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [ inputInvalid, setInputInvalid ] = useState( false );
const [ validationError, setValidationError ] = useState< Error | null >( null );

const inputRows = Math.min( maxItems, 10 );

const validateInputValue = ( value: string ) => {
setInputValue( value );
setInputInvalid( ! validateItems( value ) );
try {
const isValid = validateItems( value );
setInputInvalid( ! isValid );
setValidationError( null );
} catch ( e ) {
setInputInvalid( true );
setValidationError( e as Error );
}
};

const validateItems = ( value: string ) => {
Expand All @@ -133,14 +143,37 @@ const List: React.FC< ListProps > = ( { items, setItems, maxItems, description }
.map( line => line.trim() )
.filter( line => line.trim() !== '' );

// There should always be at least one foundation page.
if ( lines.length === 0 ) {
return false;
}

// Check if the number of items exceeds maxItems
if ( lines.length > maxItems ) {
return false;
const message = sprintf(
/* translators: %d is the maximum number of foundation page URLs. */
_n(
'You can add only %d foundation page URL.',
'You can add up to %d foundation page URLs.',
maxItems,
'jetpack-boost'
),
maxItems
);
throw new Error( message );
}

for ( const line of lines ) {
let url: URL | undefined;
try {
url = new URL( line );
} catch ( e ) {
// If the URL is invalid, they have provided a relative URL, which we will allow.
}
if (
url &&
url.origin.replace( /\/$/, '' ) !== Jetpack_Boost.site.url.replace( /\/$/, '' )
) {
throw new Error(
/* translators: %s is the URL that didn't match the site URL */
sprintf( __( 'The URL seems to be a different site: %s', 'jetpack-boost' ), line )
);
}
}

return true;
Expand All @@ -166,20 +199,7 @@ const List: React.FC< ListProps > = ( { items, setItems, maxItems, description }
onChange={ e => validateInputValue( e.target.value ) }
id="jb-foundation-pages"
/>
{ inputInvalid && (
<p className={ styles.error }>
{ sprintf(
/* translators: %d is the maximum number of foundation page URLs. */
_n(
'You must provide %d foundation page URL.',
'You must provide between 1 and %d foundation page URLs.',
maxItems,
'jetpack-boost'
),
maxItems
) }
</p>
) }
{ inputInvalid && <p className={ styles.error }>{ validationError?.message }</p> }
{ description && <div className={ styles.description }>{ description }</div> }
<Button
disabled={ items === inputValue || inputInvalid }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function get( $fallback_value = false ) {
public function set( $value ) {
$value = $this->sanitize_value( $value );

if ( empty( $value ) || count( $value ) === 1 && $value[0] === '' ) {
delete_option( $this->option_key );
return;
}

$updated = update_option( $this->option_key, $value );
if ( $updated ) {
( new Environment_Change_Detector() )->handle_foundation_pages_list_update();
Expand Down
16 changes: 0 additions & 16 deletions projects/plugins/boost/app/lib/Foundation_Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function get_pages() {
public function get_properties() {
return array(
'max_pages' => $this->get_max_pages(),
'blog_url' => $this->get_blog_url(),
);
}

Expand All @@ -60,21 +59,6 @@ private function get_max_pages() {
return Premium_Features::has_any() ? 10 : 1;
}

private function get_blog_url() {
$front_page = (int) get_option( 'page_on_front' );
$posts_page = (int) get_option( 'page_for_posts' );
if ( $posts_page ) {
$permalink = get_permalink( $posts_page );
if ( ! empty( $permalink ) ) {
return $permalink;
}
} elseif ( ! $front_page ) {
return home_url( '/' );
}

return null;
}

private function is_development_features_enabled() {
return defined( 'JETPACK_BOOST_DEVELOPMENT_FEATURES' ) && JETPACK_BOOST_DEVELOPMENT_FEATURES;
}
Expand Down

0 comments on commit a420112

Please sign in to comment.