/** * 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')); } } }
/** * Email editor */ private function candidate_notification_editor() { if (!empty($_GET['reset-email']) && !empty($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'reset')) { delete_option('job_application_candidate_email_content'); delete_option('job_application_candidate_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-candidate-notification')) { echo $this->candidate_notification_editor_save(); } ?> <p><?php _e('Below you will find the email that is sent to a candidate after submitting an application. Leave blank to disable.', '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_candidate_email_subject()); ?> " placeholder="<?php echo esc_attr(__('Subject', 'wp-job-manager-applications')); ?> " /> </p> <p> <textarea name="email-content" cols="71" rows="10" placeholder="<?php _e('N/A', 'wp-job-manager-applications'); ?> "><?php echo esc_textarea(get_job_application_candidate_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 }