Website : phonehp.ir
backdoor
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Home
Console
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
nehpir
/
public_html
/
wp-content
/
plugins
/
woocommersa
/
includes
/
API
/
Filename :
CouponsController.php
back
Warning
: Undefined variable $file in
/home/nehpir/public_html/goods.php
on line
545
Copy
<?php namespace WooCommersa\API; if (!defined('ABSPATH')) exit; use WooCommersa\Repositories\HistoryRepository; class CouponsController extends BaseController { private $history; public function __construct() { $this->history = new HistoryRepository(); add_action('rest_api_init', [$this, 'register_routes']); } public function register_routes() { register_rest_route($this->namespace, '/coupons', [ 'methods' => 'GET', 'callback' => [$this, 'get_coupons'], 'permission_callback' => [$this, 'permissions_check'], ]); register_rest_route($this->namespace, '/coupons/generate', [ 'methods' => 'POST', 'callback' => [$this, 'generate_coupons'], 'permission_callback' => [$this, 'permissions_check'], ]); register_rest_route($this->namespace, '/coupons/generate-first-purchase', [ 'methods' => 'POST', 'callback' => [$this, 'generate_first_purchase'], 'permission_callback' => [$this, 'permissions_check'], ]); register_rest_route($this->namespace, '/coupons/update', [ 'methods' => 'POST', 'callback' => [$this, 'update_coupon'], 'permission_callback' => [$this, 'permissions_check'], ]); register_rest_route($this->namespace, '/coupons/delete', [ 'methods' => 'POST', 'callback' => [$this, 'delete_coupons'], 'permission_callback' => [$this, 'permissions_check'], ]); register_rest_route($this->namespace, '/coupons/bulk-update', [ 'methods' => 'POST', 'callback' => [$this, 'batch_update'], 'permission_callback' => [$this, 'permissions_check'], ]); } public function get_coupons($request) { $paged = $request['page'] ?? 1; $per_page = $request['per_page'] ?? 20; $args = [ 'post_type' => 'shop_coupon', 'posts_per_page' => $per_page, 'paged' => $paged, 'post_status' => 'publish', ]; if (!empty($request['search'])) { $args['s'] = $request['search']; } if (!empty($request['special_type']) && $request['special_type'] === 'first_purchase') { $args['meta_query'] = $args['meta_query'] ?? []; $args['meta_query'][] = [ 'key' => '_woocommersa_first_purchase', 'value' => '1', ]; } if (!empty($request['type'])) { $args['meta_query'] = $args['meta_query'] ?? []; $args['meta_query'][] = [ 'key' => 'discount_type', 'value' => $request['type'], ]; } $query = new \WP_Query($args); $data = []; foreach ($query->posts as $post) { $coupon = new \WC_Coupon($post->ID); $data[] = [ 'id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'amount' => $coupon->get_amount(), 'usage_limit' => $coupon->get_usage_limit(), 'usage_count' => $coupon->get_usage_count(), 'expiry_date' => $coupon->get_date_expires() ? $coupon->get_date_expires()->date('Y-m-d') : '', 'minimum_amount' => $coupon->get_minimum_amount(), 'maximum_amount' => $coupon->get_maximum_amount(), 'description' => $coupon->get_description(), 'first_purchase' => $coupon->get_meta('_woocommersa_first_purchase') === '1', ]; } return new \WP_REST_Response([ 'coupons' => $data, 'total' => (int)$query->found_posts, 'pages' => (int)$query->max_num_pages, ], 200); } public function update_coupon($request) { $id = $request['id']; $field = $request['field']; $value = $request['value']; $coupon = new \WC_Coupon($id); if (!$coupon->get_id()) { return new \WP_Error('not_found', 'کوپن پیدا نشد', ['status' => 404]); } $old_value = ''; switch($field) { case 'code': $old_value = $coupon->get_code(); break; case 'amount': $old_value = $coupon->get_amount(); break; case 'type': $old_value = $coupon->get_discount_type(); break; case 'expiry_date': $old_value = $coupon->get_date_expires() ? $coupon->get_date_expires()->date('Y-m-d') : ''; break; case 'usage_limit': $old_value = $coupon->get_usage_limit(); break; case 'minimum_amount': $old_value = $coupon->get_minimum_amount(); break; case 'description': $old_value = $coupon->get_description(); break; } switch($field) { case 'code': $coupon->set_code($value); break; case 'amount': $coupon->set_amount($value); break; case 'type': $coupon->set_discount_type($value); break; case 'expiry_date': $coupon->set_date_expires($value); break; case 'usage_limit': $coupon->set_usage_limit($value); break; case 'minimum_amount': $coupon->set_minimum_amount($value); break; case 'description': $coupon->set_description($value); break; } $coupon->save(); $this->history->record(get_current_user_id(), $id, 'coupon_' . $field, $old_value, $value, '', 'manual'); return new \WP_REST_Response(['success' => true], 200); } public function delete_coupons($request) { $ids = $request['ids']; if (!is_array($ids)) return new \WP_Error('invalid', 'آیدیها نامعتبر هستند', ['status' => 400]); foreach ($ids as $id) { $coupon = new \WC_Coupon($id); $code = $coupon->get_code(); wp_delete_post($id, true); $this->history->record(get_current_user_id(), $id, 'coupon_deleted', $code, '', '', 'manual'); } return new \WP_REST_Response(['success' => true], 200); } public function generate_coupons($request) { $options = $request->get_json_params(); $count = (int)($options['count'] ?? 0); if ($count <= 0) return new \WP_Error('invalid_count', 'تعداد کوپن باید بیشتر از صفر باشد', ['status' => 400]); if ($count > 100) return new \WP_Error('too_many', 'حداکثر ۱۰۰ کوپن در هر بار مجاز است', ['status' => 400]); $prefix = sanitize_text_field($options['prefix'] ?? 'SALE-'); $type = sanitize_text_field($options['type'] ?? 'percent'); $amount = (float)($options['amount'] ?? 0); if ($amount <= 0) return new \WP_Error('invalid_amount', 'مقدار تخفیف باید بیشتر از صفر باشد', ['status' => 400]); $usage_limit = isset($options['usage_limit']) ? (int)$options['usage_limit'] : null; $expiry_date = sanitize_text_field($options['expiry_date'] ?? ''); $validity_days = (int)($options['validity_days'] ?? 0); if ($validity_days > 0) { $expiry_date = date('Y-m-d', strtotime("+$validity_days days")); } $results = []; for ($i = 0; $i < $count; $i++) { $code = $prefix . strtoupper(wp_generate_password(6, false)); $coupon = new \WC_Coupon(); $coupon->set_code($code); $coupon->set_discount_type($type); $coupon->set_amount($amount); $coupon->set_description('Auto-generated by WooCommersa'); if ($usage_limit !== null) $coupon->set_usage_limit($usage_limit); if (!empty($expiry_date)) $coupon->set_date_expires($expiry_date); $coupon->save(); $this->history->record(get_current_user_id(), $coupon->get_id(), 'coupon_generated', '', $code, '', 'manual'); $results[] = [ 'id' => $coupon->get_id(), 'code' => $code, 'type' => $type, 'amount' => $amount ]; } return new \WP_REST_Response(['success' => true, 'coupons' => $results], 200); } public function generate_first_purchase($request) { $options = $request->get_json_params(); $count = (int)($options['count'] ?? 0); if ($count <= 0) return new \WP_Error('invalid_count', 'تعداد کوپن باید بیشتر از صفر باشد', ['status' => 400]); if ($count > 50) return new \WP_Error('too_many', 'حداکثر ۵۰ کوپن خرید اول مجاز است', ['status' => 400]); $prefix = sanitize_text_field($options['prefix'] ?? 'WELCOME-'); $type = sanitize_text_field($options['type'] ?? 'percent'); $amount = (float)($options['amount'] ?? 0); if ($amount <= 0) return new \WP_Error('invalid_amount', 'مقدار تخفیف باید بیشتر از صفر باشد', ['status' => 400]); $results = []; for ($i = 0; $i < $count; $i++) { $code = $prefix . strtoupper(wp_generate_password(6, false)); $coupon = new \WC_Coupon(); $coupon->set_code($code); $coupon->set_discount_type($type); $coupon->set_amount($amount); $coupon->set_description('First Purchase Discount - Controlled by WooCommersa'); $coupon->set_usage_limit(0); $coupon->set_usage_limit_per_user(1); $coupon->update_meta_data('_woocommersa_first_purchase', '1'); $coupon->save(); $this->history->record(get_current_user_id(), $coupon->get_id(), 'coupon_generated', '', $code, '', 'manual'); $results[] = [ 'id' => $coupon->get_id(), 'code' => $code, 'type' => $type, 'amount' => $amount, 'first_purchase' => true ]; } return new \WP_REST_Response([ 'success' => true, 'coupons' => $results ], 200); } public function batch_update($request) { $params = $request->get_json_params(); $ids = $params['ids'] ?? []; $updates = $params['updates'] ?? []; $updated = 0; foreach ($ids as $id) { $coupon = new \WC_Coupon($id); if ($coupon->get_id()) { if (isset($updates['type'])) $coupon->set_discount_type($updates['type']); if (isset($updates['amount'])) { $val = $updates['amount']; if (strpos($val, '+') === 0) { $coupon->set_amount($coupon->get_amount() + (float)substr($val, 1)); } elseif (strpos($val, '-') === 0) { $coupon->set_amount(max(0, $coupon->get_amount() - (float)substr($val, 1))); } else { $coupon->set_amount((float)$val); } } if (isset($updates['expiry_date'])) $coupon->set_date_expires($updates['expiry_date']); if (isset($updates['usage_limit'])) $coupon->set_usage_limit((int)$updates['usage_limit']); if (isset($updates['minimum_amount'])) $coupon->set_minimum_amount($updates['minimum_amount']); if (isset($updates['maximum_amount'])) $coupon->set_maximum_amount($updates['maximum_amount']); $coupon->save(); $this->history->record(get_current_user_id(), $id, 'coupon_bulk_update', '', json_encode($updates), '', 'manual'); $updated++; } } return new \WP_REST_Response(['success' => true, 'updated' => $updated], 200); } }