diff --git a/assets/admin/editor-wizard/steps/patterns-step.js b/assets/admin/editor-wizard/steps/patterns-step.js index a1ac4823b9..33b1cbd4f9 100644 --- a/assets/admin/editor-wizard/steps/patterns-step.js +++ b/assets/admin/editor-wizard/steps/patterns-step.js @@ -37,7 +37,7 @@ const PatternsStep = ( { title, replaces, onCompletion } ) => { ? replacePlaceholders( blocks, replaces ) : blocks; - resetEditorBlocks( newBlocks ); + resetEditorBlocks( newBlocks, { patternName: name } ); onCompletion(); //Auto select a template if the pattern specifies an available one. diff --git a/assets/js/admin/first-course-creation-notice.js b/assets/js/admin/first-course-creation-notice.js index da0115bf35..337956a133 100644 --- a/assets/js/admin/first-course-creation-notice.js +++ b/assets/js/admin/first-course-creation-notice.js @@ -2,9 +2,10 @@ * WordPress dependencies */ import { createBlock } from '@wordpress/blocks'; -import { select, subscribe, dispatch } from '@wordpress/data'; +import { select, subscribe, dispatch, use } from '@wordpress/data'; import domReady from '@wordpress/dom-ready'; import { __ } from '@wordpress/i18n'; +import { find as findObject } from 'lodash'; /** * Internal dependencies */ @@ -50,6 +51,75 @@ export const hasPublishedLessonInOutline = ( blocks ) => { } ); }; +// Detect when a user selects a Sensei pattern. +let selectedSenseiPattern = false; + +const rewrittenActions = {}; +const originalActions = {}; + +const checkIfAnySenseiPatternGotInserted = ( ...args ) => { + const meta = findObject( args, ( item ) => item?.patternName ); + let patternName = meta?.patternName; + if ( patternName && patternName.includes( 'sensei' ) ) { + selectedSenseiPattern = true; + } +}; + +const REDUX_ACTION_OVERRIDERS = { + 'core/block-editor': { + insertBlocks: checkIfAnySenseiPatternGotInserted, + }, + 'core/editor': { + resetEditorBlocks: checkIfAnySenseiPatternGotInserted, + }, +}; + +use( ( registry ) => ( { + dispatch: ( namespace ) => { + const namespaceName = + typeof namespace === 'object' ? namespace.name : namespace; + const actions = { ...registry.dispatch( namespaceName ) }; + const overriderActions = REDUX_ACTION_OVERRIDERS[ namespaceName ]; + + if ( overriderActions ) { + Object.keys( overriderActions ).forEach( ( actionName ) => { + const originalAction = actions[ actionName ]; + const newAction = overriderActions[ actionName ]; + + if ( ! rewrittenActions[ namespaceName ] ) { + rewrittenActions[ namespaceName ] = {}; + } + if ( ! originalActions[ namespaceName ] ) { + originalActions[ namespaceName ] = {}; + } + + if ( + ! originalActions[ namespaceName ][ actionName ] || + originalActions[ namespaceName ][ actionName ] !== + originalAction + ) { + originalActions[ namespaceName ][ + actionName + ] = originalAction; + rewrittenActions[ namespaceName ][ actionName ] = ( + ...args + ) => { + try { + newAction( ...args ); + } catch ( err ) {} + return originalAction( ...args ); + }; + } + + actions[ actionName ] = + rewrittenActions[ namespaceName ][ actionName ]; + } ); + } + + return actions; + }, +} ) ); + export const handleFirstCourseCreationHelperNotice = () => { const { createInfoNotice, removeNotice } = dispatch( 'core/notices' ); const userId = select( 'core' ).getCurrentUser()?.id; @@ -69,9 +139,8 @@ export const handleFirstCourseCreationHelperNotice = () => { ); subscribe( () => { - // If the notice has been created, and the course outline block has been created, and the notice hasn't been removed, and there are published lessons in the outline, remove the notice. - // Function calls are added later to short circuit the logic for performance. if ( + selectedSenseiPattern && noticeCreated && ! noticeRemoved && hasOutlineBlock() && @@ -82,8 +151,9 @@ export const handleFirstCourseCreationHelperNotice = () => { removeNotice( noticeId ); } - // If the notice hasn't been created, and notice hasn't been dismissed, and either the course outline block hasn't been created OR there are no published lessons in the outline, create the notice. + // If the user selects a Sensei pattern, and the notice hasn't been created, and notice hasn't been dismissed, and either the course outline block hasn't been created OR there are no published lessons in the outline, create the notice. if ( + selectedSenseiPattern && ! noticeCreated && ! isFirstCourseNoticeDismissed && ! (