2323 */
2424class Static_Site_Importer_Form_Seeder {
2525
26+ /** Whether this process has explicitly completed the late Jetpack Forms init. */
27+ private static bool $ jetpack_forms_initialized = false ;
28+
2629 /** Return the provider block emitted for one mapped form binding. */
2730 public static function binding_block_markup ( array $ entity , array $ result ): string {
2831 unset( $ entity );
@@ -61,6 +64,11 @@ public static function required_block_types(): array {
6164 return array_values ( array_unique ( array_merge ( array ( 'jetpack/contact-form ' , 'jetpack/field-checkbox-multiple ' , 'jetpack/input ' , 'jetpack/label ' , 'jetpack/option ' , 'jetpack/options ' , 'jetpack/phone-input ' ), array_values ( self ::field_block_map () ) ) ) );
6265 }
6366
67+ /** @return array<int,string> Provider APIs required by the declared adapter. */
68+ public static function required_runtime_apis (): array {
69+ return array ( 'Automattic \\Jetpack \\Forms \\ContactForm \\Contact_Form ' );
70+ }
71+
6472 /**
6573 * Materialize Jetpack contact forms from a validated forms manifest.
6674 *
@@ -152,66 +160,56 @@ public static function jetpack_forms_available(): bool {
152160 return ! empty ( $ availability ['available ' ] );
153161 }
154162
155- /** Activate Jetpack's Forms module and register its server-side block types . */
163+ /** Activate and prepare Jetpack Forms through its canonical module lifecycle . */
156164 public static function prepare_jetpack_forms_runtime () {
157- if ( ! class_exists ( 'Jetpack ' ) ) {
158- return new WP_Error ( 'static_site_importer_jetpack_class_missing ' , 'Jetpack activated without exposing its runtime class. ' );
159- }
160- $ modules_class = 'Automattic \\Jetpack \\Modules ' ;
161- if ( ! class_exists ( $ modules_class ) ) {
162- return new WP_Error ( 'static_site_importer_jetpack_modules_api_missing ' , 'Jetpack activated without exposing its modules API. ' );
163- }
164- $ modules = new $ modules_class ();
165- $ status_class = 'Automattic \\Jetpack \\Status ' ;
166- $ cache_class = 'Automattic \\Jetpack \\Status \\Cache ' ;
167- $ connection_ready_callback = self ::optional_static_callback ( 'Jetpack ' , 'is_connection_ready ' );
168- if (
169- null !== $ connection_ready_callback
170- && ! $ connection_ready_callback ()
171- && class_exists ( $ status_class )
172- && ! ( new $ status_class () )->is_offline_mode ()
173- ) {
174- update_option ( 'jetpack_offline_mode ' , true , false );
175- $ cache_clear_callback = self ::optional_static_callback ( $ cache_class , 'clear ' );
176- if ( null !== $ cache_clear_callback ) {
177- $ cache_clear_callback ();
178- }
165+ if ( ! class_exists ( 'Jetpack ' ) || ! class_exists ( 'Automattic \\Jetpack \\Modules ' ) ) {
166+ return self ::jetpack_forms_runtime_error ( 'static_site_importer_jetpack_forms_runtime_missing ' , array ( 'Jetpack ' , 'Automattic \\Jetpack \\Modules ' ) );
179167 }
168+
169+ $ modules = new Automattic \Jetpack \Modules ();
180170 if ( ! $ modules ->is_active ( 'contact-form ' ) ) {
181- Jetpack::activate_module ( 'contact-form ' , false , false );
171+ if ( ! method_exists ( 'Jetpack ' , 'activate_module ' ) ) {
172+ return self ::jetpack_forms_runtime_error ( 'static_site_importer_jetpack_forms_activation_missing ' , array ( 'Jetpack::activate_module ' ) );
173+ }
174+ $ activated = Jetpack::activate_module ( 'contact-form ' , false , false );
175+ if ( false === $ activated || ( function_exists ( 'is_wp_error ' ) && is_wp_error ( $ activated ) ) ) {
176+ return self ::jetpack_forms_runtime_error ( 'static_site_importer_jetpack_forms_activation_failed ' , array ( 'contact-form ' ) );
177+ }
182178 }
183- if ( ! $ modules ->is_active ( 'contact-form ' ) ) {
184- return new WP_Error (
185- 'static_site_importer_jetpack_forms_module_inactive ' ,
186- 'Jetpack did not activate its contact-form module. ' ,
187- array (
188- 'module_active ' => false ,
189- 'availability ' => self ::jetpack_forms_availability_details (),
190- )
191- );
179+
180+ $ loader = 'Automattic \\Jetpack \\Forms \\Jetpack_Forms ' ;
181+ if ( ! class_exists ( $ loader ) || ! method_exists ( $ loader , 'load_contact_form ' ) ) {
182+ return self ::jetpack_forms_runtime_error ( 'static_site_importer_jetpack_forms_loader_missing ' , array ( $ loader . '::load_contact_form ' ) );
192183 }
184+ $ loader ::load_contact_form ();
193185
194- $ block_class = 'Automattic \\Jetpack \\Extensions \\Contact_Form \\Contact_Form_Block ' ;
195- $ register_block_callback = self ::optional_static_callback ( $ block_class , 'register_block ' );
196- $ register_child_blocks_callback = self ::optional_static_callback ( $ block_class , 'register_child_blocks ' );
197- if ( null !== $ register_block_callback && null !== $ register_child_blocks_callback ) {
198- $ register_block_callback ();
199- $ register_child_blocks_callback ();
186+ $ initializer = 'Automattic \\Jetpack \\Forms \\ContactForm \\Contact_Form_Plugin ' ;
187+ if ( function_exists ( 'did_action ' ) && did_action ( 'init ' ) && ! self ::$ jetpack_forms_initialized ) {
188+ if ( ! class_exists ( $ initializer ) || ! method_exists ( $ initializer , 'init ' ) ) {
189+ return self ::jetpack_forms_runtime_error ( 'static_site_importer_jetpack_forms_init_missing ' , array ( $ initializer . '::init ' ) );
190+ }
191+ $ initializer ::init ();
192+ self ::$ jetpack_forms_initialized = true ;
200193 }
201194
202195 $ availability = self ::jetpack_forms_availability_details ();
203- return ! empty ( $ availability ['available ' ] )
204- ? true
205- : new WP_Error ( 'static_site_importer_jetpack_forms_blocks_missing ' , 'Jetpack Forms activated without registering every required provider block. ' , array (
206- 'module_active ' => true ,
207- 'availability ' => $ availability ,
208- ) );
196+ if ( ! empty ( $ availability ['available ' ] ) ) {
197+ return true ;
198+ }
199+
200+ return self ::jetpack_forms_runtime_error (
201+ 'static_site_importer_jetpack_forms_blocks_missing ' ,
202+ array_keys ( array_filter ( $ availability ['required_blocks ' ], static fn ( bool $ registered ): bool => ! $ registered ) ),
203+ $ availability
204+ );
209205 }
210206
211- /** Resolve an optional provider API without coupling analysis to one installed version. */
212- private static function optional_static_callback ( string $ class_name , string $ method_name ): ?callable {
213- $ callback = array ( $ class_name , $ method_name );
214- return is_callable ( $ callback ) ? $ callback : null ;
207+ /** Build a bounded provider-readiness error. */
208+ private static function jetpack_forms_runtime_error ( string $ code , array $ missing , array $ details = array () ): WP_Error {
209+ return new WP_Error ( $ code , 'Jetpack Forms provider runtime is not ready. ' , array_filter ( array (
210+ 'missing ' => array_slice ( array_values ( $ missing ), 0 , 20 ),
211+ 'details ' => $ details ,
212+ ) ) );
215213 }
216214
217215 /**
@@ -220,7 +218,11 @@ private static function optional_static_callback( string $class_name, string $me
220218 * @return array<string,mixed>
221219 */
222220 public static function jetpack_forms_availability_details (): array {
223- $ contact_form_class = class_exists ( 'Automattic \\Jetpack \\Forms \\ContactForm \\Contact_Form ' );
221+ $ required_apis = array ();
222+ foreach ( self ::required_runtime_apis () as $ api ) {
223+ $ required_apis [ $ api ] = class_exists ( $ api );
224+ }
225+ $ contact_form_class = ! empty ( $ required_apis ['Automattic \\Jetpack \\Forms \\ContactForm \\Contact_Form ' ] );
224226 $ legacy_class = class_exists ( 'Grunion_Contact_Form ' ) || class_exists ( 'Contact_Form ' );
225227 $ registered_blocks = array_fill_keys ( self ::required_block_types (), false );
226228
@@ -232,13 +234,17 @@ public static function jetpack_forms_availability_details(): array {
232234 }
233235 $ contact_form_block = $ registered_blocks ['jetpack/contact-form ' ];
234236 $ field_text_block = $ registered_blocks ['jetpack/field-text ' ];
237+ $ required_blocks_available = ! empty ( $ registered_blocks ) && ! in_array ( false , $ registered_blocks , true );
238+ $ required_apis_available = ! empty ( $ required_apis ) && ! in_array ( false , $ required_apis , true );
235239
236240 return array (
237- 'available ' => ( $ contact_form_class || $ legacy_class ) && ! in_array ( false , $ registered_blocks , true ) ,
241+ 'available ' => $ required_apis_available && $ contact_form_block && $ required_blocks_available ,
238242 'contact_form_class ' => $ contact_form_class ,
239243 'legacy_class ' => $ legacy_class ,
240244 'contact_form_block ' => $ contact_form_block ,
241245 'field_text_block ' => $ field_text_block ,
246+ 'required_apis ' => $ required_apis ,
247+ 'required_blocks ' => $ registered_blocks ,
242248 'registered_blocks ' => $ registered_blocks ,
243249 );
244250 }
0 commit comments