public function displaySettingsPage() { $this->loadSettings(); if ( $this->shouldRefreshMetaBoxes ) { if ( !$this->settings->isFirstRefreshDone() ) { //Let's update the initial refresh flag before the refresh actually happens. //This helps prevent an infinite loop when the initial refresh fails. $this->settings->setFirstRefreshState(true); $this->saveSettings(); } $this->outputTemplate('box-refresh'); } else { parent::displaySettingsPage(); } } public function clearCache() { $this->hiddenBoxCache = array(); $this->settings = null; } /** * Add a script that will remove Gutenberg document panels that correspond to hidden meta boxes. */ public function enqueueGutenbergScripts() { //Some plugins load the Gutenberg editor outside the admin dashboard in which case //the get_current_screen() function might not be available. if ( !function_exists('get_current_screen') ) { return; } $currentScreen = get_current_screen(); if ( empty($currentScreen) ) { return; } $currentUser = wp_get_current_user(); $boxesToPanels = array( 'slugdiv' => 'post-link', 'postexcerpt' => 'post-excerpt', 'postimagediv' => 'featured-image', 'commentstatusdiv' => 'discussion-panel', 'categorydiv' => 'taxonomy-panel-category', 'pageparentdiv' => 'page-attributes', ); $boxesToSelectors = array( 'submitdiv' => '#editor .edit-post-post-schedule', 'formatdiv' => '#editor .editor-post-format, #editor .edit-post-post-schedule + .components-panel__row', ); $panelsToRemove = array(); $selectorsToHide = array(); $metaBoxes = $this->getScreenSettings($currentScreen->id)->getMetaBoxes(); $presentBoxes = $metaBoxes->getPresentBoxes(); foreach ($presentBoxes as $box) { if ( $box->isAvailableTo($currentUser, $this->menuEditor) ) { continue; } //What's the panel name for this box? $boxId = $box->getId(); if ( isset($boxesToPanels[$boxId]) ) { $panelsToRemove[] = $boxesToPanels[$boxId]; } else if ( preg_match('/^tagsdiv-(?P.++)$/', $boxId, $matches) ) { $panelsToRemove[] = 'taxonomy-panel-' . $matches['taxonomy']; } else if ( isset($boxesToSelectors[$boxId]) ) { $selectorsToHide[] = $boxesToSelectors[$boxId]; } //We deliberately skip non-core boxes. For now, the remove_meta_box() call //in processMetaBoxes() seems to remove them effectively. } if ( empty($panelsToRemove) && empty($selectorsToHide) ) { return; } wp_enqueue_auto_versioned_script( 'ame-hide-gutenberg-panels', plugins_url('hide-gutenberg-panels.js', __FILE__), array('wp-data', 'wp-blocks', 'wp-edit-post', 'jquery') ); wp_localize_script( 'ame-hide-gutenberg-panels', 'wsAmeGutenbergPanelData', array( 'panelsToRemove' => $panelsToRemove, 'selectorsToHide' => $selectorsToHide, ) ); } }