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