Skip to content

Commit

Permalink
Merge branch 'main' into feature/Update-composer-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
khleomix committed May 14, 2024
2 parents 0f74670 + c96c5f2 commit 1052514
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 13 deletions.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Meet WDS BT, a stylish block theme, tailored for WordPress, featuring native blo
- [Registering Block Styles](#registering-block-styles)
- [Overriding/Customizing Core Block Styles](#overridingcustomizing-core-block-styles)
- [Creating Block Variations](#creating-block-variations)
- [Unregister Blocks and Variations](#unregister-blocks-and-variations)
- [Mixins](#mixins)
- [Responsive Mixins](#responsive-mixins)
- [Mobile Only Mixins](#mobile-only-mixins)
Expand Down Expand Up @@ -122,9 +123,9 @@ From the command line, type any of the following to perform an action:

## Creating Block Variations

1. In the `assets/js/variations` directory within your theme, create a new JavaScript file. This file will contain the definition of your block variation.
1. In the `assets/js/block-variations` directory within your theme, create a new JavaScript file. This file will contain the definition of your block variation.

2. Import the newly created file into the `assets/js/variations/index.js` file. This step ensures that your variation is included in the build process.
2. Import the newly created file into the `assets/js/block-variations/index.js` file. This step ensures that your variation is included in the build process.

3. Use the `wp.blocks.registerBlockVariation()` function to officially register your block variation. This function takes the name of the original block and an object defining the variation.

Expand All @@ -147,6 +148,45 @@ From the command line, type any of the following to perform an action:

***

## Unregister Blocks and Variations

This functionality allows you to unregister and disable specific core Gutenberg blocks, styles, and variations that are not needed on your WordPress website. By removing these unused blocks and variations, you can streamline the Gutenberg editor and improve the overall performance of your site.

### Implementation

The script in `assets/js/block-filters/unregister-core-embed.js` loops through a list of unused blocks and variations, unregistering them from the Gutenberg editor. Additionally, it keeps only the specified embed variations for the core/embed block.

### Example

```javascript
// List of Gutenberg blocks to unregister
const unusedBlocks = [
'core/file',
'core/latest-comments',
'core/rss',
// Add more unused blocks as needed
];

// List of Gutenberg block variations to unregister
const unregisterBlockVariations = [
// Example:
// {
// blockName: 'core/group',
// blockVariationName: 'group-stack',
// },
];

// Keep only the necessary embed variations
const keepEmbeds = [
'twitter',
'wordpress',
'spotify',
// Add more necessary embed variations as needed.
];
```

***

## Mixins

### Responsive Mixins
Expand Down
8 changes: 8 additions & 0 deletions assets/js/block-filters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Block Filters
* Files within this directory are automatically enqueued.
* Learn more: https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/
*/

// Import the block filter file.
import './unregister-core-embed';
63 changes: 63 additions & 0 deletions assets/js/block-filters/unregister-core-embed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Functions to unregister and disable specific core Gutenberg blocks, styles, and variations.
*/

wp.domReady(() => {
// List of Gutenberg blocks to unregister.
const unusedBlocks = [
'core/file',
'core/latest-comments',
'core/rss',
'core/tag-cloud',
'core/missing',
'core/site-tagline',
'core/loginout',
'core/term-description',
'core/query-title',
];

// List of Gutenberg block variations to unregister.
const unregisterBlockVariations = [
// Example:
// {
// blockName: 'core/group',
// blockVariationName: 'group-row',
//
// blockName: 'core/group',
// blockVariationName: 'group-stack',
// },
];

// Keep only the necessary embed variations.
const embedBlockVariations = wp.blocks.getBlockVariations('core/embed');
const keepEmbeds = [
'twitter',
'wordpress',
'spotify',
'soundcloud',
'flickr',
];

// Unregister unused blocks.
for (let i = 0; i < unusedBlocks.length; i++) {
wp.blocks.unregisterBlockType(unusedBlocks[i]);
}

// Unregister unused block variations.
for (let i = 0; i < unregisterBlockVariations.length; i++) {
wp.blocks.unregisterBlockVariation(
unregisterBlockVariations[i].blockName,
unregisterBlockVariations[i].blockVariationName
);
}

// Keep only necessary embed variations.
for (let i = 0; i < embedBlockVariations.length; i++) {
if (!keepEmbeds.includes(embedBlockVariations[i].name)) {
wp.blocks.unregisterBlockVariation(
'core/embed',
embedBlockVariations[i].name
);
}
}
});
File renamed without changes.
20 changes: 10 additions & 10 deletions inc/hooks/unregister-block-variations.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?php
/**
* Unregister custom block styles.
* Functions to disable core Gutenberg blocks.
*
* @package wdsbt
*/

namespace WebDevStudios\wdsbt;

/**
* Unregister block variations.
* Prevents editors from adding unregistered core blocks to content or pages.
*
* @return void
*/
function unregister_block_variations() {

function remove_core_blocks_gutenberg_frontend() {
wp_enqueue_script(
'unregistered-blocks-list',
get_template_directory_uri() . '/assets/js/unregistered-blocks-list.js',
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
'unregister_core_blocks',
get_template_directory_uri() . '/build/js/filters.js',
array( 'wp-blocks', 'wp-dom-ready' ),
wp_get_theme()->get( 'Version' ),
false
true
);
}

add_filter( 'enqueue_block_editor_assets', __NAMESPACE__ . '\unregister_block_variations', 10, 1 );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\remove_core_blocks_gutenberg_frontend' );
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module.exports = {
entry: {
style: './assets/scss/index.scss',
index: './assets/js/index.js',
variations: './assets/js/variations/index.js',
variations: './assets/js/block-variations/index.js',
filters: './assets/js/block-filters/index.js',
...coreBlockEntryPaths,
},
output: {
Expand Down

0 comments on commit 1052514

Please sign in to comment.