admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('emailkit_nonce'),
'rest_url' => esc_url(get_rest_url(null, 'emailkit/v1/')),
'rest_nonce' => wp_create_nonce('wp_rest')
)
);
}
/**
* Add EmailKit settings content to MetForm notification settings
*/
public function metform_settings_content($email)
{
$form_id = isset($email['id']) ? $email['id'] : 0;
$mf_template_type = 'metform_form_' . $form_id; // Use form-specific template type
$builder_url = '';
$demo_url = '';
$emailkit_email_type = '';
$emailkit_template_title = '';
$template_type = '';
// Check for existing template with form-specific type
$post_id = $this->get_emailkit_post_id($mf_template_type, $form_id);
// If no form-specific template exists, check for generic metform template
if (null === $post_id) {
$emailkit_template = $this->find_emailkit_template($mf_template_type);
if (isset($emailkit_template)) {
$demo_url = $emailkit_template['file'] ?? '';
$emailkit_email_type = $emailkit_template['mail_type'] ?? '';
$emailkit_template_title = $emailkit_template['title'] ?? '';
$template_type = $emailkit_template['template_type'] ?? '';
}
} else {
$builder_url = admin_url("post.php?post={$post_id}&action=emailkit-builder");
}
$this->render_emailkit_edit_btn($builder_url, $demo_url, $emailkit_email_type, $emailkit_template_title, $template_type, $form_id);
}
/**
* Render the EmailKit edit button for MetForm
*
* @param string $builder_url The URL to the EmailKit builder for the specific template
* @param string $demo_url The URL to the demo template
* @param string $emailkit_email_type The type of email for EmailKit
* @param string $emailkit_template_title The title of the EmailKit template
* @param string $template_type The type of template (e.g., 'metform_form_123')
* @param int $form_id The ID of the MetForm form
*
* @return void
*/
public function render_emailkit_edit_btn($builder_url, $demo_url, $emailkit_email_type, $emailkit_template_title, $template_type, $form_id)
{
?>
'emailkit',
'posts_per_page' => 1,
'fields' => 'ids', // Only retrieve post IDs for efficiency
'meta_query' => [
'relation' => 'OR',
[
'relation' => 'AND',
[
'key' => 'emailkit_template_type',
'value' => $template_type,
'compare' => '='
],
[
'key' => 'emailkit_template_status',
'value' => 'active',
'compare' => '='
]
]
]
];
$query = new WP_Query($args);
$post_ids = $query->posts;
return !empty($post_ids) ? $post_ids[0] : null;
}
/**
* Find EmailKit template based on template type
*/
public function find_emailkit_template($template_type)
{
$templates = TemplateList::get_templates();
// Check if this is a form-specific template
$is_form_template = strpos($template_type, 'metform_form_') === 0;
$form_id = $is_form_template ? str_replace('metform_form_', '', $template_type) : null;
foreach ($templates as $key => $value) {
// For form templates, match by ID
if ($is_form_template && isset($value['id']) && $value['id'] == $form_id) {
return [
'file' => $value['file'] ?? '',
'mail_type' => 'metform',
'template_type' => $template_type,
'title' => $value['template_title'] ?? esc_html__('Confirmation Mail To User', 'emailkit'),
];
}
}
// Fallback to first available metform template if no exact match
foreach ($templates as $value) {
if (($value['mail_type'] ?? '') === 'metform') {
return [
'file' => $value['file'] ?? '',
'mail_type' => 'metform',
'template_type' => $template_type,
'title' => $value['template_title'] ?? esc_html__('Confirmation Mail To User', 'emailkit'),
];
}
}
return [];
}
/**
* Customize the MetForm user email body using an EmailKit template
*
* @param string $body The default email body
* @param int $form_id The ID of the MetForm form
* @param array $form_data The submitted form data
* @param array $file_info The uploaded file information
* @return string The modified email body
*/
public function use_emailkit_template_for_user_email($body, $form_id, $form_data, $file_info, $form_settings)
{
// Define the template type
$template_type = 'metform_form_' . $form_id;
// Get the post_id using the reusable method
$post_id = $this->get_emailkit_post_id($form_id, $template_type);
// If no form-specific template exists, return original MetForm content
if (!$post_id) {
return $body;
}
// Retrieve the EmailKit template HTML from post meta
$template_html = get_post_meta($post_id, 'emailkit_template_content_html', true);
// If we have template content, use it
if (!empty($template_html)) {
$template_content = $template_html;
// Append formatted form data if the "attach submission copy" setting is enabled
if (
isset($form_settings['user_email_attach_submission_copy']) &&
$form_settings['user_email_attach_submission_copy'] === '1'
) {
$form_html = \MetForm\Core\Entries\Form_Data::format_data_for_mail($form_id, $form_data, $file_info);
$template_content .= $form_html;
}
return $template_content;
}
// Fallback to original MetForm content if no template content found
return $body;
}
}