'stackmime-forms-builder' ), $args ), admin_url( 'admin.php' ) ); } /** * Number of saved entries per form. */ function stackmime_forms_entry_counts() { global $wpdb; $table = stackmime_forms_table(); $rows = $wpdb->get_results( "SELECT form_id, COUNT(*) AS n FROM {$table} WHERE status IN ('new','read') GROUP BY form_id" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $out = array(); foreach ( (array) $rows as $row ) { $out[ $row->form_id ] = (int) $row->n; } return $out; } /** * Create a form from a template. */ add_action( 'admin_post_stackmime_forms_builder_new', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'stackmime-forms' ), 403 ); } check_admin_referer( 'stackmime_forms_builder_new' ); $template = isset( $_POST['template'] ) ? sanitize_key( wp_unslash( $_POST['template'] ) ) : 'blank'; $id = stackmime_forms_create_from_template( $template ); wp_safe_redirect( $id ? stackmime_forms_builder_url( array( 'form' => $id ) ) : stackmime_forms_builder_url() ); exit; } ); /** * Save a form from the builder. */ add_action( 'admin_post_stackmime_forms_builder_save', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'stackmime-forms' ), 403 ); } $id = isset( $_POST['form_id'] ) ? sanitize_key( wp_unslash( $_POST['form_id'] ) ) : ''; check_admin_referer( 'stackmime_forms_builder_save_' . $id ); $all = stackmime_forms_builder_all(); if ( ! isset( $all[ $id ] ) ) { wp_die( esc_html__( 'That form no longer exists.', 'stackmime-forms' ), 404 ); } $raw = isset( $_POST['form_json'] ) ? json_decode( (string) wp_unslash( $_POST['form_json'] ), true ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- decoded JSON, every value is cleaned by stackmime_forms_builder_sanitize(). if ( ! is_array( $raw ) ) { wp_safe_redirect( stackmime_forms_builder_url( array( 'form' => $id, 'error' => 1, ) ) ); exit; } $all[ $id ] = stackmime_forms_builder_sanitize( $raw ); stackmime_forms_builder_put( $all ); wp_safe_redirect( stackmime_forms_builder_url( array( 'form' => $id, 'saved' => 1, ) ) ); exit; } ); /** * Duplicate or delete a form. */ add_action( 'admin_post_stackmime_forms_builder_action', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'stackmime-forms' ), 403 ); } $id = isset( $_GET['form'] ) ? sanitize_key( wp_unslash( $_GET['form'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- checked next, tied to this ID. check_admin_referer( 'stackmime_forms_builder_action_' . $id ); $do = isset( $_GET['do'] ) ? sanitize_key( wp_unslash( $_GET['do'] ) ) : ''; $all = stackmime_forms_builder_all(); $msg = ''; if ( isset( $all[ $id ] ) && 'delete' === $do ) { unset( $all[ $id ] ); stackmime_forms_builder_put( $all ); $msg = 'deleted'; } elseif ( isset( $all[ $id ] ) && 'duplicate' === $do ) { $copy = $all[ $id ]; /* translators: %s: form title. */ $copy['title'] = sprintf( __( '%s (copy)', 'stackmime-forms' ), $copy['title'] ); $new = stackmime_forms_new_id( $copy['title'] ); $all[ $new ] = $copy; stackmime_forms_builder_put( $all ); $msg = 'duplicated'; } wp_safe_redirect( stackmime_forms_builder_url( $msg ? array( 'done' => $msg ) : array() ) ); exit; } ); /** * Forms screen: the list, or the builder for one form. */ function stackmime_forms_render_builder() { if ( ! current_user_can( 'manage_options' ) ) { return; } $id = isset( $_GET['form'] ) ? sanitize_key( wp_unslash( $_GET['form'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. echo '
'; stackmime_forms_admin_header( 'forms' ); if ( $id && stackmime_forms_builder_get( $id ) ) { stackmime_forms_ui_open( false ); stackmime_forms_render_editor( $id ); stackmime_forms_ui_close( false ); } else { stackmime_forms_ui_open( true ); stackmime_forms_render_forms_list(); stackmime_forms_ui_close( true ); } echo '
'; } /** * The list of forms and the "new form" templates. */ function stackmime_forms_render_forms_list() { $builder = stackmime_forms_builder_all(); $counts = stackmime_forms_entry_counts(); $code = array_diff_key( stackmime_forms_all(), $builder ); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- display only. if ( isset( $_GET['done'] ) && 'deleted' === $_GET['done'] ) { stackmime_forms_ui_alert( 'ok', __( 'Form deleted. Its entries are kept.', 'stackmime-forms' ) ); } elseif ( isset( $_GET['done'] ) && 'duplicated' === $_GET['done'] ) { stackmime_forms_ui_alert( 'ok', __( 'Form duplicated.', 'stackmime-forms' ) ); } // phpcs:enable ?>

$def ) : ?> 'stackmime_forms_builder_action', 'form' => $fid, 'do' => $op, ), admin_url( 'admin-post.php' ) ), 'stackmime_forms_builder_action_' . $fid ); }; $n = count( array_filter( $def['fields'], function ( $f ) { return ! stackmime_forms_is_display_type( $f['type'] ); } ) ); ?> $form ) : ?>

