-
-
Save peterwilsoncc/20b1c7cdd22bbb1a660097d3558bc8a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: Testing hoisting | |
| */ | |
| // add_dependency_to_block_library(); | |
| // deregister_and_replace_blocks_styles(); | |
| // remove_theme_support_block_styles(); | |
| register_custom_block_styles(); | |
| add_filter( 'the_content', function( $content ) { | |
| // Get the name of the current git branch. | |
| $git_branch = trim( shell_exec( 'cd /vagrant/wordpress-develop/; git rev-parse --abbrev-ref HEAD' ) ); | |
| $git = '<p style="position: fixed; top: 50px; left: 0; background: yellow; z-index: 9999; padding: 5px;">Git Branch: ' . esc_html( $git_branch );; | |
| $running = apply_filters( 'peter_is_running', array() ); | |
| if ( ! empty( $running ) ) { | |
| foreach ( $running as $key => $value ) { | |
| $git .= '<br>Running: ' . esc_html( $value ); | |
| } | |
| } | |
| $git .= '</p>'; | |
| return $git . $content; | |
| } ); | |
| /** | |
| * Deregister the block library styles and replace them with inline styles. | |
| */ | |
| function deregister_and_replace_blocks_styles() { | |
| add_filter( 'peter_is_running', function( $running ) { | |
| $running[] = 'deregister_and_replace_blocks_styles'; | |
| return $running; | |
| } ); | |
| add_action( | |
| 'enqueue_block_assets', | |
| function (): void { | |
| wp_deregister_style( 'wp-block-library' ); | |
| } | |
| ); | |
| add_action( | |
| 'wp_head', | |
| static function () { | |
| echo '<style>body { background-color: red; }</style>'; | |
| } | |
| ); | |
| add_action( | |
| 'wp_footer', | |
| static function () { | |
| wp_register_style( 'lime-background', null ); | |
| wp_add_inline_style( 'lime-background', 'body { background-color: lime !important; }' ); | |
| wp_enqueue_style( 'lime-background' ); | |
| } | |
| ); | |
| } | |
| /** | |
| * Remove the block styles by removing theme support and deregistering the styles. | |
| */ | |
| function remove_theme_support_block_styles() { | |
| add_filter( 'peter_is_running', function( $running ) { | |
| $running[] = 'remove_theme_support_block_styles'; | |
| return $running; | |
| } ); | |
| add_action( | |
| 'enqueue_block_assets', | |
| function (): void { | |
| wp_deregister_style( 'wp-block-library' ); | |
| wp_register_style( 'wp-block-library', '' ); | |
| } | |
| ); | |
| add_action( | |
| 'after_setup_theme', | |
| static function (): void { | |
| remove_theme_support( 'wp-block-styles' ); | |
| }, | |
| 100 | |
| ); | |
| } | |
| /** | |
| * Add a dependency to the block library styles. | |
| * | |
| * Mess around with the block library style dependencies to add an inline stylesheet beforehand. | |
| */ | |
| function add_dependency_to_block_library() { | |
| add_filter( 'peter_is_running', function( $running ) { | |
| $running[] = 'add_dependency_to_block_library'; | |
| return $running; | |
| } ); | |
| add_action( | |
| 'enqueue_block_assets', | |
| function (): void { | |
| wp_register_style( 'dependency-added-to-wp-block-library', null ); | |
| // The cascade will replace this if all is going well. | |
| wp_add_inline_style( 'dependency-added-to-wp-block-library', '.wp-block-gallery img { display: none; }' ); | |
| global $wp_styles; | |
| $wp_styles->registered['wp-block-library']->deps[] = 'dependency-added-to-wp-block-library'; | |
| } | |
| ); | |
| } | |
| /** | |
| * Replaces the wp-block-library with custom code. | |
| * | |
| * The code is actually removing the styles alltogether but the use case is a | |
| * developer that has written their own css for the blocks used on their site. | |
| * | |
| * @return void | |
| */ | |
| function register_custom_block_styles() { | |
| add_filter( 'peter_is_running', function( $running ) { | |
| $running[] = 'register_custom_block_styles'; | |
| return $running; | |
| } ); | |
| add_action( | |
| 'enqueue_block_assets', | |
| function (): void { | |
| wp_add_inline_style( 'wp-block-library', '.wp-block-gallery img { display: none; }' ); | |
| global $wp_styles; | |
| // var_dump( $wp_styles->registered['wp-block-library'] ); exit; | |
| $wp_styles->registered['wp-block-library']->src = null; | |
| unset( $wp_styles->registered['wp-block-library']->extra['path'] ); | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment