SHARE
TWEET

Untitled

a guest Jun 25th, 2015 359 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   public function __construct($file = '', $version = '1.0.0')
  2.   {
  3.      add_action('init', array($this, 'form_submission'), 10, 0);
  4.   }
  5.  
  6.   public function form_submission()
  7.   {
  8.     global $error;
  9.  
  10.     $error = new WP_error();
  11.    
  12.     // Is this the correct way to submit the form??
  13.     if(isset($_POST['submit']) && $_POST['action'] == 'fb_events_add')
  14.     {
  15.       // Do some basic validation, required fields below
  16.       $required = array('name', 'details', 'location', 'start_date', 'start_time');
  17.  
  18.       // If is admin area, then also require 'user_id'
  19.       if(is_admin())
  20.         $required[] = 'user_id';
  21.  
  22.       foreach($required as $field)
  23.       {
  24.         if(empty($_POST[$field]))
  25.           $error->add('field', str_replace("_", " ", ucwords($field)) . ' field is required');
  26.       }
  27.  
  28.       // Form validation has passed
  29.       if(!is_wp_error($error))
  30.       {
  31.         // Manual upload if 'image' field is set
  32.         if(!empty($_POST['image']))
  33.         {
  34.           if(!function_exists('wp_handle_upload'))
  35.             require_once(ABSPATH . 'wp-admin/includes/file.php');
  36.  
  37.           $uploaded_image = $_FILES['file'];
  38.           $upload_overrides = array('test_form' => false);
  39.  
  40.           add_filter('upload_dir', array($this, 'fbe_upload_dir'));
  41.           // Set upload_dir to /wp-content/uploads/images/
  42.           $upload = wp_upload_dir();
  43.           remove_filter('upload_dir', array($this, 'fbe_upload_dir'));
  44.  
  45.           $movefile = wp_handle_upload($uploaded_image, $upload_overrides);
  46.          
  47.           if($movefile && !isset($movefile['error']))
  48.           {
  49.             // If successfully moved, set location for inserting to database
  50.             $image = "images/" . basename($uploaded_flyer);
  51.           }
  52.           else
  53.           {
  54.             // Move failed, add error
  55.             $error->add('file', 'There was an error: ' . $movefile['error']);
  56.           }
  57.         }
  58.         // Uploading using WP Media in admin area
  59.         elseif(!empty($_POST['image_url']))
  60.         {
  61.           $image = str_replace(site_url() . '/wp-content/', '', $_POST['image_url']);
  62.         }
  63.         // If non of the above, the image_url_dropdown (an array of the 'uploads/images' folder) should be used
  64.         else
  65.         {
  66.           $image = "images/" . $_POST['image_url_dropdown'];
  67.         }
  68.  
  69.         // If still no errors, continue processing the form
  70.         if(!is_wp_error($error))
  71.         {
  72.           // Are we adding a new entry or editing an existing entry
  73.           if(isset($_GET['action']) && $_GET['action'] == 'edit')
  74.           {
  75.             // If editing, make sure the current logged in user owns the current event
  76.             if(get_current_user_id() != $_POST['user_id'])
  77.             {
  78.               wp_die('You do not have permission to update this event.');
  79.             }
  80.             else
  81.             {
  82.               // Update the database
  83.               $result = $this->db->update(
  84.                 $this->fb_events_table,
  85.                 array(
  86.                   'name'        => sanitize_text_field($_POST['name']),
  87.                   'slug'        => $this->get_slug($_POST['name']),
  88.                   'details'     => esc_textarea($_POST['details']),
  89.                   'location'    => sanitize_text_field($_POST['location']),
  90.                   'start_date'  => $this->date_to_mysql($_POST['start_date']),
  91.                   'end_date'    => $this->date_to_mysql($_POST['end_date']),
  92.                   'start_time'  => $_POST['start_time'],
  93.                   'end_time'    => $_POST['end_time'],
  94.                   'image'       => $image,
  95.                   'comments'    => $_POST['comments'],
  96.                   'featured'    => $_POST['featured'],
  97.                   'user_id'     => get_current_user_id()
  98.                 ),
  99.                 array('id' => $_GET['event'])
  100.               );            
  101.             }
  102.           }
  103.           else
  104.           {
  105.             // 'edit' is not set, so we must be inserting a new entry
  106.             $result = $this->db->insert(
  107.               $this->fb_events_table,
  108.               array(
  109.                 'name'        => sanitize_text_field($_POST['name']),
  110.                 'slug'        => $this->get_slug($_POST['name']),
  111.                 'details'     => esc_textarea($_POST['details']),
  112.                 'location'    => sanitize_text_field($_POST['location']),
  113.                 'start_date'  => $this->date_to_mysql($_POST['start_date']),
  114.                 'end_date'    => $this->date_to_mysql($_POST['end_date']),
  115.                 'start_time'  => $_POST['start_time'],
  116.                 'end_time'    => $_POST['end_time'],
  117.                 'image'       => $image,
  118.                 'comments'    => $_POST['comments'],
  119.                 'featured'    => $_POST['featured'],
  120.                 'user_id'     => $_POST['user_id']
  121.               ),
  122.               array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d')
  123.             );
  124.            
  125.             // If has been successfully inserted set success message and unset requests.
  126.             if($result > 0)
  127.             {
  128.               $success = "Thank you, the event has been " . (isset($_GET['action']) ? "updated" : "added");
  129.               unset($_GET['action']);
  130.               $_POST = [];
  131.             }
  132.             else
  133.             {
  134.               // There was an error, add it to WP_Error
  135.               $error = "An unknown error has occoured";
  136.               $error->add('file', 'ERROR: ' . $this->db->print_error());
  137.             }
  138.           }
  139.         }
  140.         else
  141.         {
  142.           // If there was an error uploading images
  143.           echo "<p>" . $error->get_error_message() . "</p>";
  144.         }
  145.       }
  146.       else
  147.       {  
  148.         // If form validation failed loop through errors
  149.         foreach($error->get_error_messages() as $msg)
  150.         {
  151.           echo "<div class=\"error\"><p>{$msg}</p></div>";
  152.         }
  153.       }      
  154.     }
  155.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top