Skip to content

Instantly share code, notes, and snippets.

@dlh01
Last active August 7, 2023 17:59
Show Gist options
  • Save dlh01/c99a48d888ca77a0f98a9209da37532d to your computer and use it in GitHub Desktop.
Save dlh01/c99a48d888ca77a0f98a9209da37532d to your computer and use it in GitHub Desktop.
Infinite loop test theme
<?php
add_action(
'enqueue_block_editor_assets',
function () {
wp_enqueue_script(
'title-checker-js',
get_theme_file_uri( 'title-checker.js' ),
[],
null,
true
);
}
);
<?php
// Nothing
/*
Theme Name: Infinite Loop Test Theme
*/
const {
i18n: { __ },
plugins: {
registerPlugin,
},
data: {
useSelect,
useDispatch,
},
} = wp;
function TitleChecker() {
const postTitle = useSelect((select) => select('core/editor').getEditedPostAttribute('title'));
const titleIsOk = 0 < postTitle.length;
const { lockPostSaving, unlockPostSaving } = useDispatch('core/editor');
const { createWarningNotice, removeNotice } = useDispatch('core/notices');
/**
* If post title is empty, then lock post saving and display a warning notice
* else release lock and remove notice.
*/
if (! titleIsOk) {
lockPostSaving('title-empty-lock');
createWarningNotice(
__('Please enter a headline before publishing.', 'my-textdomain'),
{ id: 'title-empty-lock', isDismissible: false },
);
} else {
unlockPostSaving('title-empty-lock');
removeNotice('title-empty-lock');
}
// On the site where this issue occurred, a <PluginPrePublishPanel> was rendered here.
return null;
}
registerPlugin('empty-title-check', { render: TitleChecker });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment