Files

346 lines
13 KiB
PHP

<?php
/**
* Email assignments admin (CRUD).
*
* @package WC_Email_Discount_Manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* List, create, edit and delete email assignments.
*/
class WC_EDM_Admin_Emails {
/**
* Handle save/delete/toggle actions for the emails tab. Runs on admin_init.
*/
public function handle_actions() {
// Save (create or update).
if ( isset( $_POST['wc_edm_action'] ) && 'save_email' === $_POST['wc_edm_action'] ) {
check_admin_referer( 'wc_edm_save_email' );
$id = isset( $_POST['email_id'] ) ? absint( $_POST['email_id'] ) : 0;
$pattern = isset( $_POST['email_pattern'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['email_pattern'] ) ) ) : '';
$type = isset( $_POST['assignment_type'] ) ? sanitize_key( wp_unslash( $_POST['assignment_type'] ) ) : 'group';
if ( ! in_array( $type, array( 'group', 'coupon', 'custom' ), true ) ) {
$type = 'group';
}
if ( '' === $pattern ) {
wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => '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();
?>
<div class="wc-edm-header-row">
<h2><?php esc_html_e( 'Email Assignments', 'wc-email-discount' ); ?></h2>
<a class="button button-primary" href="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails', 'action' => 'new' ) ) ); ?>">
<?php esc_html_e( 'Add New Assignment', 'wc-email-discount' ); ?>
</a>
</div>
<p class="description">
<?php esc_html_e( 'Patterns: full email ([email protected]), domain (@company.com) or partial match (john).', 'wc-email-discount' ); ?>
</p>
<?php if ( empty( $assignments ) ) : ?>
<p><?php esc_html_e( 'No email assignments found.', 'wc-email-discount' ); ?></p>
<?php return; ?>
<?php endif; ?>
<table class="widefat striped wc-edm-table">
<thead>
<tr>
<th><?php esc_html_e( 'Email Pattern', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'Type', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'Assignment', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'Status', 'wc-email-discount' ); ?></th>
<th class="wc-edm-actions-col"><?php esc_html_e( 'Actions', 'wc-email-discount' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $assignments as $assignment ) : ?>
<?php
$id = (int) $assignment->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' ),
);
?>
<tr>
<td><a href="<?php echo esc_url( $edit_url ); ?>"><strong><?php echo esc_html( $assignment->email_pattern ); ?></strong></a></td>
<td>
<span class="wc-edm-badge wc-edm-badge-<?php echo esc_attr( $assignment->assignment_type ); ?>">
<?php echo esc_html( $type_labels[ $assignment->assignment_type ] ?? $assignment->assignment_type ); ?>
</span>
</td>
<td><?php echo esc_html( WC_EDM_Helpers::describe_assignment( $assignment ) ); ?></td>
<td>
<a href="<?php echo esc_url( $toggle_url ); ?>" class="wc-edm-status <?php echo $assignment->is_active ? 'is-active' : 'is-inactive'; ?>">
<?php echo $assignment->is_active ? esc_html__( 'Active', 'wc-email-discount' ) : esc_html__( 'Inactive', 'wc-email-discount' ); ?>
</a>
</td>
<td class="wc-edm-actions-col">
<a class="button button-small" href="<?php echo esc_url( $edit_url ); ?>"><?php esc_html_e( 'Edit', 'wc-email-discount' ); ?></a>
<a class="button button-small wc-edm-delete" href="<?php echo esc_url( $delete_url ); ?>"><?php esc_html_e( 'Delete', 'wc-email-discount' ); ?></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
/**
* Render the create/edit form.
*
* @param int $id Assignment ID (0 for new).
*/
private function render_form( $id ) {
$assignment = $id ? WC_EDM_DB::get_email( $id ) : null;
$groups = WC_EDM_DB::get_groups();
if ( $id && ! $assignment ) {
echo '<p>' . esc_html__( 'Assignment not found.', 'wc-email-discount' ) . '</p>';
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 );
}
?>
<h2><?php echo $assignment ? esc_html__( 'Edit Email Assignment', 'wc-email-discount' ) : esc_html__( 'Add Email Assignment', 'wc-email-discount' ); ?></h2>
<form method="post" action="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails' ) ) ); ?>">
<?php wp_nonce_field( 'wc_edm_save_email' ); ?>
<input type="hidden" name="wc_edm_action" value="save_email">
<input type="hidden" name="email_id" value="<?php echo esc_attr( $assignment ? (int) $assignment->id : 0 ); ?>">
<table class="form-table" role="presentation">
<tr>
<th scope="row">
<label for="wc-edm-email-pattern"><?php esc_html_e( 'Email Pattern', 'wc-email-discount' ); ?></label>
</th>
<td>
<input type="text" id="wc-edm-email-pattern" name="email_pattern" class="regular-text" required
value="<?php echo esc_attr( $assignment ? $assignment->email_pattern : '' ); ?>"
placeholder="[email protected]">
<p class="description">
<?php esc_html_e( 'Full email ([email protected]), domain (@company.com) or partial match (john).', 'wc-email-discount' ); ?>
</p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Assignment Type', 'wc-email-discount' ); ?></th>
<td>
<fieldset id="wc-edm-assignment-type">
<label>
<input type="radio" name="assignment_type" value="group" <?php checked( $type, 'group' ); ?>>
<?php esc_html_e( 'Discount Group', 'wc-email-discount' ); ?>
</label><br>
<label>
<input type="radio" name="assignment_type" value="coupon" <?php checked( $type, 'coupon' ); ?>>
<?php esc_html_e( 'Coupon Code', 'wc-email-discount' ); ?>
</label><br>
<label>
<input type="radio" name="assignment_type" value="custom" <?php checked( $type, 'custom' ); ?>>
<?php esc_html_e( 'Custom Discount %', 'wc-email-discount' ); ?>
</label>
</fieldset>
</td>
</tr>
<tr class="wc-edm-type-fields" data-type="group">
<th scope="row">
<label for="wc-edm-email-group"><?php esc_html_e( 'Discount Group', 'wc-email-discount' ); ?></label>
</th>
<td>
<select id="wc-edm-email-group" name="group_id">
<option value=""><?php esc_html_e( '— Select a group —', 'wc-email-discount' ); ?></option>
<?php foreach ( $groups as $group ) : ?>
<option value="<?php echo esc_attr( (int) $group->id ); ?>"
<?php selected( $assignment && (int) $assignment->group_id === (int) $group->id ); ?>>
<?php echo esc_html( $group->name . ' (' . wc_format_localized_decimal( $group->discount_percent ) . '%)' ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'Inherits the group\'s discount percent and categories.', 'wc-email-discount' ); ?></p>
</td>
</tr>
<tr class="wc-edm-type-fields" data-type="coupon">
<th scope="row">
<label for="wc-edm-email-coupon"><?php esc_html_e( 'Coupon Code', 'wc-email-discount' ); ?></label>
</th>
<td>
<?php if ( empty( $coupon_codes ) ) : ?>
<input type="text" id="wc-edm-email-coupon" name="coupon_code" class="regular-text" value="">
<p class="description"><?php esc_html_e( 'No WooCommerce coupons found — enter a coupon code manually, or create one under Marketing → Coupons.', 'wc-email-discount' ); ?></p>
<?php else : ?>
<select id="wc-edm-email-coupon" name="coupon_code">
<option value=""><?php esc_html_e( '— Select a coupon —', 'wc-email-discount' ); ?></option>
<?php foreach ( $coupon_codes as $code ) : ?>
<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $current_coupon, $code ); ?>>
<?php echo esc_html( $code ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'Your existing WooCommerce coupons. The selected coupon is auto-applied at checkout.', 'wc-email-discount' ); ?></p>
<?php endif; ?>
</td>
</tr>
<tr class="wc-edm-type-fields" data-type="custom">
<th scope="row">
<label for="wc-edm-email-custom"><?php esc_html_e( 'Custom Discount %', 'wc-email-discount' ); ?></label>
</th>
<td>
<input type="number" id="wc-edm-email-custom" name="custom_discount" min="0" max="100" step="0.01"
value="<?php echo esc_attr( $assignment && null !== $assignment->custom_discount ? $assignment->custom_discount : '' ); ?>"> %
<p class="description"><?php esc_html_e( 'Applied to the entire cart.', 'wc-email-discount' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Free Shipping', 'wc-email-discount' ); ?></th>
<td>
<label>
<input type="checkbox" name="free_shipping" value="1" <?php checked( $free_shipping, 1 ); ?>>
<?php esc_html_e( 'Grant free shipping', 'wc-email-discount' ); ?>
</label>
<p class="description"><?php esc_html_e( 'All shipping methods become free at checkout for matching emails. Works with every assignment type.', 'wc-email-discount' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Status', 'wc-email-discount' ); ?></th>
<td>
<label>
<input type="checkbox" name="is_active" value="1" <?php checked( $is_active, 1 ); ?>>
<?php esc_html_e( 'Active', 'wc-email-discount' ); ?>
</label>
</td>
</tr>
</table>
<p class="submit">
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Assignment', 'wc-email-discount' ); ?></button>
<a class="button" href="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'emails' ) ) ); ?>">
<?php esc_html_e( 'Cancel', 'wc-email-discount' ); ?>
</a>
</p>
</form>
<?php
}
}