'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 '