Skip to content

Commit

Permalink
add Block Patterns handler
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
kjroelke committed Aug 2, 2024
1 parent a291472 commit 0be148d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ A Template repo to help spin up custom child themes for clients quickly.

# Changelog

## 0.3.0

- Remove Core Patterns
- Disable Block Locking UI for non-admin users.
- Register Kingdom One pattern with basic php file.

## 0.2.2

- Add max-width and content sizes for Cornerstone with WordPress variables (generated by theme.json)
Expand Down
26 changes: 20 additions & 6 deletions includes/class-theme-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,35 @@ class Theme_Init {
*/
private Asset_Handler $asset_handler;

/**
* Block Handler
*
* @var Block_Handler $block_handler
*/
private Block_Handler $block_handler;

/**
* Theme_Init constructor.
*/
public function __construct() {
$this->load_required_files();
$this->asset_handler = new Asset_Handler( true );
$this->block_handler = new Block_Handler();
add_action( 'wp_enqueue_scripts', array( $this->asset_handler, 'enqueue_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this->asset_handler, 'dequeue_scripts' ), 40 );
add_action( 'init', array( $this->block_handler, 'register_patterns_category' ) );
add_action( 'after_setup_theme', array( $this, 'theme_setup' ) );
}

/**
* Load the required files
*/
private function load_required_files() {
$helpers = array( 'asset-handler' => null );
foreach ( $helpers as $file => $class ) {
$helpers = array( 'asset-handler', 'block-handler' );
foreach ( $helpers as $file ) {
require_once get_theme_file_path( "/includes/theme-helpers/class-{$file}.php" );
if ( $class ) {
$class = "KingdomOne\\{$class}";
new $class();
}
}

$files = array(
'login-handler' => 'Login_Handler',
'admin-dashboard-handler' => 'Admin_Dashboard_Handler',
Expand All @@ -54,4 +61,11 @@ private function load_required_files() {
}
}
}

/**
* Theme Setup
*/
public function theme_setup() {
remove_theme_support( 'core-block-patterns' );
}
}
58 changes: 58 additions & 0 deletions includes/theme-helpers/class-block-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Block Handler
* Helper class for handling WordPress Blocks
*
* @package KingdomOne
*/

namespace KingdomOne;

/**
* Class Block_Handler
*/
class Block_Handler {
/**
* Block_Handler constructor.
*/
public function __construct() {
add_filter(
'block_editor_settings_all',
array( $this, 'hide_block_locking_ui' ),
10,
2
);
add_filter( 'should_load_remote_block_patterns', '__return_false' );
}


/**
* Registers a "Kingdom One" Category to place all custom patterns inside of.
*/
public function register_patterns_category() {
register_block_pattern_category(
'kingdomone',
array(
'label' => esc_html__( 'Kingdom One', 'kingdomone' ),
'description' => esc_html__( 'Custom Patterns built by Kingdom One', 'kingdomone' ),
)
);
}

/**
* Hides the Block Lock settings for non-admin users on Tour post type.
*
* @param array $settings Default editor settings.
* @param \WP_Block_Editor_Context $context the block context object.
*
* @return array
*/
public function hide_block_locking_ui( $settings, $context ): array {
$is_admin = current_user_can( 'edit_files' );
if ( ! $is_admin ) {
$settings['canLockBlocks'] = false;
$settings['codeEditingEnabled'] = false;
}
return $settings;
}
}
31 changes: 31 additions & 0 deletions patterns/hero.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Title: Hero
* Slug: kingdomone/hero
* Categories: kingdomone
* Description: A hero block with a title, subtitle, and button. This is an example file. Delete me and add your own.
*
* @package KingdomOne
* @subpackage BlockPatterns
*/

?>
<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading">Here is an h2</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I'm doing some typing now.</p>
<!-- /wp:paragraph -->

<!-- wp:buttons -->
<div class="wp-block-buttons">
<!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Test!</a></div>
<!-- /wp:button -->
</div>
<!-- /wp:buttons -->
</div>
<!-- /wp:group -->
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Theme URI: https://github.com/kingdom-one/[client-repo]
Author: Kingdom One
Author URI: https://wwww.kingdomone.co
Description: A custom Wordpress theme for [Client Name], built on the back of Pro Theme (by Themeco).
Version: 0.2.2
Version: 0.3.0
License: GNU General Public License v2 or later
Template: pro
*/

0 comments on commit 0be148d

Please sign in to comment.