Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: remove blocks by page template #957

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
All notable changes to this project will be documented in this file.

## 2022.03
* Added: Block Page Template Filter: Allow certain blocks to only be chosen when using a specific page template.
* Changed: Replaced `msawicki/acf-menu-chooser` with a forked https://github.com/moderntribe/acf-menu-chooser that includes security fixes and is also added to packagist.
* Updated: ACF (5.12), Tribe Libs (3.4.10), Redirection (5.2.3), Yoast (18.2), TEC (5.14.0.4)
* Updated: Aligns accordion component with WAI-ARIA standard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

use Codeception\Test\Unit;
use Tribe\Project\Blocks\Block_Page_Template_Filter;

final class BlockPageTemplateFilterTest extends Unit {

public function test_it_prefixes_blocks(): void {
$block_list = [
'page-templates/custom-template-one.php' => [
'block_one',
'block_two',
],
'page-templates/custom-template-two.php' => [
'block_three',
'block_four',
],
];

$filter = new Block_Page_Template_Filter( $block_list );
$list = $filter->get_block_list();

$this->assertCount( 2, $list );

$this->assertSame( [
'acf/block_one',
'acf/block_two',
], $list['page-templates/custom-template-one.php'] );

$this->assertSame( [
'acf/block_three',
'acf/block_four',
], $list['page-templates/custom-template-two.php'] );
}

}
6 changes: 4 additions & 2 deletions wp-content/plugins/core/src/Assets/Admin/JS_Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class JS_Config {
public function get_data(): array {
if ( ! isset( $this->data ) ) {
$this->data = [
'images_url' => trailingslashit( get_stylesheet_directory_uri() ) . 'assets/img/admin/',
'block_denylist' => (array) apply_filters( 'tribe/project/blocks/denylist', [] ),
'images_url' => trailingslashit( get_stylesheet_directory_uri() ) . 'assets/img/admin/',
'block_denylist' => (array) apply_filters( 'tribe/project/blocks/denylist', [] ),
'block_page_template_filter' => (array) apply_filters( 'tribe/project/blocks/page_template_filter', [] ),

];

$this->data = (array) apply_filters( 'core_admin_js_config', $this->data );
Expand Down
40 changes: 40 additions & 0 deletions wp-content/plugins/core/src/Blocks/Block_Page_Template_Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Tribe\Project\Blocks;

class Block_Page_Template_Filter {

/**
* A list indexed by page template, containing
* the block names assigned to that page template.
*
* @var array<string, string[]>
*/
protected array $block_list;

public function __construct( array $block_list = [] ) {
$this->block_list = $block_list;
$this->prefix_blocks();
}

/**
* @filter tribe/project/blocks/page_template_filter
*
* @return array<string, string[]>
*/
public function get_block_list(): array {
return $this->block_list;
}

/**
* Prefix all block names with "acf/"
*
* @return void
*/
protected function prefix_blocks(): void {
array_walk_recursive( $this->block_list, static fn ( &$name ) =>
$name = sprintf( 'acf/%s', $name )
);
}

}
23 changes: 19 additions & 4 deletions wp-content/plugins/core/src/Blocks/Blocks_Definer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Blocks_Definer implements Definer_Interface {

public function define(): array {
return [
self::TYPES => DI\add( [
self::TYPES => DI\add( [
DI\get( Accordion::class ),
DI\get( Buttons::class ),
DI\get( Card_Grid::class ),
Expand Down Expand Up @@ -60,7 +60,7 @@ public function define(): array {
*
* @see: https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#using-a-deny-list
*/
self::DENY_LIST => [
self::DENY_LIST => [
'core/archives',
'core/button',
'core/buttons',
Expand Down Expand Up @@ -90,7 +90,7 @@ public function define(): array {
*
* TODO: Create a proper thumbnail of the style for the block editor: http://p.tri.be/dmsAwK
*/
self::STYLES => DI\add( [
self::STYLES => DI\add( [
DI\factory( static function () {
return new Block_Style_Override( [ 'core/paragraph' ], [
[
Expand All @@ -113,7 +113,22 @@ public function define(): array {
} ),
] ),

Block_Deny_List::class => DI\autowire()->constructor( DI\get( self::DENY_LIST ) ),
Block_Deny_List::class => DI\autowire()->constructor( DI\get( self::DENY_LIST ) ),

/**
* Limit certain blocks to being available only on certain page templates.
*
* key: The page template slug.
* values: A list of block names that will only appear when that page template is selected.
*
* @see get_page_template_slug()
*/
Block_Page_Template_Filter::class => DI\autowire()
->constructor( [
/*'page-templates/page-template-name.php' => [
Block_Type::NAME,
],*/
] ),
];
}

Expand Down
10 changes: 9 additions & 1 deletion wp-content/plugins/core/src/Blocks/Blocks_Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ public function register(): void {
}, 10, 4 );

/**
* Adds the deny list to the JS_Config class.
* Adds the Deny List to the JS_Config class.
*/
add_filter( 'tribe/project/blocks/denylist', function ( array $types ): array {
return $this->container->get( Block_Deny_List::class )->filter_block_denylist( $types );
}, 10, 1 );

/**
* Limit certain blocks to being available only on certain page templates
* by adding a global object list via the JS_Config class.
*/
add_filter( 'tribe/project/blocks/page_template_filter', function (): array {
return $this->container->get( Block_Page_Template_Filter::class )->get_block_list();
}, 10, 1 );

add_action( 'after_setup_theme', function (): void {
$this->container->get( Theme_Support::class )->register_theme_supports();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ const wp = window.modern_tribe_admin_config || {};

export const HMR_DEV = wp.hmr_dev || 0;
export const BLOCK_DENYLIST = wp.block_denylist || [];
export const BLOCK_PAGE_TEMPLATE_FILTER = wp.block_page_template_filter || [];
2 changes: 2 additions & 0 deletions wp-content/themes/core/assets/js/src/admin/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import hooks from './hooks';
import types from './types';
import * as tools from 'utils/tools';
import denyBlocks from './deny-blocks';
import pageTemplateBlockFilter from './page-template-block-filter';

/**
* @function init
Expand All @@ -12,6 +13,7 @@ const init = () => {
hooks();
types();
denyBlocks();
pageTemplateBlockFilter();

if ( tools.getNodes( '#editor.block-editor__container', false, document, true )[ 0 ] ) {
import( './preview' /* webpackChunkName:"editor-preview" */ ).then( ( module ) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { select, subscribe } from '@wordpress/data';
import { BLOCK_PAGE_TEMPLATE_FILTER } from '../config/wp-settings';
import { getBlockTypes, unregisterBlockType, registerBlockType } from '@wordpress/blocks';

const state = {
template: 'default', // The default WordPress template
deletedBlockMap: new Map(), // Stores the state of blocks that have been unregistered.
};

const { isTyping } = select( 'core/block-editor' );

/**
* Registers a block if it's been unregistered.
*
* @param {string} block - The block name.
*/
const restoreBlock = ( block ) => {
if ( ! state.deletedBlockMap.has( block ) ) {
return;
}

registerBlockType( block, state.deletedBlockMap.get( block ) );
state.deletedBlockMap.delete( block );
};

const updateAvailableBlocks = () => {
const templateMap = new Map( Object.entries( BLOCK_PAGE_TEMPLATE_FILTER ) );
const blockTypeMap = new Map( getBlockTypes().map( block => [ block.name, block ] ) );

if ( templateMap.size < 1 ) {
return false;
}

if ( templateMap.has( state.template ) ) {
const blocks = templateMap.get( state.template );
const restored = [];

for ( const block of blocks ) {
restoreBlock( block );
restored.push( block );
}

// Remove the blocks assigned to this template, so they aren't unregistered.
templateMap.delete( state.template );

if ( restored.length ) {
console.info( `SquareOne Admin: Restored blocks: "${ restored.join( ',' ) }" for template: ${ state.template }` );
}
}

// Unregister all page template blocks that aren't for the current page template.
const removedBlocks = [];

templateMap.forEach( blocks => {
for ( const block of blocks ) {
if ( blockTypeMap.has( block ) && ! state.deletedBlockMap.get( block ) ) {
state.deletedBlockMap.set( block, blockTypeMap.get( block ) );
unregisterBlockType( block );
removedBlocks.push( block );
}
}
} );

if ( removedBlocks.length ) {
console.info( `SquareOne Admin: Removed blocks: "${ removedBlocks.join( ',' ) }"` );
}
};

/**
* Subscribes an event listener to determine if we need
* to adjust the available blocks based on if the page template changed.
*/
const pageTemplateBlockFilter = () => {
subscribe( () => {
if ( isTyping() === true ) {
return false;
}

const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );

if ( newTemplate !== undefined && newTemplate !== state.template ) {
state.template = newTemplate;
updateAvailableBlocks();
}
} );
};

/**
* @function init
* @description Initialize module
*/

const init = () => {
// Set the initial block state
updateAvailableBlocks();
pageTemplateBlockFilter();
};

export default init;
1 change: 0 additions & 1 deletion wp-content/themes/core/components/card/js/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import delegate from 'delegate';
import * as tools from 'utils/tools';


/* Maximum amount of time between mousedown & mouseup to be considered a true click */
const MOUSEUP_THRESHOLD = 200;

Expand Down