/** * Show message if already applied */ public function already_applied_message() { global $post; if (user_has_applied_for_job(get_current_user_id(), $post->ID)) { get_job_manager_template('applied-notice.php', array(), 'wp-job-manager-applications', JOB_MANAGER_APPLICATIONS_PLUGIN_DIR . '/templates/'); } }
/** * 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')); } } }
/** * Disable application form if needed */ public function disable_application_form($template, $template_name) { global $post; if ('job-application.php' === $template_name && get_option('job_application_prevent_multiple_applications') && is_user_logged_in() && user_has_applied_for_job(get_current_user_id(), $post->ID)) { return locate_job_manager_template('application-form-applied.php', 'wp-job-manager-applications', JOB_MANAGER_APPLICATIONS_PLUGIN_DIR . '/templates/'); } return $template; }