Site icon Hip-Hop Website Design and Development

JavaScript for role not being executed or enqueued

I’m trying to adapt Christine’s answer here to hide taxonomy panels from the Gutenberg editor for users who belong to the Author role, but it seems no matter what I do my JavaScript files never get executed, or at least enqueued.

Here is the relevant code from my functions.php:

function irm_lockdown_author_role() {

....

$userrole = wp_get_current_user();

if (isset($userrole->roles[0])) {
    $current_role = $userrole->roles[0]; 
} else {
    $current_role = 'no_role';
}   
       # check current role and execute only if current user's role is "author" 
if ('author' == $current_role) {  

...

function irm_gutenberg_register_files() {
    // script file
    wp_register_script(
        'cc-block-script',
        get_stylesheet_directory_uri() . '/functions.js', // adjust the path to the JS file
        array('wp-blocks', 'wp-edit-post')
    );

    // register block editor script
    register_block_type('cc/ma-block-files', array(
        'editor_script' => 'cc-block-script'
    ));
}
add_action('init', 'irm_gutenberg_register_files');

if (!wp_script_is('cc-block-script', 'enqueued')) {
    echo '<p>Script failed to queue up!</p>'; 
   }
}

...

add_action('current_screen', 'irm_lockdown_author_role');

Note the debug conditional towards the bottom, which does get executed and therefore seems to indicate that the functions.js JavaScript file is failing to be enqueued. Regardless, here’s the contents of functions.js:

wp.data.dispatch('core/edit-post').removeEditorPanel('taxonomy-panel-category'); // category
wp.data.dispatch('core/edit-post').removeEditorPanel('taxonomy-panel-post_tag'); // tags
wp.data.dispatch('core/edit-post').removeEditorPanel('page-attributes'); // page attributes
wp.data.dispatch('core/edit-post').removeEditorPanel('post-excerpt'); // Excerpt
wp.data.dispatch('core/edit-post').removeEditorPanel('discussion-panel'); // Discussion