-
Notifications
You must be signed in to change notification settings - Fork 854
Forms: Add form editor #46444
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
Forms: Add form editor #46444
Changes from 32 commits
f99ced4
450a20a
a773728
fe9ae69
9c7769f
0f34bbf
ac19ee7
34aa133
97dc16d
6955ec8
be905d6
1ce6287
af6aba4
c97fc46
e21b799
117de34
ebcdb85
a58e054
aaa2528
dddf110
94a376a
7592d53
a5f24aa
dbd2863
f62e29d
543bd41
2801f02
5eefef6
11a412d
c4e7886
cd58a05
7f63ed8
6b7a364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Forms: Add form editor under a feature flag |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| <?php | ||
| /** | ||
| * Jetpack forms editor. | ||
| * | ||
| * @package automattic/jetpack-forms | ||
| */ | ||
|
|
||
| namespace Automattic\Jetpack\Forms\Editor; | ||
|
|
||
| use Automattic\Jetpack\Assets; | ||
| use Automattic\Jetpack\Forms\ContactForm\Contact_Form; | ||
|
|
||
| /** | ||
| * Class Form_Editor | ||
| * | ||
| * Handles the form editor functionality for jetpack-form post type. | ||
| */ | ||
| class Form_Editor { | ||
|
|
||
| /** | ||
| * Script handle for the form editor. | ||
| * | ||
| * @var string | ||
| */ | ||
| const SCRIPT_HANDLE = 'jetpack-form-editor'; | ||
|
|
||
| /** | ||
| * Initialize the form editor. | ||
| */ | ||
| public static function init() { | ||
| add_filter( 'allowed_block_types_all', array( __CLASS__, 'allowed_blocks_for_jetpack_form' ), 10, 2 ); | ||
| add_filter( 'block_editor_settings_all', array( __CLASS__, 'block_editor_settings_all' ), 10, 2 ); | ||
| add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Restrict allowed blocks when editing jetpack-form posts. | ||
| * | ||
| * Only allows field blocks and supporting blocks. The contact-form block is excluded | ||
| * because visual wrapping is handled via DOM manipulation in the editor script. | ||
| * | ||
| * @param bool|array $allowed_block_types Array of block type slugs, or boolean to enable/disable all. | ||
| * @param object $editor_context The current editor context. | ||
| * @return bool|array Array of allowed block types for jetpack-form posts. | ||
| */ | ||
| public static function allowed_blocks_for_jetpack_form( $allowed_block_types, $editor_context ) { | ||
| // Only apply to jetpack-form post type. | ||
| if ( ! isset( $editor_context->post ) || Contact_Form::POST_TYPE !== $editor_context->post->post_type ) { | ||
| return $allowed_block_types; | ||
| } | ||
|
|
||
| // Allow only field blocks, button, and core blocks. | ||
| // Visual wrapping is handled by JavaScript DOM manipulation. | ||
| return array( | ||
| // Field blocks. | ||
| 'jetpack/field-name', | ||
| 'jetpack/field-email', | ||
| 'jetpack/field-url', | ||
| 'jetpack/field-telephone', | ||
| 'jetpack/field-textarea', | ||
| 'jetpack/field-checkbox', | ||
| 'jetpack/field-checkbox-multiple', | ||
| 'jetpack/field-radio', | ||
| 'jetpack/field-select', | ||
| 'jetpack/field-date', | ||
| 'jetpack/field-consent', | ||
| 'jetpack/field-rating', | ||
| 'jetpack/field-text', | ||
| 'jetpack/field-number', | ||
| 'jetpack/field-file-upload', | ||
|
|
||
| // Supporting blocks. | ||
| 'jetpack/button', | ||
| 'jetpack/label', | ||
| 'jetpack/input', | ||
| 'jetpack/options', | ||
| 'jetpack/option', | ||
| 'jetpack/phone-input', | ||
|
|
||
| // Multistep blocks. | ||
| 'jetpack/form-step', | ||
| 'jetpack/form-step-container', | ||
| 'jetpack/form-step-divider', | ||
| 'jetpack/form-step-navigation', | ||
| 'jetpack/form-progress-indicator', | ||
|
|
||
| // Core blocks for rich content. | ||
| 'core/audio', | ||
| 'core/columns', | ||
| 'core/column', | ||
| 'core/group', | ||
| 'core/heading', | ||
| 'core/html', | ||
| 'core/image', | ||
| 'core/list', | ||
| 'core/list-item', | ||
| 'core/paragraph', | ||
| 'core/row', | ||
| 'core/separator', | ||
| 'core/spacer', | ||
| 'core/stack', | ||
| 'core/subhead', | ||
| 'core/video', | ||
enejb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need jetpack/contact-form in this list? |
||
| } | ||
|
|
||
| /** | ||
| * Modify block editor settings for jetpack-form posts. | ||
| * | ||
| * @param array $settings Block editor settings. | ||
| * @param object $editor_context The current editor context. | ||
| * @return array Modified block editor settings for jetpack-form posts. | ||
| */ | ||
| public static function block_editor_settings_all( $settings, $editor_context ) { | ||
| // Only apply to jetpack-form post type. | ||
| if ( ! isset( $editor_context->post ) || Contact_Form::POST_TYPE !== $editor_context->post->post_type ) { | ||
| return $settings; | ||
| } | ||
|
|
||
| // Disable block locking capability. | ||
| $settings['canLockBlocks'] = false; | ||
|
|
||
| return $settings; | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue admin scripts for block editor. | ||
| * Loads in all post block editor contexts (excluding the site editor) so that the | ||
| * rename command is available and can be used when a form block is selected. | ||
enejb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public static function enqueue_admin_scripts() { | ||
| $screen = get_current_screen(); | ||
|
|
||
| // Only load in block editor contexts, not site editor | ||
| if ( ! $screen || $screen->id === 'site-editor' || ! $screen->is_block_editor ) { | ||
enejb marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to only be loading this JS when post_type === jetpack_form? I think that's also possible with the $screen var.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my testing when I do that (limit by post type) then the switching in the block editor (clicking the edit form button) doesn't quite work as expected. But since this feature doesn't load on the site-editor removing the script from the site editor is fine. |
||
| return; | ||
| } | ||
| $asset_file = __DIR__ . '/../../dist/form-editor/jetpack-form-editor.asset.php'; | ||
| if ( ! file_exists( $asset_file ) ) { | ||
| // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | ||
| error_log( 'Form Editor asset file not found: ' . $asset_file ); | ||
enejb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| $asset = require $asset_file; | ||
| Assets::register_script( | ||
| self::SCRIPT_HANDLE, | ||
| '../../dist/form-editor/jetpack-form-editor.js', | ||
| __FILE__, | ||
| array( | ||
| 'in_footer' => true, | ||
| 'textdomain' => 'jetpack-forms', | ||
| 'enqueue' => true, | ||
| 'dependencies' => $asset['dependencies'], | ||
| 'version' => $asset['version'], | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@enejb - the form editor if feature flagged, but this build code will always run and include the feature flagged form editor. Same with other package.json updates below specific to the form editor.
Is that a concern?
Update: I was also trying find other places the changes might leak out of the feature flag.
Anyways, just worth doing a sanity check that any code outside the feature flag will be safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing this up. This will definitely create scripts even though the feature flag is not enabled.
I think this is fine since there shouldn't be an impact on the user. (They are not downloading) the script when they visit the editor. (for example) but we might be distributing the assets (js and css) files even though they the site is under the feature flag. ( but the same cases exists for PHP files)