template_types = [
"New Order" => 'New Order - WC ',
"Cancelled order" => 'Cancelled order - WC ',
"Failed Order" => 'Failed Order - WC ',
"Order On Hold" => 'Order On Hold - WC ',
"Processing Order" => 'Processing Order - WC ',
"Completed Order" => 'Completed Order - WC ',
"Refunded Order" => 'Refunded Order - WC ',
"Customer Invoice" => 'Customer Invoice - WC ',
"Customer Note" => 'Customer Note - WC ',
"Reset Password" => 'Reset Password - WC ',
"New Account" => 'New Account - WC ',
];
add_action('add_meta_boxes', [$this, 'add']);
add_action('save_post', [$this, 'save']);
}
public function add()
{
add_meta_box("metaBox_id", "Email Details", [$this, 'emailTemplate'], ["emailkit"], "advanced", "high", null);
}
/**
* MetaBox Template
*/
public function emailTemplate($object)
{
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
template_types[$emailkit_template_type])) {
update_post_meta($post_id, 'emailkit_template_type', $emailkit_template_type);
}
if (isset($_POST['emailkit_template_content_html'])) {
$template_html = sanitize_post( wp_unslash($_POST['emailkit_template_content_html']), 'raw');
update_post_meta($post_id, 'emailkit_template_content_html', $template_html);
}
if (isset($_POST['emailkit_template_content_object'])) {
$template_obj = sanitize_text_field(wp_unslash($_POST['emailkit_template_content_object']));
update_post_meta($post_id, 'emailkit_template_content_object', $template_obj);
}
if (isset($_POST["emailkit_template_status"])) {
$type = sanitize_text_field(wp_unslash($_POST['emailkit_template_type']));
$this->deactivateTemplateTypes($type);
update_post_meta($post_id, 'emailkit_template_status', 'Active');
} else {
update_post_meta($post_id, 'emailkit_template_status', 'Inactive');
}
global $wpdb;
$new_title = sanitize_text_field(wp_unslash($_POST['emailkit_template_title']));
$wpdb->update(
$wpdb->posts,
array('post_title' => $new_title),
array('ID' => $post_id),
array('%s'),
array('%d')
);
}
public function deactivateTemplateTypes($type)
{
$query = array(
'post_type' => 'emailkit',
'meta_query' => array(
array(
'key' => 'emailkit_template_type',
'value' => $type,
'compare' => '=',
),
array(
'key' => 'emailkit_template_status',
'value' => 'Active',
'compare' => '=',
),
'relation' => 'AND',
'fields' => 'ids'
)
);
$data = new \WP_Query($query);
if (isset($data)) {
$postsIds = wp_list_pluck($data->posts, 'ID');
foreach ($postsIds as $id) {
update_post_meta($id, 'emailkit_template_status', 'Inactive');
}
}
}
}