| 1 |
<?php |
| 2 |
namespace Emailkit\Admin\Emails; |
| 3 |
|
| 4 |
defined( "ABSPATH") || exit; |
| 5 |
|
| 6 |
class EmailLists { |
| 7 |
const NEW_ORDER = "new_order"; |
| 8 |
const CANCELLED_ORDER = "cancelled_order"; |
| 9 |
const FAILED_ORDER = "failed_order"; |
| 10 |
const FAILED_ORDER_CUSTOMER = "customer_failed_order"; |
| 11 |
const ORDER_ON_HOLD = "customer_on_hold_order"; |
| 12 |
const PROCESSING_ORDER = "customer_processing_order"; |
| 13 |
const COMPLETED_ORDER = "customer_completed_order"; |
| 14 |
const REFUNDED_ORDER = "customer_refunded_order"; |
| 15 |
const LOW_STOCK = "low_stock"; |
| 16 |
const NO_STOCK = "no_stock"; |
| 17 |
const BACK_ORDER = "back_order"; |
| 18 |
const CUSTOMER_INVOICE_OR_ORDER_DETAILS = "customer_invoice"; |
| 19 |
// const ORDER_DETAILS = "Order Details"; |
| 20 |
const CUSTOMER_NOTE = "customer_note"; |
| 21 |
const RESET_PASSWORD = "customer_reset_password"; |
| 22 |
const NEW_ACCOUNT = "customer_new_account"; |
| 23 |
const WP_NEW_REGISTER = "new_register"; |
| 24 |
const WP_RESET_PASSWORD = "reset_account"; |
| 25 |
const PARTIAL_REFUND = "partial_refund"; |
| 26 |
const PAYMENT_GATEWAY_ENABLED = "admin_payment_gateway_enabled"; |
| 27 |
const METFORM = "metform"; |
| 28 |
const POPUPKIT = "popupkit"; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Get woocommerce template names |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public static function woocommerce_email($template_type = '') { |
| 36 |
$list = [ |
| 37 |
'Select Template' => esc_html__('Select Template', 'emailkit'), |
| 38 |
self::NEW_ORDER => esc_html__('New Order', 'emailkit'), |
| 39 |
self::CANCELLED_ORDER => esc_html__('Cancelled order', 'emailkit'), |
| 40 |
self::FAILED_ORDER => esc_html__('Failed Order - Admin', 'emailkit'), |
| 41 |
self::FAILED_ORDER_CUSTOMER => esc_html__('Failed Order - Customer', 'emailkit'), |
| 42 |
self::ORDER_ON_HOLD => esc_html__('Order On Hold', 'emailkit'), |
| 43 |
self::PROCESSING_ORDER => esc_html__('Processing Order', 'emailkit'), |
| 44 |
self::COMPLETED_ORDER => esc_html__('Completed Order', 'emailkit'), |
| 45 |
self::LOW_STOCK => esc_html__('Low Stock', 'emailkit'), |
| 46 |
self::NO_STOCK => esc_html__('No Stock', 'emailkit'), |
| 47 |
self::BACK_ORDER => esc_html__('Back Order', 'emailkit'), |
| 48 |
self::REFUNDED_ORDER => esc_html__('Refunded Order', 'emailkit'), |
| 49 |
self::PARTIAL_REFUND => esc_html__('Partial Refund', 'emailkit'), |
| 50 |
self::CUSTOMER_INVOICE_OR_ORDER_DETAILS => esc_html__('Customer Invoice ', 'emailkit'), |
| 51 |
self::CUSTOMER_NOTE => esc_html__('Customer Note', 'emailkit'), |
| 52 |
self::NEW_ACCOUNT => esc_html__('New Account', 'emailkit'), |
| 53 |
self::RESET_PASSWORD => esc_html__('Reset Password', 'emailkit'), |
| 54 |
self::PAYMENT_GATEWAY_ENABLED => esc_html__('Payment Gateway Enabled', 'emailkit'), |
| 55 |
]; |
| 56 |
|
| 57 |
if($template_type){ |
| 58 |
return isset($list[$template_type]) ? $list[$template_type] : ''; |
| 59 |
} |
| 60 |
|
| 61 |
return $list; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get wordpress template names |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public static function wordpress_email($template_type = '') { |
| 69 |
$list = [ |
| 70 |
'Select Template' => esc_html__('Select Template', 'emailkit'), |
| 71 |
self::WP_NEW_REGISTER => esc_html__('New Account', 'emailkit'), |
| 72 |
self::WP_RESET_PASSWORD => esc_html__('Reset Password', 'emailkit'), |
| 73 |
]; |
| 74 |
|
| 75 |
if ($template_type) { |
| 76 |
return isset($list[$template_type]) ? $list[$template_type] : ''; |
| 77 |
} |
| 78 |
|
| 79 |
return $list; |
| 80 |
} |
| 81 |
|
| 82 |
public static function metform_email($template_type = '') { |
| 83 |
$list = ['Select Template' => esc_html__('Select Form', 'emailkit')]; |
| 84 |
|
| 85 |
$metform_forms = get_posts([ |
| 86 |
'post_type' => 'metform-form', |
| 87 |
'post_status' => 'publish', |
| 88 |
'numberposts' => -1, |
| 89 |
'orderby' => 'title', |
| 90 |
'order' => 'ASC', |
| 91 |
]); |
| 92 |
|
| 93 |
// Free: Check for templates |
| 94 |
$templates = get_posts([ |
| 95 |
'post_type' => 'emailkit', |
| 96 |
'meta_query' => [['key' => ' ', 'value' => 'metform_form_', 'compare' => 'LIKE']], |
| 97 |
'posts_per_page' => 1, |
| 98 |
'fields' => 'ids', |
| 99 |
'orderby' => 'ID', |
| 100 |
'order' => 'ASC' |
| 101 |
]); |
| 102 |
|
| 103 |
if (defined('EMAILKITPRO_VERSION') || class_exists('EmailKitPro')) { |
| 104 |
// Pro: Show all forms |
| 105 |
foreach ($metform_forms as $form) { |
| 106 |
$list['metform_form_' . $form->ID] = esc_html__('Form: ', 'emailkit') . $form->post_title; |
| 107 |
} |
| 108 |
} else { |
| 109 |
if (empty($templates)) { |
| 110 |
// No templates: Show all forms |
| 111 |
foreach ($metform_forms as $form) { |
| 112 |
$list['metform_form_' . $form->ID] = esc_html__('Form: ', 'emailkit') . $form->post_title; |
| 113 |
} |
| 114 |
} else { |
| 115 |
// Templates exist: Show only forms with templates |
| 116 |
$form_ids = []; |
| 117 |
foreach ($templates as $template_id) { |
| 118 |
$form_id = get_post_meta($template_id, 'emailkit_form_id', true) ?: |
| 119 |
(preg_match('/metform_form_(\d+)/', get_post_meta($template_id, 'emailkit_template_type', true), $matches) ? $matches[1] : 0); |
| 120 |
if ($form_id) $form_ids[] = (int) $form_id; |
| 121 |
} |
| 122 |
foreach ($metform_forms as $form) { |
| 123 |
if (in_array($form->ID, $form_ids)) { |
| 124 |
$list['metform_form_' . $form->ID] = esc_html__('Form: ', 'emailkit') . $form->post_title; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if( !class_exists( 'EmailKitPro' ) && is_array($metform_forms) && count($metform_forms) > 1 && !empty($templates)) { |
| 131 |
|
| 132 |
$list['more-forms'] = esc_html__( 'More Forms...', 'emailkit' ); |
| 133 |
} |
| 134 |
|
| 135 |
return $template_type ? ($list[$template_type] ?? '') : $list; |
| 136 |
} |
| 137 |
|
| 138 |
public static function popupkit_email($template_type = '') { |
| 139 |
$list = ['Select Template' => esc_html__('Select Popup', 'emailkit')]; |
| 140 |
|
| 141 |
$popupkit_popups = get_posts([ |
| 142 |
'post_type' => 'popupkit-campaigns', |
| 143 |
'post_status' => 'publish', |
| 144 |
'numberposts' => -1, |
| 145 |
'orderby' => 'title', |
| 146 |
'order' => 'ASC', |
| 147 |
]); |
| 148 |
|
| 149 |
// Free: Check for existing templates linked to popupkit |
| 150 |
$templates = get_posts([ |
| 151 |
'post_type' => 'emailkit', |
| 152 |
'meta_query' => [['key' => 'emailkit_template_type', 'value' => 'popupkit_popup_', 'compare' => 'LIKE']], |
| 153 |
'posts_per_page' => 1, |
| 154 |
'fields' => 'ids', |
| 155 |
'orderby' => 'ID', |
| 156 |
'order' => 'ASC' |
| 157 |
]); |
| 158 |
|
| 159 |
if (defined('EMAILKITPRO_VERSION') || class_exists('EmailKitPro')) { |
| 160 |
// Pro: Show all popups |
| 161 |
foreach ($popupkit_popups as $popup) { |
| 162 |
$list['popupkit_popup_' . $popup->ID] = esc_html__('Popup: ', 'emailkit') . $popup->post_title; |
| 163 |
} |
| 164 |
} else { |
| 165 |
if (empty($templates)) { |
| 166 |
// No templates: Show all popups |
| 167 |
foreach ($popupkit_popups as $popup) { |
| 168 |
$list['popupkit_popup_' . $popup->ID] = esc_html__('Popup: ', 'emailkit') . $popup->post_title; |
| 169 |
} |
| 170 |
} else { |
| 171 |
// Templates exist: Show only popups that already have a template |
| 172 |
$popup_ids = []; |
| 173 |
foreach ($templates as $template_id) { |
| 174 |
$popup_id = get_post_meta($template_id, 'emailkit_popup_id', true) ?: |
| 175 |
(preg_match('/popupkit_popup_(\d+)/', get_post_meta($template_id, 'emailkit_template_type', true), $matches) ? $matches[1] : 0); |
| 176 |
if ($popup_id) $popup_ids[] = (int) $popup_id; |
| 177 |
} |
| 178 |
foreach ($popupkit_popups as $popup) { |
| 179 |
if (in_array($popup->ID, $popup_ids)) { |
| 180 |
$list['popupkit_popup_' . $popup->ID] = esc_html__('Popup: ', 'emailkit') . $popup->post_title; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (!class_exists('EmailKitPro') && is_array($popupkit_popups) && count($popupkit_popups) > 1 && !empty($templates)) { |
| 187 |
$list['more-popups'] = esc_html__('More Popups...', 'emailkit'); |
| 188 |
} |
| 189 |
|
| 190 |
return $template_type ? ($list[$template_type] ?? '') : $list; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Get saved templates from database |
| 196 |
* @return array |
| 197 |
*/ |
| 198 |
public static function saved_templates() : array { |
| 199 |
|
| 200 |
$emailkit_saved_templates = []; |
| 201 |
|
| 202 |
$templates = get_posts([ |
| 203 |
'post_type' => 'emailkit-template', |
| 204 |
'numberposts' => -1, |
| 205 |
]); |
| 206 |
|
| 207 |
|
| 208 |
foreach ( $templates as $template ) { |
| 209 |
|
| 210 |
setup_postdata( $template ); |
| 211 |
|
| 212 |
$template_id = get_post_meta( $template->ID, 'emailkit_template_id', true ); //get the id of main template to fetch types and etc |
| 213 |
|
| 214 |
$emailkit_saved_templates[] = array( |
| 215 |
'title' => $template->post_title, |
| 216 |
'id' => $template->ID, |
| 217 |
'template_id' => $template_id, |
| 218 |
'email_type' => get_post_meta( $template_id, 'emailkit_email_type', true ), |
| 219 |
'template_type' => get_post_meta( $template_id, 'emailkit_template_type', true ), |
| 220 |
'template_status' => get_post_meta( $template_id, 'emailkit_template_status', true ), |
| 221 |
); |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
wp_reset_postdata(); |
| 226 |
|
| 227 |
|
| 228 |
return $emailkit_saved_templates; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
} |