Can't replace Gutenberg editor's pre-publish conditions with my own

I'm trying to adapt Welcher's answer here to replace Gutenberg's default pre-publish conditions with my own - namely for requiring that a category and a featured image are set. However, no matter what I try I can't seem to get his code working for me.

Here is the content of my pre-publish-checks.js:

const { registerPlugin } = wp.plugins;
const { PluginPrePublishPanel } = wp.editPost;
const { select, dispatch } = wp.data;
const { count } = wp.wordcount;
const { serialize } = wp.blocks;
const { PanelBody } = wp.components;

const PrePublishCheckList = () = {
    let lockPost = false;

    // Get the cats
    const cats = select('core/editor').getEditedPostAttribute('categories');
    let catsMessage = 'Set';
    if (!cats.length) {
        lockPost = true;
        catsMessage = 'Not set';
    } else {
        // Check that the cat is not uncategorized - this assumes that the ID of Uncategorized is 1, which it would be for most installs.
        if (cats.length === 1  cats[0] === 1) {
            lockPost = true;
            catsMessage = 'Please set an appropriate category.';
        }
    }

    // Get the featured image
    const featuredImageID = select('core/editor').getEditedPostAttribute('featured_media');
    let featuredImage = 'Set';

    if (featuredImageID === 0) {
        lockPost = true;
        featuredImage = 'Not set';
    }

    // Do we need to lock the post?
    if (lockPost === true) {
        dispatch('core/editor').lockPostSaving();
    } else {
        dispatch('core/editor').unlockPostSaving();
    }
    return (
        PluginPrePublishPanel title={'Publish Checklist'}
            pbCategories:/b {catsMessage}/p
            pbFeatured Image:/b {featuredImage}/p
        /PluginPrePublishPanel
    )
};

registerPlugin('pre-publish-checklist', { render: PrePublishCheckList });

And here is where it's being enqueued in functions.php:

function enqueue_block_editor_assets() {
    wp_enqueue_script(
        'pre-publish-checks', // Handle.
        get_stylesheet_directory_uri() . '/pre-publish-checks.js',
        array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-edit-post', 'word-count') // Dependencies, defined in JS file
    );
}
add_action('enqueue_block_editor_assets', 'enqueue_block_editor_assets');

I can't use a build process for the current project - could this be the reason why? Does Welcher's solution require the build process that he details in his answer, or should it be working without one?

Topic functions wp-enqueue-script publish Wordpress javascript

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.