CYS - Schedule fetching patterns only once an hour (#49754)

* Schedule fetching patterns only one an hour

* Add changefile(s) from automation for the following project(s): woocommerce

* Add notice

* Fix lint error

* Update copy

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-07-23 21:03:26 +02:00 committed by GitHub
parent 37441c6c52
commit efa506afdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 1 deletions

View File

@ -46,6 +46,8 @@ import { useEditorBlocks } from '../../hooks/use-editor-blocks';
import { isTrackingAllowed } from '../../utils/is-tracking-allowed';
import clsx from 'clsx';
import './style.scss';
import { usePatterns } from '~/customize-store/assembler-hub/hooks/use-patterns';
import { THEME_SLUG } from '~/customize-store/data/constants';
const isActiveElement = ( path: string | undefined, category: string ) => {
if ( path?.includes( category ) ) {
@ -105,6 +107,17 @@ export const SidebarNavigationScreenHomepagePTK = ( {
}, initialAccumulator );
}, [ blocks ] );
const { blockPatterns, isLoading: isLoadingPatterns } = usePatterns();
const patternsFromPTK = blockPatterns.filter(
( pattern ) =>
! pattern.name.includes( THEME_SLUG ) &&
! pattern.name.includes( 'woocommerce' ) &&
pattern.source !== 'core' &&
pattern.source !== 'pattern-directory/featured' &&
pattern.source !== 'pattern-directory/theme' &&
pattern.source !== 'pattern-directory/core'
);
let notice;
if ( isNetworkOffline ) {
notice = __(
@ -121,6 +134,11 @@ export const SidebarNavigationScreenHomepagePTK = ( {
'Opt in to <OptInModal>usage tracking</OptInModal> to get access to more patterns.',
'woocommerce'
);
} else if ( ! isLoadingPatterns && patternsFromPTK.length === 0 ) {
notice = __(
'Unfortunately, a technical issue is preventing more patterns from being displayed. Please <FetchPatterns>try again</FetchPatterns> later.',
'woocommerce'
);
}
const [ isModalOpen, setIsModalOpen ] = useState( false );
@ -245,6 +263,22 @@ export const SidebarNavigationScreenHomepagePTK = ( {
variant="link"
/>
),
FetchPatterns: (
<Button
onClick={ () => {
if ( isIframe( window ) ) {
sendMessageToParent( {
type: 'INSTALL_PATTERNS',
} );
} else {
sendEvent(
'INSTALL_PATTERNS'
);
}
} }
variant="link"
/>
),
} ) }
</p>
{ isModalOpen && (

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
CYS - Schedule the `fetch_patterns` actions only once every hour.

View File

@ -91,11 +91,13 @@ class PTKPatternsStore {
* @return void
*/
private function schedule_action_if_not_pending( $action ) {
if ( as_has_scheduled_action( $action ) ) {
$last_request = get_transient( 'last_fetch_patterns_request' );
if ( as_has_scheduled_action( $action ) || false !== $last_request ) {
return;
}
as_schedule_single_action( time(), $action );
set_transient( 'last_fetch_patterns_request', time(), HOUR_IN_SECONDS );
}
/**