id == 'viwec_template' ) {
global $wp_meta_boxes;
$wp_meta_boxes = [];
}
}
public function remove_action() {
if ( get_current_screen()->id == 'viwec_template' ) {
remove_all_actions( 'admin_notices' );
}
}
public static function init() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function delete_view_action( $actions ) {
global $post_type;
if ( 'viwec_template' === $post_type ) {
unset( $actions['view'] );
}
return $actions;
}
public function delete_permalink( $link ) {
global $post_type;
if ( 'viwec_template' === $post_type ) {
$link = '';
}
return $link;
}
public function register_custom_post_type() {
if ( ! is_admin() ) {
return;
}
$labels = array(
'name' => _x( 'Email Templates', 'Post Type General Name', 'viwec-email-template-customizer' ),
'singular_name' => _x( 'Email Templates', 'Post Type Singular Name', 'viwec-email-template-customizer' ),
'menu_name' => esc_html__( 'Email Templates', 'viwec-email-template-customizer' ),
'parent_item_colon' => esc_html__( 'Parent Email', 'viwec-email-template-customizer' ),
'all_items' => esc_html__( 'All Emails', 'viwec-email-template-customizer' ),
'add_new_item' => esc_html__( 'Add New Email Template', 'viwec-email-template-customizer' ),
'add_new' => esc_html__( 'Add New', 'viwec-email-template-customizer' ),
'edit_item' => esc_html__( 'Edit Email Templates', 'viwec-email-template-customizer' ),
'update_item' => esc_html__( 'Update Email Templates', 'viwec-email-template-customizer' ),
'search_items' => esc_html__( 'Search Email Templates', 'viwec-email-template-customizer' ),
'not_found' => esc_html__( 'Not Found', 'viwec-email-template-customizer' ),
'not_found_in_trash' => esc_html__( 'Not found in Trash', 'viwec-email-template-customizer' ),
);
$args = array(
'label' => esc_html__( 'Email Templates', 'viwec-email-template-customizer' ),
'labels' => $labels,
'supports' => array( 'title' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'product',
'query_var' => true,
'capabilities' => apply_filters( 'viwec_capabilities_role', array() ),
'create_posts' => apply_filters( 'viwec_create_posts_role', '' ),
'menu_position' => 2,
'map_meta_cap' => true,
'rewrite' => array( 'slug' => VIWEC_SLUG ),
'menu_icon' => 'dashicons-email'
);
// Registering your Custom Post Type
register_post_type( 'viwec_template', $args );
}
public function builder_page( $post ) {
if ( $post->post_type !== 'viwec_template' ) {
return;
}
$this->email_builder_box( $post );
?>
ID, 'viwec_custom_css', true );
$direction = get_post_meta( $post->ID, 'viwec_settings_direction', true );
wc_get_template( 'email-editor.php',
array(
'admin_bar_stt' => $admin_bar_stt,
'custom_css' => $custom_css,
'direction' => $direction
),
'woocommerce-email-template-customizer',
VIWEC_TEMPLATES );
}
public function email_type_box( $post ) {
wc_get_template( 'email-type.php',
[
'type_selected' => get_post_meta( $post->ID, 'viwec_settings_type', true ),
'direction_selected' => get_post_meta( $post->ID, 'viwec_settings_direction', true )
],
'woocommerce-email-template-customizer',
VIWEC_TEMPLATES );
}
public function email_attachment_box( $post ) {
?>
ID, 'viwec_setting_rules', true );
$params = [
'type_selected' => get_post_meta( $post->ID, 'viwec_settings_type', true ),
'categories_selected' => $settings['categories'] ?? [],
'countries_selected' => $settings['countries'] ?? [],
'languages_selected' => $settings['languages'] ?? [],
'min_subtotal' => $settings['min_subtotal'] ?? '',
'max_subtotal' => $settings['max_subtotal'] ?? ''
];
wc_get_template( 'email-rules.php', $params, 'woocommerce-email-template-customizer', VIWEC_TEMPLATES );
}
public function email_testing_box( $post ) {
$_orders = [];
$statuses = wc_get_order_statuses();
if ( ! empty( $statuses ) && is_array( $statuses ) ) {
foreach ( $statuses as $status => $name ) {
$arg = [ 'numberposts' => 1, 'status' => $status ];
$orders = wc_get_orders( $arg );
if ( ! empty( $orders ) ) {
$_orders[] = current( $orders );
}
}
}
wc_get_template( 'email-testing.php', [ 'orders' => $_orders ], '', VIWEC_TEMPLATES );
}
public function exim_data() {
?>
ID, 'viwec_admin_note', true );
?>
preview = true;
$order_id ? $email_render->order( $order_id ) : $email_render->demo_order();
$email_render->demo_new_user();
$email_render->render( $data );
$custom_css = isset( $_POST['custom_css'] ) ? sanitize_text_field( wp_unslash( $_POST['custom_css'] ) ) : '';
printf( '', wp_kses_post( $custom_css ) );
wp_die();
}
public function preview_custom_css() {
$custom_css = isset( $_POST['custom_css'] ) ? sanitize_text_field( wp_unslash( $_POST['custom_css'] ) ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Missing
return $custom_css;
}
public function get_error_send_mail( $message ) {
$this->send_test_error_message = wp_json_encode( $message->errors );
}
public function send_test_email() {
if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'viwec_nonce' ) ) ) {
return;
}
$data = isset( $_POST['data'] ) ? json_decode( stripslashes( html_entity_decode( sanitize_text_field( htmlentities($_POST['data'] ) ) ) ), true ) : '';
$order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : '';
$email_render = Email_Render::init();
$email_render->preview = true;
$order_id ? $email_render->order( $order_id ) : $email_render->demo_order();
$email_render->demo_new_user();
add_filter( 'viwec_after_render_style', [ $this, 'preview_custom_css' ] );
ob_start();
$email_render->render( $data );
$email = ob_get_clean();
remove_filter( 'viwec_after_render_style', [ $this, 'preview_custom_css' ] );
$headers [] = "Content-Type: text/html";
$subject = esc_html__( 'WooCommerce Email Customizer test email template', 'viwec-email-template-customizer' );
$mail_to = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
$result = false;
if ( is_email( $mail_to ) ) {
$result = wp_mail( $mail_to, $subject, $email, $headers );
}
$error_mess = esc_html__( "Mailing Error Found:", 'viwec-email-template-customizer' );
if ( $this->send_test_error_message ) {
$error_mess .= $this->send_test_error_message;
}
$message = $result ? esc_html__( 'Email was sent successfully', 'viwec-email-template-customizer' ) : $error_mess;
$result ? wp_send_json_success( $message ) : wp_send_json_error( $message );
}
public function background_image_style() {
if ( get_current_screen()->id == 'viwec_template' ) {
$img_map = Init::$img_map;
$css = '';
foreach ( $img_map['social_icons'] as $type => $data ) {
if ( is_array( $data ) && ! empty( $data ) ) {
foreach ( $data as $slug => $name ) {
$img = VIWEC_IMAGES . $slug . '.png';
$css .= ".mce-i-{$slug}{background: url('{$img}') !important; background-size: cover !important;}";
}
}
}
foreach ( $img_map['infor_icons'] as $type => $data ) {
if ( is_array( $data ) && ! empty( $data ) ) {
foreach ( $data as $slug => $name ) {
$img = VIWEC_IMAGES . $slug . '.png';
$css .= ".mce-i-{$slug}{background: url('{$img}') !important; background-size: cover !important;}";
}
}
}
wp_register_style( 'viwec-inline-style', false, '', 1 );
wp_enqueue_style( 'viwec-inline-style' );
wp_add_inline_style( 'viwec-inline-style', $css );
}
if ( isset( $_GET['page'], $_GET['tab'] ) && $_GET['page'] == 'wc-settings' && $_GET['tab'] == 'email' ) {// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$css = ".viwec-edit-button{background-color:#ffffff; color:#333333; padding:4px 10px; border-radius:3px;display:inline-flex;border:1px solid #999999;}";
$css .= ".viwec-email-status{line-height:1 !important;}";
wp_add_inline_style( 'woocommerce_admin_styles', $css );
ob_start();
?>
', '' ], '', $script );
wp_add_inline_script( 'woocommerce_admin', $script, 'after' );
}
}
public function add_column_header( $cols ) {
$cols = [
'cb' => '',
'title' => esc_html__( 'Email subject', 'viwec-email-template-customizer' ),
'type' => esc_html__( 'Type', 'viwec-email-template-customizer' ),
'recipient' => esc_html__( 'Recipient', 'viwec-email-template-customizer' ),
'note' => esc_html__( 'Note', 'viwec-email-template-customizer' ),
'date' => esc_html__( 'Date', 'viwec-email-template-customizer' )
];
return $cols;
}
public function add_column_content( $col, $post_id ) {
$wc_mails = Utils::get_email_ids();
$recipient = Utils::get_email_recipient();
$type = get_post_meta( $post_id, 'viwec_settings_type', true );
switch ( $col ) {
case 'type':
$type = $wc_mails[ $type ] ?? '';
echo esc_html( $type );
break;
case 'recipient':
echo ! empty( $recipient[ $type ] ) ? esc_html( $recipient[ $type ] ) : esc_html__( 'Customer', 'viwec-email-template-customizer' );
break;
case 'note':
$note = get_post_meta( $post_id, 'viwec_admin_note', true );
echo esc_html( $note );
break;
}
}
public function post_row_actions( $action, $post ) {
if ( $post->post_type === 'viwec_template' ) {
unset( $action['inline hide-if-no-js'] );
$href = admin_url( "post.php?action=viwec_duplicate&id={$post->ID}" );
$action = [ 'viwec-duplicate' => "" . esc_html__( 'Duplicate', 'viwec-email-template-customizer' ) . "" ] + $action;
}
return $action;
}
public function duplicate_template() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$dup_id = ! empty( $_GET['id'] ) ? absint( $_GET['id'] ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $dup_id ) {
$current_post = get_post( $dup_id );
$args = [
'post_title' => 'Copy of ' . $current_post->post_title,
'post_type' => $current_post->post_type,
];
$new_id = wp_insert_post( $args );
$email_type = get_post_meta( $dup_id, 'viwec_settings_type', true );
$email_structure = get_post_meta( $dup_id, 'viwec_email_structure', true );
$email_categories = get_post_meta( $dup_id, 'viwec_settings_categories', true );
$email_countries = get_post_meta( $dup_id, 'viwec_settings_countries', true );
update_post_meta( $new_id, 'viwec_settings_type', $email_type );
update_post_meta( $new_id, 'viwec_email_structure', str_replace( '\\', '\\\\', $email_structure ) );
update_post_meta( $new_id, 'viwec_settings_categories', $email_categories );
update_post_meta( $new_id, 'viwec_settings_countries', $email_countries );
wp_safe_redirect( admin_url( "post.php?post={$new_id}&action=edit" ) );
exit;
}
}
public function add_filter_dropdown() {
if ( get_current_screen()->id === 'edit-viwec_template' ) {
$emails = Utils::get_email_ids();
echo '';
}
}
public function parse_query_filter( $query ) {
global $pagenow;
$post_type = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( is_admin() && $pagenow == 'edit.php' && $post_type == 'viwec_template' && ! empty( $_GET['viwec_template_filter'] ) ) {// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$query->query_vars['meta_key'] = 'viwec_settings_type';// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$query->query_vars['meta_value'] = sanitize_text_field( wp_unslash( $_GET['viwec_template_filter'] ) );// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.DB.SlowDBQuery.slow_db_query_meta_value
$query->query_vars['meta_compare'] = '=';
}
return $query;
}
public function change_text_add_title( $title ) {
if ( get_current_screen()->id == 'viwec_template' ) {
$title = esc_html__( 'Add Email Subject', 'viwec-email-template-customizer' );
echo "";
}
return $title;
}
public function change_admin_bar_stt() {
$current_stt = Utils::get_admin_bar_stt();
$new_stt = $current_stt ? false : true;
$result = update_option( 'viwec_admin_bar_stt', $new_stt );
if ( $result ) {
wp_send_json_success( $new_stt );
} else {
wp_send_json_error();
}
wp_die();
}
public function add_edit_buttons( $email ) {
$email_ids = Utils::get_email_ids();
echo '';
if ( in_array( $email->id, array_keys( $email_ids ) ) ) {
$href = admin_url( "edit.php?post_type=viwec_template&viwec_template_filter={$email->id}" );
$title = esc_html__( 'Edit with WC Email Template Customizer', 'viwec-email-template-customizer' );
} else {
$href = admin_url( "edit.php?post_type=viwec_template&viwec_template_filter=default" );
$title = esc_html__( 'Edit with WC Email Template Customizer - Default template', 'viwec-email-template-customizer' );
}
printf( " ", esc_url( $href ), esc_html( $title ) );
$email_statuses = [
'enable' => esc_html__( 'Enable', 'viwec-email-template-customizer' ),
'disable' => esc_html__( 'Disable', 'viwec-email-template-customizer' )
];
$options = get_option( 'viwec_emails_status', [] );
$status = $options[ $email->id ] ?? 'enable';
?>
';
}
public function add_edit_buttons_columns( $columns ) {
unset( $columns['actions'] );
$columns['viwec-edit'] = esc_html__( 'Email Template Customize', 'viwec-email-template-customizer' );
$columns['actions'] = '';
return $columns;
}
public function add_other_type( $email_ids ) {
return $email_ids;
}
public function search_coupon() {
if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'viwec_nonce' ) ) ) {
return;
}
$q = ! empty( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : '';
if ( $q ) {
$args = [
'numberposts' => - 1,
'post_type' => 'shop_coupon',
's' => $q
];
$coupons = get_posts( $args );
if ( ! empty( $coupons ) && is_array( $coupons ) ) {
$result = [];
foreach ( $coupons as $coupon ) {
$result[] = [ 'id' => strtoupper( $coupon->post_title ), 'text' => strtoupper( $coupon->post_title ) ];
}
wp_send_json( $result );
}
}
wp_die();
}
public function search_post() {
if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'viwec_nonce' ) ) ) {
return;
}
$q = ! empty( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : '';
if ( $q ) {
$args = [
'numberposts' => - 1,
'post_type' => 'post',
's' => $q
];
$posts = get_posts( $args );
if ( ! empty( $posts ) && is_array( $posts ) ) {
$result = [];
foreach ( $posts as $post ) {
$result[] = [ 'id' => $post->ID, 'text' => strtoupper( $post->post_title ), 'content' => do_shortcode( $post->post_content ) ];
}
wp_send_json( $result );
}
}
wp_die();
}
public function set_email_status() {
if ( ! current_user_can( 'manage_woocommerce' )
|| ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'viwec_set_email_status' ) ) ) {
return;
}
$id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '';
$status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
if ( $id && $status ) {
$options = get_option( 'viwec_emails_status', [] );
$options[ $id ] = $status;
update_option( 'viwec_emails_status', $options );
wp_send_json( [
'status' => 'success',
] );
}
wp_send_json( [
'status' => 'error',
] );
}
public function use_note( $post ) {
if ( $post->post_type == 'viwec_template' ) {
printf( "",
esc_html__( 'Note', 'viwec-email-template-customizer' ),
esc_html__( 'To display the content of the 3rd plugins (Checkout Field Editor, Flexible Checkout Fields,...), drag and drop the WC Hook element to the position which you want to display it in email template.', 'viwec-email-template-customizer' ),
esc_url( 'https://docs.villatheme.com/woocommerce-email-template-customizer/#configuration_child_menu_4783' ),
esc_html__( 'View detail in document.', 'viwec-email-template-customizer' )
);
}
}
public function update_button() {
$check_updated = get_option( 'viwec_email_update_button' );
if ( $check_updated ) {
return;
}
$posts = get_posts( [ 'post_type' => 'viwec_template', 'post_status' => 'any', 'posts_per_page' => - 1 ] );
if ( ! empty( $posts ) && is_array( $posts ) ) {
$end = count( $posts );
foreach ( $posts as $i => $post ) {
$post_id = $post->ID;
$old = get_post_meta( $post_id, 'viwec_email_structure', true );
$data = json_decode( html_entity_decode( $old ), true );
foreach ( (array)$data['rows'] as $row_key => $row ) {
if ( ! empty( $row['cols'] ) ) {
foreach ( $row['cols'] as $col_key => $cols ) {
if ( ! empty( $cols['elements'] ) ) {
foreach ( $cols['elements'] as $el_key => $element ) {
if ( $element['type'] == 'html/button' ) {
$new_el = $element;
$new_el['style']['line-height'] = '46px';
unset( $new_el['childStyle']['a']['padding'] );
$data['rows'][ $row_key ]['cols'][ $col_key ]['elements'][ $el_key ] = $new_el;
}
}
}
}
}
}
$data = htmlentities( wp_json_encode( $data ) );
$data = str_replace( '\\', '\\\\', $data );
update_post_meta( $post_id, 'viwec_email_structure', $data );
if ( $i + 1 == $end ) {
update_option( 'viwec_email_update_button', true, 'no' );
}
}
}
}
}
|