Skip to content
This repository was archived by the owner on May 2, 2022. It is now read-only.
Merged
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
5 changes: 4 additions & 1 deletion config/webpack.config.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ module.exports = {
// Styles.
{
test: /\.css$/,
include: path.resolve(process.cwd(), settings.paths.src.css),
include: [
path.resolve(process.cwd(), settings.paths.src.base),
path.resolve(process.cwd(), settings.paths.src.blocks),
],
use: [
{
loader: MiniCssExtractPlugin.loader,
Expand Down
3 changes: 2 additions & 1 deletion config/webpack.settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
'styleguide-style': './assets/css/styleguide/styleguide.css',

// Blocks
'example-block': './includes/blocks/example-block/register.js',
'example-block': './includes/blocks/example-block/',
},
filename: {
js: 'js/[name].js',
Expand All @@ -24,6 +24,7 @@ module.exports = {
paths: {
src: {
base: './assets/',
blocks: './includes/blocks/',
css: './assets/css/',
js: './assets/js/',
},
Expand Down
22 changes: 14 additions & 8 deletions includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ function setup() {
// Require custom blocks.
require_once TENUP_SCAFFOLD_BLOCK_DIR . '/example-block/register.php';

// Filter the plugins URL to allow us to have blocks in themes with linked assets. i.e editorScripts
add_filter( 'plugins_url', $n( 'filter_plugins_url' ), 10, 2 );
add_filter( 'init', $n( 'register' ) );

// Call block register functions.
Example\register();
}

/**
* Register Blocks.
*/
function register() {
// Filter the plugins URL to allow us to have blocks in themes with linked assets. i.e editorScripts
add_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10, 2 );
// Register Blocks
Example\register();
// <---- Add blocks here...
// Remove the filter after we register the blocks
remove_filter( 'plugins_url', $n( 'filter_plugins_url' ), 10, 2 );

remove_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10 );
}

/**
* Filter the plugins_url to allow us to use assets from theme.
*
Expand All @@ -46,8 +53,7 @@ function setup() {
*/
function filter_plugins_url( $url, $path ) {
$file = preg_replace( '/\.\.\//', '', $path );
$url = trailingslashit( get_stylesheet_directory_uri() ) . $file;
return $url;
return trailingslashit( get_stylesheet_directory_uri() ) . $file;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions includes/blocks/example-block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"type" : "string"
}
},
"editorScript": "file:../../../dist/js/example-block.js"
}
"editorScript": "file:../../../dist/js/example-block.js",
"editorStyle": "file:../../../dist/css/example-block.css"
}
23 changes: 10 additions & 13 deletions includes/blocks/example-block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ import { editPropsShape } from './props-shape';
* @param {Object} props The block props
* @return {Function} Render the edit screen
*/
const ExampleBockEdit = (props) => {
const {
attributes: { customTitle },
className,
setAttributes,
} = props;

const ExampleBockEdit = ({
attributes: { customTitle: currentTitle },
className,
setAttributes,
}) => {
return (
<div className={className}>
<div className={`${className}`}>
<RichText
className="wp-block-example-block__title"
tagName="h2"
placeholder={__('Custom Title')}
value={customTitle}
onChange={(title) => setAttributes({ customTitle: title })}
value={currentTitle}
onChange={(customTitle) => setAttributes({ customTitle })}
/>
</div>
);
};
// Set the propTypes
ExampleBockEdit.propTypes = {
...editPropsShape,
};
ExampleBockEdit.propTypes = editPropsShape;
export default ExampleBockEdit;
7 changes: 7 additions & 0 deletions includes/blocks/example-block/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Example Block
*/
.wp-block-example-block__title {
border: 3px dashed #ccc;
padding: 1em;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import { registerBlockType } from '@wordpress/blocks';
*/
import edit from './edit';
import save from './save';
import json from './block.json';

const { name } = json;
import { name } from './block.json';
import './index.css';

/**
* Register block
*/
export default registerBlockType(name, {
registerBlockType(name, {
title: __('Example Block'),
description: __('An Example Block'),
edit,
Expand Down
21 changes: 18 additions & 3 deletions includes/blocks/example-block/markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
* Example block markup
*
* @package TenUpScaffold\Blocks\Example
*
* @var $args
*/

// Set defaults.
$args = wp_parse_args(
$args,
[
'attributes' => [
'customTitle' => __( 'Custom title default', 'tenup' ),
],
'class_name' => 'wp-block-tenup-example',
]
);

?>
<h2 class="example-block-title">
<?php echo wp_kses_post( $attributes['customTitle'] ); ?>
</h2>
<div class="<?php echo esc_attr( $args['class_name'] ); ?>">
<h2 class="wp-block-example-block__title">
<?php echo wp_kses_post( $args['attributes']['customTitle'] ); ?>
</h2>
</div>
12 changes: 11 additions & 1 deletion includes/blocks/example-block/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ function register() {
*/
function render_block_callback( $attributes, $content, $block ) {
ob_start();
require TENUP_SCAFFOLD_BLOCK_DIR . 'example-block/markup.php';
get_template_part(
'includes/blocks/example-block/markup',
null,
[
'class_name' => 'wp-block-tenup-example',
'attributes' => $attributes,
'content' => $content,
'block' => $block,
]
);

return ob_get_clean();
}
4 changes: 1 addition & 3 deletions includes/blocks/example-block/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*
* @return {null} Dynamic blocks do not save the HTML.
*/
const ExampleBlockSave = () => {
return null;
};
const ExampleBlockSave = () => null;

export default ExampleBlockSave;