mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Integrations
/
WooCommerce
/
class-coupon-code-generator.php
Renderer
3 months ago
class-coupon-code-generator.php
3 months ago
class-initializer.php
3 months ago
index.php
9 months ago
class-coupon-code-generator.php
125 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; |
| 6 | use Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Renderer\Blocks\Coupon_Code; |
| 7 | class Coupon_Code_Generator { |
| 8 | const MAX_CODE_RETRIES = 5; |
| 9 | public function init(): void { |
| 10 | add_filter( 'woocommerce_coupon_code_block_auto_generate', array( $this, 'generate_coupon' ), 10, 3 ); |
| 11 | } |
| 12 | public function generate_coupon( string $coupon_code, array $attrs, Rendering_Context $rendering_context ): string { |
| 13 | if ( ! empty( $coupon_code ) ) { |
| 14 | return $coupon_code; |
| 15 | } |
| 16 | if ( $rendering_context->get( 'is_user_preview' ) ) { |
| 17 | return Coupon_Code::COUPON_CODE_PLACEHOLDER; |
| 18 | } |
| 19 | if ( ! function_exists( 'wc_get_coupon_types' ) || ! class_exists( 'WC_Coupon' ) ) { |
| 20 | return ''; |
| 21 | } |
| 22 | try { |
| 23 | $coupon = new \WC_Coupon(); |
| 24 | $coupon->set_code( $this->generate_unique_code() ); |
| 25 | $discount_type = $this->validate_discount_type( $attrs['discountType'] ?? 'percent' ); |
| 26 | $coupon->set_discount_type( $discount_type ); |
| 27 | if ( isset( $attrs['amount'] ) ) { |
| 28 | $coupon->set_amount( (float) $attrs['amount'] ); |
| 29 | } |
| 30 | if ( ! empty( $attrs['expiryDay'] ) ) { |
| 31 | $expiration = time() + ( (int) $attrs['expiryDay'] * DAY_IN_SECONDS ); |
| 32 | $coupon->set_date_expires( $expiration ); |
| 33 | } |
| 34 | $coupon->set_free_shipping( ! empty( $attrs['freeShipping'] ) ); |
| 35 | $coupon->set_minimum_amount( (float) ( $attrs['minimumAmount'] ?? 0 ) ); |
| 36 | $coupon->set_maximum_amount( (float) ( $attrs['maximumAmount'] ?? 0 ) ); |
| 37 | $coupon->set_individual_use( ! empty( $attrs['individualUse'] ) ); |
| 38 | $coupon->set_exclude_sale_items( ! empty( $attrs['excludeSaleItems'] ) ); |
| 39 | $coupon->set_product_ids( $this->extract_ids( $attrs['productIds'] ?? array() ) ); |
| 40 | $coupon->set_excluded_product_ids( $this->extract_ids( $attrs['excludedProductIds'] ?? array() ) ); |
| 41 | $coupon->set_product_categories( $this->extract_ids( $attrs['productCategoryIds'] ?? array() ) ); |
| 42 | $coupon->set_excluded_product_categories( $this->extract_ids( $attrs['excludedProductCategoryIds'] ?? array() ) ); |
| 43 | $email_restrictions = $this->parse_email_restrictions( $attrs['emailRestrictions'] ?? '' ); |
| 44 | $recipient = $rendering_context->get_recipient_email(); |
| 45 | if ( $recipient && is_email( $recipient ) ) { |
| 46 | $email_restrictions[] = $recipient; |
| 47 | } |
| 48 | $coupon->set_email_restrictions( array_unique( $email_restrictions ) ); |
| 49 | $usage_limit = $attrs['usageLimit'] ?? 0; |
| 50 | $usage_limit_per_user = $attrs['usageLimitPerUser'] ?? 0; |
| 51 | $coupon->set_usage_limit( is_numeric( $usage_limit ) ? (int) $usage_limit : 0 ); |
| 52 | $coupon->set_usage_limit_per_user( is_numeric( $usage_limit_per_user ) ? (int) $usage_limit_per_user : 0 ); |
| 53 | $coupon->set_description( |
| 54 | __( 'Auto-generated coupon by WooCommerce Email Editor', 'woocommerce' ) |
| 55 | ); |
| 56 | $coupon->save(); |
| 57 | return $coupon->get_code(); |
| 58 | } catch ( \Exception $e ) { |
| 59 | wc_get_logger()->error( |
| 60 | 'Coupon auto-generation failed: ' . $e->getMessage(), |
| 61 | array( 'source' => 'email-editor-coupon-generator' ) |
| 62 | ); |
| 63 | return ''; |
| 64 | } |
| 65 | } |
| 66 | private function parse_email_restrictions( $raw ): array { |
| 67 | if ( ! is_string( $raw ) || '' === $raw ) { |
| 68 | return array(); |
| 69 | } |
| 70 | $emails = array_map( 'trim', explode( ',', $raw ) ); |
| 71 | return array_values( |
| 72 | array_filter( |
| 73 | $emails, |
| 74 | function ( string $email ): bool { |
| 75 | return (bool) is_email( $email ); |
| 76 | } |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | private function validate_discount_type( string $type ): string { |
| 81 | $valid_types = array_keys( wc_get_coupon_types() ); |
| 82 | return in_array( $type, $valid_types, true ) ? $type : 'percent'; |
| 83 | } |
| 84 | private function generate_unique_code(): string { |
| 85 | for ( $i = 0; $i < self::MAX_CODE_RETRIES; $i++ ) { |
| 86 | $code = $this->generate_random_code(); |
| 87 | $existing = wc_get_coupon_id_by_code( $code ); |
| 88 | if ( ! $existing ) { |
| 89 | return $code; |
| 90 | } |
| 91 | } |
| 92 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message, not rendered output. |
| 93 | throw new \RuntimeException( 'Failed to generate a unique coupon code.' ); |
| 94 | } |
| 95 | private function generate_random_code(): string { |
| 96 | $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
| 97 | $length = strlen( $characters ) - 1; |
| 98 | $segment1 = ''; |
| 99 | $segment2 = ''; |
| 100 | $segment3 = ''; |
| 101 | for ( $i = 0; $i < 4; $i++ ) { |
| 102 | $segment1 .= $characters[ random_int( 0, $length ) ]; |
| 103 | } |
| 104 | for ( $i = 0; $i < 6; $i++ ) { |
| 105 | $segment2 .= $characters[ random_int( 0, $length ) ]; |
| 106 | } |
| 107 | for ( $i = 0; $i < 4; $i++ ) { |
| 108 | $segment3 .= $characters[ random_int( 0, $length ) ]; |
| 109 | } |
| 110 | return $segment1 . '-' . $segment2 . '-' . $segment3; |
| 111 | } |
| 112 | private function extract_ids( array $items ): array { |
| 113 | return array_map( |
| 114 | function ( $item ): int { |
| 115 | if ( ! is_array( $item ) ) { |
| 116 | return 0; |
| 117 | } |
| 118 | $id = $item['id'] ?? 0; |
| 119 | return is_numeric( $id ) ? (int) $id : 0; |
| 120 | }, |
| 121 | $items |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 |