__( 'Name', 'pubforms' ), 'type' => 'text', 'required' => 1 ), array( 'label' => __( 'Email', 'pubforms' ), 'type' => 'email', 'required' => 1 ), array( 'label' => __( 'Message', 'pubforms' ), 'type' => 'textarea' ), ) ); } function pubforms_render_fields_box( $post ) { wp_nonce_field( 'pubforms_save_form', 'pubforms_nonce' ); $form = pubforms_get_form( $post->ID ); $fields = $form ? $form['fields'] : array(); if ( 'auto-draft' === $post->post_status ) { $templates = pubforms_templates(); $defaults = pubforms_default_fields(); echo '
'; echo '

' . esc_html__( 'Start from a template', 'pubforms' ) . ' ' . esc_html__( '(optional)', 'pubforms' ) . '

'; echo '
'; $cards = array_merge( array( '' => array( 'name' => __( 'Blank form', 'pubforms' ), 'description' => __( 'Start with a name, email and message.', 'pubforms' ), 'fields' => $defaults, ), ), $templates ); foreach ( $cards as $slug => $template ) { $slug = (string) $slug; echo ''; } echo '
'; echo '

' . esc_html__( 'Pick a template and its fields and settings appear below; you can edit them before or after saving.', 'pubforms' ) . '

'; echo '
'; // The field rows (and settings) for every template, embedded with the picker so the builder can // swap them in immediately. application/json means it is data (parsed with textContent), never executed. $previews = array(); foreach ( $cards as $slug => $template ) { $tfields = pubforms_sanitize_fields( $template['fields'] ); ob_start(); foreach ( $tfields as $i => $field ) { pubforms_render_field_row( $i, $field, $tfields ); } $previews[ (string) $slug ] = array( 'rows' => ob_get_clean(), 'count' => count( $tfields ), 'settings' => empty( $template['settings'] ) ? null : pubforms_sanitize_settings( $template['settings'] ), ); } echo ''; } if ( ! $fields ) { // A new form starts with a name, an email and a message. $fields = pubforms_default_fields(); } ?> $field ) : ?>

ID ); $s = $form ? $form['settings'] : pubforms_default_settings(); ?> post_status ) { echo '

' . esc_html__( 'Save the form to get its shortcode.', 'pubforms' ) . '

'; return; } echo '

' . esc_html__( 'Paste this shortcode into any page or post, or use the "PubForms Form" block:', 'pubforms' ) . '

'; echo '

ID . '"]' ) . '" onclick="this.select();" aria-label="' . esc_attr__( 'Shortcode', 'pubforms' ) . '">

'; $counts = pubforms_entry_counts( $post->ID ); /* translators: 1: total entries, 2: unread entries */ echo '

' . esc_html( sprintf( __( 'Entries: %1$d (%2$d unread)', 'pubforms' ), $counts['total'], $counts['unread'] ) ) . '

'; if ( 'publish' !== $post->post_status ) { echo '

' . esc_html__( 'Publish the form so visitors can see it.', 'pubforms' ) . '

'; } } add_action( 'save_post_pubforms_form', function ( $post_id ) { if ( ! isset( $_POST['pubforms_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pubforms_nonce'] ) ), 'pubforms_save_form' ) ) { return; } if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $post_id ) ) { return; } // The sanitizers validate every field, so the raw arrays are passed straight in. $template_slug = sanitize_key( $_POST['pubforms_template'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification -- nonce verified above. $template = null; if ( '' !== $template_slug && '' === get_post_meta( $post_id, '_pubforms_fields', true ) ) { $template = pubforms_template( $template_slug ); } if ( $template ) { update_post_meta( $post_id, '_pubforms_fields', pubforms_sanitize_fields( $template['fields'] ) ); if ( ! empty( $template['settings'] ) ) { update_post_meta( $post_id, '_pubforms_settings', pubforms_sanitize_settings( $template['settings'] ) ); } } else { update_post_meta( $post_id, '_pubforms_fields', pubforms_sanitize_fields( wp_unslash( $_POST['pubforms_fields'] ?? array() ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = pubforms_sanitize_settings( wp_unslash( $_POST['pubforms_settings'] ?? array() ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // Webhooks make the server send requests to addresses of the editor's choosing, so only administrators set them. if ( ! current_user_can( 'manage_options' ) ) { $previous = get_post_meta( $post_id, '_pubforms_settings', true ); $settings['webhook_urls'] = is_array( $previous ) ? (string) ( $previous['webhook_urls'] ?? '' ) : ''; } update_post_meta( $post_id, '_pubforms_settings', $settings ); } } ); add_action( 'admin_enqueue_scripts', function ( $hook ) { $screen = get_current_screen(); if ( $screen && 'pubforms_form' === $screen->post_type && in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { wp_enqueue_style( 'pubforms-builder', PUBFORMS_URL . 'assets/admin-builder.css', array(), pubforms_asset_version( 'admin-builder.css' ) ); wp_enqueue_script( 'pubforms-builder', PUBFORMS_URL . 'assets/admin-builder.js', array(), pubforms_asset_version( 'admin-builder.js' ), true ); } } ); /* ---------------------------------------------------------- Forms list */ add_filter( 'manage_pubforms_form_posts_columns', function ( $columns ) { $out = array(); foreach ( $columns as $key => $label ) { $out[ $key ] = $label; if ( 'title' === $key ) { $out['pubforms_shortcode'] = __( 'Shortcode', 'pubforms' ); $out['pubforms_entries'] = __( 'Entries', 'pubforms' ); } } return $out; } ); add_action( 'manage_pubforms_form_posts_custom_column', function ( $column, $post_id ) { if ( 'pubforms_shortcode' === $column ) { echo '' . esc_html( '[pubforms id="' . $post_id . '"]' ) . ''; } elseif ( 'pubforms_entries' === $column ) { $c = pubforms_entry_counts( $post_id ); /* translators: 1: total entries, 2: unread entries */ echo '' . esc_html( sprintf( __( '%1$d (%2$d unread)', 'pubforms' ), $c['total'], $c['unread'] ) ) . ''; } }, 10, 2 );