/**
  * Send the application email if posted
  */
 public function application_form_handler()
 {
     if (!empty($_POST['wp_job_manager_send_application'])) {
         $this->init_fields();
         add_action('job_content_start', array($this, 'application_form_result'));
         try {
             $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'));
             }
             // Validate posted fields
             foreach ($this->fields as $key => $field) {
                 // Get fields
                 switch ($field['type']) {
                     case "textarea":
                         $values[$key] = isset($_POST[$key]) ? str_replace('[nl]', "\n", sanitize_text_field(str_replace("\n", '[nl]', strip_tags(stripslashes($_POST[$key]))))) : '';
                         break;
                     case "file":
                         $values[$key] = $this->upload_file($key, $field);
                         break;
                     case "multiselect":
                         $values[$key] = isset($_POST[$key]) ? array_map('sanitize_text_field', $_POST[$key]) : '';
                         break;
                     default:
                         $values[$key] = isset($_POST[$key]) ? sanitize_text_field($_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']));
                 }
                 // Errprs
                 if (is_wp_error($values[$key])) {
                     throw new Exception($field['label'] . ': ' . $values[$key]->get_error_message());
                 }
                 // Extra validation rules
                 switch ($key) {
                     case 'candidate_email':
                         if (empty($values[$key]) || !is_email($values[$key])) {
                             throw new Exception(__('Please provide a valid email address', 'wp-job-manager-applications'));
                         }
                         break;
                 }
             }
             // Validation hook
             $valid = apply_filters('application_form_validate_fields', true, $this->fields, $values);
             if (is_wp_error($valid)) {
                 throw new Exception($valid->get_error_message());
             }
             // Prepare meta data to save
             if (!empty($values['application_attachment'])) {
                 foreach ($values['application_attachment'] as $attachment) {
                     if (!is_wp_error($attachment)) {
                         if (1 === sizeof($values['application_attachment'])) {
                             $meta['_attachment'] = $attachment['url'];
                             $meta['_attachment_file'] = $attachment['file'];
                         } else {
                             $meta['_attachment'][] = $attachment['url'];
                             $meta['_attachment_file'][] = $attachment['file'];
                         }
                     }
                 }
             }
             if (!empty($values['resume_id']) && function_exists('get_resume_share_link')) {
                 $meta['_resume_id'] = $values['resume_id'];
             }
             // Filter meta
             $meta = apply_filters('job_application_form_posted_meta', $meta, $values);
             // Create application
             if (!($application_id = create_job_application($job_id, $values['candidate_name'], $values['candidate_email'], $values['application_message'], $meta))) {
                 throw new Exception(__('Could not create job application', 'wp-job-manager-applications'));
             }
             // Message to display
             $this->message = __('Your job application has been submitted successfully', 'wp-job-manager-applications');
             // Trigger action
             do_action('new_job_application', $application_id, $job_id);
         } catch (Exception $e) {
             $this->error = $e->getMessage();
         }
     }
 }
 /**
  * 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'));
         }
     }
 }
 /**
  * Handle applications via Resume Manager's Form
  * @param  int $user_id
  * @param  int $job_id
  * @param  int $resume_id
  * @param  string $application_message
  */
 public function handle_applied_with_resume($user_id, $job_id, $resume_id, $application_message, $sent_email = true)
 {
     if (!$job_id) {
         return;
     }
     $user = get_user_by('id', $user_id);
     $resume_link = get_resume_share_link($resume_id);
     $candidate_name = get_post_meta($resume_id, '_candidate_name', true);
     $candidate_email = get_post_meta($resume_id, '_candidate_email', true);
     if (empty($candidate_email)) {
         $candidate_email = $user->user_email;
     }
     $application_meta = array();
     $application_meta['_resume_id'] = $resume_id;
     $get_meta = array('_candidate_title' => __('Title', 'wp-job-manager-applications'), '_candidate_location' => __('Location', 'wp-job-manager-applications'));
     foreach ($get_meta as $key => $label) {
         if ($value = get_post_meta($resume_id, $key, true)) {
             $application_meta[$label] = $value;
         }
     }
     create_job_application($job_id, $candidate_name, $candidate_email, $application_message, $application_meta, !$sent_email, 'resume-manager');
 }