Пример #1
0
 /**
  * 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);
     }
 }
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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);
 }