Skip to content

Commit

Permalink
feat: new block w
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahcssiqueira committed Dec 4, 2023
1 parent 85d33b2 commit 9d9abf5
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 1 deletion.
Empty file removed blocks/block-w/.gitkeep
Empty file.
21 changes: 21 additions & 0 deletions blocks/block-w/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"title": "Block W",
"name": "xywz-blocks/block-w",
"description": "Block X",
"version": "1.0.0-beta",
"category": "xywz-blocks",
"icon": "feedback",
"supports": {
"html": true
},
"attributes": {
"exampleAttribute": {
"type": "string"
}
},
"keywords": ["basic", "model"],
"editorScript": "file:./build/index.js",
"editorStyle": "file:./src/style.css"
}
1 change: 1 addition & 0 deletions blocks/block-w/build/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-element', 'wp-i18n'), 'version' => '0b7ff61aac7e64581ba9');
1 change: 1 addition & 0 deletions blocks/block-w/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions blocks/block-w/src/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from "react";
import { __ } from "@wordpress/i18n";
import { useBlockProps } from "@wordpress/block-editor";

function Edit(props) {
function setExampleAttribute(e) {
props.setAttributes({ exampleAttribute: e.target.value });
}

return (
<div {...useBlockProps()}>
<div>
<h4>Block W</h4>
<p>Insert the attribute value below</p>
<input
type="text"
value={props.attributes.exampleAttribute}
onChange={setExampleAttribute}
placeholder="Attribute value that will be displayed in the frontend"
/>
</div>
</div>
);
}

export default Edit;
10 changes: 10 additions & 0 deletions blocks/block-w/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { registerBlockType } from "@wordpress/blocks";

import Edit from "./edit";
import Save from "./save";
import metadata from "../block.json";

registerBlockType(metadata, {
edit: Edit,
save: Save,
});
12 changes: 12 additions & 0 deletions blocks/block-w/src/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { __ } from "@wordpress/i18n";
import { useBlockProps } from "@wordpress/block-editor";

function Save(props) {
return (
<p {...useBlockProps.save()}>
{__(<div>{props.attributes.exampleAttribute}</div>)}
</p>
);
}

export default Save;
60 changes: 60 additions & 0 deletions inc/class-block-w.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Block W
*
* @package xywzblocks
*/

namespace XYWZBlocks\Inc;

/**
* Block W
*/
class Block_W {


/**
* Constructor for handle WordPress Hooks
*/
public function __construct() {
add_action( 'init', [ $this, 'block_w_register' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'block_w_enqueues' ] );
add_filter( 'block_categories_all', [ $this, 'register_new_category' ], 10, 2 );
}

/**
* Register Block
*/
public function block_w_register() {
register_block_type( __DIR__ );
}

/**
* Enqueues
*/
public function block_w_enqueues() {
wp_enqueue_script(
'block-w',
plugin_dir_url( __FILE__ ) . '../blocks/block-w/build/index.js',
[ 'wp-blocks', 'wp-i18n', 'wp-editor' ]
);

wp_enqueue_style(
'block-w',
plugin_dir_url( __FILE__ ) . '..style/style.css',
[],
);
}

/**
* Register custom category
*/
public function register_new_category( $categories ) {
$categories[] = [
'slug' => 'xywz-blocks',
'title' => 'XYW...Z Blocks',
];

return $categories;
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"start:block-x": "wp-scripts start ./blocks/block-x/src/index.js --output-path=./blocks/block-x/build",
"build:block-x": "wp-scripts build ./blocks/block-x/src/index.js --output-path=./blocks/block-x/build",
"start:block-y": "wp-scripts start ./blocks/block-y/src/index.js --output-path=./blocks/block-y/build",
"build:block-y": "wp-scripts build ./blocks/block-y/src/index.js --output-path=./blocks/block-y/build"
"build:block-y": "wp-scripts build ./blocks/block-y/src/index.js --output-path=./blocks/block-y/build",
"start:block-w": "wp-scripts start ./blocks/block-w/src/index.js --output-path=./blocks/block-w/build",
"build:block-w": "wp-scripts build ./blocks/block-w/src/index.js --output-path=./blocks/block-w/build"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions xywz-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@
require dirname( __FILE__ ) . '/inc/class-block-y.php';
use XYWZBlocks\Inc\Block_Y;
new Block_Y();

/**
* Block W
*/
require dirname( __FILE__ ) . '/inc/class-block-w.php';
use XYWZBlocks\Inc\Block_W;
new Block_W();

0 comments on commit 9d9abf5

Please sign in to comment.