=> Jetpack_Forms::is_mailpoet_enabled(), // So the file field's "Maximum files" control offers exactly what the site will honour, // including when a filter has raised the ceiling. 'maxFilesLimit' => Contact_Form_Field::get_file_field_max_files_limit(), ), ); wp_add_inline_script( $handle, 'window.jpFormsBlocks = ' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' ); } /** * Conditionally loads the AI form generation integration script. * * This script is only loaded when: * 1. The AI Assistant extension is available (ai-assistant-form-support) * 2. The central-form-management feature flag is enabled * * By checking these conditions in PHP, we ensure no JavaScript is loaded * when either the AI extension is disabled or central form management is off. * * This is hooked at priority 11 on enqueue_block_editor_assets to ensure * it runs after Jetpack_Gutenberg registers extensions at priority 10. */ public static function maybe_load_ai_integration() { // Bail if the user cannot manage the block — jp-forms-blocks won't be registered. if ( ! self::can_manage_block() ) { return; } // Check if central form management is enabled. if ( ! Contact_Form_Plugin::has_editor_feature_flag( 'central-form-management' ) ) { return; } // Check if AI Assistant form support is available. // This extension is set as available when the AI Assistant block is registered. if ( ! class_exists( 'Jetpack_Gutenberg' ) ) { return; } // Ensure extensions are registered by calling get_cached_availability(). \Jetpack_Gutenberg::get_cached_availability(); if ( ! \Jetpack_Gutenberg::is_available( 'ai-assistant-form-support' ) ) { return; } Assets::register_script( 'jp-forms-ai-plugin', '../../../dist/blocks/ai-form-plugin.js', __FILE__, array( 'dependencies' => array( 'jp-forms-blocks' ), 'in_footer' => true, 'textdomain' => 'jetpack-forms', 'enqueue' => true, ) ); } /** * Add REST API endpoints to the block editor preload list. * * @param array $paths Existing paths to preload. * @return array Updated paths to preload. */ public static function preload_endpoints( $paths ) { $paths[] = array( '/wp/v2/feedback/config', 'GET' ); $paths[] = array( '/wp/v2/feedback/config?_locale=user', 'GET' ); return $paths; } /** * Loads scripts */ public static function load_view_scripts() { if ( is_admin() ) { // A block's view assets will not be required in wp-admin. return; } Assets::register_script( 'jp-forms-blocks', '../../../dist/blocks/view.js', __FILE__, array( 'in_footer' => true, 'strategy' => 'defer', 'textdomain' => 'jetpack-forms', 'enqueue' => true, ) ); } /** * Register a minimal jetpack-blocks-editor script when the Blocks module is inactive. * * The Forms editor JS depends on jetpack-blocks-editor for the * Jetpack_Editor_Initial_State global, which includes block availability data. * Normally the Blocks module registers this, but Forms needs to work independently. */ private static function maybe_register_blocks_editor_script() { if ( wp_script_is( 'jetpack-blocks-editor', 'registered' ) ) { return; } // When the Blocks module is active it will register the real script, so bail. if ( class_exists( 'Jetpack' ) && ( new Modules() )->is_active( 'blocks' ) ) { return; } // Register a minimal script to satisfy the dependency. wp_register_script( 'jetpack-blocks-editor', '', array(), JETPACK__VERSION, true ); wp_register_style( 'jetpack-blocks-editor', false, array(), JETPACK__VERSION ); // Provide the initial state the Forms editor JS expects: // - available_blocks: the JS block registration checks this before calling registerBlockType. // - modules: the useModuleStatus hook reads this to decide whether to show the block // or an "Activate Forms" placeholder. // - feature_flags: hasFeatureFlag() reads this for central-form-management, // form-webhooks, and multistep-form. wp_localize_script( Shared_Stores_Assets::SCRIPT_HANDLE, 'Jetpack_Editor_Initial_State', array( 'available_blocks' => array( 'contact-form' => array( 'available' => true ), ), 'modules' => array( 'contact-form' => array( 'activated' => true ), ), 'feature_flags' => apply_filters( 'jetpack_block_editor_feature_flags', array() ), ) ); } /** * Check if the current user can view the block. * Every user can see it if the Contact Form module is active, * but if it is inactive, only admins can see it. * * This is only useful when the Contact Form package is used within the Jetpack plugin, * where the module logic exists. * * @since 0.49.0 * * @return bool */ public static function can_manage_block() { if ( /** * Allow third-parties to override the form block's visibility. * * @since 0.49.0 * * @module contact-form * * @param bool $can_manage_block Whether the current user can manage the block. */ apply_filters( 'jetpack_contact_form_can_manage_block', false ) ) { return true; } if ( ! class_exists( 'Jetpack' ) ) { return true; } return ( new Modules() )->is_active( 'contact-form' ) || current_user_can( 'jetpack_activate_modules' ); } }