Skip to content

Commit

Permalink
Introduce block (and everything related) to print labels (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion authored Oct 9, 2023
2 parents 667c483 + 9c486a3 commit f629614
Show file tree
Hide file tree
Showing 42 changed files with 1,158 additions and 128 deletions.
8 changes: 8 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@
</properties>
</rule>

<rule ref="HM.Files.NamespaceDirectoryName.NoIncDirectory">
<exclude-pattern>*/src/*</exclude-pattern>
</rule>

<rule ref="HM.Files.FunctionFileName.WrongFile">
<exclude-pattern>*/src/*</exclude-pattern>
</rule>

</ruleset>
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.

24 changes: 8 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"name": "figuren-theater/label-printing",
"description": "Create printable labels with blocks.",
"license": "GPL-3.0+",
"license": "GPL-3.0-or-later",
"type": "wordpress-plugin",
"keywords": [
"wordpress",
"wordpress-plugin"
"wordpress-plugin",
"Theater",
"Gutenberg",
"Blocks",
"Label-Printing",
"print.css"
],
"authors": [
{
Expand All @@ -28,12 +33,6 @@
"require-dev": {
"figuren-theater/code-quality": "*"
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
"autoload": {
"classmap": [
"inc/"
Expand All @@ -46,17 +45,10 @@
"allow-plugins": {
"composer/installers": true,
"koodimonni/composer-dropin-installer": true,
"figuren-theater/altis-core": true,
"phpstan/extension-installer": true,
"ergebnis/composer-normalize": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"figuren-theater/ft-core": true
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
},
"extra": {
"altis": {
"install-overrides": []
}
}
}
2 changes: 1 addition & 1 deletion composer.lock

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

47 changes: 11 additions & 36 deletions inc/block-styles/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
namespace Figuren_Theater\Label_Printing\Block_Styles;

/**
* Register module.
* Register block-styles.
*
* This function registers block styles used by the (inner workings of the) Label Printing block.
*
* @return void
*/
function register() :void {
add_action( 'init', __NAMESPACE__ . '\\bootstrap', 1 );
\add_action( 'init', __NAMESPACE__ . '\\bootstrap', 1 );
}

/**
* Bootstrap module, when enabled.
* Bootstrap block-style registration.
*
* This function registers two block styles using the WordPress core API.
*
* @todo #9 Use JS to register block-styles
* @todo #10 Use wp-scripts to build CSS from SCSS
Expand All @@ -27,55 +31,26 @@ function register() :void {
*/
function bootstrap() :void {

// Register A4 portrait block style.
\register_block_style(
'core/group',
[
'name' => 'label-overview-portrait',
'label' => __( 'A4 portrait', 'label-printing' ),
'is_default' => false,
'inline_style' => '.wp-block-group.is-style-label-overview-portrait { --label-printing-doc-width: 21cm; --label-printing-doc-height: 29.7cm }' . get_print_css_basics(),
'inline_style' => '.wp-block-group.is-style-label-overview-portrait { --label-printing-doc-width: 21cm; --label-printing-doc-height: 29.7cm }',
]
);

// Register A4 landscape block style.
\register_block_style(
'core/group',
[
'name' => 'label-overview-landscape',
'label' => __( 'A4 landscape', 'label-printing' ),
'is_default' => false,
'inline_style' => '.wp-block-group.is-style-label-overview-landscape { --label-printing-doc-width: 29.7cm; --label-printing-doc-height: 21cm }' . get_print_css_basics(),
'inline_style' => '.wp-block-group.is-style-label-overview-landscape { --label-printing-doc-width: 29.7cm; --label-printing-doc-height: 21cm }',
]
);
}

/**
* Get common used CSS for print & screen.
*
* @return string
*/
function get_print_css_basics() : string {
return '
.wp-block-group.alignwide[class*="is-style-label-overview-"] {
max-width: var(--label-printing-doc-width) !important;
}
@media print {
html, body, .wp-site-blocks {
margin: 0 !important;
padding: 0 !important;
}
}
@media screen {
.wp-block-group.alignwide[class*="is-style-label-overview-"] {
box-shadow: 0.3em 0.3em 1em grey;
}
.wp-block-group.alignwide[class*="is-style-label-overview-"] > .wp-block-group > * {
outline: 1px dashed lightgrey;
}
body:has([class*="is-style-label-overview-"]) {
background-color: Gainsboro !important;
padding: 2cm 0 5cm;
}
}
';
}
17 changes: 4 additions & 13 deletions inc/block-variations/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use Figuren_Theater\Label_Printing;

/**
* Register module.
* Register block-variations.
*
* @return void
*/
function register() :void {
\add_action( 'init', __NAMESPACE__ . '\\register_assets', 5 );

\add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_assets' );

}

/**
Expand All @@ -27,17 +25,13 @@ function register() :void {
* @return void
*/
function register_assets() :void {

\array_map(
__NAMESPACE__ . '\\register_asset',
get_assets()
);
\array_map( __NAMESPACE__ . '\\register_asset', get_assets() );
}

/**
* Get backend-only editor assets.
*
* @return string[]
* @return string[] Array of asset slugs.
*/
function get_assets() : array {
return [
Expand Down Expand Up @@ -96,10 +90,7 @@ function register_asset( string $asset ) : void {
* @return void
*/
function enqueue_assets() : void {
\array_map(
__NAMESPACE__ . '\\enqueue_asset',
get_assets()
);
\array_map( __NAMESPACE__ . '\\enqueue_asset', get_assets() );
}

/**
Expand Down
Loading

0 comments on commit f629614

Please sign in to comment.