Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function __construct($file = '', $version = '1.0.0')
- {
- add_action('init', array($this, 'form_submission'), 10, 0);
- }
- public function form_submission()
- {
- global $error;
- $error = new WP_error();
- // Is this the correct way to submit the form??
- if(isset($_POST['submit']) && $_POST['action'] == 'fb_events_add')
- {
- // Do some basic validation, required fields below
- $required = array('name', 'details', 'location', 'start_date', 'start_time');
- // If is admin area, then also require 'user_id'
- if(is_admin())
- $required[] = 'user_id';
- foreach($required as $field)
- {
- if(empty($_POST[$field]))
- $error->add('field', str_replace("_", " ", ucwords($field)) . ' field is required');
- }
- // Form validation has passed
- if(!is_wp_error($error))
- {
- // Manual upload if 'image' field is set
- if(!empty($_POST['image']))
- {
- if(!function_exists('wp_handle_upload'))
- require_once(ABSPATH . 'wp-admin/includes/file.php');
- $uploaded_image = $_FILES['file'];
- $upload_overrides = array('test_form' => false);
- add_filter('upload_dir', array($this, 'fbe_upload_dir'));
- // Set upload_dir to /wp-content/uploads/images/
- $upload = wp_upload_dir();
- remove_filter('upload_dir', array($this, 'fbe_upload_dir'));
- $movefile = wp_handle_upload($uploaded_image, $upload_overrides);
- if($movefile && !isset($movefile['error']))
- {
- // If successfully moved, set location for inserting to database
- $image = "images/" . basename($uploaded_flyer);
- }
- else
- {
- // Move failed, add error
- $error->add('file', 'There was an error: ' . $movefile['error']);
- }
- }
- // Uploading using WP Media in admin area
- elseif(!empty($_POST['image_url']))
- {
- $image = str_replace(site_url() . '/wp-content/', '', $_POST['image_url']);
- }
- // If non of the above, the image_url_dropdown (an array of the 'uploads/images' folder) should be used
- else
- {
- $image = "images/" . $_POST['image_url_dropdown'];
- }
- // If still no errors, continue processing the form
- if(!is_wp_error($error))
- {
- // Are we adding a new entry or editing an existing entry
- if(isset($_GET['action']) && $_GET['action'] == 'edit')
- {
- // If editing, make sure the current logged in user owns the current event
- if(get_current_user_id() != $_POST['user_id'])
- {
- wp_die('You do not have permission to update this event.');
- }
- else
- {
- // Update the database
- $result = $this->db->update(
- $this->fb_events_table,
- array(
- 'name' => sanitize_text_field($_POST['name']),
- 'slug' => $this->get_slug($_POST['name']),
- 'details' => esc_textarea($_POST['details']),
- 'location' => sanitize_text_field($_POST['location']),
- 'start_date' => $this->date_to_mysql($_POST['start_date']),
- 'end_date' => $this->date_to_mysql($_POST['end_date']),
- 'start_time' => $_POST['start_time'],
- 'end_time' => $_POST['end_time'],
- 'image' => $image,
- 'comments' => $_POST['comments'],
- 'featured' => $_POST['featured'],
- 'user_id' => get_current_user_id()
- ),
- array('id' => $_GET['event'])
- );
- }
- }
- else
- {
- // 'edit' is not set, so we must be inserting a new entry
- $result = $this->db->insert(
- $this->fb_events_table,
- array(
- 'name' => sanitize_text_field($_POST['name']),
- 'slug' => $this->get_slug($_POST['name']),
- 'details' => esc_textarea($_POST['details']),
- 'location' => sanitize_text_field($_POST['location']),
- 'start_date' => $this->date_to_mysql($_POST['start_date']),
- 'end_date' => $this->date_to_mysql($_POST['end_date']),
- 'start_time' => $_POST['start_time'],
- 'end_time' => $_POST['end_time'],
- 'image' => $image,
- 'comments' => $_POST['comments'],
- 'featured' => $_POST['featured'],
- 'user_id' => $_POST['user_id']
- ),
- array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d')
- );
- // If has been successfully inserted set success message and unset requests.
- if($result > 0)
- {
- $success = "Thank you, the event has been " . (isset($_GET['action']) ? "updated" : "added");
- unset($_GET['action']);
- $_POST = [];
- }
- else
- {
- // There was an error, add it to WP_Error
- $error = "An unknown error has occoured";
- $error->add('file', 'ERROR: ' . $this->db->print_error());
- }
- }
- }
- else
- {
- // If there was an error uploading images
- echo "<p>" . $error->get_error_message() . "</p>";
- }
- }
- else
- {
- // If form validation failed loop through errors
- foreach($error->get_error_messages() as $msg)
- {
- echo "<div class=\"error\"><p>{$msg}</p></div>";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement