/**
  * Send the application email if posted
  */
 public function application_form_handler()
 {
     if (!empty($_POST['wp_job_manager_send_application'])) {
         try {
             $fields = $this->get_fields();
             $values = array();
             $job_id = absint($_POST['job_id']);
             $job = get_post($job_id);
             $meta = array();
             if (empty($job_id) || !$job || 'job_listing' !== $job->post_type) {
                 throw new Exception(__('Invalid job', 'wp-job-manager-applications'));
             }
             if (get_option('job_application_prevent_multiple_applications') && user_has_applied_for_job(get_current_user_id(), $job_id)) {
                 throw new Exception(__('You have already applied for this job', 'wp-job-manager-applications'));
             }
             // Validate posted fields
             foreach ($fields as $key => $field) {
                 $field['rules'] = array_filter(isset($field['rules']) ? (array) $field['rules'] : array());
                 switch ($field['type']) {
                     case "file":
                         $values[$key] = $this->upload_file($key, $field);
                         if (is_wp_error($values[$key])) {
                             throw new Exception($field['label'] . ': ' . $values[$key]->get_error_message());
                         }
                         break;
                     default:
                         $values[$key] = isset($_POST[$key]) ? $this->sanitize_text_field_with_linebreaks($_POST[$key]) : '';
                         break;
                 }
                 // Validate required
                 if ($field['required'] && empty($values[$key])) {
                     throw new Exception(sprintf(__('"%s" is a required field', 'wp-job-manager-applications'), $field['label']));
                 }
                 // Extra validation rules
                 if (!empty($field['rules']) && !empty($values[$key])) {
                     foreach ($field['rules'] as $rule) {
                         switch ($rule) {
                             case 'email':
                             case 'from_email':
                                 if (!is_email($values[$key])) {
                                     throw new Exception($field['label'] . ': ' . __('Please provide a valid email address', 'wp-job-manager-applications'));
                                 }
                                 break;
                             case 'numeric':
                                 if (!is_numeric($values[$key])) {
                                     throw new Exception($field['label'] . ': ' . __('Please enter a number', 'wp-job-manager-applications'));
                                 }
                                 break;
                         }
                     }
                 }
             }
             // Validation hook
             $valid = apply_filters('application_form_validate_fields', true, $fields, $values);
             if (is_wp_error($valid)) {
                 throw new Exception($valid->get_error_message());
             }
             // Prepare meta data to save
             $from_name = array();
             $from_email = '';
             $application_message = array();
             $meta['_secret_dir'] = self::$secret_dir;
             $meta['_attachment'] = array();
             $meta['_attachment_file'] = array();
             foreach ($fields as $key => $field) {
                 if (empty($values[$key])) {
                     continue;
                 }
                 $field['rules'] = array_filter(isset($field['rules']) ? (array) $field['rules'] : array());
                 if (in_array('from_name', $field['rules'])) {
                     $from_name[] = $values[$key];
                 }
                 if (in_array('from_email', $field['rules'])) {
                     $from_email = $values[$key];
                 }
                 if (in_array('message', $field['rules'])) {
                     $application_message[] = $values[$key];
                 }
                 if (in_array('resume_id', $field['rules'])) {
                     $meta['_resume_id'] = absint($values[$key]);
                     continue;
                 }
                 if ('file' === $field['type']) {
                     if (!empty($values[$key])) {
                         $index = 1;
                         foreach ($values[$key] as $attachment) {
                             if (!is_wp_error($attachment)) {
                                 if (in_array('attachment', $field['rules'])) {
                                     $meta['_attachment'][] = $attachment->url;
                                     $meta['_attachment_file'][] = $attachment->file;
                                 } else {
                                     $meta[$field['label'] . ' ' . $index] = $attachment->url;
                                 }
                             }
                             $index++;
                         }
                     }
                 } elseif ('checkbox' === $field['type']) {
                     $meta[$field['label']] = $values[$key] ? __('Yes', 'wp-job-manager-applications') : __('No', 'wp-job-manager-applications');
                 } elseif (is_array($values[$key])) {
                     $meta[$field['label']] = implode(', ', $values[$key]);
                 } else {
                     $meta[$field['label']] = $values[$key];
                 }
             }
             $from_name = implode(' ', $from_name);
             $application_message = implode("\n\n", $application_message);
             $meta = apply_filters('job_application_form_posted_meta', $meta, $values);
             // Create application
             if (!($application_id = create_job_application($job_id, $from_name, $from_email, $application_message, $meta))) {
                 throw new Exception(__('Could not create job application', 'wp-job-manager-applications'));
             }
             // Candidate email
             $candidate_email_content = get_job_application_candidate_email_content();
             if ($candidate_email_content) {
                 $existing_shortcode_tags = $GLOBALS['shortcode_tags'];
                 remove_all_shortcodes();
                 job_application_email_add_shortcodes(array('application_id' => $application_id, 'job_id' => $job_id, 'user_id' => get_current_user_id(), 'candidate_name' => $from_name, 'candidate_email' => $from_email, 'application_message' => $application_message, 'meta' => $meta));
                 $subject = do_shortcode(get_job_application_candidate_email_subject());
                 $message = do_shortcode($candidate_email_content);
                 $message = str_replace("\n\n\n\n", "\n\n", implode("\n", array_map('trim', explode("\n", $message))));
                 $is_html = $message != strip_tags($message);
                 // Does this message contain formatting already?
                 if ($is_html && !strstr($message, '<p') && !strstr($message, '<br')) {
                     $message = nl2br($message);
                 }
                 $GLOBALS['shortcode_tags'] = $existing_shortcode_tags;
                 $headers = array();
                 $headers[] = 'From: ' . get_bloginfo('name') . ' <noreply@' . str_replace(array('http://', 'https://', 'www.'), '', site_url('')) . '>';
                 $headers[] = $is_html ? 'Content-Type: text/html' : 'Content-Type: text/plain';
                 $headers[] = 'charset=utf-8';
                 wp_mail(apply_filters('create_job_application_candidate_notification_recipient', $from_email, $job_id, $application_id), apply_filters('create_job_application_candidate_notification_subject', $subject, $job_id, $application_id), apply_filters('create_job_application_candidate_notification_message', $message), apply_filters('create_job_application_candidate_notification_headers', $headers, $job_id, $application_id), apply_filters('create_job_application_candidate_notification_attachments', array(), $job_id, $application_id));
             }
             // Message to display
             add_action('job_content_start', array($this, 'application_form_success'));
             // Trigger action
             do_action('new_job_application', $application_id, $job_id);
         } catch (Exception $e) {
             $this->error = $e->getMessage();
             add_action('job_content_start', array($this, 'application_form_errors'));
         }
     }
 }
 /**
  * Create a new job application
  * @param  int $job_id
  * @param  string $candidate_name
  * @param  string $application_message
  * @param  string $candidate_email
  * @param  array  $meta
  * @param  bool $notification
  * @return int|bool success
  */
 function create_job_application($job_id, $candidate_name, $candidate_email, $application_message, $meta = array(), $notification = true, $source = '')
 {
     $job = get_post($job_id);
     if (!$job || $job->post_type !== 'job_listing') {
         return false;
     }
     $application_data = array('post_title' => wp_kses_post($candidate_name), 'post_content' => wp_kses_post($application_message), 'post_status' => current(array_keys(get_job_application_statuses())), 'post_type' => 'job_application', 'comment_status' => 'closed', 'post_author' => $job->post_author, 'post_parent' => $job_id);
     $application_id = wp_insert_post($application_data);
     if ($application_id) {
         update_post_meta($application_id, '_job_applied_for', $job->post_title);
         update_post_meta($application_id, '_candidate_email', $candidate_email);
         update_post_meta($application_id, '_candidate_user_id', get_current_user_id());
         update_post_meta($application_id, '_rating', 0);
         update_post_meta($application_id, '_application_source', $source);
         if ($meta) {
             foreach ($meta as $key => $value) {
                 update_post_meta($application_id, $key, $value);
             }
         }
         if ($notification) {
             $method = get_the_job_application_method($job_id);
             if ("email" === $method->type) {
                 $send_to = $method->raw_email;
             } elseif ($job->post_author) {
                 $user = get_user_by('id', $job->post_author);
                 $send_to = $user->user_email;
             } else {
                 $send_to = '';
             }
             if ($send_to) {
                 $attachments = array();
                 if (!empty($meta['_attachment_file'])) {
                     if (is_array($meta['_attachment_file'])) {
                         foreach ($meta['_attachment_file'] as $file) {
                             $attachments[] = $file;
                         }
                     } else {
                         $attachments[] = $meta['_attachment_file'];
                     }
                 }
                 $existing_shortcode_tags = $GLOBALS['shortcode_tags'];
                 remove_all_shortcodes();
                 job_application_email_add_shortcodes(array('application_id' => $application_id, 'job_id' => $job_id, 'user_id' => get_current_user_id(), 'candidate_name' => $candidate_name, 'candidate_email' => $candidate_email, 'application_message' => $application_message, 'meta' => $meta));
                 $subject = do_shortcode(get_job_application_email_subject());
                 $message = do_shortcode(get_job_application_email_content());
                 $message = str_replace("\n\n\n\n", "\n\n", implode("\n", array_map('trim', explode("\n", $message))));
                 $is_html = $message != strip_tags($message);
                 // Does this message contain formatting already?
                 if ($is_html && !strstr($message, '<p') && !strstr($message, '<br')) {
                     $message = nl2br($message);
                 }
                 $GLOBALS['shortcode_tags'] = $existing_shortcode_tags;
                 $headers = array();
                 $headers[] = 'From: ' . $candidate_name . ' <' . $candidate_email . '>';
                 $headers[] = 'Reply-To: ' . $candidate_email;
                 $headers[] = $is_html ? 'Content-Type: text/html' : 'Content-Type: text/plain';
                 $headers[] = 'charset=utf-8';
                 wp_mail(apply_filters('create_job_application_notification_recipient', $send_to, $job_id, $application_id), apply_filters('create_job_application_notification_subject', $subject, $job_id, $application_id), apply_filters('create_job_application_notification_message', $message), apply_filters('create_job_application_notification_headers', $headers, $job_id, $application_id), apply_filters('create_job_application_notification_attachments', $attachments, $job_id, $application_id));
             }
         }
         return $application_id;
     }
     return false;
 }