Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const onChooseOrCreate = useCallback( async () => {
const nextAttributes = { slug, theme };
if ( postId !== undefined && postId !== null ) {
// Existing template part found.
nextAttributes.postId = postId;
} else {
// Create a new template part.
try {
const cleanSlug = cleanForSlug( slug );
const templatePart = await saveEntityRecord(
'postType',
'wp_template_part',
{
title: cleanSlug,
status: 'publish',
slug: cleanSlug,
meta: { theme },
}
);
nextAttributes.postId = templatePart.id;
} catch ( err ) {
setHelp( __( 'Error adding template.' ) );
}
}
setAttributes( nextAttributes );
}, [ slug, onRequestClose ] );
return (
! slugs.loading &&
isOpen && (
<div>
<button disabled="{">
{ __( 'Add' ) }
</button>
</div>
)
);
}
setTitle( title ) {
this.props.setAttributes( {
title,
id: cleanForSlug( title ),
} );
}
onChange={ ( value ) => {
setTitle( value );
setSlug( cleanForSlug( value ) );
} }
allowedFormats={ [] }
isOpened,
onTogglePanel,
isEditable,
postLink,
permalinkParts,
editPermalink,
forceEmptyField,
setState,
postTitle,
postSlug,
postID,
postTypeLabel,
} ) {
const { prefix, suffix } = permalinkParts;
let prefixElement, postNameElement, suffixElement;
const currentSlug = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID;
if ( isEditable ) {
prefixElement = prefix && (
<span>{ prefix }</span>
);
postNameElement = currentSlug && (
<span>{ currentSlug }</span>
);
suffixElement = suffix && (
<span>{ suffix }</span>
);
}
return (
onBlur={ ( event ) => {
editPermalink( cleanForSlug( event.target.value ) );
if ( forceEmptyField ) {
setState( {
forceEmptyField: false,
} );
}
} }
/>
const setSlug = useCallback( ( nextSlug ) => {
_setSlug( nextSlug );
setHelp( cleanForSlug( nextSlug ) );
}, [] );
setTitle( title ) {
this.props.setAttributes( {
title,
id: cleanForSlug( title ),
} );
}
const add = useCallback( async () => {
_setSlug( '' );
const cleanSlug = cleanForSlug( slug );
try {
const template = await saveEntityRecord( 'postType', 'wp_template', {
title: cleanSlug,
status: 'publish',
slug: cleanSlug,
} );
onAddTemplateId( template.id );
onRequestClose();
} catch ( err ) {
setHelp( __( 'Error adding template.' ) );
}
}, [ slug, onRequestClose ] );
return (
( nextSlug ) => {
_setSlug( nextSlug );
const cleanSlug = cleanForSlug( nextSlug );
setHelp(
slugs[ cleanSlug ] ?
__( 'Template already exists, edit it instead.' ) :
cleanSlug
);
},
[ slugs ]