@wordpress/create-block-tutorial-template 2.32.1-next.f8d8eceb.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 3.0.0 (2023-11-16)
6
+
7
+ ### Breaking Change
8
+
9
+ - Update the block example scaffolded by the template.
10
+
11
+ ## 2.33.0 (2023-11-02)
12
+
5
13
  ## 2.32.0 (2023-10-18)
6
14
 
7
15
  ## 2.31.0 (2023-10-05)
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Create Block Tutorial Template
2
2
 
3
- This is a template for [`@wordpress/create-block`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/create-block/README.md) that is the finished version of the block in the official [WordPress Tutorial](https://github.com/WordPress/gutenberg/tree/HEAD/docs/getting-started/create-block/README.md) for the block editor.
3
+ This is a template for [`@wordpress/create-block`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/create-block/README.md) that creates an example "Copyright Date" block. This block is used in the official WordPress block development [Quick Start Guide](https://developer.wordpress.org/block-editor/getting-started/quick-start-guide).
4
4
 
5
5
  ## Usage
6
6
 
@@ -10,6 +10,8 @@ This block template can be used by running the following command:
10
10
  npx @wordpress/create-block --template @wordpress/create-block-tutorial-template
11
11
  ```
12
12
 
13
+ Use the default options when prompted in the terminal.
14
+
13
15
  ## Contributing to this package
14
16
 
15
17
  This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
@@ -1,17 +1,40 @@
1
1
  /**
2
- * WordPress components that create the necessary UI elements for the block
2
+ * Retrieves the translation of text.
3
3
  *
4
- * @see https://developer.wordpress.org/block-editor/packages/packages-components/
4
+ * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
5
5
  */
6
- import { TextControl } from '@wordpress/components';
6
+ import { __ } from '@wordpress/i18n';
7
7
 
8
8
  /**
9
- * React hook that is used to mark the block wrapper element.
10
- * It provides all the necessary props like the class name.
9
+ * Imports the InspectorControls component, which is used to wrap
10
+ * the block's custom controls that will appear in in the Settings
11
+ * Sidebar when the block is selected.
12
+ *
13
+ * Also imports the React hook that is used to mark the block wrapper
14
+ * element. It provides all the necessary props like the class name.
11
15
  *
16
+ * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#inspectorcontrols
12
17
  * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
13
18
  */
14
- import { useBlockProps } from '@wordpress/block-editor';
19
+ import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
20
+
21
+ /**
22
+ * Imports the necessary components that will be used to create
23
+ * the user interface for the block's settings.
24
+ *
25
+ * @see https://developer.wordpress.org/block-editor/reference-guides/components/panel/#panelbody
26
+ * @see https://developer.wordpress.org/block-editor/reference-guides/components/text-control/
27
+ * @see https://developer.wordpress.org/block-editor/reference-guides/components/toggle-control/
28
+ */
29
+ import { PanelBody, TextControl, ToggleControl } from '@wordpress/components';
30
+
31
+ /**
32
+ * Imports the useEffect React Hook. This is used to set an attribute when the
33
+ * block is loaded in the Editor.
34
+ *
35
+ * @see https://react.dev/reference/react/useEffect
36
+ */
37
+ import { useEffect } from 'react';
15
38
 
16
39
  /**
17
40
  * The edit function describes the structure of your block in the context of the
@@ -26,13 +49,59 @@ import { useBlockProps } from '@wordpress/block-editor';
26
49
  * @return {Element} Element to render.
27
50
  */
28
51
  export default function Edit( { attributes, setAttributes } ) {
29
- const blockProps = useBlockProps();
52
+ const { fallbackCurrentYear, showStartingYear, startingYear } = attributes;
53
+
54
+ // Get the current year and make sure it's a string.
55
+ const currentYear = new Date().getFullYear().toString();
56
+
57
+ // When the block loads, set the fallbackCurrentYear attribute to the
58
+ // current year if it's not already set.
59
+ useEffect( () => {
60
+ if ( currentYear !== fallbackCurrentYear ) {
61
+ setAttributes( { fallbackCurrentYear: currentYear } );
62
+ }
63
+ }, [ currentYear, fallbackCurrentYear, setAttributes ] );
64
+
65
+ let displayDate;
66
+
67
+ // Display the starting year as well if supplied by the user.
68
+ if ( showStartingYear && startingYear ) {
69
+ displayDate = startingYear + '–' + currentYear;
70
+ } else {
71
+ displayDate = currentYear;
72
+ }
73
+
30
74
  return (
31
- <div { ...blockProps }>
32
- <TextControl
33
- value={ attributes.message }
34
- onChange={ ( val ) => setAttributes( { message: val } ) }
35
- />
36
- </div>
75
+ <>
76
+ <InspectorControls>
77
+ <PanelBody title={ __( 'Settings', '{{textdomain}}' ) }>
78
+ <ToggleControl
79
+ checked={ showStartingYear }
80
+ label={ __(
81
+ 'Show starting year',
82
+ '{{textdomain}}'
83
+ ) }
84
+ onChange={ () =>
85
+ setAttributes( {
86
+ showStartingYear: ! showStartingYear,
87
+ } )
88
+ }
89
+ />
90
+ { showStartingYear && (
91
+ <TextControl
92
+ label={ __(
93
+ 'Starting year',
94
+ '{{textdomain}}'
95
+ ) }
96
+ value={ startingYear }
97
+ onChange={ ( value ) =>
98
+ setAttributes( { startingYear: value } )
99
+ }
100
+ />
101
+ ) }
102
+ </PanelBody>
103
+ </InspectorControls>
104
+ <p { ...useBlockProps() }>© { displayDate }</p>
105
+ </>
37
106
  );
38
107
  }
@@ -5,49 +5,41 @@
5
5
  */
6
6
  import { registerBlockType } from '@wordpress/blocks';
7
7
 
8
- /**
9
- * Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
10
- * All files containing `style` keyword are bundled together. The code used
11
- * gets applied both to the front of your site and to the editor. All other files
12
- * get applied to the editor only.
13
- *
14
- * @see https://www.npmjs.com/package/@wordpress/scripts#using-css
15
- */
16
- import './style.scss';
17
- import './editor.scss';
18
-
19
8
  /**
20
9
  * Internal dependencies
21
10
  */
22
11
  import Edit from './edit';
23
- {{#isStaticVariant}}
24
12
  import save from './save';
25
- {{/isStaticVariant}}
26
13
  import metadata from './block.json';
27
14
 
15
+ /**
16
+ * Define a custom SVG icon for the block. This icon will appear in
17
+ * the Inserter and when the user selects the block in the Editor.
18
+ */
19
+ const calendarIcon = (
20
+ <svg
21
+ viewBox="0 0 24 24"
22
+ xmlns="http://www.w3.org/2000/svg"
23
+ aria-hidden="true"
24
+ focusable="false"
25
+ >
26
+ <path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm.5 16c0 .3-.2.5-.5.5H5c-.3 0-.5-.2-.5-.5V7h15v12zM9 10H7v2h2v-2zm0 4H7v2h2v-2zm4-4h-2v2h2v-2zm4 0h-2v2h2v-2zm-4 4h-2v2h2v-2zm4 0h-2v2h2v-2z"></path>
27
+ </svg>
28
+ );
29
+
28
30
  /**
29
31
  * Every block starts by registering a new block type definition.
30
32
  *
31
33
  * @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
32
34
  */
33
35
  registerBlockType( metadata.name, {
34
- /**
35
- * Used to construct a preview for the block to be shown in the block inserter.
36
- */
37
- example: {
38
- attributes: {
39
- message: '{{title}}',
40
- },
41
- },
36
+ icon: calendarIcon,
42
37
  /**
43
38
  * @see ./edit.js
44
39
  */
45
40
  edit: Edit,
46
- {{#isStaticVariant}}
47
-
48
41
  /**
49
42
  * @see ./save.js
50
43
  */
51
44
  save,
52
- {{/isStaticVariant}}
53
45
  } );
@@ -1,10 +1,33 @@
1
- {{#isDynamicVariant}}
2
1
  <?php
3
2
  /**
3
+ * PHP file to use when rendering the block type on the server to show on the front end.
4
+ *
5
+ * The following variables are exposed to the file:
6
+ * $attributes (array): The block attributes.
7
+ * $content (string): The block default content.
8
+ * $block (WP_Block): The block instance.
9
+ *
4
10
  * @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render
5
11
  */
6
- ?>
7
- <p <?php echo get_block_wrapper_attributes(); ?>>
8
- <?php echo esc_html( $attributes['message'] ); ?>
9
- </p>
10
- {{/isDynamicVariant}}
12
+
13
+ // Get the current year.
14
+ $current_year = date( "Y" );
15
+
16
+ // Determine which content to display.
17
+ if ( isset( $attributes['fallbackCurrentYear'] ) && $attributes['fallbackCurrentYear'] === $current_year ) {
18
+
19
+ // The current year is the same as the fallback, so use the block content saved in the database (by the save.js function).
20
+ $block_content = $content;
21
+ } else {
22
+
23
+ // The current year is different from the fallback, so render the updated block content.
24
+ if ( ! empty( $attributes['startingYear'] ) && ! empty( $attributes['showStartingYear'] ) ) {
25
+ $display_date = $attributes['startingYear'] . '–' . $current_year;
26
+ } else {
27
+ $display_date = $current_year;
28
+ }
29
+
30
+ $block_content = '<p ' . get_block_wrapper_attributes() . '>© ' . esc_html( $display_date ) . '</p>';
31
+ }
32
+
33
+ echo wp_kses_post( $block_content );
@@ -1,4 +1,3 @@
1
- {{#isStaticVariant}}
2
1
  /**
3
2
  * React hook that is used to mark the block wrapper element.
4
3
  * It provides all the necessary props like the class name.
@@ -16,10 +15,27 @@ import { useBlockProps } from '@wordpress/block-editor';
16
15
  *
17
16
  * @param {Object} props Properties passed to the function.
18
17
  * @param {Object} props.attributes Available block attributes.
18
+ *
19
19
  * @return {Element} Element to render.
20
20
  */
21
21
  export default function save( { attributes } ) {
22
- const blockProps = useBlockProps.save();
23
- return <div { ...blockProps }>{ attributes.message }</div>;
22
+ const { fallbackCurrentYear, showStartingYear, startingYear } = attributes;
23
+
24
+ // If there is no fallbackCurrentYear, which could happen if the block
25
+ // is loaded from a template/pattern, return null. In this case, block
26
+ // rendering will be handled by the render.php file.
27
+ if ( ! fallbackCurrentYear ) {
28
+ return null;
29
+ }
30
+
31
+ let displayDate;
32
+
33
+ // Display the starting year as well if supplied by the user.
34
+ if ( showStartingYear && startingYear ) {
35
+ displayDate = startingYear + '–' + fallbackCurrentYear;
36
+ } else {
37
+ displayDate = fallbackCurrentYear;
38
+ }
39
+
40
+ return <p { ...useBlockProps.save() }>© { displayDate }</p>;
24
41
  }
25
- {{/isStaticVariant}}
package/index.js CHANGED
@@ -5,35 +5,35 @@ const { join } = require( 'path' );
5
5
 
6
6
  module.exports = {
7
7
  defaultValues: {
8
- slug: 'gutenpride',
9
- category: 'text',
10
- title: 'Gutenpride',
11
- description:
12
- 'A Gutenberg block to show your pride! This block enables you to type text and style it with the color font Gilbert from Type with Pride.',
13
- dashicon: 'flag',
8
+ slug: 'copyright-date-block',
9
+ title: 'Copyright Date',
10
+ description: "Display your site's copyright date.",
14
11
  attributes: {
15
- message: {
12
+ fallbackCurrentYear: {
13
+ type: 'string',
14
+ },
15
+ showStartingYear: {
16
+ type: 'boolean',
17
+ },
18
+ startingYear: {
16
19
  type: 'string',
17
- source: 'text',
18
- selector: 'div',
19
20
  },
20
21
  },
21
22
  supports: {
23
+ color: {
24
+ background: false,
25
+ text: true,
26
+ },
22
27
  html: false,
23
- },
24
- },
25
- variants: {
26
- static: {},
27
- dynamic: {
28
- attributes: {
29
- message: {
30
- type: 'string',
31
- },
28
+ typography: {
29
+ fontSize: true,
32
30
  },
33
- render: 'file:./render.php',
34
31
  },
32
+ editorScript: 'file:./index.js',
33
+ render: 'file:./render.php',
34
+ example: {},
35
+ wpEnv: true,
35
36
  },
36
37
  pluginTemplatesPath: join( __dirname, 'plugin-templates' ),
37
38
  blockTemplatesPath: join( __dirname, 'block-templates' ),
38
- assetsPath: join( __dirname, 'assets' ),
39
39
  };
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@wordpress/create-block-tutorial-template",
3
- "version": "2.32.1-next.f8d8eceb.0",
4
- "description": "Template for @wordpress/create-block used in the official WordPress tutorial.",
3
+ "version": "3.0.0",
4
+ "description": "This is a template for @wordpress/create-block that creates an example 'Copyright Date' block. This block is used in the official WordPress block development Quick Start Guide.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
7
7
  "keywords": [
8
8
  "wordpress",
9
9
  "create block",
10
- "block template"
10
+ "block template",
11
+ "quick start"
11
12
  ],
12
- "homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/docs/getting-started/create-block",
13
+ "homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/docs/getting-started/quick-start-guide",
13
14
  "repository": {
14
15
  "type": "git",
15
16
  "url": "https://github.com/WordPress/gutenberg.git",
@@ -21,5 +22,5 @@
21
22
  "publishConfig": {
22
23
  "access": "public"
23
24
  },
24
- "gitHead": "8d8fd197e202b8104ffb1cb83048efd0a6c3faf4"
25
+ "gitHead": "839018ff6029ba749780e288e08ff9cd898e50e8"
25
26
  }
@@ -8,7 +8,7 @@
8
8
  * Description: {{description}}
9
9
  {{/description}}
10
10
  * Version: {{version}}
11
- * Requires at least: 6.1
11
+ * Requires at least: 6.2
12
12
  * Requires PHP: 7.0
13
13
  {{#author}}
14
14
  * Author: {{author}}
@@ -41,7 +41,7 @@ if ( ! defined( 'ABSPATH' ) ) {
41
41
  *
42
42
  * @see https://developer.wordpress.org/reference/functions/register_block_type/
43
43
  */
44
- function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
44
+ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_init() {
45
45
  register_block_type( __DIR__ . '/build' );
46
46
  }
47
- add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
47
+ add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_init' );
@@ -3,7 +3,7 @@
3
3
  Contributors: {{author}}
4
4
  {{/author}}
5
5
  Tags: block
6
- Tested up to: 6.1
6
+ Tested up to: 6.4
7
7
  Stable tag: {{version}}
8
8
  {{#license}}
9
9
  License: {{license}}
Binary file
@@ -1,13 +0,0 @@
1
- /**
2
- * The following styles get applied inside the editor only.
3
- *
4
- * Replace them with your own styles or remove the file completely.
5
- */
6
-
7
- .wp-block-{{namespace}}-{{slug}} input[type="text"] {
8
- font-family: Gilbert, sans-serif;
9
- font-size: 64px;
10
- color: inherit;
11
- background: inherit;
12
- border: 0;
13
- }
@@ -1,17 +0,0 @@
1
- /**
2
- * The following styles get applied both on the front of your site
3
- * and in the editor.
4
- *
5
- * Replace them with your own styles or remove the file completely.
6
- */
7
-
8
- @font-face {
9
- font-family: Gilbert;
10
- src: url(../assets/gilbert-color.otf);
11
- font-weight: 700;
12
- }
13
-
14
- .wp-block-{{namespace}}-{{slug}} {
15
- font-family: Gilbert, sans-serif;
16
- font-size: 64px;
17
- }