plugin = $plugin; } public function register(): void { add_shortcode('ssffw_form', [$this, 'shortcode']); add_shortcode('ssf_form', [$this, 'shortcode']); // Retained for pages created by 0.1.0. add_action('init', [$this, 'block']); add_action('wp_enqueue_scripts', [$this, 'page_assets']); add_action('admin_post_nopriv_ssffw_submit', [$this, 'fallback_submit']); add_action('admin_post_ssffw_submit', [$this, 'fallback_submit']); } public function page_assets(): void { global $post; $needed = $post instanceof \WP_Post && (has_shortcode($post->post_content, 'ssffw_form') || has_shortcode($post->post_content, 'ssf_form') || (function_exists('has_block') && (has_block('ssffw/forms', $post) || has_block('ssf/forms', $post)))); if (!apply_filters('ssffw_enqueue_page_assets', $needed)) { return; } $base = plugin_dir_url(dirname(__DIR__) . '/ssf-forms.php'); wp_enqueue_style('ssffw-frontend', $base . 'assets/frontend.css', [], SSFFW_VERSION); wp_enqueue_script('ssffw-frontend', $base . 'assets/frontend.js', ['wp-i18n'], SSFFW_VERSION, true); wp_set_script_translations('ssffw-frontend', 'ssf-forms'); } public function block(): void { if (!function_exists('register_block_type')) { return; } $base = plugin_dir_url(dirname(__DIR__) . '/ssf-forms.php'); wp_register_script( 'ssffw-block', $base . 'assets/block.js', ['wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-api-fetch', 'wp-i18n'], SSFFW_VERSION, true, ); wp_set_script_translations('ssffw-block', 'ssf-forms'); foreach (['ssffw/forms', 'ssf/forms'] as $block_name) { register_block_type($block_name, [ 'api_version' => 3, 'editor_script' => 'ssffw-block', 'attributes' => ['id' => ['type' => 'number', 'default' => 0]], 'render_callback' => function ($attributes) { return $this->shortcode(['id' => absint($attributes['id'] ?? 0)]); }, ]); } } public function shortcode($attributes, $content = null, $tag = 'ssffw_form'): string { $attributes = shortcode_atts(['id' => 0], $attributes, $tag); $form = $this->plugin->repository->get_form(absint($attributes['id'])); if (!$form || 'publish' !== ($form['status'] ?? '')) { return '
' . esc_html__('This form is not available right now.', 'ssf-forms') . '
'; } if ( method_exists($this->plugin->validator, 'supports_form') && !$this->plugin->validator->supports_form($form) ) { return '' . esc_html__( 'This form is temporarily unavailable. Please contact the website owner.', 'ssf-forms', ) . '
'; } return $this->render_form($form); } public function render_form(array $form, array $values = [], array $errors = []): string { $base = plugin_dir_url(dirname(__DIR__) . '/ssf-forms.php'); wp_enqueue_style('ssffw-frontend', $base . 'assets/frontend.css', [], SSFFW_VERSION); wp_enqueue_script('ssffw-frontend', $base . 'assets/frontend.js', ['wp-i18n'], SSFFW_VERSION, true); wp_set_script_translations('ssffw-frontend', 'ssf-forms'); do_action('ssffw_form_assets', $form); $this->render_count++; $id = absint($form['id']); $dom = 'ssffw-' . esc_attr($id) . '-' . $this->render_count; $settings = $form['settings'] ?? []; $appearance = $settings['appearance'] ?? []; $public = [ 'id' => $id, 'version' => absint($form['version'] ?? 1), 'fields' => $form['fields'] ?? [], 'settings' => [ 'submit_label' => $settings['submit_label'] ?? __('Send message', 'ssf-forms'), 'success_message' => $settings['success_message'] ?? __('Thank you. Your submission has been saved.', 'ssf-forms'), 'allow_resume' => !empty($settings['allow_resume']), 'resume_days' => absint($settings['resume_days'] ?? 7), 'steps' => $settings['steps'] ?? [], ], ]; if (is_user_logged_in()) { $public['restNonce'] = wp_create_nonce('wp_rest'); } $vars = []; foreach ( ['accent' => '#087f8c', 'background' => '#ffffff', 'text' => '#142c3c', 'border' => '#cbd5df'] as $key => $default ) { $vars[] = '--ssffw-' . $key . ':' . (sanitize_hex_color($appearance[$key] ?? '') ?: $default); } $vars[] = '--ssffw-radius:' . min(32, max(0, absint($appearance['radius'] ?? 8))) . 'px'; $vars[] = '--ssffw-width:' . min(1200, max(240, absint($appearance['width'] ?? 680))) . 'px'; ob_start(); echo ''; return (string) ob_get_clean(); } private function render_field( array $field, $value, array $form, string $dom, string $error, ): string { $type = $field['type'] ?? 'text'; $extension = apply_filters('ssffw_render_field', null, $field, $value, $form); if (null !== $extension) { return '' . esc_html($field['help']) . '
' : '') . ''; } $mark = $required ? ' ' . esc_html__('(required)', 'ssf-forms') . '' : ''; $control = ''; if (in_array($type, ['radio', 'checkbox'], true)) { $control .= ''; } elseif ('consent' === $type) { $control = ''; } else { $control .= ''; if ('textarea' === $type) { $control .= ''; } elseif ('select' === $type) { $control .= ''; } else { $control .= ''; } } if (!empty($field['help'])) { $control .= '' . esc_html($field['help']) . '
'; } $control .= '' . esc_html($error) . '
'; return $wrapper . $control . ''; } public function fallback_submit(): void { // Validate scalar shapes and the nonce before looking up a fallback form. foreach (['form_id', 'submission_id', 'form_nonce', 'website', '_ssffw_return'] as $key) { if (isset($_POST[$key]) && !is_string($_POST[$key])) { wp_die(esc_html__('Invalid form request.', 'ssf-forms'), '', ['response' => 400]); } } $id = isset($_POST['form_id']) ? absint($_POST['form_id']) : 0; $nonce = isset($_POST['form_nonce']) ? sanitize_text_field(wp_unslash($_POST['form_nonce'])) : ''; if (!wp_verify_nonce($nonce, 'ssffw_submit_' . $id)) { wp_die( esc_html__('This form has expired. Refresh the page and try again.', 'ssf-forms'), '', ['response' => 403], ); } if (!empty($_FILES)) { wp_die(esc_html__('This form does not accept direct file uploads.', 'ssf-forms'), '', ['response' => 400]); } $fallback_form = $this->plugin->repository->get_form($id); foreach ($fallback_form['fields'] ?? [] as $interactive_field) { if (in_array($interactive_field['type'] ?? '', ['file', 'repeater'], true)) { wp_die( esc_html__( 'This form needs JavaScript for its private upload or repeatable controls. No submission was saved. Enable JavaScript and return to the form, or contact the website owner.', 'ssf-forms', ), esc_html__('Interactive form controls', 'ssf-forms'), ['response' => 400, 'back_link' => true], ); } } $request = new \WP_REST_Request('POST', '/ssffw/v1/forms/' . $id . '/submit'); $request->set_body_params([ 'values' => // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Schema validation sanitizes declared fields before every extension hook and storage; raw payload bytes are retained solely for idempotency comparison. isset($_POST['values']) && is_array($_POST['values']) ? wp_unslash($_POST['values']) : [], 'submission_id' => sanitize_text_field(wp_unslash($_POST['submission_id'] ?? '')), 'form_nonce' => sanitize_text_field(wp_unslash($_POST['form_nonce'] ?? '')), 'website' => sanitize_text_field(wp_unslash($_POST['website'] ?? '')), ]); $response = rest_do_request($request); $data = $response->get_data(); $back = wp_validate_redirect( esc_url_raw(wp_unslash($_POST['_ssffw_return'] ?? '')), home_url('/'), ); $form = $this->plugin->repository->get_form($id); $success = $response->get_status() < 400; $message = $success ? $form['settings']['success_message'] ?? __('Thank you. Your submission has been saved.', 'ssf-forms') : $data['message'] ?? __('Your submission could not be saved. Please try again.', 'ssf-forms'); $html = '' . esc_html($message) . '
'; if (!$success && !empty($data['data']['fields'])) { $html .= '' . esc_html__('Reference', 'ssf-forms') . ': #' . absint($data['id']) . '
'; } $html .= ''; wp_die(wp_kses_post($html), esc_html__('Form submission', 'ssf-forms'), [ 'response' => $success ? 200 : absint($response->get_status()), ]); } }