|
<?php |
|
|
|
/** |
|
* Block Editor: Auto Expand Advanced & All Sidebar Panels [SnipSnip.pro] |
|
* |
|
* Block Editor: Auto Expand Advanced & All Sidebar Panels [SnipSnip.pro] - When using Block Editor, anytime the content in the sidebar changes, any panels that are closed will open automatically. https://snipsnip.pro/s/751 |
|
*/ |
|
if (!class_exists('Expand_Sidebar_Panels_On_Mutation')) { |
|
class Expand_Sidebar_Panels_On_Mutation { |
|
public function __construct() { |
|
add_action('admin_footer', array($this, 'add_inline_script')); |
|
} |
|
|
|
public function add_inline_script() { |
|
if (!function_exists('\get_current_screen')) { |
|
return false; |
|
} |
|
$screen = \get_current_screen(); |
|
if (method_exists($screen, 'is_block_editor') && $screen->is_block_editor()) { |
|
?> |
|
<script type="text/javascript"> |
|
setTimeout(() => { |
|
|
|
(function () { |
|
function expandPanels() { |
|
const panelBodies = document.querySelectorAll('div.components-panel__body:not(.is-opened)'); |
|
panelBodies.forEach((panelBody) => { |
|
const buttons = panelBody.querySelectorAll('button.components-button'); |
|
buttons.forEach((button) => button.click()); |
|
}); |
|
} |
|
|
|
let activeTimer = null; |
|
let observer = null; |
|
|
|
function setupObserver() { |
|
const targetElement = document.querySelector('div.interface-navigable-region.interface-interface-skeleton__sidebar'); |
|
|
|
if (targetElement) { |
|
if (observer) { |
|
observer.disconnect(); |
|
} |
|
|
|
observer = new MutationObserver((mutationsList, observer) => { |
|
for (let mutation of mutationsList) { |
|
// Check if the mutation or its descendants match your desired criteria |
|
if (mutation.type === 'childList' && mutation.target.classList.contains('block-editor-block-inspector')) { |
|
if (activeTimer) { |
|
clearTimeout(activeTimer); |
|
} |
|
activeTimer = setTimeout(expandPanels, 500); |
|
} |
|
} |
|
}); |
|
|
|
observer.observe(targetElement, { childList: true, subtree: true }); |
|
} else { |
|
setTimeout(setupObserver, 100); |
|
} |
|
} |
|
|
|
setupObserver(); |
|
console.log("Block Editor: Auto Expand Advanced & All Sidebar Panels [SnipSnip.pro]"); |
|
})(); |
|
|
|
}, 7373); |
|
</script> |
|
<?php |
|
} |
|
} |
|
} |
|
|
|
new Expand_Sidebar_Panels_On_Mutation(); |
|
} |
I hate clicking "Advanced" in the Block Editor, so after a couple old versions, I think I've finally landed on this solution which watches for the sidebar content to change and expands the Advanced (or any other) sections automatically.
Read all about on a blog post here: https://snipsnip.pro/s/751