Agentic
4 months ago
BlockTemplates
4 weeks ago
EmailImprovements
1 year ago
EmailPreview
4 weeks ago
Emails
1 year ago
ImportExport
1 year ago
Logging
1 year ago
Marketing
2 years ago
Notes
4 weeks ago
Onboarding
5 months ago
Orders
4 weeks ago
ProductForm
2 years ago
ProductReviews
4 weeks ago
RemoteFreeExtensions
4 weeks ago
Schedulers
4 weeks ago
Settings
2 weeks ago
Suggestions
4 weeks ago
WCPayPromotion
10 months ago
ActivityPanels.php
3 years ago
Analytics.php
4 weeks ago
CategoryLookup.php
4 years ago
Coupons.php
1 year ago
CouponsMovedTrait.php
5 months ago
CustomerEffortScoreTracks.php
7 months ago
Events.php
2 months ago
FeaturePlugin.php
4 months ago
Homescreen.php
1 month ago
Loader.php
4 weeks ago
Marketing.php
1 year ago
Marketplace.php
5 months ago
MobileAppBanner.php
4 years ago
OrderMilestoneEasterEgg.php
4 weeks ago
RemoteInboxNotifications.php
3 years ago
Settings.php
2 weeks ago
ShippingLabelBanner.php
1 year ago
ShippingLabelBannerDisplayRules.php
1 year ago
SiteHealth.php
3 years ago
Survey.php
4 years ago
SystemStatusReport.php
1 year ago
Translations.php
1 year ago
WCAdminAssets.php
2 weeks ago
WCAdminSharedSettings.php
1 year ago
WCAdminUser.php
9 months ago
WcPayWelcomePage.php
1 year ago
Coupons.php
121 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Marketing > Coupons. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin; |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\Features\Features; |
| 9 | use Automattic\WooCommerce\Admin\PageController; |
| 10 | |
| 11 | /** |
| 12 | * Contains backend logic for the Coupons feature. |
| 13 | */ |
| 14 | class Coupons { |
| 15 | |
| 16 | use CouponsMovedTrait; |
| 17 | |
| 18 | /** |
| 19 | * Class instance. |
| 20 | * |
| 21 | * @var Coupons instance |
| 22 | */ |
| 23 | protected static $instance = null; |
| 24 | |
| 25 | /** |
| 26 | * Get class instance. |
| 27 | */ |
| 28 | public static function get_instance() { |
| 29 | if ( ! self::$instance ) { |
| 30 | self::$instance = new self(); |
| 31 | } |
| 32 | return self::$instance; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Hook into WooCommerce. |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | if ( ! is_admin() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // If the main marketing feature is disabled, don't modify coupon behavior. |
| 44 | if ( ! Features::is_enabled( 'marketing' ) ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | // Only support coupon modifications if coupons are enabled. |
| 49 | if ( ! wc_coupons_enabled() ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | add_action( 'admin_enqueue_scripts', array( $this, 'maybe_add_marketing_coupon_script' ) ); |
| 54 | add_action( 'woocommerce_register_post_type_shop_coupon', array( $this, 'move_coupons' ) ); |
| 55 | add_action( 'admin_head', array( $this, 'fix_coupon_menu_highlight' ), 99 ); |
| 56 | add_action( 'admin_menu', array( $this, 'maybe_add_coupon_menu_redirect' ) ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Maybe add menu item back in original spot to help people transition |
| 61 | */ |
| 62 | public function maybe_add_coupon_menu_redirect() { |
| 63 | if ( ! $this->should_display_legacy_menu() ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | add_submenu_page( |
| 68 | 'woocommerce', |
| 69 | __( 'Coupons', 'woocommerce' ), |
| 70 | __( 'Coupons', 'woocommerce' ), |
| 71 | 'manage_options', |
| 72 | 'coupons-moved', |
| 73 | array( $this, 'coupon_menu_moved' ) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Call back for transition menu item |
| 79 | */ |
| 80 | public function coupon_menu_moved() { |
| 81 | wp_safe_redirect( $this->get_legacy_coupon_url(), 301 ); |
| 82 | exit(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Modify registered post type shop_coupon |
| 87 | * |
| 88 | * @param array $args Array of post type parameters. |
| 89 | * |
| 90 | * @return array the filtered parameters. |
| 91 | */ |
| 92 | public function move_coupons( $args ) { |
| 93 | $args['show_in_menu'] = current_user_can( 'manage_woocommerce' ) ? 'woocommerce-marketing' : true; |
| 94 | return $args; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Undo WC modifications to $parent_file for 'shop_coupon' |
| 99 | */ |
| 100 | public function fix_coupon_menu_highlight() { |
| 101 | global $parent_file, $post_type; |
| 102 | |
| 103 | if ( $post_type === 'shop_coupon' ) { |
| 104 | $parent_file = 'woocommerce-marketing'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Maybe add our wc-admin coupon scripts if viewing coupon pages |
| 110 | */ |
| 111 | public function maybe_add_marketing_coupon_script() { |
| 112 | $curent_screen = PageController::get_instance()->get_current_page(); |
| 113 | if ( ! isset( $curent_screen['id'] ) || $curent_screen['id'] !== 'woocommerce-coupons' ) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | WCAdminAssets::register_style( 'marketing-coupons', 'style' ); |
| 118 | WCAdminAssets::register_script( 'wp-admin-scripts', 'marketing-coupons', true ); |
| 119 | } |
| 120 | } |
| 121 |