/** * AJAX handler to generate the form preview * @since 1.0 * @author Will the Web Mechanic <*****@*****.**> * @link http://willthewebmechanic.com */ public function generate_preview() { $form_args = array('survey_id' => $_POST['survey_id']); $form_elements_array = $_POST; /** * This filter facilitates the modification of form elements * prior to the form being output to preview, and prior to the * form elements being json_encoded for db storage. The intended use is * to allow for elements to exist within the form builder that do not have * a purpose in the survey form. As an example, if an radio element were * added to the form builder to choose a type of advanced validation, and * that radio was used to add/enable a rule field, the rule field is the * one that wants to be saved to the db, but the radio doesn't. Get rid of * the radio via apply_filters( 'awesome_surveys_form_preview' ). */ $form_elements_array['options'] = isset($form_elements_array['options']) ? apply_filters('awesome_surveys_form_preview', $form_elements_array['options']) : array(); if (!class_exists('Form')) { include_once plugin_dir_path(__FILE__) . 'PFBC/Form.php'; include_once plugin_dir_path(__FILE__) . 'PFBC/Overrides.php'; } $saved_elements = !empty($_POST['existing_elements']) && 'null' != $_POST['existing_elements'] ? stripslashes($_POST['existing_elements']) : get_post_meta($_POST['survey_id'], 'existing_elements', true); $this->existing_elements = !empty($saved_elements) && 'null' != $saved_elements ? array_merge(json_decode($saved_elements, true), array($form_elements_array['options'])) : array($form_elements_array['options']); $form = new FormOverrides(); $form->configure(array('class' => 'pure-form pure-form-stacked')); $preview_form = $this->get_form_preview_html($_POST['survey_id']); $post_content = $this->awesome_surveys_render_form($form_args); $form = new FormOverrides('save-survey'); $form->configure(array('class' => 'save')); $form->addElement(new Element_HTML('<hr>')); $form->addElement(new Element_Button(__('Reset', 'awesome-surveys'), 'button', array('class' => 'button-secondary reset-button', 'name' => 'reset'))); $save_form = $form->render(true); $pattern = '/<form action="[^"]+"/'; $replacement = '<div'; $save_form = preg_replace($pattern, $replacement, $save_form); $pattern = '/<\\/form>/'; $replacement = '</div>'; $save_form = preg_replace($pattern, $replacement, $save_form); $preview_form = preg_replace($pattern, $replacement, $preview_form); $pattern = '/method="post"/'; $replacement = ''; $save_form = preg_replace($pattern, $replacement, $save_form); $data = array(array($preview_form . $save_form), array(esc_html($post_content)), array(json_encode($this->existing_elements))); wp_send_json_success($data); }
function wwmas_post_content_generator($args = array(), $elements = array()) { if (!class_exists('Form')) { include_once WWM_AWESOME_SURVEYS_PATH . 'includes/PFBC/Form.php'; include_once WWM_AWESOME_SURVEYS_PATH . 'includes/PFBC/Overrides.php'; } $nonce = 'answer_survey_nonce'; $has_options = array('Element_Select', 'Element_Checkbox', 'Element_Radio'); $form_output = new FormOverrides(); $form_output->configure(array('class' => 'answer-survey pure-form pure-form-stacked', 'action' => $_SERVER['REQUEST_URI'])); $form_output->addElement(new Element_HTML('<div class="overlay"><span class="preloader"></span></div>')); $questions_count = 0; foreach ($elements as $element) { $method = $element['type']; $atts = $rules = $options = array(); if ('Element_Select' == $method) { $options[''] = __('make a selection...', 'awesome-surveys'); } if (isset($element['validation']['rules'])) { foreach ($element['validation']['rules'] as $key => $value) { if ('' != $value) { $rules['data-rule-' . $key] = $value; } } } if (in_array($method, $has_options)) { $atts = array_merge($atts, $rules); if (isset($element['default'])) { $atts['value'] = $element['default']; } if (isset($element['validation']['required']) && false != $element['validation']['required']) { $atts['required'] = 'required'; } foreach ($element['value'] as $key => $value) { /** * append :pfbc to the key so that pfbc doesn't freak out * about numerically keyed arrays. */ $options[$value . ':pfbc'] = stripslashes($element['label'][$key]); } } else { $options = array_merge($options, $rules); if (isset($element['default'])) { $options['value'] = $element['default']; } if (isset($element['validation']['required']) && false != $element['validation']['required']) { $options['required'] = 'required'; } } $form_output->addElement(new $method(stripslashes($element['name']), 'question[' . $questions_count . ']', $options, $atts)); $questions_count++; } $form_output->addElement(new Element_Hidden('answer_survey_nonce', $nonce)); $form_output->addElement(new Element_Hidden('survey_id', '', array('value' => $args['survey_id']))); $form_output->addElement(new Element_Hidden('action', 'answer-survey')); $form_output->addElement(new Element_Button(__('Submit Response', 'awesome-surveys'), 'submit', array('class' => 'button-primary', 'disabled' => 'disabled'))); return $form_output->render(true); }
protected function get_form_preview_html($post_id = 0) { $output = null; if (!class_exists('Form')) { include_once plugin_dir_path(__FILE__) . 'PFBC/Form.php'; include_once plugin_dir_path(__FILE__) . 'PFBC/Overrides.php'; } if (!isset($this->existing_elements)) { $this->existing_elements = json_decode(get_post_meta($post_id, 'existing_elements', true), true); } $required_is_option = array('Element_Textbox', 'Element_Textarea', 'Element_Email', 'Element_Number'); $elements_count = 0; if (!isset($this->buttons) || empty($this->buttons)) { $this->buttons = $this->get_buttons(); } $form = new FormOverrides(); $form->configure(array('class' => 'pure-form pure-form-stacked')); if (isset($this->existing_elements) && !empty($this->existing_elements)) { foreach ($this->existing_elements as $element) { $method = $this->buttons[$element['type']]['type']; $options = $atts = $rules = array(); if (isset($element['validation']['rules']) && is_array($element['validation']['rules'])) { foreach ($element['validation']['rules'] as $key => $value) { if ('' != $value && !is_null($value)) { $rules['data-' . $key] = $value; } } } if (in_array($method, $required_is_option) && !empty($rules)) { $options = array_merge($options, $rules); } else { $atts = array_merge($options, $rules); } if (!empty($element['validation']['required']) && 'false' != $element['validation']['required']) { if (in_array($method, $required_is_option)) { $options['required'] = 1; $options['class'] = 'required'; } else { $atts['required'] = 1; $atts['class'] = 'required'; } } $max = isset($element['label']) ? count($element['label']) : 0; for ($iterations = 0; $iterations < $max; $iterations++) { /** * Since the pfbc is being used, and it has some weird issue with values of '0', but * it will work if you append :pfbc to it...not well documented, but it works! */ $options[$element['value'][$iterations] . ':pfbc'] = htmlentities(stripslashes($element['label'][$iterations])); } $atts['value'] = isset($element['default']) ? $element['default'] : null; $has_responses = get_post_meta($post_id, '_response', true); $class = empty($has_responses) ? 'single-element-edit' : 'label-edit'; $form->addElement(new Element_HTML('<div class="' . $class . '">')); if ('Element_HTML' == $method) { $form->addElement(new Element_HTML(balanceTags(wp_kses_post($element['name']), true))); } else { $form->addElement(new $method(htmlentities(stripslashes($element['name'])), sanitize_title($element['name']), $options, $atts)); } $form->addElement(new Element_HTML('<div class="button-holder">')); $word = 'html' == $element['type'] ? 'HTML' : __('question', 'awesome-surveys'); $edit_button_html = 'html' == $element['type'] ? '' : '<button class="element-edit" data-action="edit" data-index="' . $elements_count . '">' . __('Edit question', 'awesome-surveys') . '</button>'; if (empty($has_responses)) { $form->addElement(new Element_HTML('<button class="element-edit" data-action="delete" data-index="' . $elements_count . '">' . sprintf('%s %s', __('Delete', 'awesome-surveys'), $word) . '</button>' . $edit_button_html)); } else { $form->addElement(new Element_HTML($edit_button_html)); } $form->addElement(new Element_HTML('</div><div class="clear"></div></div>')); $elements_count++; } $output = $form->render(true); } $pattern = '/<form action="[^"]+"/'; $replacement = '<div'; $output = preg_replace($pattern, $replacement, $output); $pattern = '/method="post"/'; $replacement = ''; $output = preg_replace($pattern, $replacement, $output); $pattern = '/<\\/form/'; $replacement = '</div'; $output = preg_replace($pattern, $replacement, $output); return $output; }