[stackmime_form id=""]

[stackmime_form id=""]

$tpl ) : ?>
true ) ); wp_add_inline_script( 'stackmime-forms-builder', 'window.StackMimeFormsBuilder = ' . wp_json_encode( array( 'form' => $def, 'types' => stackmime_forms_field_types(), 'optionTypes' => stackmime_forms_option_types(), 'displayTypes' => stackmime_forms_display_types(), 'i18n' => array( 'label' => __( 'Label', 'stackmime-forms' ), 'key' => __( 'Field key', 'stackmime-forms' ), 'keyHelp' => __( 'Used in emails as {key} and in the CSV. Letters, numbers and underscores.', 'stackmime-forms' ), 'placeholder' => __( 'Placeholder', 'stackmime-forms' ), 'help' => __( 'Help text', 'stackmime-forms' ), 'text' => __( 'Text', 'stackmime-forms' ), 'options' => __( 'Options (one per line)', 'stackmime-forms' ), 'required' => __( 'Required', 'stackmime-forms' ), 'width' => __( 'Width', 'stackmime-forms' ), 'full' => __( 'Full width', 'stackmime-forms' ), 'half' => __( 'Half width', 'stackmime-forms' ), 'maxChoices' => __( 'Most choices allowed (0 = no limit)', 'stackmime-forms' ), 'moveUp' => __( 'Move up', 'stackmime-forms' ), 'moveDown' => __( 'Move down', 'stackmime-forms' ), 'duplicate' => __( 'Duplicate', 'stackmime-forms' ), 'remove' => __( 'Remove', 'stackmime-forms' ), 'edit' => __( 'Edit', 'stackmime-forms' ), 'done' => __( 'Done', 'stackmime-forms' ), 'drag' => __( 'Drag to reorder', 'stackmime-forms' ), 'empty' => __( 'No fields yet. Add one below.', 'stackmime-forms' ), 'choose' => __( 'Choose…', 'stackmime-forms' ), 'unsaved' => __( 'You have unsaved changes.', 'stackmime-forms' ), 'removeAsk' => __( 'Remove this field?', 'stackmime-forms' ), 'send' => __( 'Send', 'stackmime-forms' ), 'inactive' => __( 'This field comes from an add-on that is not active, so it is not shown on your site. It is kept here so nothing is lost.', 'stackmime-forms' ), ), ) ) . ';', 'before' ); wp_enqueue_style( 'stackmime-forms-preview', STACKMIME_FORMS_URL . 'assets/css/forms.css', array(), STACKMIME_FORMS_VERSION ); $s = $def['settings']; // phpcs:disable WordPress.Security.NonceVerification.Recommended -- display only. if ( isset( $_GET['saved'] ) ) { stackmime_forms_ui_alert( 'ok', __( 'Form saved.', 'stackmime-forms' ) ); } if ( isset( $_GET['error'] ) ) { stackmime_forms_ui_alert( 'bad', __( 'The form could not be read. Nothing was changed; please try again.', 'stackmime-forms' ) ); } // phpcs:enable ?>
[stackmime_form id=""]