Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function* resetEditorBlocks( blocks, options = {} ) {
const {
__unstableShouldCreateUndoLevel,
selectionStart,
selectionEnd,
} = options;
const edits = { blocks, selectionStart, selectionEnd };
if ( __unstableShouldCreateUndoLevel !== false ) {
const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
const noChange =
( yield select( 'core', 'getEditedEntityRecord', 'postType', type, id ) )
.blocks === edits.blocks;
if ( noChange ) {
return yield dispatch(
'core',
'__unstableCreateUndoLevel',
'postType',
type,
id
);
}
// We create a new function here on every persistent edit
// to make sure the edit makes the post dirty and creates
// a new undo level.
export function* refreshPost() {
const post = yield select(
STORE_KEY,
'getCurrentPost'
);
const postTypeSlug = yield select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield select(
'core',
'getPostType',
postTypeSlug
);
const newPost = yield apiFetch(
{
// Timestamp arg allows caller to bypass browser caching, which is
// expected for this specific function.
);
if ( error ) {
const args = getNotificationArgumentsForSaveFail( {
post: previousRecord,
edits,
error,
} );
if ( args.length ) {
yield dispatch( 'core/notices', 'createErrorNotice', ...args );
}
} else {
const updatedRecord = yield select( STORE_KEY, 'getCurrentPost' );
const args = getNotificationArgumentsForSaveSuccess( {
previousPost: previousRecord,
post: updatedRecord,
postType: yield select( 'core', 'getPostType', updatedRecord.type ),
options,
} );
if ( args.length ) {
yield dispatch( 'core/notices', 'createSuccessNotice', ...args );
}
// Make sure that any edits after saving create an undo level and are
// considered for change detection.
if ( ! options.isAutosave ) {
yield dispatch( 'core/block-editor', '__unstableMarkLastChangeAsPersistent' );
}
}
}
export function* getTemplates(
// Resolver has the same signature as corresponding selector without the initial state argument
verticalId: Parameters< typeof import('./selectors')[ 'getTemplates' ] >[ 1 ]
) {
const resp = yield apiFetch( {
url: `https://public-api.wordpress.com/wpcom/v2/verticals/${ verticalId }/templates`,
} );
return receiveTemplates( verticalId, resp.templates );
}
export function* __internalGetDomainSuggestions(
// Resolver has the same signature as corresponding selector without the initial state argument
queryObject: Parameters< typeof import('./selectors').__internalGetDomainSuggestions >[ 1 ]
) {
const url = 'https://public-api.wordpress.com/rest/v1.1/domains/suggestions';
// If normalized search string (`query`) contains no alphanumerics, endpoint 404s
if ( ! queryObject.query ) {
return receiveDomainSuggestions( queryObject, [] );
}
// `credentials` and `mode` args are needed since we're accessing the WP.com REST API
// (rather than the WP Core REST API)
const suggestions = yield apiFetch( {
credentials: 'same-origin',
mode: 'cors',
url: addQueryArgs( url, queryObject ),
} );
return receiveDomainSuggestions( queryObject, suggestions );
}
for ( const langSlug of navigator.languages ) {
const language = getLanguage( langSlug.toLowerCase() );
if ( language ) {
locale = language.langSlug;
break;
}
}
}
let localeQueryParam;
if ( locale && locale !== 'en' ) {
// v2 api request
localeQueryParam = { _locale: locale };
}
const verticals = yield apiFetch( { url: addQueryArgs( url, localeQueryParam ) } );
// @TODO: validate and normalize verticals?
return receiveVerticals( verticals );
}
export function* refreshPost() {
const post = yield select(
STORE_KEY,
'getCurrentPost'
);
const postTypeSlug = yield select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield select(
'core',
'getPostType',
postTypeSlug
);
const newPost = yield apiFetch(
{
// Timestamp arg allows caller to bypass browser caching, which is
// expected for this specific function.
path: `/wp/v2/${ postType.rest_base }/${ post.id }` +
`?context=edit&_timestamp=${ Date.now() }`,
}
);
yield dispatch(
STORE_KEY,
'resetPost',
newPost
);
}
export function* getCollection( namespace, resourceName, query, ids ) {
const route = yield select(
SCHEMA_STORE_KEY,
'getRoute',
namespace,
resourceName,
ids
);
const queryString = addQueryArgs( '', query );
if ( ! route ) {
yield receiveCollection( namespace, resourceName, queryString, ids );
return;
}
try {
const {
response = DEFAULT_EMPTY_ARRAY,
headers,
export function* savePost( options = {} ) {
if ( ! ( yield select( STORE_KEY, 'isEditedPostSaveable' ) ) ) {
return;
}
let edits = {
content: yield select( STORE_KEY, 'getEditedPostContent' ),
};
if ( ! options.isAutosave ) {
yield dispatch( STORE_KEY, 'editPost', edits, { undoIgnore: true } );
}
yield __experimentalRequestPostUpdateStart( options );
const previousRecord = yield select( STORE_KEY, 'getCurrentPost' );
edits = {
id: previousRecord.id,
...( yield select(
'core',
'getEntityRecordNonTransientEdits',
export function* trashPost() {
const postTypeSlug = yield select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield select(
'core',
'getPostType',
postTypeSlug
);
yield dispatch(
'core/notices',
'removeNotice',
TRASH_POST_NOTICE_ID
);
try {
const post = yield select(
STORE_KEY,