Гравитационные формы + пользовательский тип сообщения, не сохраняющий флажки или DropDown - PullRequest
0 голосов
/ 20 мая 2019

У меня есть члены Пользовательский тип сообщения.Я использую формы Gravity и плагины + Custom Fields, чтобы создать ожидающую запись моего CPT, заполненную полями из гравитационной формы.Это работает, за исключением того, что флажки и 1 выпадающий список не сохраняются в CPT.После создания элемента я могу вручную сохранить значение, но оно просто не переносится из представления гравитационных форм.

Мне интересно, есть ли способ заставить поля работать?Я видел пример того, как кто-то использовал gform_after_submission для исправления флажка ACF, у которого была та же проблема, но я новичок в GF, и мой метабокс не из ACF.

Любая помощь с лучшим способом исправить исинтаксис для использования в моей ситуации высоко ценится.Код, который генерирует мой мета-блок CPT, приведен ниже.

/**
 * Generated by the WordPress Meta Box Generator at 
 */
class Rational_Meta_Box {
    private $screens = array(
        'Member',
    );
    private $fields = array(
        array(
            'id' => 'description',
            'label' => 'Description',
            'type' => 'textarea',
        ),
        array(
            'id' => 'fname',
            'label' => 'First Name',
            'type' => 'text',
        ),
        array(
            'id' => 'lname',
            'label' => 'Last Name',
            'type' => 'text',
        ),

        array(
            'id' => 'contact',
            'label' => 'Contact',
            'type' => 'text',
        ),
        array(
            'id' => 'address',
            'label' => 'Street Address',
            'type' => 'text',
        ),
        array(
            'id' => 'po-box',
            'label' => 'PO Box',
            'type' => 'text',
        ),
        array(
            'id' => 'city',
            'label' => 'City',
            'type' => 'text',
        ),
        array(
            'id' => 'state',
            'label' => 'State',
            'type' => 'text',
        ),
        array(
            'id' => 'zip',
            'label' => 'ZIP',
            'type' => 'text',
        ),
        array(
            'id' => 'country',
            'label' => 'Country',
            'type' => 'text',
        ),
        array(
            'id' => 'map_check',
            'label' => 'Do NOT show on Map',
            'type' => 'checkbox',
        ),
        array(
            'id' => 'phone',
            'label' => 'Phone',
            'type' => 'tel',
        ),
        array(
            'id' => 'email',
            'label' => 'Email',
            'type' => 'email',
        ),

            array(
            'id' => 'check_email',
            'label' => 'Do NOT show email',
            'type' => 'checkbox',
        ),
        array(
            'id' => 'website',
            'label' => 'Website',
            'type' => 'url',
        ),
        array(
            'id' => 'facebook',
            'label' => 'Facebook',
            'type' => 'url',
        ),
        array(
            'id' => 'annual-chamber-dues',
            'label' => 'Annual Chamber Dues',
            'type' => 'select',
            'options' => array(
                '55' => '1-4 employees - $55',
                '90' => '5-9 employees - $90',
                '110' => '10-15 employees - $110',
                '160' => '16+ employees - $160',
                '25' => 'Associate Membership (non-business) - $25',
                '50' => 'Non-profit Groups - $50',
            ),
        ),
        array(
            'id' => 'comments',
            'label' => 'Comments',
            'type' => 'textarea',
        ),
         array(
            'id' => 'featured',
            'label' => 'Featured Member',
            'type' => 'checkbox',
        ), 
    );

    /**
     * Class construct method. Adds actions to their respective WordPress hooks.
     */
    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
        add_action( 'save_post', array( $this, 'save_post' ) );
    }

    /**
     * Hooks into WordPress' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */
    public function add_meta_boxes() {
        foreach ( $this->screens as $screen ) {
            add_meta_box(
                'elc-member',
                __( 'ElC-Member', 'quark' ),
                array( $this, 'add_meta_box_callback' ),
                $screen,
                'advanced',
                'default'
            );
        }
    }

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */
    public function add_meta_box_callback( $post ) {
        wp_nonce_field( 'elc_member_data', 'elc_member_nonce' );
        echo 'Data for the members';
        $this->generate_fields( $post );
    }

    /**
     * Generates the field's HTML for the meta box.
     */
    public function generate_fields( $post ) {
        $output = '';
        foreach ( $this->fields as $field ) {
            $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
            $db_value = get_post_meta( $post->ID, 'elc_member_' . $field['id'], true );
            switch ( $field['type'] ) {
                case 'checkbox':
                    $input = sprintf(
                        '<input %s id="%s" name="%s" type="checkbox" value="1">',
                        $db_value === '1' ? 'checked' : '',
                        $field['id'],
                        $field['id']
                    );
                    break;
                case 'select':
                    $input = sprintf(
                        '<select id="%s" name="%s">',
                        $field['id'],
                        $field['id']
                    );
                    foreach ( $field['options'] as $key => $value ) {
                        $field_value = !is_numeric( $key ) ? $key : $value;
                        $input .= sprintf(
                            '<option %s value="%s">%s</option>',
                            $db_value === $field_value ? 'selected' : '',
                            $field_value,
                            $value
                        );
                    }
                    $input .= '</select>';
                    break;
                case 'textarea':
                    $input = sprintf(
                        '<textarea class="large-text" id="%s" name="%s" rows="6">%s</textarea>',
                        $field['id'],
                        $field['id'],
                        $db_value
                    );
                    break;
                default:
                    $input = sprintf(
                        '<input %s id="%s" name="%s" type="%s" value="%s">',
                        $field['type'] !== 'color' ? 'class="regular-text"' : '',
                        $field['id'],
                        $field['id'],
                        $field['type'],
                        $db_value
                    );
            }
            $output .= $this->row_format( $label, $input );
        }
        echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
    }

    /**
     * Generates the HTML for table rows.
     */
    public function row_format( $label, $input ) {
        return sprintf(
            '<tr><th scope="row">%s</th><td>%s</td></tr>',
            $label,
            $input
        );
    }
    /**
     * Hooks into WordPress' save_post function
     */
    public function save_post( $post_id ) {
        if ( ! isset( $_POST['elc_member_nonce'] ) )
            return $post_id;

        $nonce = $_POST['elc_member_nonce'];
        if ( !wp_verify_nonce( $nonce, 'elc_member_data' ) )
            return $post_id;

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        foreach ( $this->fields as $field ) {
            if ( isset( $_POST[ $field['id'] ] ) ) {
                switch ( $field['type'] ) {
                    case 'email':
                        $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                        break;
                    case 'text':
                        $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                        break;
                }
                update_post_meta( $post_id, 'elc_member_' . $field['id'], $_POST[ $field['id'] ] );
            } else if ( $field['type'] === 'checkbox' ) {
                update_post_meta( $post_id, 'elc_member_' . $field['id'], '0' );
            }
        }
    }
}
new Rational_Meta_Box;
...