true, 'strategy' => 'defer' ) ); wp_localize_script( 'fwren-form', 'FWREN', array( 'rest' => esc_url_raw( rest_url( 'fwren/v1/' ) ), 'locale' => get_locale(), 'i18n' => array( 'required' => __( 'This field is required.', 'fieldwren' ), 'email' => __( 'Please enter a valid email address.', 'fieldwren' ), 'url' => __( 'Please enter a valid URL.', 'fieldwren' ), 'number' => __( 'Please enter a number.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'min' => __( 'Please enter a value of at least %s.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'max' => __( 'Please enter a value no greater than %s.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'minlength' => __( 'Please enter at least %d characters.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'maxlength' => __( 'Please enter no more than %d characters.', 'fieldwren' ), 'pattern' => __( 'Please match the requested format.', 'fieldwren' ), 'confirm' => __( 'The email addresses do not match.', 'fieldwren' ), 'phone' => __( 'Please enter a valid phone number.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'maxSel' => __( 'Please choose no more than %d options.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'minSel' => __( 'Please choose at least %d options.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'fileSize' => __( '%1$s is larger than %2$s MB.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'fileType' => __( '%s is not an allowed file type.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'fileCount' => __( 'You can upload up to %d files.', 'fieldwren' ), 'uploading' => __( 'Please wait until the uploads finish.', 'fieldwren' ), 'remove' => __( 'Remove', 'fieldwren' ), 'error' => __( 'Something went wrong. Please try again.', 'fieldwren' ), 'fix' => __( 'Please correct the highlighted fields.', 'fieldwren' ), 'weekend' => __( 'Please choose a weekday.', 'fieldwren' ), 'blocked' => __( 'This date is not available.', 'fieldwren' ), /* translators: placeholders are filled with the field value, limit or name. */ 'stepOf' => __( 'Step %1$d of %2$d', 'fieldwren' ), 'saved' => __( 'Progress saved.', 'fieldwren' ), 'copy' => __( 'Copy link', 'fieldwren' ), 'copied' => __( 'Copied!', 'fieldwren' ), 'search' => __( 'Search…', 'fieldwren' ), 'noResults' => __( 'No results', 'fieldwren' ), 'weak' => __( 'Weak', 'fieldwren' ), 'medium' => __( 'Medium', 'fieldwren' ), 'strong' => __( 'Strong', 'fieldwren' ), 'pressEnter' => __( 'press Enter ↵', 'fieldwren' ), 'ok' => __( 'OK', 'fieldwren' ), 'previewOk' => __( 'Preview: the form is valid.', 'fieldwren' ), 'previewNote' => __( 'Nothing was submitted, saved or emailed. These values would be sent:', 'fieldwren' ), ), ) ); } /** Field styles inside the block editor canvas. */ public static function editor_canvas_assets() { if ( ! is_admin() ) { return; } wp_enqueue_style( 'fwren-form-editor-canvas', FWREN_URL . 'assets/frontend/form.css', array(), FWREN_VERSION ); Global_Styles::enqueue(); $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && Plugin::POST_TYPE === $screen->post_type ) { wp_enqueue_style( 'fwren-editor-canvas', FWREN_URL . 'assets/editor/canvas.css', array(), FWREN_VERSION ); } } /** * [fieldwren_form id="12" title="yes" class=""] * * @param array $atts Attributes. * @return string */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => 0, 'title' => '', 'class' => '', ), (array) $atts ); return self::render( (int) $atts['id'], $atts ); } /** * Render a form. * * @param int $form_id Form id. * @param array $args title, class, prefill, content + preview (backend preview). * @return string */ public static function render( $form_id, array $args = array() ) { $args = wp_parse_args( $args, array( 'title' => '', 'class' => '', 'content' => null, 'preview' => false, 'post' => null ) ); $preview = ! empty( $args['preview'] ) && current_user_can( 'fwren_manage_forms' ); // Preview of a form that doesn't exist yet (template library): an unsaved post object. $virtual = $preview && $args['post'] instanceof \WP_Post; $post = $virtual ? $args['post'] : get_post( $form_id ); if ( $virtual ) { $form_id = (int) $post->ID; $args['content'] = (string) $post->post_content; } if ( ! $post || Plugin::POST_TYPE !== $post->post_type || ( 'publish' !== $post->post_status && ! current_user_can( 'fwren_manage_forms' ) ) ) { return current_user_can( 'fwren_manage_forms' ) ? '

' . esc_html__( 'Form not found.', 'fieldwren' ) . '

' : ''; } $settings = Form::settings( $form_id ); $schema = Schema::get( $form_id ); // Preview of unsaved editor content: render those blocks, compile them without saving. if ( $preview && is_string( $args['content'] ) ) { $post = clone $post; $post->post_content = $args['content']; $schema = Schema::compile( 0, $args['content'] ); } // Restrictions that hide the form (a preview shows the form anyway). $block = $preview ? true : Submission::availability( $form_id, $settings ); if ( is_wp_error( $block ) ) { return '
' . wp_kses_post( $block->get_error_message() ) . '
'; } $prev_form = self::$form_id; $prev_instance = self::$instance; self::$form_id = (int) $form_id; self::$count++; self::$instance = 'f' . $form_id . '-' . self::$count; self::$captcha_printed = false; self::$prefill = apply_filters( 'fwren/prefill', isset( $args['prefill'] ) ? (array) $args['prefill'] : array(), $form_id ); wp_enqueue_style( 'fwren-form' ); wp_enqueue_script( 'fwren-form' ); Global_Styles::enqueue(); Spam::enqueue_captcha( $form_id, $settings ); $inline_css = ''; if ( class_exists( '\FWREN_BC\Plugin' ) ) { if ( $preview && is_string( $args['content'] ) ) { $inline_css = self::blocks_css( parse_blocks( $post->post_content ) ); } else { \FWREN_BC\Plugin::instance()->enqueue_post_css( $form_id ); } wp_enqueue_style( 'fwren-bc-frontend' ); } do_action( 'fwren/before_render', $form_id, $settings, $schema ); $body = self::render_body( $post, $schema, $settings ); $layout = $settings['layout']; $classes = array( 'fwren-form', 'fwren-form-' . $form_id, ); if ( ! empty( $schema['flags']['has_steps'] ) ) { $classes[] = 'fwren-has-steps'; } if ( ! empty( $settings['conversational']['enabled'] ) ) { $classes[] = 'fwren-conversational'; } if ( $args['class'] ?? '' ) { $classes[] = $args['class']; } $style = ''; $config = array( 'id' => (int) $form_id, 'instance' => self::$instance, 'ajax' => ! empty( $layout['ajax'] ), 'scrollError' => ! empty( $layout['scroll_to_error'] ), 'steps' => array( 'count' => max( 1, count( $schema['steps'] ) ), 'titles' => wp_list_pluck( $schema['steps'], 'title' ), 'progress' => $settings['steps']['progress'], 'persist' => ! empty( $settings['steps']['persist'] ), 'scrollTop'=> ! empty( $settings['steps']['scroll_top'] ), ), 'save' => ! empty( $settings['save_resume']['enabled'] ), 'autoSave' => ! empty( $settings['save_resume']['auto_save'] ), 'analytics' => ! $preview && ! empty( $settings['analytics']['enabled'] ), 'preview' => $preview, 'conv' => ! empty( $settings['conversational']['enabled'] ), 'prefill' => self::$prefill, 'payment' => $settings['payment'], 'track' => $preview ? array( 'datalayer' => false ) : apply_filters( 'fwren/tracking_config', (array) Settings::get( 'tracking', array( 'datalayer' => true ) ), $form_id ), ); $config = apply_filters( 'fwren/frontend_config', $config, $form_id, $settings, $schema ); $hp = Spam::honeypot_name( $form_id ); $source = self::current_url(); ob_start(); echo '
'; if ( '' !== $inline_css ) { echo ''; // phpcs:ignore WordPress.Security.EscapeOutput -- Generated by CSS_Generator from sanitized settings. } if ( ! empty( $args['title'] ) && 'no' !== $args['title'] ) { echo '

' . esc_html( get_the_title( $post ) ) . '

'; } if ( ! empty( $settings['conversational']['enabled'] ) && ( $settings['conversational']['start_title'] || $settings['conversational']['start_text'] ) ) { echo '

' . esc_html( $settings['conversational']['start_title'] ) . '

' . wp_kses_post( wpautop( $settings['conversational']['start_text'] ) ) . '
'; } printf( '
', esc_attr( implode( ' ', $classes ) ), esc_attr( self::$instance ), esc_url( $source ), esc_attr( wp_json_encode( $config ) ), $style ? ' style="' . esc_attr( $style ) . '"' : '', esc_attr( get_the_title( $post ) ) ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; // phpcs:ignore WordPress.Security.NonceVerification echo ''; if ( $settings['spam']['honeypot'] ) { echo ''; } echo ''; echo $body; // phpcs:ignore WordPress.Security.EscapeOutput echo '
'; echo ''; if ( '' !== trim( (string) $layout['custom_css'] ) ) { echo ''; // phpcs:ignore } if ( '' !== trim( (string) $layout['custom_js'] ) ) { echo ''; // phpcs:ignore } echo '
'; $html = ob_get_clean(); Analytics::track_view( $form_id ); self::$form_id = $prev_form; self::$instance = $prev_instance; return apply_filters( 'fwren/form_html', $html, $form_id ); } /** * Blocks → fields markup, split in steps. * * @param \WP_Post $post Form post. * @param array $schema Schema. * @param array $settings Settings. * @return string */ /** @return bool Whether a parsed block is, or contains, the named block. */ private static function contains_block( array $block, $name ) { if ( ($block['blockName'] ?? '') === $name ) { return true; } foreach ( (array) ( $block['innerBlocks'] ?? array() ) as $inner ) { if ( self::contains_block( $inner, $name ) ) { return true; } } return false; } private static function render_body( $post, array $schema, array $settings ) { $blocks = parse_blocks( $post->post_content ); $groups = array( array() ); $steps = array(); $has_sub = false; foreach ( $blocks as $b ) { if ( empty( $b['blockName'] ) ) { continue; } if ( 'fwren/step' === $b['blockName'] ) { $type = Fields::by_block( 'fwren/step' ); $steps[] = $type->prepare_settings( (array) $b['attrs'] ); $groups[] = array(); continue; } if ( ! $has_sub && self::contains_block( $b, 'fwren/submit' ) ) { $has_sub = true; // Also when nested in a container (split layouts). } $groups[ count( $groups ) - 1 ][] = $b; } $multi = count( $groups ) > 1; $total = count( $groups ); $out = ''; if ( $multi && 'none' !== $settings['steps']['progress'] ) { $out .= self::progress( $schema, $settings, $total ); } foreach ( $groups as $i => $group ) { $html = ''; foreach ( $group as $b ) { $html .= ( new \WP_Block( $b, array( 'postId' => $post->ID, 'postType' => $post->post_type ) ) )->render(); } $is_last = $i === $total - 1; if ( $is_last && ! $has_sub ) { $html .= self::default_submit(); } if ( ! $multi ) { return $out . '
' . $html . '
'; } $nav = '
'; if ( $i > 0 ) { $nav .= ''; } if ( ! $is_last ) { $nav .= ''; } $nav .= '
'; $out .= '
' . $html . '
' . $nav . '
'; } return $out; } /** * CSS of unsaved blocks (preview), generated the same way as the saved post CSS file. * * @param array $blocks Parsed blocks. * @return string */ private static function blocks_css( array $blocks ) { $css = ''; $fw = \FWREN_BC\Plugin::instance(); foreach ( $blocks as $b ) { $type = $fw->get_block( $b['blockName'] ?? '' ); if ( $type ) { $css .= \FWREN_BC\CSS_Generator::generate_block( $type, $type->prepare_settings( (array) ( $b['attrs'] ?? array() ) ) ); } if ( ! empty( $b['innerBlocks'] ) ) { $css .= self::blocks_css( $b['innerBlocks'] ); } } return $css; } /** Step progress markup. */ private static function progress( array $schema, array $settings, $total ) { $titles = wp_list_pluck( $schema['steps'], 'title' ); if ( 'steps' === $settings['steps']['progress'] ) { $h = '
    '; foreach ( $titles as $i => $t ) { $h .= '
  1. ' . ( $i + 1 ) . '' . esc_html( $t ) . '
  2. '; } return $h . '
'; } /* translators: 1: step, 2: total */ $label = sprintf( __( 'Step %1$d of %2$d', 'fieldwren' ), 1, $total ); return '
' . esc_html( $label ) . '' . esc_html( $titles[0] ?? '' ) . '
'; } /** Submit button when the form has no Submit block. */ private static function default_submit() { $cap = self::before_submit(); return '
' . $cap . '
'; } /** Captcha markup when no Captcha block placed it yet. */ public static function before_submit() { if ( self::$captcha_printed || self::is_editor() ) { return ''; } self::$captcha_printed = true; return Spam::captcha_markup( self::$form_id ); } /** @return string URL of the current request. */ public static function current_url() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; if ( wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return wp_get_referer() ? wp_get_referer() : home_url( '/' ); } return home_url( remove_query_arg( array( 'fwren_resume', 'fwren_confirm', 'fwren_msg' ), $uri ) ); } /** * Prefix every selector of custom CSS with the form wrapper. * * @param string $css CSS. * @param string $scope Scope selector. * @return string */ public static function scope_css( $css, $scope ) { $css = wp_strip_all_tags( (string) $css ); $css = str_replace( array( '{{form}}', 'selector' ), $scope, $css ); return preg_replace_callback( '/(^|\})\s*([^{}@]+)\{/m', static function ( $m ) use ( $scope ) { $sels = array_map( static function ( $s ) use ( $scope ) { $s = trim( $s ); return '' === $s || 0 === strpos( $s, $scope ) || preg_match( '/^(from|to|\d+%)$/', $s ) ? $s : $scope . ' ' . $s; }, explode( ',', $m[2] ) ); return $m[1] . ' ' . implode( ', ', $sels ) . '{'; }, $css ); } }