241 lines
8.8 KiB
PHP
241 lines
8.8 KiB
PHP
<?php
|
|||
|
|
/**
|
||
|
|
* Discount groups admin (CRUD).
|
||
|
|
*
|
||
|
|
* @package WC_Email_Discount_Manager
|
||
|
|
*/
|
||
|
|
|
||
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* List, create, edit and delete discount groups.
|
||
|
|
*/
|
||
|
|
class WC_EDM_Admin_Groups {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle save/delete actions for the groups tab. Runs on admin_init.
|
||
|
|
*/
|
||
|
|
public function handle_actions() {
|
||
|
|
// Save (create or update).
|
||
|
|
if ( isset( $_POST['wc_edm_action'] ) && 'save_group' === $_POST['wc_edm_action'] ) {
|
||
|
|
check_admin_referer( 'wc_edm_save_group' );
|
||
|
|
|
||
|
|
$id = isset( $_POST['group_id'] ) ? absint( $_POST['group_id'] ) : 0;
|
||
|
|
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
|
||
|
|
$percent = isset( $_POST['discount_percent'] ) ? WC_EDM_Helpers::sanitize_percent( $_POST['discount_percent'] ) : 0;
|
||
|
|
|
||
|
|
$category_ids = isset( $_POST['category_ids'] ) && is_array( $_POST['category_ids'] )
|
||
|
|
? array_map( 'absint', wp_unslash( $_POST['category_ids'] ) )
|
||
|
|
: array();
|
||
|
|
|
||
|
|
if ( '' === $name ) {
|
||
|
|
wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', 'message' => 'error' ) ) );
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = array(
|
||
|
|
'name' => $name,
|
||
|
|
'discount_percent' => $percent,
|
||
|
|
);
|
||
|
|
|
||
|
|
if ( $id ) {
|
||
|
|
WC_EDM_DB::update_group( $id, $data );
|
||
|
|
} else {
|
||
|
|
$id = WC_EDM_DB::insert_group( $data );
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $id ) {
|
||
|
|
WC_EDM_DB::set_group_categories( $id, $category_ids );
|
||
|
|
}
|
||
|
|
|
||
|
|
wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', '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_group_' . $id );
|
||
|
|
|
||
|
|
WC_EDM_DB::delete_group( $id );
|
||
|
|
|
||
|
|
wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', 'message' => 'deleted' ) ) );
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Render the groups 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 groups.
|
||
|
|
*/
|
||
|
|
private function render_list() {
|
||
|
|
$groups = WC_EDM_DB::get_groups();
|
||
|
|
?>
|
||
|
|
<div class="wc-edm-header-row">
|
||
|
|
<h2><?php esc_html_e( 'Discount Groups', 'wc-email-discount' ); ?></h2>
|
||
|
|
<a class="button button-primary" href="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', 'action' => 'new' ) ) ); ?>">
|
||
|
|
<?php esc_html_e( 'Add New Group', 'wc-email-discount' ); ?>
|
||
|
|
</a>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<?php if ( empty( $groups ) ) : ?>
|
||
|
|
<p><?php esc_html_e( 'No discount groups found.', 'wc-email-discount' ); ?></p>
|
||
|
|
<?php return; ?>
|
||
|
|
<?php endif; ?>
|
||
|
|
|
||
|
|
<table class="widefat striped wc-edm-table">
|
||
|
|
<thead>
|
||
|
|
<tr>
|
||
|
|
<th><?php esc_html_e( 'Name', 'wc-email-discount' ); ?></th>
|
||
|
|
<th><?php esc_html_e( 'Discount', 'wc-email-discount' ); ?></th>
|
||
|
|
<th><?php esc_html_e( 'Categories', 'wc-email-discount' ); ?></th>
|
||
|
|
<th class="wc-edm-actions-col"><?php esc_html_e( 'Actions', 'wc-email-discount' ); ?></th>
|
||
|
|
</tr>
|
||
|
|
</thead>
|
||
|
|
<tbody>
|
||
|
|
<?php foreach ( $groups as $group ) : ?>
|
||
|
|
<?php
|
||
|
|
$category_ids = WC_EDM_DB::get_group_category_ids( (int) $group->id );
|
||
|
|
$names = array();
|
||
|
|
|
||
|
|
foreach ( $category_ids as $category_id ) {
|
||
|
|
$term = get_term( $category_id, 'product_cat' );
|
||
|
|
if ( $term && ! is_wp_error( $term ) ) {
|
||
|
|
$names[] = $term->name;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$edit_url = WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', 'action' => 'edit', 'id' => (int) $group->id ) );
|
||
|
|
|
||
|
|
$delete_url = wp_nonce_url(
|
||
|
|
WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', 'action' => 'delete', 'id' => (int) $group->id ) ),
|
||
|
|
'wc_edm_delete_group_' . (int) $group->id
|
||
|
|
);
|
||
|
|
?>
|
||
|
|
<tr>
|
||
|
|
<td><a href="<?php echo esc_url( $edit_url ); ?>"><strong><?php echo esc_html( $group->name ); ?></strong></a></td>
|
||
|
|
<td><span class="wc-edm-badge wc-edm-badge-percent"><?php echo esc_html( wc_format_localized_decimal( $group->discount_percent ) ); ?>%</span></td>
|
||
|
|
<td><?php echo $names ? esc_html( implode( ', ', $names ) ) : '<em>' . esc_html__( 'None', 'wc-email-discount' ) . '</em>'; ?></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 Group ID (0 for new).
|
||
|
|
*/
|
||
|
|
private function render_form( $id ) {
|
||
|
|
$group = $id ? WC_EDM_DB::get_group( $id ) : null;
|
||
|
|
$selected_ids = $id ? WC_EDM_DB::get_group_category_ids( $id ) : array();
|
||
|
|
$categories = WC_EDM_Helpers::get_product_categories();
|
||
|
|
|
||
|
|
$categories_by_id = array();
|
||
|
|
foreach ( $categories as $category ) {
|
||
|
|
$categories_by_id[ $category->term_id ] = $category;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $id && ! $group ) {
|
||
|
|
echo '<p>' . esc_html__( 'Group not found.', 'wc-email-discount' ) . '</p>';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
?>
|
||
|
|
<h2><?php echo $group ? esc_html__( 'Edit Discount Group', 'wc-email-discount' ) : esc_html__( 'Add Discount Group', 'wc-email-discount' ); ?></h2>
|
||
|
|
|
||
|
|
<form method="post" action="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups' ) ) ); ?>">
|
||
|
|
<?php wp_nonce_field( 'wc_edm_save_group' ); ?>
|
||
|
|
<input type="hidden" name="wc_edm_action" value="save_group">
|
||
|
|
<input type="hidden" name="group_id" value="<?php echo esc_attr( $group ? (int) $group->id : 0 ); ?>">
|
||
|
|
|
||
|
|
<table class="form-table" role="presentation">
|
||
|
|
<tr>
|
||
|
|
<th scope="row">
|
||
|
|
<label for="wc-edm-group-name"><?php esc_html_e( 'Group Name', 'wc-email-discount' ); ?></label>
|
||
|
|
</th>
|
||
|
|
<td>
|
||
|
|
<input type="text" id="wc-edm-group-name" name="name" class="regular-text" required
|
||
|
|
value="<?php echo esc_attr( $group ? $group->name : '' ); ?>">
|
||
|
|
</td>
|
||
|
|
</tr>
|
||
|
|
<tr>
|
||
|
|
<th scope="row">
|
||
|
|
<label for="wc-edm-group-percent"><?php esc_html_e( 'Discount %', 'wc-email-discount' ); ?></label>
|
||
|
|
</th>
|
||
|
|
<td>
|
||
|
|
<input type="number" id="wc-edm-group-percent" name="discount_percent" min="0" max="100" step="0.01" required
|
||
|
|
value="<?php echo esc_attr( $group ? $group->discount_percent : '' ); ?>"> %
|
||
|
|
<p class="description"><?php esc_html_e( 'Applies to all products in the categories below.', 'wc-email-discount' ); ?></p>
|
||
|
|
</td>
|
||
|
|
</tr>
|
||
|
|
<tr>
|
||
|
|
<th scope="row"><?php esc_html_e( 'Product Categories', 'wc-email-discount' ); ?></th>
|
||
|
|
<td>
|
||
|
|
<div class="wc-edm-category-picker">
|
||
|
|
<select id="wc-edm-category-select">
|
||
|
|
<option value=""><?php esc_html_e( '— Select a category —', 'wc-email-discount' ); ?></option>
|
||
|
|
<?php foreach ( $categories as $category ) : ?>
|
||
|
|
<option value="<?php echo esc_attr( $category->term_id ); ?>"
|
||
|
|
<?php disabled( in_array( $category->term_id, $selected_ids, true ) ); ?>>
|
||
|
|
<?php echo esc_html( $category->name ); ?>
|
||
|
|
</option>
|
||
|
|
<?php endforeach; ?>
|
||
|
|
</select>
|
||
|
|
<button type="button" class="button" id="wc-edm-add-category">
|
||
|
|
<?php esc_html_e( 'Add Category', 'wc-email-discount' ); ?>
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<ul id="wc-edm-selected-categories" class="wc-edm-sortable">
|
||
|
|
<?php foreach ( $selected_ids as $category_id ) : ?>
|
||
|
|
<?php
|
||
|
|
if ( ! isset( $categories_by_id[ $category_id ] ) ) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
?>
|
||
|
|
<li data-id="<?php echo esc_attr( $category_id ); ?>">
|
||
|
|
<span class="dashicons dashicons-menu wc-edm-drag-handle"></span>
|
||
|
|
<span class="wc-edm-item-label"><?php echo esc_html( $categories_by_id[ $category_id ]->name ); ?></span>
|
||
|
|
<button type="button" class="wc-edm-remove-category" aria-label="<?php esc_attr_e( 'Remove', 'wc-email-discount' ); ?>">×</button>
|
||
|
|
<input type="hidden" name="category_ids[]" value="<?php echo esc_attr( $category_id ); ?>">
|
||
|
|
</li>
|
||
|
|
<?php endforeach; ?>
|
||
|
|
</ul>
|
||
|
|
<p class="description"><?php esc_html_e( 'Drag to reorder. The discount applies to all listed categories.', 'wc-email-discount' ); ?></p>
|
||
|
|
</td>
|
||
|
|
</tr>
|
||
|
|
</table>
|
||
|
|
|
||
|
|
<p class="submit">
|
||
|
|
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Group', 'wc-email-discount' ); ?></button>
|
||
|
|
<a class="button" href="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups' ) ) ); ?>">
|
||
|
|
<?php esc_html_e( 'Cancel', 'wc-email-discount' ); ?>
|
||
|
|
</a>
|
||
|
|
</p>
|
||
|
|
</form>
|
||
|
|
<?php
|
||
|
|
}
|
||
|
|
}
|