public function save() { \Taco\MrSpicy::setSuccess(); $fields = $this->getFields(); $captured_data = []; foreach ($fields as $k => $v) { if ($k === 'captured_data' || $k === 'form_config') { continue; } $captured_data[$k] = $this->get($k); } $form_config = \FormConfig::find($this->get('form_config')); $captured_data['form_configuration'] = $form_config->get('post_title'); $this->set('captured_data', json_encode($captured_data)); return parent::save(); }
/** * creates a new MrSpicy and associated configuration * @param $args array * @param $template_callback callable * @return MrSpicy object */ public function __construct($args, $template_callback = null) { // set submit action uri self::setSubmitActionURI(); $defaults = array('conf_name' => '', 'fields' => array(), 'css_class' => '', 'id' => '', 'method' => 'post', 'action' => null, 'novalidate' => false, 'hide_labels' => false, 'column_classes' => 'small-12 columns', 'exclude_post_content' => false, 'lock' => false, 'submit_button_text' => 'submit', 'success_message' => null, 'error_message' => null, 'success_redirect_url' => null, 'label_field_wrapper' => '\\Taco\\MrSpicy::rowColumnWrap', 'on_success' => null, 'use_honeypot' => false, 'honeypot_field_name' => 'your_webite_url', 'test_with_fakes' => false, 'use_recaptcha' => false, 'google_recaptcha_site_key' => null, 'google_recaptcha_secret_key' => null); // we need this to uniquely identify the form conf that will get created or loaded if (!(array_key_exists('conf_name', $args) && strlen($args['conf_name']))) { throw new Exception('conf_name must be defined in the args array'); exit; } // if the form configuration exists, load it $db_conf = $this->findFormConfigInstance($args['conf_name']); if ($db_conf) { $this->conf_instance = $db_conf; } else { $this->conf_instance = new \FormConfig(); $this->conf_instance->set('unique_id', md5($args['conf_name'])); } $conf_fields = $this->conf_instance->getFields(); // load global defaults // TODO: find a better way than using a global global $mr_spicy_forms_defaults_path; if (strlen($mr_spicy_forms_defaults_path)) { $global_defaults = (include $mr_spicy_forms_defaults_path); } else { $global_defaults = (include __DIR__ . '/../forms-defaults.php'); } // get the default form action $defaults['action'] = array_key_exists('form_action', $global_defaults) && !is_null($global_defaults['form_action']) ? $global_defaults['form_action'] : self::getSubmitActionURI(); // assign only the fields specified above and in the form conf foreach ($args as $k => $v) { if ($k === 'fields' && $args['fields'] !== 'auto') { $this->fields = $v; continue; } if ($k == 'on_success') { continue; } if (array_key_exists($k, $conf_fields)) { $this->conf_instance->set($args[$k], $args[$k]); } if (array_key_exists($k, $defaults)) { $this->settings[$k] = $args[$k]; } } /* We cannot use a closure with an event callback * where a redirect url is defined * Instead we must use a string */ if (!is_object($this->settings['on_success']) && strlen($this->settings['on_success']) && strlen($this->settings['success_redirect_url'])) { $this->conf_instance->set('on_success', $this->settings['on_success']); } else { $this->conf_instance->set('on_success', null); if (!strlen($this->conf_instance->get('success_redirect_url'))) { $this->conf_instance->set('success_redirect_url', null); } } // if a callback is defined call it on success if (self::$success === true) { if ($args['on_success'] && !strlen($this->settings['success_redirect_url'])) { $taco_object = \Taco\Post::find(self::$entry_id); if (is_string($args['on_success'])) { // is it a string? $class_and_method = explode('::', $args['on_success']); $method_class = $class_and_method[0]; $class_method = $class_and_method[1]; $method_class::$class_method($taco_object, $this); } else { // it's a closure $args['on_success']($taco_object, $this); } } } // --- messages --- // first get global default messages $defaults['success_message'] = $global_defaults['success_message']; $defaults['error_message'] = $global_defaults['error_message']; // second get developer's hardcoded per form settings from $args if (array_key_exists('success_message', $this->settings)) { $defaults['success_message'] = $this->settings['success_message']; } if (array_key_exists('error_message', $this->settings)) { $defaults['error_message'] = $this->settings['error_message']; } // lastly use the WordPress admin's message settings if (strlen($this->conf_instance->get('form_success_message'))) { $this->settings['success_message'] = $this->conf_instance->get('form_success_message'); } if (strlen($this->conf_instance->get('form_error_message'))) { $this->settings['error_message'] = $this->conf_instance->get('form_error_message'); } // merge default settings with user settings $this->settings = array_merge($defaults, $this->settings); // wrapper label/field method (is it the default, and is it a method or func?) if (is_string($this->settings['label_field_wrapper'])) { $wrapper_callable = explode('::', $this->settings['label_field_wrapper']); if (count($wrapper_method) > 1) { $wrapper_callable = current($wrapper_callable); } $this->settings['label_field_wrapper'] = $wrapper_callable; } // assign post title to instance $this->conf_instance->set('post_title', $this->get('conf_name')); if ($this->settings['fields'] !== 'auto') { $this->conf_instance->set('fields', serialize($this->fields)); } // don't use a closure if the success_redirect_url is used if (strlen($this->settings['success_redirect_url']) && is_string($args['on_success'])) { $this->conf_instance->set('on_success', $args['on_success']); } // throw an error if settings includes a success_redirect_url with a closure if (!is_string($args['on_success']) && strlen($this->settings['success_redirect_url'])) { throw new Exception('MrSpicy: If you are using "success_redirect_url", you cannot use a closure. Use must specifiy valid string callback e.g. "MyClass::myMethod"', 1); } // assign conf machine name $this->conf_machine_name = \AppLibrary\Str::machine($this->get('conf_name'), '-'); $this->conf_instance->assign(['use_honeypot' => $this->settings['use_honeypot'], 'honeypot_field_name' => $this->settings['honeypot_field_name']]); $this->conf_instance->set('use_recaptcha', $this->settings['use_recaptcha']); $this->conf_instance->set('google_recaptcha_site_key', $this->settings['google_recaptcha_site_key']); $this->conf_instance->set('google_recaptcha_secret_key', $this->settings['google_recaptcha_secret_key']); $this->conf_instance->set('post_name', $this->conf_machine_name); // assign redirect url from dev's settings // if the admin adds this value, it will need to be overridden from wp-admin if (!strlen($this->conf_instance->get('success_redirect_url'))) { $this->conf_instance->set('success_redirect_url', $this->settings['success_redirect_url']); } /* Do not save settings if locked (prevents extra db/backend work) * Checking for the prod environment could * be one way of automatically turning the lock on or off */ if (!($this->settings['lock'] && $this->conf_instance->get('ID'))) { // if the entry doesn't exist create it in the db $this->conf_ID = $this->conf_instance->save(); // get the updated form conf after save $this->conf_instance = \FormConfig::find($this->conf_ID); } return $this; }