add_control( 'switch_text', array( 'label' => __( 'Text next to the switch', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Yes, please', 'fieldwren' ) ) ); $this->add_control( 'on_value', array( 'label' => __( 'Value when on', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Yes', 'fieldwren' ) ) ); $this->add_control( 'off_value', array( 'label' => __( 'Value when off', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'No', 'fieldwren' ) ) ); $this->add_control( 'checked', array( 'label' => __( 'On by default', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) ); $this->add_control( 'on_color', array( 'label' => __( 'On color', 'fieldwren' ), 'type' => Controls::COLOR, 'selectors' => array( '{{WRAPPER}} .fwren-toggle' => '--fwren-toggle-on: {{VALUE}};' ) ) ); } protected function schema_extra( array $s ) { return array( 'on_value' => (string) $s['on_value'], 'off_value' => (string) $s['off_value'] ); } public function sanitize( $raw, array $f ) { $raw = is_array( $raw ) ? (string) end( $raw ) : (string) $raw; return $raw === $f['on_value'] ? $f['on_value'] : $f['off_value']; } public function validate( $value, array $f ) { if ( ! empty( $f['required'] ) && $value !== $f['on_value'] ) { return array( $f['required_message'] ? $f['required_message'] : __( 'Please switch this on to continue.', 'fieldwren' ) ); } return array(); } protected function render_input( array $s, $key, $id ) { echo ''; echo ''; } } /** Segmented single choice. */ class Toggle_Field extends Radio_Field { public function get_name() { return 'toggle'; } public function get_title() { return __( 'Toggle Select', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'leftright' ); } public function get_keywords() { return array( 'segmented', 'toggle', 'button group', 'single choice' ); } public function get_field_type() { return 'toggle'; } protected function supports_images() { return false; } protected function supports_dynamic() { return false; } protected function default_options() { return array( array( 'label' => __( 'Monthly', 'fieldwren' ), 'value' => 'monthly' ), array( 'label' => __( 'Yearly', 'fieldwren' ), 'value' => 'yearly' ), ); } protected function register_choice_controls() { $this->add_control( 'full_width', array( 'label' => __( 'Stretch to full width', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) ); } protected function render_input( array $s, $key, $id ) { $s['display'] = 'toggle' . ( 'yes' === ( $s['full_width'] ?? '' ) ? ' fwren-choices--toggle-full' : '' ); $s['other'] = ''; parent::render_input( $s, $key, $id ); } } /** Like / Dislike. */ class Like_Field extends Field_Block { public function get_name() { return 'like'; } public function get_title() { return __( 'Like / Dislike', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'thumbs-up' ); } public function get_keywords() { return array( 'thumbs', 'like', 'dislike', 'feedback', 'vote' ); } public function get_category() { return 'fwren-choice'; } protected function has_placeholder() { return false; } protected function has_default() { return false; } protected function default_label() { return __( 'Was this helpful?', 'fieldwren' ); } protected function register_type_controls() { $this->add_control( 'like_text', array( 'label' => __( 'Like text', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Like', 'fieldwren' ) ) ); $this->add_control( 'dislike_text', array( 'label' => __( 'Dislike text', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Dislike', 'fieldwren' ) ) ); $this->add_control( 'icons_only', array( 'label' => __( 'Icons only', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) ); } protected function schema_extra( array $s ) { return array( 'options' => array( array( 'label' => (string) $s['like_text'], 'value' => 'like', 'calc' => '1', 'image' => '', 'selected' => false, 'stock' => null ), array( 'label' => (string) $s['dislike_text'], 'value' => 'dislike', 'calc' => '0', 'image' => '', 'selected' => false, 'stock' => null ), ), ); } public function sanitize( $raw, array $f ) { return in_array( $raw, array( 'like', 'dislike' ), true ) ? $raw : ''; } public function format( $value, array $f, $mode = 'html' ) { if ( '' === (string) $value ) { return ''; } $label = 'like' === $value ? $f['options'][0]['label'] : $f['options'][1]['label']; $out = ( 'like' === $value ? 'πŸ‘ ' : 'πŸ‘Ž ' ) . $label; return 'html' === $mode ? esc_html( $out ) : $out; } public function numeric( $value, array $f ) { return 'like' === $value ? 1.0 : 0.0; } protected function render_label( array $s, $id, $tag = 'label' ) { parent::render_label( $s, $id, 'div' ); } protected function render_input( array $s, $key, $id ) { $up = ''; $down = ''; $only = 'yes' === $s['icons_only']; echo '
'; foreach ( array( 'like' => array( $up, $s['like_text'] ), 'dislike' => array( $down, $s['dislike_text'] ) ) as $v => $p ) { echo ''; // phpcs:ignore } echo '
'; } } /** Rich text (bold, italic, lists, links). */ class Richtext_Field extends Field_Block { public function get_name() { return 'richtext'; } public function get_title() { return __( 'Rich Text Editor', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'editor-paste-word' ); } public function get_keywords() { return array( 'text editor', 'wysiwyg', 'formatted', 'rich text' ); } public function get_category() { return 'fwren-advanced'; } protected function has_validation() { return true; } protected function register_type_controls() { $this->add_control( 'editor_height', array( 'label' => __( 'Height (px)', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 160, 'inline' => true ) ); } protected function register_validation_controls() { $this->add_control( 'maxlength', array( 'label' => __( 'Maximum characters (text only)', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) ); } protected function schema_extra( array $s ) { return array( 'maxlength' => '' !== (string) $s['maxlength'] ? (int) $s['maxlength'] : null ); } private static function allowed() { return array( 'p' => array(), 'br' => array(), 'div' => array(), 'strong' => array(), 'b' => array(), 'em' => array(), 'i' => array(), 'u' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(), 'a' => array( 'href' => true ), ); } public function sanitize( $raw, array $f ) { $html = wp_kses( is_array( $raw ) ? '' : (string) $raw, self::allowed() ); return '' === trim( wp_strip_all_tags( $html ) ) ? '' : trim( $html ); } public function validate( $value, array $f ) { $len = function_exists( 'mb_strlen' ) ? mb_strlen( wp_strip_all_tags( (string) $value ) ) : strlen( wp_strip_all_tags( (string) $value ) ); if ( ! empty( $f['maxlength'] ) && $len > $f['maxlength'] ) { /* translators: %d characters */ return array( sprintf( __( 'Please enter no more than %d characters.', 'fieldwren' ), $f['maxlength'] ) ); } return array(); } public function format( $value, array $f, $mode = 'html' ) { return 'html' === $mode ? wp_kses( (string) $value, self::allowed() ) : trim( wp_strip_all_tags( str_replace( array( '

', '
', '' ), "\n", (string) $value ) ) ); } protected function render_input( array $s, $key, $id ) { $tools = array( 'bold' => array( 'B', __( 'Bold', 'fieldwren' ) ), 'italic' => array( 'I', __( 'Italic', 'fieldwren' ) ), 'underline' => array( 'U', __( 'Underline', 'fieldwren' ) ), 'insertUnorderedList' => array( 'β€’', __( 'Bullet list', 'fieldwren' ) ), 'insertOrderedList' => array( '1.', __( 'Numbered list', 'fieldwren' ) ), 'createLink' => array( 'πŸ”—', __( 'Link', 'fieldwren' ) ), 'removeFormat' => array( '⌫', __( 'Clear formatting', 'fieldwren' ) ), ); echo '
'; echo ''; echo '
'; echo ''; echo '
'; } protected function render_label( array $s, $id, $tag = 'label' ) { parent::render_label( $s, $id, 'div' ); } } /** Map location picker (OpenStreetMap / Leaflet; no API key). */ class Map_Field extends Field_Block { const LEAFLET = '1.9.4'; public function get_name() { return 'map'; } public function get_title() { return __( 'Map Location', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'location-alt' ); } public function get_keywords() { return array( 'map', 'location', 'pin', 'coordinates', 'gps' ); } public function get_category() { return 'fwren-advanced'; } protected function has_placeholder() { return false; } protected function has_default() { return false; } protected function register_type_controls() { $this->add_control( 'lat', array( 'label' => __( 'Start latitude', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => '23.7806' ) ); $this->add_control( 'lng', array( 'label' => __( 'Start longitude', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => '90.4070' ) ); $this->add_control( 'zoom', array( 'label' => __( 'Zoom', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 12, 'min' => 1, 'max' => 19, 'inline' => true ) ); $this->add_control( 'map_height', array( 'label' => __( 'Map height (px)', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 300, 'inline' => true ) ); $this->add_control( 'search', array( 'label' => __( 'Address search', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes' ) ); $this->add_control( 'locate', array( 'label' => __( '"Use my location" button', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes' ) ); } protected function schema_extra( array $s ) { return array( 'map' => true ); } public function sanitize( $raw, array $f ) { $raw = is_array( $raw ) ? $raw : array(); $lat = isset( $raw['lat'] ) && is_numeric( $raw['lat'] ) ? round( (float) $raw['lat'], 7 ) : ''; $lng = isset( $raw['lng'] ) && is_numeric( $raw['lng'] ) ? round( (float) $raw['lng'], 7 ) : ''; return array( 'lat' => $lat, 'lng' => $lng, 'address' => sanitize_text_field( (string) ( $raw['address'] ?? '' ) ), ); } public function is_empty( $value ) { return ! is_array( $value ) || '' === ( $value['lat'] ?? '' ) || '' === ( $value['lng'] ?? '' ); } public function validate( $value, array $f ) { if ( $this->is_empty( $value ) ) { return array(); } if ( abs( $value['lat'] ) > 90 || abs( $value['lng'] ) > 180 ) { return array( __( 'Please pick a location on the map.', 'fieldwren' ) ); } return array(); } public function format( $value, array $f, $mode = 'html' ) { if ( $this->is_empty( $value ) ) { return ''; } $coords = $value['lat'] . ', ' . $value['lng']; $url = 'https://www.openstreetmap.org/?mlat=' . rawurlencode( (string) $value['lat'] ) . '&mlon=' . rawurlencode( (string) $value['lng'] ) . '#map=17/' . $value['lat'] . '/' . $value['lng']; if ( 'html' !== $mode ) { return trim( ( $value['address'] ? $value['address'] . ' ' : '' ) . '(' . $coords . ') ' . $url ); } return ( $value['address'] ? esc_html( $value['address'] ) . '
' : '' ) . 'πŸ“ ' . esc_html( $coords ) . ''; } protected function render_label( array $s, $id, $tag = 'label' ) { parent::render_label( $s, $id, 'div' ); } protected function render_input( array $s, $key, $id ) { if ( Renderer::is_editor() ) { echo '
πŸ—Ί ' . esc_html__( 'Map (OpenStreetMap) – visitors click to drop a pin', 'fieldwren' ) . '
'; return; } wp_enqueue_style( 'fwren-leaflet', FWREN_URL . 'assets/vendor/leaflet/leaflet.css', array(), self::LEAFLET ); wp_enqueue_script( 'fwren-leaflet', FWREN_URL . 'assets/vendor/leaflet/leaflet.js', array(), self::LEAFLET, array( 'in_footer' => true, 'strategy' => 'defer' ) ); echo '
'; if ( 'yes' === $s['search'] || 'yes' === $s['locate'] ) { echo '
'; if ( 'yes' === $s['search'] ) { echo ''; } if ( 'yes' === $s['locate'] ) { echo ''; } echo '
'; } echo '
'; echo '
'; foreach ( array( 'lat', 'lng', 'address' ) as $p ) { echo ''; } echo '
'; } } /** Self-hosted captcha: a maths sum or your own question. */ class Simple_Captcha_Field extends Field_Block { public function get_name() { return 'simple-captcha'; } public function get_title() { return __( 'Simple Captcha', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'shield' ); } public function get_keywords() { return array( 'captcha', 'math', 'question', 'spam', 'human' ); } public function get_category() { return 'fwren-advanced'; } public function get_field_type() { return 'simple_captcha'; } public function key_prefix() { return 'captcha'; } protected function has_default() { return false; } protected function has_conditions() { return false; } protected function default_label() { return __( 'Are you human?', 'fieldwren' ); } protected function register_type_controls() { $this->add_control( 'mode', array( 'label' => __( 'Question', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => 'math', 'options' => array( 'math' => __( 'Random sum (e.g. 4 + 7)', 'fieldwren' ), 'custom' => __( 'My own question', 'fieldwren' ) ) ) ); $this->add_control( 'question', array( 'label' => __( 'Question', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'What colour is the sky on a clear day?', 'fieldwren' ), 'condition' => array( 'mode' => 'custom' ) ) ); $this->add_control( 'answer', array( 'label' => __( 'Accepted answers (comma separated)', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => 'blue', 'condition' => array( 'mode' => 'custom' ) ) ); } protected function schema_extra( array $s ) { return array( 'required' => true, 'sensitive' => true, // Never stored, emailed or sent on. 'captcha' => true, 'mode' => $s['mode'], 'answers' => array_filter( array_map( static function ( $a ) { return strtolower( trim( $a ) ); }, explode( ',', (string) $s['answer'] ) ) ), ); } /** @return array [ question, token ] for a new sum. */ public static function challenge() { $a = wp_rand( 1, 9 ); $b = wp_rand( 1, 9 ); $t = time(); $sig = substr( hash_hmac( 'sha256', $a . '|' . $b . '|' . $t, wp_salt( 'nonce' ) ), 0, 20 ); return array( sprintf( '%d + %d = ?', $a, $b ), $a . '.' . $b . '.' . $t . '.' . $sig ); } public function sanitize( $raw, array $f ) { $raw = is_array( $raw ) ? $raw : array( 'a' => (string) $raw ); return array( 'a' => strtolower( trim( sanitize_text_field( (string) ( $raw['a'] ?? '' ) ) ) ), 't' => preg_replace( '/[^a-f0-9.]/', '', (string) ( $raw['t'] ?? '' ) ), ); } public function is_empty( $value ) { return ! is_array( $value ) || '' === ( $value['a'] ?? '' ); } public function validate( $value, array $f ) { $wrong = array( __( 'That answer is not right. Please try again.', 'fieldwren' ) ); if ( 'custom' === $f['mode'] ) { return in_array( $value['a'], (array) $f['answers'], true ) ? array() : $wrong; } $p = explode( '.', $value['t'] ); if ( 4 !== count( $p ) || ! hash_equals( substr( hash_hmac( 'sha256', $p[0] . '|' . $p[1] . '|' . $p[2], wp_salt( 'nonce' ) ), 0, 20 ), $p[3] ) || time() - (int) $p[2] > WEEK_IN_SECONDS ) { return array( __( 'The question expired. Please reload the page.', 'fieldwren' ) ); } return (string) ( (int) $p[0] + (int) $p[1] ) === $value['a'] ? array() : $wrong; } public function format( $value, array $f, $mode = 'html' ) { return ''; } protected function render_input( array $s, $key, $id ) { if ( 'custom' === $s['mode'] ) { $q = (string) $s['question']; $tok = ''; } else { list( $q, $tok ) = self::challenge(); } echo '
'; echo ''; if ( $tok ) { echo ''; } echo '
'; } protected function add_render_attributes() { parent::add_render_attributes(); $this->add_render_attribute( '_wrapper', 'class', 'is-required' ); } } /** Live review of the answers (usually on the last step). */ class Summary_Block extends Layout_Block { public function get_name() { return 'summary'; } public function get_title() { return __( 'Summary (review answers)', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'list-view' ); } public function get_keywords() { return array( 'summary', 'review', 'confirm', 'overview' ); } protected function register_layout_controls() { $this->start_controls_section( 'section_summary', array( 'label' => __( 'Summary', 'fieldwren' ) ) ); $this->add_control( 'title', array( 'label' => __( 'Title', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => __( 'Please review your answers', 'fieldwren' ) ) ); $this->add_control( 'exclude', array( 'label' => __( 'Leave out field keys (comma separated)', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => 'utm_source, page_url' ) ); $this->add_control( 'show_empty', array( 'label' => __( 'Show unanswered fields', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) ); $this->end_controls_section(); } protected function render_layout( $content ) { $s = $this->settings; echo '
'; if ( '' !== trim( (string) $s['title'] ) ) { echo '

' . esc_html( $s['title'] ) . '

'; } echo '
' . ( Renderer::is_editor() ? '
' . esc_html__( 'Name', 'fieldwren' ) . '
Jane Doe
' . esc_html__( 'Email', 'fieldwren' ) . '
jane@example.com
' : '' ) . '
'; } }