Skip to content

Commit

Permalink
Introduce block (and everything related) to print labels
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Oct 8, 2023
1 parent c331c35 commit b6b2e85
Show file tree
Hide file tree
Showing 29 changed files with 809 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/label-overview.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-blocks', 'wp-i18n'), 'version' => 'eb65da5231c4319dc505');
<?php return array('dependencies' => array('wp-blocks', 'wp-i18n'), 'version' => '5c7e8717e8026da41a9a');
2 changes: 1 addition & 1 deletion build/label-overview.js

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

41 changes: 41 additions & 0 deletions build/label-printing/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "figuren-theater/label-printing",
"version": "0.1.0",
"title": "Label Printing",
"category": "widgets",
"icon": "grid-view",
"description": "Make labels now.",
"example": {},
"attributes": {
"wpLabelPost": {
"type": "number",
"default": 0
},
"labelHeight": {
"type": "number",
"default": 0
},
"labelWidth": {
"type": "number",
"default": 0
},
"align": {
"type": "string",
"default": "wide"
}
},
"supports": {
"html": false,
"reusable": false,
"multiple": false,
"align": [
"wide"
]
},
"textdomain": "label-printing",
"editorScript": "file:label-printing.js",
"editorStyle": "file:./label-printing.css",
"style": "file:./style-label-printing.css"
}
50 changes: 50 additions & 0 deletions build/label-printing/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Figuren_Theater label_printing Blocks.
*
* @package figuren-theater/label-printing
*/

namespace Figuren_Theater\Label_Printing\Blocks\Printing;

use Figuren_Theater\Label_Printing\Patterns;

/**
* Render callback of the 'figuren-theater/label-printing' block.
*
* Shows a printable sheet with multiple same instances of the coosen label.
*
* @param array $attributes The blocks attributes.
*
* @return string
*/
function render( array $attributes ) : string {

// Get the label post object based on the provided attribute.
$label = \get_post( $attributes['wpLabelPost'] );

if ( ! $label instanceof \WP_Post ) {
return '';
}

// Define the pattern slug for the label.
$pattern_slug = 'figuren-theater/label-view-a4-' . $label->ID;

// Get the labels measurements.
$meta = \get_post_meta( $label->ID, Patterns\META_KEY, true );

// Check if the metadata is valid.
if ( ! \is_array( $meta ) || ! isset( $meta['height'] ) || ! isset( $meta['width'] ) ) {
return '';
}

// Generate Inline-CSS using a custom-property to set the labels printing dimensions.
$css = \sprintf(
'<style>:root { --label-printing-height:%smm;--label-printing-width:%smm; }</style>',
$meta['height'],
$meta['width'],
);

// Render the Inline-CSS and the block pattern with the specified pattern slug.
return $css . \do_blocks( '<!-- wp:pattern {"slug":"' . $pattern_slug . '"} -->' );
}
1 change: 1 addition & 0 deletions build/label-printing/label-printing.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '6395cbb53da3629088d7');
1 change: 1 addition & 0 deletions build/label-printing/label-printing.css

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

1 change: 1 addition & 0 deletions build/label-printing/label-printing.js

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

1 change: 1 addition & 0 deletions build/label-printing/style-label-printing.css

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

18 changes: 18 additions & 0 deletions build/label-proxy/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "figuren-theater/label-proxy",
"version": "0.1.0",
"title": "Label Proxy",
"category": "widgets",
"icon": "networking",
"description": "Shows the content of the Label provided by the label-creator block, inside the print-sheet template for a given physical label.",
"example": {},
"supports": {
"html": false,
"reusable": false,
"inserter": false
},
"textdomain": "label-printing",
"editorScript": "file:label-proxy.js"
}
42 changes: 42 additions & 0 deletions build/label-proxy/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Figuren_Theater label_printing Blocks.
*
* @package figuren-theater/label-printing
*/

namespace Figuren_Theater\Label_Printing\Blocks\Proxy;

/**
* Render callback of the 'figuren-theater/label-proxy' block.
*
* Works like a 'local reusable block' as long as we have no native one.
*
* @todo #12 Create a local-only reusable-block or a ‚reusable-block light‘
*
* @return string
*/
function render() : string {

$post = \get_post();

$blocks = \parse_blocks( $post->post_content );

foreach ( $blocks as $block ) {

/* // Works*/
if ( 'figuren-theater/label-printing' === $block['blockName'] ) {

\ob_start();
foreach ( $block['innerBlocks'] as $block ) {

echo \esc_html( \apply_filters( 'the_content', \render_block( $block ) ) );
}
return \ob_get_clean();

break;

}
}

}
1 change: 1 addition & 0 deletions build/label-proxy/label-proxy.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-blocks', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '118f2f1019f3791772f6');
1 change: 1 addition & 0 deletions build/label-proxy/label-proxy.js

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

45 changes: 45 additions & 0 deletions src/block-editor/blocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# multiple-blocks-template

This template will scaffold a plugin that is set up for registering multiple blocks in a single plugin create using the [`@wordpress/create-block`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/) tool.

## Usage
Run the following in the terminal of your choice:

`npx @wordpress/create-block example-plugin --template @ryanwelcher/multiple-blocks-template`

The name of the plugin ( example-plugin in the example above) is used as both the name of the plugin and the name of the block being created. This is due to the current `@wordpress/create-block` tool expecting a single block per plugin.


## Structure

Once the command has completed, the following structure will be created.

```
- assets - this contains an internal template for subsequent blocks
- build
- src
--> block-editor
---> blocks
----> {example-block}
-----> block.json
-----> edit.js
-----> editor.scss
-----> index.js
-----> save.js
-----> style.scss
.editorconfig
.eslintrc
.gitignore
{example-block}.php
package.json
package-lock.json
webpack.config.js
```

### Adding another block requires the following steps:

1. From the root of the plugin, cd into the includes/block-editor/blocks directory
2. Run the following command: `npx @wordpress/create-block --template ../../../assets
3. Add a new entry point in `webpack.config.js` that points to the new block.
4. ~~Update the `$blocks` array in the main plugin php file to include the new block.~~
5. Update the editorScripts, editorStyles, and styles items in the newly added block.
39 changes: 39 additions & 0 deletions src/block-editor/blocks/label-printing/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "figuren-theater/label-printing",
"version": "0.1.0",
"title": "Label Printing",
"category": "widgets",
"icon": "grid-view",
"description": "Make labels now.",
"example": {},
"attributes": {
"wpLabelPost": {
"type": "number",
"default": 0
},
"labelHeight": {
"type": "number",
"default": 0
},
"labelWidth": {
"type": "number",
"default": 0
},
"align": {
"type": "string",
"default": "wide"
}
},
"supports": {
"html": false,
"reusable": false,
"multiple": false,
"align": ["wide"]
},
"textdomain": "label-printing",
"editorScript": "file:label-printing.js",
"editorStyle": "file:./label-printing.css",
"style": "file:./style-label-printing.css"
}
Loading

0 comments on commit b6b2e85

Please sign in to comment.