cache = Cache::for('forms', WEEK_IN_SECONDS); // Initialize forms from filter $this->forms = $this->registerForms(); $this->form_contact = apply_filters('jvb_form_contact', ''); // Hook into the CustomBlocks render system add_filter('jvb_render_block_jvb_forms', [$this, 'render'], 10, 2); // Register forms data for the block editor add_action('enqueue_block_editor_assets', [$this, 'localizeFormsData']); add_action('init', [$this, 'registerBlock']); add_filter('render_block', [$this, 'maybeEnqueueScripts'], 10, 2); } /** * Enqueue scripts when rendering form block */ public function maybeEnqueueScripts(string $block_content, array $block): string { // Only process our form blocks if ($block['blockName'] !== 'jvb/forms') { return $block_content; } // Enqueue Turnstile if needed if (Site::hasIntegration('cloudflare')) { $cloudflare = JVB()->connect('cloudflare'); if ($cloudflare->isSetUp()) { $cloudflare->enqueueTurnstileScripts(); } } return $block_content; } public function registerBlock() { register_block_type($this->path, [ 'render_callback' => [$this, 'render'], 'style' => 'jvb-icons-forms', ]); } /** * Register available forms */ protected function registerForms(): array { // Default forms can be registered here $default_forms = []; // Allow other plugins to register forms $forms = apply_filters('jvb_register_forms', $default_forms); // Process forms to ensure they have proper structure $processed_forms = []; foreach ($forms as $form_key => $form_config) { $processed_forms[$form_key] = $this->processFormConfig($form_config); } return $processed_forms; } /** * Process form configuration to ensure proper structure */ protected function processFormConfig(array $config): array { $defaults = [ 'title' => 'Form', 'description' => [], 'submit' => 'Submit', 'success_title' => 'Thank You!', 'success_message' => ['Your message has been sent successfully.'], 'email_to' => get_option('admin_email'), 'email_subject' => 'New Form Submission', 'fields' => [], 'sections' => [] ]; return array_merge($defaults, $config); } /** * Render the form block */ public function render(array $block, string $content): string { $form_type = $block['formType']; if (empty($form_type) || !isset($this->forms[$form_type])) { return '
' . wp_kses_post($message) . '
'; } } if ($submission_data) { echo '' . wp_kses_post($this->form_contact) . '
'; } echo '' . esc_html($message) . '
'; if (!empty($this->form_contact)) { $output .= '' . wp_kses_post($this->form_contact) . '
'; } $output .= '' . wp_kses_post($desc) . '
'; } } echo ''; } /** * Localize forms data for block editor */ public function localizeFormsData(): void { $form_types = [ [ 'label' => __('Select a form type', 'jvb'), 'value' => '' ] ]; foreach ($this->forms as $form_key => $form_config) { $form_types[] = [ 'label' => $form_config['title'] ?? ucwords(str_replace('-', ' ', $form_key)), 'value' => $form_key ]; } wp_localize_script('jvb-forms-editor-script', 'jvbFormsData', [ 'formTypes' => $form_types, 'availableForms' => $this->forms, 'nonce' => wp_create_nonce('jvbForm') ]); } /** * Get registered forms */ public static function getForms(): array { return self::getInstance()->forms??[]; } /** * Get specific form configuration */ public static function getForm($type):array|null { return self::getInstance()->forms[$type] ?? null; } }