'emails', 'message' => 'error' ) ) ); exit; } $data = array( 'email_pattern' => $pattern, 'assignment_type' => $type, 'group_id' => null, 'coupon_code' => null, 'custom_discount' => null, 'is_active' => isset( $_POST['is_active'] ) ? 1 : 0, 'free_shipping' => isset( $_POST['free_shipping'] ) ? 1 : 0, ); // Only the fields for the chosen type are stored. switch ( $type ) { case 'group': $data['group_id'] = isset( $_POST['group_id'] ) ? absint( $_POST['group_id'] ) : 0; if ( ! $data['group_id'] ) { $data['group_id'] = null; } break; case 'coupon': $data['coupon_code'] = isset( $_POST['coupon_code'] ) ? wc_format_coupon_code( wp_unslash( $_POST['coupon_code'] ) ) : ''; break; case 'custom': $data['custom_discount'] = isset( $_POST['custom_discount'] ) ? WC_EDM_Helpers::sanitize_percent( $_POST['custom_discount'] ) : 0; break; } if ( $id ) { WC_EDM_DB::update_email( $id, $data ); } else { $id = WC_EDM_DB::insert_email( $data ); } wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'message' => $id ? 'saved' : 'error' ) ) ); exit; } // Delete. if ( isset( $_GET['action'], $_GET['id'] ) && 'delete' === $_GET['action'] ) { $id = absint( $_GET['id'] ); check_admin_referer( 'wc_edm_delete_email_' . $id ); WC_EDM_DB::delete_email( $id ); wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'message' => 'deleted' ) ) ); exit; } // Toggle active. if ( isset( $_GET['action'], $_GET['id'] ) && 'toggle' === $_GET['action'] ) { $id = absint( $_GET['id'] ); check_admin_referer( 'wc_edm_toggle_email_' . $id ); $assignment = WC_EDM_DB::get_email( $id ); if ( $assignment ) { WC_EDM_DB::set_email_active( $id, $assignment->is_active ? 0 : 1 ); } wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'message' => 'saved' ) ) ); exit; } } /** * Render the emails tab (list or edit form). */ public function render() { $action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( 'new' === $action || 'edit' === $action ) { $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $this->render_form( $id ); return; } $this->render_list(); } /** * Render the list of email assignments. */ private function render_list() { $assignments = WC_EDM_DB::get_emails(); ?>

id; $edit_url = WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'action' => 'edit', 'id' => $id ) ); $delete_url = wp_nonce_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'action' => 'delete', 'id' => $id ) ), 'wc_edm_delete_email_' . $id ); $toggle_url = wp_nonce_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'action' => 'toggle', 'id' => $id ) ), 'wc_edm_toggle_email_' . $id ); $type_labels = array( 'group' => __( 'Group', 'wc-email-discount' ), 'coupon' => __( 'Coupon', 'wc-email-discount' ), 'custom' => __( 'Custom', 'wc-email-discount' ), ); ?>
email_pattern ); ?> assignment_type ] ?? $assignment->assignment_type ); ?> is_active ? esc_html__( 'Active', 'wc-email-discount' ) : esc_html__( 'Inactive', 'wc-email-discount' ); ?>
' . esc_html__( 'Assignment not found.', 'wc-email-discount' ) . '

'; return; } $type = $assignment ? $assignment->assignment_type : 'group'; $is_active = $assignment ? (int) $assignment->is_active : 1; $free_shipping = $assignment ? (int) ! empty( $assignment->free_shipping ) : 0; $coupon_codes = WC_EDM_Helpers::get_coupon_codes(); $current_coupon = $assignment && $assignment->coupon_code ? $assignment->coupon_code : ''; // Keep a stored code selectable even if the coupon was since deleted. if ( $current_coupon && ! in_array( $current_coupon, $coupon_codes, true ) ) { array_unshift( $coupon_codes, $current_coupon ); } ?>