| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\EmailSettings; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
use EmailKit\Admin\TemplateList; |
| 7 |
use EmailKit\Admin\Emails\EmailLists; |
| 8 |
use EmailKit\Admin\Emails\Helpers\Utils; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class WcEmailSettings { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
|
| 16 |
add_filter( 'woocommerce_email_setting_columns', [$this, 'emailkit_email_setting_columns' ] ); |
| 17 |
add_action( 'woocommerce_email_setting_column_template', array( $this, 'emailkit_column_template' ) ); |
| 18 |
add_action( 'admin_enqueue_scripts',[$this, 'woocomerce_integration'] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function emailkit_email_setting_columns ($array) { |
| 22 |
|
| 23 |
if ( isset( $array['actions'] ) ) { |
| 24 |
unset( $array['actions'] ); |
| 25 |
return array_merge( |
| 26 |
$array, |
| 27 |
array( |
| 28 |
'template' => 'EmailKit', |
| 29 |
'actions' => '', |
| 30 |
) |
| 31 |
); |
| 32 |
} |
| 33 |
return $array; |
| 34 |
} |
| 35 |
|
| 36 |
public function emailkit_column_template($email) { |
| 37 |
|
| 38 |
$wc_template_type = $email->id; |
| 39 |
$builder_url= ''; |
| 40 |
$demo_url = ''; |
| 41 |
$emailkit_email_type = ''; |
| 42 |
$emailkit_template_title = ''; |
| 43 |
$template_type = ''; |
| 44 |
// Get the post ID based on email type and template types |
| 45 |
$post_id = $this->get_emailkit_post_id($wc_template_type); |
| 46 |
|
| 47 |
|
| 48 |
$emailkit_template = $this->find_emailkit_template($wc_template_type); |
| 49 |
|
| 50 |
if (null === $post_id) { |
| 51 |
if (isset($emailkit_template)) { |
| 52 |
$demo_url = isset($emailkit_template['file']) ? $emailkit_template['file'] : ''; |
| 53 |
$emailkit_email_type = isset($emailkit_template['mail_type']) ? $emailkit_template['mail_type'] : ''; |
| 54 |
$emailkit_template_title = isset($emailkit_template['title']) ? $emailkit_template['title'] : ''; |
| 55 |
$template_type = isset($emailkit_template['template_type']) ? $emailkit_template['template_type'] : ''; |
| 56 |
} |
| 57 |
} else { |
| 58 |
|
| 59 |
$builder_url = admin_url("post.php?post={$post_id}&action=emailkit-builder"); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
echo wp_kses( '<td class="wc-email-settings-table-template emailkit-woocom-wrap"> |
| 64 |
<button id="" class="emailkit-woocom-btn emailkit-open-new-form-editor-modal emailkit-edit-button" |
| 65 |
target="_blank" |
| 66 |
href="' . esc_url($builder_url) . '" |
| 67 |
data-editor-template-url="' . esc_attr($demo_url) . '" |
| 68 |
data-emailkit-email-type="' . esc_attr($emailkit_email_type) . '" |
| 69 |
data-emailkit-template-title="' . esc_attr($emailkit_template_title) . '" |
| 70 |
data-emailkit-template-type="' . esc_attr($template_type) . '" |
| 71 |
data-emailkit-template="' . esc_attr($emailkit_email_type) . '&emailkit_template_type=' . esc_attr($emailkit_template_title) . '">' |
| 72 |
. esc_html(__('Edit With Emailkit', 'emailkit')) . |
| 73 |
'</button> |
| 74 |
</td>', Utils::get_kses_array() ); |
| 75 |
} |
| 76 |
|
| 77 |
private function get_emailkit_post_id($wc_template_type) { |
| 78 |
|
| 79 |
$args = array( |
| 80 |
'post_type' => 'emailkit', |
| 81 |
'posts_per_page' => -1, |
| 82 |
'meta_query' => array( |
| 83 |
'relation' => 'AND', // Use AND relation for matching both conditions |
| 84 |
array( |
| 85 |
'key' => 'emailkit_template_type', |
| 86 |
'value' => $wc_template_type, |
| 87 |
), |
| 88 |
/* array( |
| 89 |
'key' => 'emailkit_template_status', |
| 90 |
'value' => 'inactive', // Modify this value based on your requirements |
| 91 |
), */ |
| 92 |
), |
| 93 |
); |
| 94 |
|
| 95 |
$query = new WP_Query($args); |
| 96 |
$post_ids = array(); |
| 97 |
|
| 98 |
|
| 99 |
if ($query->have_posts()) { |
| 100 |
while ($query->have_posts()) { |
| 101 |
$query->the_post(); |
| 102 |
$post_ids[] = get_the_ID(); |
| 103 |
} |
| 104 |
wp_reset_postdata(); |
| 105 |
} |
| 106 |
|
| 107 |
// error_log(\print_r($post_ids,true)); |
| 108 |
// Return the first matching post ID |
| 109 |
return $post_ids[0]?? null; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
function woocomerce_integration() { |
| 114 |
|
| 115 |
wp_enqueue_script("emailkit-admin-wc-js" , EMAILKIT_ADMIN . 'EmailSettings/Wcintegration.js' , ['jquery'], EMAILKIT_VERSION, true); |
| 116 |
wp_localize_script( 'emailkit-admin-wc-js', 'woocommerce', |
| 117 |
array( |
| 118 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 119 |
'nonce' => wp_create_nonce('emailkit_nonce'), |
| 120 |
'rest_url' => esc_url(get_rest_url(null, 'emailkit/v1/')), |
| 121 |
'rest_nonce' => wp_create_nonce('wp_rest') |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
function find_emailkit_template($wc_template_type) { |
| 129 |
$templates = TemplateList::get_templates(); |
| 130 |
$template_title = EmailLists::woocommerce_email($wc_template_type); |
| 131 |
|
| 132 |
foreach ($templates as $key => $value) { |
| 133 |
$email_type = $value['mail_type']; |
| 134 |
$template_email_type = $value['title']; |
| 135 |
|
| 136 |
|
| 137 |
if ($email_type == 'woocommerce' && $wc_template_type == $template_email_type) { |
| 138 |
|
| 139 |
|
| 140 |
return [ |
| 141 |
'file' => $value['file'], |
| 142 |
'mail_type' => $email_type, |
| 143 |
'template_type' => $wc_template_type, |
| 144 |
'title' => $template_title, |
| 145 |
]; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return []; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
} |