| | |
| | | <?php |
| | | namespace JVBase\blocks; |
| | | |
| | | use JVBase\managers\CacheManager; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\managers\CloudflareTurnstile; |
| | | use Exception; |
| | | use JVBase\utility\Features; |
| | | use WP_Block; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\meta\Form; |
| | | use JVBase\base\Site; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | |
| | | */ |
| | | class FormBlock |
| | | { |
| | | protected static FormBlock|null $instance = null; |
| | | protected CacheManager $cache; |
| | | protected static ?FormBlock $instance = null; |
| | | protected Cache $cache; |
| | | protected array $forms; |
| | | protected string $form_contact; |
| | | |
| | |
| | | |
| | | public function __construct() |
| | | { |
| | | $this->cache = CacheManager::for('form_blocks', WEEK_IN_SECONDS); |
| | | $this->cache = Cache::for('forms', WEEK_IN_SECONDS); |
| | | |
| | | // Initialize forms from filter |
| | | $this->forms = $this->registerForms(); |
| | | $this->form_contact = apply_filters('jvb_form_contact', ''); |
| | |
| | | } |
| | | |
| | | // Enqueue Turnstile if needed |
| | | if (Features::forSite()->hasIntegration('cloudflare')) { |
| | | if (Site::hasIntegration('cloudflare')) { |
| | | $cloudflare = JVB()->connect('cloudflare'); |
| | | if ($cloudflare->isSetUp()) { |
| | | $cloudflare->enqueueTurnstileScripts(); |
| | |
| | | } |
| | | |
| | | $cache_key = $this->cache->generateKey($block); |
| | | $cached = $this->cache->get($cache_key); |
| | | $cached = false; |
| | | if ($cached) { |
| | | return $cached; |
| | | } |
| | | |
| | | $rendered = $this->renderForm($form_type, $block); |
| | | |
| | | $this->cache->set($cache_key, $rendered); |
| | | return $rendered; |
| | | return $this->cache->remember( |
| | | $cache_key, |
| | | function() use ($form_type, $block) { |
| | | return $this->renderForm($form_type, $block); |
| | | } |
| | | ); |
| | | } |
| | | |
| | | /** |
| | |
| | | $this->renderTurnstile(); |
| | | $this->renderFormEnd($type, $form_id); |
| | | echo '</div>'; |
| | | |
| | | return ob_get_clean(); |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | // Create MetaManager instance for form rendering |
| | | $meta = new MetaManager(); |
| | | |
| | | // If sections are defined, render in sections |
| | | if (!empty($form_config['sections'])) { |
| | | $this->renderSections($type, $meta); |
| | | $this->renderSections($type); |
| | | } else { |
| | | echo jvbFormStatus(); |
| | | // Render fields directly |
| | | foreach ($form_config['fields'] as $field_name => $field_config) { |
| | | $meta->render('form', $field_name, $field_config); |
| | | echo Form::render($field_name, null, $field_config); |
| | | } |
| | | $submit_text = $form_config['submit'] ?? 'Submit'; |
| | | echo '<button type="submit" class="button primary">' . esc_html($submit_text) . '</button>'; |
| | |
| | | /** |
| | | * Render form sections |
| | | */ |
| | | protected function renderSections(string $type, MetaManager $meta): void |
| | | protected function renderSections(string $type): void |
| | | { |
| | | $form_config = $this->forms[$type]; |
| | | $sections = $form_config['sections']; |
| | |
| | | }); |
| | | |
| | | foreach ($section_fields as $field_name => $field_config) { |
| | | $meta->render('form', $field_name, $field_config); |
| | | echo Form::render($field_name, null, $field_config); |
| | | } |
| | | |
| | | // Add step navigation buttons |
| | |
| | | */ |
| | | protected function renderTurnstile(): void |
| | | { |
| | | if (!Features::hasIntegration('cloudflare')) { |
| | | if (!Site::hasIntegration('cloudflare')) { |
| | | return; |
| | | } |
| | | |