/**
 * Add shortcodes for email content
 * @param  array $data
 */
function job_application_email_add_shortcodes($data)
{
    extract($data);
    $job_title = strip_tags(get_the_title($job_id));
    $dashboard_id = get_option('job_manager_job_dashboard_page_id');
    $job_dashboard_url = $dashboard_id ? htmlspecialchars_decode(add_query_arg(array('action' => 'show_applications', 'job_id' => $job_id), get_permalink($dashboard_id))) : '';
    $meta_data = array();
    $company_name = get_the_company_name($job_id);
    $application_id = $data['application_id'];
    $user_id = $data['user_id'];
    add_shortcode('from_name', function ($atts, $content = '') use($candidate_name) {
        return job_application_email_shortcode_handler($atts, $content, $candidate_name);
    });
    add_shortcode('from_email', function ($atts, $content = '') use($candidate_email) {
        return job_application_email_shortcode_handler($atts, $content, $candidate_email);
    });
    add_shortcode('message', function ($atts, $content = '') use($application_message) {
        return job_application_email_shortcode_handler($atts, $content, $application_message);
    });
    add_shortcode('job_id', function ($atts, $content = '') use($job_id) {
        return job_application_email_shortcode_handler($atts, $content, $job_id);
    });
    add_shortcode('job_title', function ($atts, $content = '') use($job_title) {
        return job_application_email_shortcode_handler($atts, $content, $job_title);
    });
    add_shortcode('job_dashboard_url', function ($atts, $content = '') use($job_dashboard_url) {
        return job_application_email_shortcode_handler($atts, $content, $job_dashboard_url);
    });
    add_shortcode('company_name', function ($atts, $content = '') use($company_name) {
        return job_application_email_shortcode_handler($atts, $content, $company_name);
    });
    add_shortcode('application_id', function ($atts, $content = '') use($application_id) {
        return job_application_email_shortcode_handler($atts, $content, $application_id);
    });
    add_shortcode('user_id', function ($atts, $content = '') use($user_id) {
        return job_application_email_shortcode_handler($atts, $content, $user_id);
    });
    add_shortcode('job_post_meta', function ($atts, $content = '') use($job_id) {
        $atts = shortcode_atts(array('key' => ''), $atts);
        $value = get_post_meta($job_id, sanitize_text_field($atts['key']), true);
        return job_application_email_shortcode_handler($atts, $content, $value);
    });
    foreach (get_job_application_form_fields() as $key => $field) {
        if (in_array('message', $field['rules']) || in_array('from_name', $field['rules']) || in_array('from_email', $field['rules']) || in_array('attachment', $field['rules'])) {
            continue;
        }
        $value = isset($meta[$field['label']]) ? $meta[$field['label']] : '';
        if ($field['type'] === 'resumes' && function_exists('get_resume_share_link') && $value) {
            $value = get_resume_share_link($value);
        }
        $meta_data[$field['label']] = $value;
        add_shortcode($key, function ($atts, $content = '') use($value) {
            return job_application_email_shortcode_handler($atts, $content, $value);
        });
    }
    $meta_data = array_filter($meta_data);
    $meta_data_strings = array();
    foreach ($meta_data as $label => $value) {
        $meta_data_strings[] = $label . ': ' . $value;
    }
    $meta_data_strings = implode("\n", $meta_data_strings);
    add_shortcode('meta_data', function ($atts, $content = '') use($meta_data_strings) {
        return job_application_email_shortcode_handler($atts, $content, $meta_data_strings);
    });
    do_action('job_application_email_add_shortcodes', $data);
}
 /**
  * Init form fields
  */
 public function init_fields()
 {
     if (!empty($this->fields)) {
         return;
     }
     $current_user = is_user_logged_in() ? wp_get_current_user() : false;
     $this->fields = get_job_application_form_fields();
     // Handle values
     foreach ($this->fields as $key => $field) {
         if (!isset($this->fields[$key]['value'])) {
             $this->fields[$key]['value'] = '';
         }
         $field['rules'] = array_filter(isset($field['rules']) ? (array) $field['rules'] : array());
         // Special field type handling
         if (in_array('from_name', $field['rules'])) {
             if ($current_user) {
                 $this->fields[$key]['value'] = $current_user->first_name . ' ' . $current_user->last_name;
             }
         }
         if (in_array('from_email', $field['rules'])) {
             if ($current_user) {
                 $this->fields[$key]['value'] = $current_user->user_email;
             }
         }
         if ('select' === $field['type'] && !$this->fields[$key]['required']) {
             $this->fields[$key]['options'] = array_merge(array(0 => __('Choose an option', 'wp-job-manager-applications')), $this->fields[$key]['options']);
         }
         if ('resumes' === $field['type']) {
             if (function_exists('get_resume_share_link') && is_user_logged_in()) {
                 $args = apply_filters('resume_manager_get_application_form_resumes_args', array('post_type' => 'resume', 'post_status' => array('publish', 'pending', 'hidden'), 'ignore_sticky_posts' => 1, 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'desc', 'author' => get_current_user_id()));
                 $resumes = array();
                 $resume_posts = get_posts($args);
                 foreach ($resume_posts as $resume) {
                     $resumes[$resume->ID] = $resume->post_title;
                 }
             } else {
                 $resumes = null;
             }
             // No resumes? Don't show field.
             if (!$resumes) {
                 unset($this->fields[$key]);
                 continue;
             }
             // If resume field is required, and use has 1 only, hide the option (hidden input)
             if ($this->fields[$key]['required'] && 1 === sizeof($resumes)) {
                 $this->fields[$key]['type'] = 'single-resume';
                 $this->fields[$key]['value'] = current(array_keys($resumes));
                 $this->fields[$key]['description'] = '<a href="' . esc_url(get_permalink(current(array_keys($resumes)))) . '" target="_blank">' . current($resumes) . '</a>';
             } else {
                 if (!$this->fields[$key]['required']) {
                     $resumes = array_merge(array(0 => __('Choose an online resume...', 'wp-job-manager-applications')), $resumes);
                 }
                 $this->fields[$key]['type'] = 'select';
                 $this->fields[$key]['options'] = $resumes;
             }
             $this->fields[$key]['rules'][] = 'resume_id';
         }
         // Check for already posted values
         $this->fields[$key]['value'] = isset($_POST[$key]) ? $this->sanitize_text_field_with_linebreaks($_POST[$key]) : $this->fields[$key]['value'];
     }
     uasort($this->fields, array($this, 'sort_by_priority'));
 }
    /**
     * Output the form editor
     */
    private function form_editor()
    {
        if (!empty($_GET['reset-fields']) && !empty($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'reset')) {
            delete_option('job_application_form_fields');
            echo '<div class="updated"><p>' . __('The fields were successfully reset.', 'wp-job-manager-applications') . '</p></div>';
        }
        if (!empty($_POST) && !empty($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'save-fields')) {
            echo $this->form_editor_save();
        }
        $fields = get_job_application_form_fields();
        $field_rules = apply_filters('job_application_form_field_rules', array(__('Validation', 'wp-job-manager-applications') => array('required' => __('Required', 'wp-job-manager-applications'), 'email' => __('Email', 'wp-job-manager-applications'), 'numeric' => __('Numeric', 'wp-job-manager-applications')), __('Data Handling', 'wp-job-manager-applications') => array('from_name' => __('From Name', 'wp-job-manager-applications'), 'from_email' => __('From Email', 'wp-job-manager-applications'), 'message' => __('Message', 'wp-job-manager-applications'), 'attachment' => __('Attachment', 'wp-job-manager-applications'))));
        $field_types = apply_filters('job_application_form_field_types', array('text' => __('Text', 'wp-job-manager-applications'), 'textarea' => __('Textarea', 'wp-job-manager-applications'), 'file' => __('File', 'wp-job-manager-applications'), 'select' => __('Select', 'wp-job-manager-applications'), 'multiselect' => __('Multiselect', 'wp-job-manager-applications'), 'checkbox' => __('Checkbox', 'wp-job-manager-applications'), 'resumes' => __('Resume', 'wp-job-manager-applications'), 'output-content' => __('Output content', 'wp-job-manager-applications')));
        if (!function_exists('get_resume_share_link')) {
            unset($field_types['resumes']);
        }
        ?>
		<table class="widefat">
			<thead>
				<tr>
					<th width="1%">&nbsp;</th>
					<th><?php 
        _e('Field Label', 'wp-job-manager-applications');
        ?>
</th>
					<th width="1%"><?php 
        _e('Type', 'wp-job-manager-applications');
        ?>
</th>
					<th><?php 
        _e('Description', 'wp-job-manager-applications');
        ?>
</th>
					<th><?php 
        _e('Placeholder / Options', 'wp-job-manager-applications');
        ?>
</th>
					<th width="1%"><?php 
        _e('Validation / Rules', 'wp-job-manager-applications');
        ?>
</th>
					<th width="1%" class="field-actions">&nbsp;</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th colspan="4">
						<a class="button add-field" href="#"><?php 
        _e('Add field', 'wp-job-manager-applications');
        ?>
</a>
					</th>
					<th colspan="4" class="save-actions">
						<a href="<?php 
        echo wp_nonce_url(add_query_arg('reset-fields', 1), 'reset');
        ?>
" class="reset"><?php 
        _e('Reset to defaults', 'wp-job-manager-applications');
        ?>
</a>
						<input type="submit" class="save-fields button-primary" value="<?php 
        _e('Save Changes', 'wp-job-manager-applications');
        ?>
" />
					</th>
				</tr>
			</tfoot>
			<tbody id="form-fields" data-field="<?php 
        ob_start();
        $index = -1;
        $field_key = '';
        $field = array('type' => 'text', 'label' => '', 'placeholder' => '');
        include 'views/html-form-field-editor-row.php';
        echo esc_attr(ob_get_clean());
        ?>
"><?php 
        foreach ($fields as $field_key => $field) {
            $index++;
            include 'views/html-form-field-editor-row.php';
        }
        ?>
</tbody>
		</table>
		<?php 
    }