/**
  * 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;
 }
    /**
     * Email editor
     */
    private function employer_notification_editor()
    {
        if (!empty($_GET['reset-email']) && !empty($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'reset')) {
            delete_option('job_application_email_content');
            delete_option('job_application_email_subject');
            echo '<div class="updated"><p>' . __('The email was successfully reset.', 'wp-job-manager-applications') . '</p></div>';
        }
        if (!empty($_POST) && !empty($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'save-employer-notification')) {
            echo $this->employer_notification_editor_save();
        }
        ?>
		<p><?php 
        _e('Below you will find the email that is sent to an employer after a candidate submits an application.', 'wp-job-manager-applications');
        ?>
</p>
		<div class="wp-job-applications-email-content-wrapper">
			<div class="wp-job-applications-email-content">
				<p>
					<input type="text" name="email-subject" value="<?php 
        echo esc_attr(get_job_application_email_subject());
        ?>
" placeholder="<?php 
        echo esc_attr(__('Subject', 'wp-job-manager-applications'));
        ?>
" />
				</p>
				<p>
					<textarea name="email-content" cols="71" rows="10"><?php 
        echo esc_textarea(get_job_application_email_content());
        ?>
</textarea>
				</p>
			</div>
			<div class="wp-job-applications-email-content-tags">
				<p><?php 
        _e('The following tags can be used to add content dynamically:', 'wp-job-manager-applications');
        ?>
</p>
				<ul>
					<?php 
        foreach (get_job_application_email_tags() as $tag => $name) {
            ?>
						<li><code>[<?php 
            echo esc_html($tag);
            ?>
]</code> - <?php 
            echo wp_kses_post($name);
            ?>
</li>
					<?php 
        }
        ?>
				</ul>
				<p><?php 
        _e('All tags can be passed a prefix and a suffix which is only output when the value is set e.g. <code>[job_title prefix="Job Title: " suffix="."]</code>', 'wp-job-manager-applications');
        ?>
</p>
			</div>
		</div>
		<p class="submit-email save-actions">
			<a href="<?php 
        echo wp_nonce_url(add_query_arg('reset-email', 1), 'reset');
        ?>
" class="reset"><?php 
        _e('Reset to defaults', 'wp-job-manager-applications');
        ?>
</a>
			<input type="submit" class="save-email button-primary" value="<?php 
        _e('Save Changes', 'wp-job-manager-applications');
        ?>
" />
		</p>
		<?php 
    }