Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update form block to use block.json for server-side block registration #41568

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Feb 5, 2025

Proposed changes:

Proposes using the block.json for the contact form block's PHP block registration.

Currently for the contact form block:

  1. There's a JavaScript declaration of the block type settings/metadata -
    export const settings = {
    apiVersion: 3,
    title: __( 'Form', 'jetpack-forms' ),
    description: __(
    'Create forms to collect data from site visitors and manage their responses.',
    'jetpack-forms'
    ),
    icon: { src: icon, foreground: getIconColor() },
    keywords: [
    _x( 'email', 'block search term', 'jetpack-forms' ),
    _x( 'feedback', 'block search term', 'jetpack-forms' ),
    _x( 'contact form', 'block search term', 'jetpack-forms' ),
    ],
    supports: {
    color: {
    link: true,
    gradients: true,
    },
    html: false,
    spacing: {
    padding: true,
    margin: true,
    },
    align: [ 'wide', 'full' ],
    },
    attributes: defaultAttributes,
    edit,
    save: () => {
    const blockProps = useBlockProps.save();
    return (
    <div { ...blockProps }>
    <InnerBlocks.Content />
    </div>
    );
    },
    example: {
    innerBlocks: [
    {
    name: 'jetpack/field-name',
    attributes: { required: true, label: __( 'Name', 'jetpack-forms' ) },
    },
    {
    name: 'jetpack/field-email',
    attributes: { required: true, label: __( 'Email', 'jetpack-forms' ) },
    },
    {
    name: 'jetpack/field-textarea',
    attributes: { label: __( 'Message', 'jetpack-forms' ) },
    },
    {
    name: 'jetpack/button',
    attributes: {
    text: __( 'Contact Us', 'jetpack-forms' ),
    element: 'button',
    lock: { remove: true },
    },
    },
    ],
    },
    styles: [
    { name: 'default', label: 'Default', isDefault: true },
    { name: 'animated', label: 'Animated' },
    { name: 'outlined', label: 'Outlined' },
    // Need to figure out some details. Putting on hold for now
    // { name: 'below', label: 'Below' },
    ],
    variations,
    category: 'contact-form',
    transforms,
    deprecated,
    };
  2. There's a block.json with block type settings/metadata, where many of the settings are copied, but this is unused by the jetpack codebase. I think it's probably there for the plugin directory -
    {
    "$schema": "https://schemas.wp.org/trunk/block.json",
    "apiVersion": 3,
    "name": "jetpack/contact-form",
    "title": "Form",
    "description": "Create forms to collect data from site visitors and manage their responses.",
    "keywords": [ "email", "feedback", "contact form" ],
    "version": "12.5.0",
    "textdomain": "jetpack-forms",
    "category": "contact-form",
    "icon": "<svg viewBox='0 0 24 24' width='24' height='24' xmlns='http://www.w3.org/2000/svg'><path d='m13 7.5 h 5 v 1.5 h -5 v -1.5z'/><path d='m13 15 h 5 v 1.5 h -5 v -1.5z'/><path d='m19.01,3H4.99c-1.1,0-1.99.89-1.99,1.99v14.02c0,1.1.89,1.99,1.99,1.99h14.02c1.1,0,1.99-.89,1.99-1.99V4.99c0-1.1-.89-1.99-1.99-1.99Zm.49,15.99c0,.28-.23.51-.51.51H5.01c-.28,0-.51-.23-.51-.51V5.01c0-.28.23-.51.51-.51h13.98c.28,0,.51.23.51.51v13.98Z'/><path d='m9.46,13h-1.92c-.85,0-1.54.69-1.54,1.54v1.92c0,.85.69,1.54,1.54,1.54h1.92c.85,0,1.54-.69,1.54-1.54v-1.92c0-.85-.69-1.54-1.54-1.54Zm.04,3.5h-2v-2h2v2Z'/><path d='m9.46,6h-1.92c-.85,0-1.54.69-1.54,1.54v1.92c0,.85.69,1.54,1.54,1.54h1.92c.85,0,1.54-.69,1.54-1.54v-1.92c0-.85-.69-1.54-1.54-1.54Zm.04,3.5h-2v-2h2v2Z'/></svg>",
    "supports": {
    "color": {
    "link": true,
    "gradients": true
    },
    "html": false,
    "spacing": {
    "padding": true,
    "margin": true
    },
    "align": [ "wide", "full" ]
    },
    "attributes": {},
    "editorScript": "file:./editor.js"
    }
  3. There's a PHP registration, and the same block type settings/metadata can also be provided in the array, but there are none declared -
    public static function register_block() {
    Blocks::jetpack_register_block(
    'jetpack/contact-form',
    array(
    'render_callback' => array( __CLASS__, 'gutenblock_render_form' ),
    )
    );

The impact of the third one is that the block doesn't declare any of its block supports properties or attribute definitions (no default values) for the server side implementation of the block. It hasn't caused an issue right now, but for #41428, layout support would have to be triplicated across all three places.

Looking at other blocks in the jetpack codebase, it seems the PHP registration (3) can use the values from the block.json by passing an absolute path with the location of the file to jetpack_register_block method instead of the block name. This PR uses the __DIR__ constant to do that, just like other blocks do.

I'm not sure if there are any unwanted side effects from doing this, but I thought I'd make a PR to propose it ... I still need to test it on WPCOM, but hopefully it works there too.

In the future, it might be nice to be able to import the JSON values into the JS block registration code too so that there's a single source of truth, but perhaps that will require some updates to the jetpack build code.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  1. Add different types of form blocks to a post
  2. Check that the block appears on the frontend as expected

@talldan talldan added [Type] Bug When a feature is broken and / or not performing as intended [Status] Needs Team Review [Block] Contact Form Form block (also see Contact Form label) [Package] Forms labels Feb 5, 2025
@talldan talldan self-assigned this Feb 5, 2025
Copy link
Contributor

github-actions bot commented Feb 5, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the update/contact-form-block-to-use-block-json-on-server branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack update/contact-form-block-to-use-block-json-on-server
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

github-actions bot commented Feb 5, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!

Copy link
Contributor

github-actions bot commented Feb 5, 2025

Code Coverage Summary

This PR did not change code coverage!

That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷

Full summary · PHP report · JS report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Contact Form Form block (also see Contact Form label) [Feature] Contact Form [Package] Forms [Status] Needs Team Review [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant