/** * Given a list of fields, validate some data. * * @param array $fields List of args that would be sent to FormBuilder::input() * @param array $data (optional) The data to validate. Defaults to $_POST * @param array $to_update (optional) Existing data to populate. Necessary for nested values * * @return array */ public static function validate_post_data($fields, $data = null, $to_update = array()) { if (null === $data) { $data = stripslashes_deep($_POST); } foreach ($fields as $field) { $value = FormBuilder::get_value($field['name'], $data); $fieldObj = FormField::create($field); $value = $fieldObj->validate($value); if (null !== $value) { self::set_value($to_update, $field['name'], $value); } } return $to_update; }
/** * Generate the corresponding HTML for a field. * * @param mixed $value (optional) * * @return string */ public function render($value = null) { if (null === $value && isset($this->default)) { $value = $this->default; } $args = $this->args; if (null !== $value) { $this->_set_value($args, $value); } $args['name'] = FormBuilder::get_name($args['name']); return str_replace(FormBuilder::TOKEN, $this->_render($args), $this->wrap); }
/** * Saves metabox form data. * * @param int $post_id * * @return void */ protected function save($post_id) { $form_fields = $this->form_fields(); $to_update = FormBuilder::validate_post_data($form_fields); // Filter data $to_update = $this->before_save($to_update, $post_id); // Validate dataset $is_valid = $this->validate_post_data($to_update, $post_id); if ($is_valid instanceof \WP_Error && $is_valid->get_error_codes()) { $error_data = array('fields' => $is_valid->get_error_codes(), 'messages' => $is_valid->get_error_messages(), 'data' => $to_update); update_post_meta($post_id, '_error_data_' . $this->id, $error_data); $location = add_query_arg('message', 1, get_edit_post_link($post_id, 'url')); wp_redirect(esc_url_raw(apply_filters('redirect_post_location', $location, $post_id))); exit; } foreach ($to_update as $key => $value) { update_post_meta($post_id, $key, $value); } }
/** * Get default values for one or all fields. * * @param string|array $field (optional) The field to get. * * @return mixed Whatever is in those fields. */ public function get_defaults($field = null) { return FormBuilder::get_value($field, $this->defaults); }
/** * Mimics scbForms::form_wrap() * * $this->form_wrap( $content ); // generates a form with a default submit button * * $this->form_wrap( $content, false ); // generates a form with no submit button * * // the second argument is sent to submit_button() * $this->form_wrap( $content, array( * 'text' => 'Save changes', * 'name' => 'action', * ) ); * * @see scbForms::form_wrap() * * @param string $content * @param boolean|string|array $submit_button (optional) * * @return string */ public function form_wrap($content, $submit_button = true) { if (is_array($submit_button)) { $content .= $this->submit_button($submit_button); } else { if (true === $submit_button) { $content .= $this->submit_button(); } else { if (false !== strpos($submit_button, '<input')) { $content .= $submit_button; } else { if (false !== strpos($submit_button, '<button')) { $content .= $submit_button; } else { if (false !== $submit_button) { $button_args = array_slice(func_get_args(), 1); $content .= call_user_func_array(array($this, 'submit_button'), $button_args); } } } } } return FormBuilder::form_wrap($content, $this->nonce); }