__( 'Delete the selected entries? This cannot be undone.', 'mammoth-forms' ) ) ); if ( ! self::current_screen_is_builder() ) { return; } $form_id = isset( $_GET['form'] ) ? (int) $_GET['form'] : 0; // phpcs:ignore WordPress.Security.NonceVerification $form = Mammoth_Forms::get( $form_id ); if ( ! $form ) { return; } // The preview pane renders the real thing, so it needs the real assets. Mammoth_Render::register_assets(); wp_enqueue_style( 'mammoth-form' ); wp_enqueue_script( 'mammoth-form' ); // The Image field picks from the media library. wp_enqueue_media(); wp_enqueue_script( 'mammoth-formula', MAMMOTH_PLUGIN_URL . 'assets/js/formula.js', array(), MAMMOTH_VERSION, true ); wp_enqueue_script( 'mammoth-builder', MAMMOTH_PLUGIN_URL . 'admin/js/builder.js', array( 'mammoth-formula' ), MAMMOTH_VERSION, true ); wp_localize_script( 'mammoth-builder', 'MAMMOTH_BUILDER', apply_filters( 'mammoth_builder_config', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'mammoth_builder' ), 'formId' => $form_id, 'title' => $form['title'], 'config' => $form['config'], 'types' => Mammoth_Fields::types(), 'settingDefs' => Mammoth_Fields::setting_defs(), 'numberFormats' => Mammoth_Forms::number_formats(), 'fieldDefaults' => Mammoth_Fields::field_defaults(), 'operators' => Mammoth_Conditions::operators(), 'isPro' => mammoth_is_pro(), 'proUrl' => admin_url( 'admin.php?page=mammoth-pro' ), 'listUrl' => admin_url( 'admin.php?page=' . self::PAGE ), 'shortcode' => '[mammoth id="' . $form_id . '"]', 'settingsSchema' => self::settings_schema(), 'pages' => self::page_choices(), 'globalTags' => array( 'all_fields', 'form_title', 'site_name', 'site_url', 'date', 'entry_id', 'entry_link' ), 'i18n' => self::i18n(), ), $form ) ); } private static function i18n() { return array( 'fields' => __( 'Fields', 'mammoth-forms' ), 'settings' => __( 'Settings', 'mammoth-forms' ), 'notifications' => __( 'Notifications', 'mammoth-forms' ), 'preview' => __( 'Preview', 'mammoth-forms' ), 'save' => __( 'Save form', 'mammoth-forms' ), 'saving' => __( 'Saving…', 'mammoth-forms' ), 'saved' => __( 'Saved', 'mammoth-forms' ), 'saveFailed' => __( 'Could not save. Please try again.', 'mammoth-forms' ), 'addField' => __( 'Drag a field here', 'mammoth-forms' ), 'emptyCanvas' => __( 'Drag fields from the left, or click one to add it.', 'mammoth-forms' ), 'fieldSettings' => __( 'Field settings', 'mammoth-forms' ), 'noSelection' => __( 'Select a field on the canvas to edit it.', 'mammoth-forms' ), 'duplicate' => __( 'Duplicate', 'mammoth-forms' ), /* translators: %s: field label */ 'rangeFrom' => __( '%s (from)', 'mammoth-forms' ), /* translators: %s: field label */ 'rangeTo' => __( '%s (to)', 'mammoth-forms' ), 'delete' => __( 'Delete', 'mammoth-forms' ), 'confirmDelete' => __( 'Delete this field?', 'mammoth-forms' ), 'insertField' => __( 'Insert a field', 'mammoth-forms' ), 'availableFields' => __( 'Click to insert into the formula:', 'mammoth-forms' ), 'formulaValid' => __( 'Formula looks good.', 'mammoth-forms' ), 'formulaResult' => __( 'Test result:', 'mammoth-forms' ), 'unknownFields' => __( 'No field has this key, so it would count as 0:', 'mammoth-forms' ), 'selfReference' => __( 'A formula cannot use its own field.', 'mammoth-forms' ), 'addRule' => __( 'Add rule', 'mammoth-forms' ), 'noRules' => __( 'Always shown. Add a rule to make this field conditional.', 'mammoth-forms' ), 'addOption' => __( 'Add choice', 'mammoth-forms' ), 'optionLabel' => __( 'Label', 'mammoth-forms' ), 'optionValue' => __( 'Value', 'mammoth-forms' ), 'proBadge' => __( 'Pro', 'mammoth-forms' ), 'proOnly' => __( 'This is a Mammoth Forms Pro feature.', 'mammoth-forms' ), 'chainWarning' => __( 'Formulas read the fields visitors fill in: another calculated field counts as 0 here. To build on its result, repeat its formula inside this one.', 'mammoth-forms' ), 'shortcodeCopied' => __( 'Shortcode copied.', 'mammoth-forms' ), 'basic' => __( 'Basic', 'mammoth-forms' ), 'calc' => __( 'Numbers', 'mammoth-forms' ), 'choice' => __( 'Choices', 'mammoth-forms' ), 'layout' => __( 'Layout', 'mammoth-forms' ), 'advanced' => __( 'Advanced', 'mammoth-forms' ), 'chooseImage' => __( 'Choose image', 'mammoth-forms' ), 'replaceImage' => __( 'Replace image', 'mammoth-forms' ), 'useImage' => __( 'Use this image', 'mammoth-forms' ), 'removeImage' => __( 'Remove', 'mammoth-forms' ), 'noImage' => __( 'No image yet', 'mammoth-forms' ), 'imageUrl' => __( 'Or paste an image address:', 'mammoth-forms' ), 'addRow' => __( 'Row', 'mammoth-forms' ), 'addColumn' => __( 'Column', 'mammoth-forms' ), 'removeRow' => __( 'Remove row', 'mammoth-forms' ), 'removeColumn' => __( 'Remove column', 'mammoth-forms' ), 'tableInsert' => __( 'Click a cell, then a key to insert it:', 'mammoth-forms' ), 'tableItem' => __( 'Item', 'mammoth-forms' ), 'tableAmount' => __( 'Amount', 'mammoth-forms' ), 'confirmation' => __( 'Confirmation', 'mammoth-forms' ), 'consentDefault' => __( 'I agree to the {privacy_policy}.', 'mammoth-forms' ), 'reset' => __( 'Reset', 'mammoth-forms' ), 'insertTag' => __( 'Click to insert:', 'mammoth-forms' ), 'choosePage' => __( '— Choose a page —', 'mammoth-forms' ), ); } /** Published pages for the confirmation page picker. */ private static function page_choices() { $out = array(); foreach ( get_pages( array( 'post_status' => 'publish', 'number' => 500, 'sort_column' => 'post_title' ) ) as $page ) { $out[] = array( 'id' => (int) $page->ID, 'title' => '' !== $page->post_title ? $page->post_title : '#' . $page->ID, ); } return $out; } /** * Form-level settings, described the same way field settings are so the * builder can render them generically. */ public static function settings_schema() { $schema = array( 'general' => array( 'title' => __( 'General', 'mammoth-forms' ), 'fields' => array( 'description' => array( 'type' => 'textarea', 'label' => __( 'Form description', 'mammoth-forms' ), ), 'submit_text' => array( 'type' => 'text', 'label' => __( 'Submit button text', 'mammoth-forms' ), ), 'labels_position' => array( 'type' => 'select', 'label' => __( 'Label position', 'mammoth-forms' ), 'choices' => array( 'top' => __( 'Above the field', 'mammoth-forms' ), 'left' => __( 'Left of the field', 'mammoth-forms' ), ), ), ), ), 'look' => array( 'title' => __( 'Look', 'mammoth-forms' ), 'fields' => array( 'style_theme' => array( 'type' => 'select', 'label' => __( 'Style', 'mammoth-forms' ), 'choices' => array( 'default' => __( 'Like my theme', 'mammoth-forms' ), 'clean' => __( 'Clean', 'mammoth-forms' ), 'rounded' => __( 'Soft & rounded', 'mammoth-forms' ), ), // Styles an add-on adds survive a save while it is switched off. 'keep_unknown' => true, ), 'style_accent' => array( 'type' => 'color', 'label' => __( 'Main colour', 'mammoth-forms' ), 'help' => __( 'Button, focus outline and selected choices. Empty = the style\'s own colour.', 'mammoth-forms' ), ), 'style_radius' => array( 'type' => 'select', 'label' => __( 'Corners', 'mammoth-forms' ), 'choices' => array( '' => __( 'As the style', 'mammoth-forms' ), '0' => __( 'Square', 'mammoth-forms' ), '4' => __( 'Slightly rounded', 'mammoth-forms' ), '10' => __( 'Rounded', 'mammoth-forms' ), '24' => __( 'Pill', 'mammoth-forms' ), ), ), 'style_size' => array( 'type' => 'select', 'label' => __( 'Size', 'mammoth-forms' ), 'choices' => array( 'normal' => __( 'Normal', 'mammoth-forms' ), 'large' => __( 'Large', 'mammoth-forms' ), ), ), 'style_width' => array( 'type' => 'select', 'label' => __( 'Width', 'mammoth-forms' ), 'choices' => array( 'full' => __( 'As wide as the page area', 'mammoth-forms' ), '720' => __( 'Up to 720 px, centred', 'mammoth-forms' ), '560' => __( 'Up to 560 px, centred', 'mammoth-forms' ), ), ), ), ), 'sending' => array( 'title' => __( 'Sending', 'mammoth-forms' ), 'tab' => 'notifications', 'fields' => array( 'email_sending' => array( 'type' => 'select', 'label' => __( 'Send this form\'s emails', 'mammoth-forms' ), 'help' => __( 'In the background the visitor sees the confirmation at once and the emails follow a few seconds later.', 'mammoth-forms' ), 'choices' => array( '' => __( 'As in Settings > Email', 'mammoth-forms' ), 'now' => __( 'Right away', 'mammoth-forms' ), 'background' => __( 'In the background', 'mammoth-forms' ), ), ), ), ), 'confirmation' => array( 'title' => __( 'After the form is sent', 'mammoth-forms' ), 'tab' => 'confirmation', 'fields' => array( 'confirmation_type' => array( 'type' => 'select', 'label' => __( 'Show', 'mammoth-forms' ), 'choices' => array( 'message' => __( 'A message', 'mammoth-forms' ), 'page' => __( 'Another page of this site', 'mammoth-forms' ), 'url' => __( 'Any web address', 'mammoth-forms' ), ), ), 'success_message' => array( 'type' => 'textarea', 'rows' => 8, 'tags' => true, 'label' => __( 'Message', 'mammoth-forms' ), 'help' => __( 'Text or HTML. Tags such as {total} or {all_fields} show the visitor\'s answers and results.', 'mammoth-forms' ), 'show_if' => array( 'confirmation_type' => 'message' ), ), 'after_submit' => array( 'type' => 'select', 'label' => __( 'The form itself', 'mammoth-forms' ), 'choices' => array( 'reset' => __( 'Stays, emptied for a new answer', 'mammoth-forms' ), 'hide' => __( 'Is hidden; only the message shows', 'mammoth-forms' ), ), 'show_if' => array( 'confirmation_type' => 'message' ), ), 'confirmation_page' => array( 'type' => 'page', 'label' => __( 'Page', 'mammoth-forms' ), 'show_if' => array( 'confirmation_type' => 'page' ), ), 'redirect_url' => array( 'type' => 'text', 'tags' => true, 'label' => __( 'Web address', 'mammoth-forms' ), 'help' => __( 'Tags work here too and are encoded for the address: https://example.com/thanks?total={total:value}', 'mammoth-forms' ), 'show_if' => array( 'confirmation_type' => 'url' ), ), ), ), 'numbers' => array( 'title' => __( 'Number format', 'mammoth-forms' ), 'fields' => array( 'number_format' => array( 'type' => 'select', 'label' => __( 'Numbers are written', 'mammoth-forms' ), 'help' => __( 'For every result, table cell, email and entry of this form. Decimal places are set on each calculated field, and so is whether it groups thousands.', 'mammoth-forms' ), 'choices' => Mammoth_Forms::number_format_choices(), ), 'decimal_sep' => array( 'type' => 'text', 'label' => __( 'Decimal separator', 'mammoth-forms' ), 'show_if' => array( 'number_format' => 'custom' ), ), 'thousand_sep' => array( 'type' => 'text', 'label' => __( 'Thousands separator', 'mammoth-forms' ), 'help' => __( 'Empty = no separator: 10000.', 'mammoth-forms' ), 'show_if' => array( 'number_format' => 'custom' ), ), ), ), 'restrictions' => array( 'title' => __( 'Submissions', 'mammoth-forms' ), 'fields' => array( 'save_entries' => array( 'type' => 'toggle', 'label' => __( 'Store entries in the database', 'mammoth-forms' ), ), 'honeypot' => array( 'type' => 'toggle', 'label' => __( 'Honeypot spam protection', 'mammoth-forms' ), ), 'login_required' => array( 'type' => 'toggle', 'label' => __( 'Logged-in users only', 'mammoth-forms' ), ), 'url_prefill' => array( 'type' => 'toggle', 'label' => __( 'Fill fields from the page address', 'mammoth-forms' ), 'help' => __( 'A link such as ?mf_room=Kitchen&mf_area=20 opens the form with those answers. Use mf_ and the field key.', 'mammoth-forms' ), ), ), ), 'notifications' => array( 'title' => __( 'Admin notification', 'mammoth-forms' ), 'tab' => 'notifications', 'fields' => array( 'notify_admin' => array( 'type' => 'toggle', 'label' => __( 'Email me on every submission', 'mammoth-forms' ), ), 'notify_to' => array( 'type' => 'text', 'label' => __( 'Send to', 'mammoth-forms' ), 'help' => __( 'Comma separated. Defaults to the site admin email.', 'mammoth-forms' ), ), 'notify_bcc' => array( 'type' => 'text', 'tags' => true, 'label' => __( 'Bcc', 'mammoth-forms' ), 'help' => __( 'Optional, comma separated.', 'mammoth-forms' ), ), 'notify_from_name' => array( 'type' => 'text', 'label' => __( 'From name', 'mammoth-forms' ), 'help' => __( 'Empty = the site name.', 'mammoth-forms' ), ), 'notify_from_email' => array( 'type' => 'text', 'label' => __( 'From email', 'mammoth-forms' ), 'help' => __( 'Empty = the default sender (Settings > Email). Use an address of your own domain.', 'mammoth-forms' ), ), 'notify_reply_to' => array( 'type' => 'text', 'tags' => true, 'label' => __( 'Reply-To', 'mammoth-forms' ), 'help' => __( 'For example {email}, so "Reply" answers the visitor.', 'mammoth-forms' ), ), 'notify_subject' => array( 'type' => 'text', 'tags' => true, 'label' => __( 'Subject', 'mammoth-forms' ), ), 'notify_format' => array( 'type' => 'select', 'label' => __( 'Format', 'mammoth-forms' ), 'choices' => array( 'html' => __( 'HTML', 'mammoth-forms' ), 'plain' => __( 'Plain text', 'mammoth-forms' ), ), ), 'notify_body' => array( 'type' => 'textarea', 'rows' => 10, 'tags' => true, 'label' => __( 'Message', 'mammoth-forms' ), 'help' => __( '{all_fields} lists every answer and result; or write your own text and place single fields such as {name} or {total}.', 'mammoth-forms' ), ), ), ), ); $schema['visitor'] = array( 'title' => __( 'Email to the visitor', 'mammoth-forms' ), 'tab' => 'notifications', 'fields' => array( 'confirm_visitor' => array( 'type' => 'toggle', 'label' => __( 'Send the visitor a "we received your message" email', 'mammoth-forms' ), 'help' => __( 'A short confirmation. At most 5 an hour to the same address, so the form cannot be used to send mail to strangers.', 'mammoth-forms' ), ), 'confirm_to' => array( 'type' => 'text', 'tags' => true, 'label' => __( 'Send to', 'mammoth-forms' ), 'help' => __( 'The tag of the email field, usually {email}.', 'mammoth-forms' ), 'show_if' => array( 'confirm_visitor' => 'yes' ), ), 'confirm_subject' => array( 'type' => 'text', 'tags' => true, 'label' => __( 'Subject', 'mammoth-forms' ), 'help' => __( 'Empty = "We received your message".', 'mammoth-forms' ), 'show_if' => array( 'confirm_visitor' => 'yes' ), ), 'confirm_body' => array( 'type' => 'textarea', 'rows' => 8, 'tags' => true, 'label' => __( 'Message', 'mammoth-forms' ), 'help' => __( 'Empty = a short thank-you with the name of your site. Text or HTML; tags work here.', 'mammoth-forms' ), 'show_if' => array( 'confirm_visitor' => 'yes' ), ), ), ); return apply_filters( 'mammoth_settings_schema', $schema ); } /* ---------------------------------------------------------------- actions */ public static function handle_actions() { if ( ! current_user_can( self::capability() ) ) { return; } if ( ! isset( $_GET['mammoth_action'] ) ) { return; } $action = sanitize_key( $_GET['mammoth_action'] ); $id = isset( $_GET['form'] ) ? (int) $_GET['form'] : 0; $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; // Deleting one entry: the token belongs to that entry. $target = 'delete_entry' === $action ? ( isset( $_GET['entry'] ) ? (int) $_GET['entry'] : 0 ) : $id; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only picks which nonce is verified next. if ( ! wp_verify_nonce( $nonce, 'mammoth_' . $action . '_' . $target ) ) { wp_die( esc_html__( 'Security check failed.', 'mammoth-forms' ) ); } switch ( $action ) { case 'create': $slug = isset( $_GET['template'] ) ? sanitize_key( $_GET['template'] ) : 'blank'; $template = Mammoth_Templates::get( $slug ); if ( $template && 'blank' !== $slug ) { $new = Mammoth_Forms::create( $template['title'], $template['config'] ); } else { $new = Mammoth_Forms::create( __( 'Untitled calculator', 'mammoth-forms' ) ); } wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '&action=edit&form=' . $new ) ); exit; case 'duplicate': $new = Mammoth_Forms::duplicate( $id ); wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '&mammoth_notice=duplicated' . ( $new ? '&form=' . $new : '' ) ) ); exit; case 'delete': Mammoth_Forms::delete( $id ); wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '&mammoth_notice=deleted' ) ); exit; case 'delete_entry': $entry_id = isset( $_GET['entry'] ) ? (int) $_GET['entry'] : 0; Mammoth_Entries::delete( $entry_id ); wp_safe_redirect( admin_url( 'admin.php?page=mammoth-entries&mammoth_notice=entry_deleted' ) ); exit; case 'export': Mammoth_Entries::export_csv( $id ); exit; } } public static function ajax_save() { check_ajax_referer( 'mammoth_builder', 'nonce' ); if ( ! current_user_can( self::capability() ) ) { wp_send_json_error( array( 'message' => __( 'You are not allowed to do that.', 'mammoth-forms' ) ), 403 ); } $form_id = isset( $_POST['form_id'] ) ? (int) $_POST['form_id'] : 0; $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; $config = isset( $_POST['config'] ) ? Mammoth_Forms::sanitize_config_json( wp_unslash( $_POST['config'] ) ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitize_config_json() cleans every field and setting. if ( ! $form_id || ! is_array( $config ) ) { wp_send_json_error( array( 'message' => __( 'Nothing to save.', 'mammoth-forms' ) ), 400 ); } if ( '' === trim( $title ) ) { $title = __( 'Untitled calculator', 'mammoth-forms' ); } // Report formula problems back, without blocking the save. $warnings = array(); foreach ( $config['fields'] as $field ) { if ( 'calculated' !== $field['type'] || '' === $field['formula'] ) { continue; } $check = Mammoth_Formula::validate( $field['formula'] ); if ( is_wp_error( $check ) ) { $warnings[ $field['key'] ] = $check->get_error_message(); } } Mammoth_Forms::update( $form_id, $title, $config ); Mammoth_Health::record( $form_id, Mammoth_Health::check( $config ) ); wp_send_json_success( array( 'config' => $config, 'warnings' => $warnings, 'message' => __( 'Saved', 'mammoth-forms' ), ) ); } /** * Renders the current (unsaved) builder state as a real form. */ public static function ajax_preview() { check_ajax_referer( 'mammoth_builder', 'nonce' ); if ( ! current_user_can( self::capability() ) ) { wp_send_json_error( array(), 403 ); } $form_id = isset( $_POST['form_id'] ) ? (int) $_POST['form_id'] : 0; $config = isset( $_POST['config'] ) ? Mammoth_Forms::sanitize_config_json( wp_unslash( $_POST['config'] ) ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitize_config_json() cleans every field and setting. $form = array( 'id' => $form_id, 'title' => '', 'config' => is_array( $config ) ? $config : Mammoth_Forms::normalize_config( array() ), ); wp_send_json_success( array( 'html' => Mammoth_Render::form( $form ) ) ); } /* ---------------------------------------------------------------- screens */ /** * Add New: pick a template (or start blank). Creating goes through the * nonce-checked "create" action. */ public static function render_new() { $templates = Mammoth_Templates::all(); $categories = Mammoth_Templates::categories(); $groups = array(); foreach ( $templates as $slug => $template ) { $cat = isset( $template['category'], $categories[ $template['category'] ] ) ? $template['category'] : 'basic'; $groups[ $cat ][] = array_merge( $template, array( 'slug' => $slug ) ); } ?>
' . esc_html( $map[ $notice ] ) . '
' . esc_html__( 'Back to entries', 'mammoth-forms' ) . '
'; return; } if ( 'export' === $view ) { Mammoth_Transfer::render_export( $form ); } else { Mammoth_Transfer::render_import( $form ); } echo ''; return; } Mammoth_Entries_Screen::render_list( $form_id, $page ); echo ''; } private static function render_entry( $entry_id ) { $entry = Mammoth_Entries::get( $entry_id ); if ( ! $entry ) { echo 'IF({area} > 50, {area} * 12, 600)
TIER('shipping', {weight})