adapter = $adapter; $this->settings = $settings; $this->compiler = $compiler; } /** * Enqueue the progressive-enhancement runtime when at least one form is * explicitly enabled and compatible. * * @return void */ public function enqueue() { $forms = $this->adapter->discover(); $exposed = $this->settings->exposed_forms( $forms ); if ( empty( $exposed ) ) { return; } $handle = 'modelgain-webmcp-forms'; $runtime_path = plugin_dir_path( MODELGAIN_WEBMCP_FORMS_FILE ) . 'assets/js/webmcp-runtime.js'; $runtime_hash = is_readable( $runtime_path ) ? hash_file( 'sha256', $runtime_path ) : false; $runtime_version = MODELGAIN_WEBMCP_FORMS_VERSION; if ( is_string( $runtime_hash ) ) { $runtime_version .= '-' . substr( $runtime_hash, 0, 12 ); } wp_register_script( $handle, plugins_url( 'assets/js/webmcp-runtime.js', MODELGAIN_WEBMCP_FORMS_FILE ), array(), $runtime_version, true ); $config = array( 'forms' => array() ); foreach ( $exposed as $item ) { $form = $item['form']; $setting = $item['settings']; $config['forms'][] = array( 'formId' => $form->id(), 'toolName' => $setting['tool_name'], 'title' => $form->title(), 'description' => sprintf( /* translators: %s: Contact Form 7 form title. */ __( 'Fill the %s Contact Form 7 form for human review. This tool fills fields only and does not submit the form.', 'modelgain-webmcp-forms' ), $form->title() ), 'inputSchema' => $this->compiler->compile( $form ), ); } $encoded_config = wp_json_encode( $config, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); if ( false === $encoded_config ) { return; } wp_add_inline_script( $handle, 'window.ModelGainWebMCPForms = ' . $encoded_config . ';', 'before' ); wp_enqueue_script( $handle ); } }