2020-06-16 02:30:41 +00:00
< ? php
/**
* WooCommerce Admin Coupon Page Moved provider .
*
2020-10-23 06:41:45 +00:00
* Adds a notice when the store manager access the coupons page via the old WooCommerce > Coupons menu .
2020-06-16 02:30:41 +00:00
*/
namespace Automattic\WooCommerce\Admin\Notes ;
2021-03-26 17:00:05 +00:00
use Automattic\WooCommerce\Admin\Features\Features ;
2020-06-16 02:30:41 +00:00
use Automattic\WooCommerce\Admin\Features\CouponsMovedTrait ;
use stdClass ;
use WC_Data_Store ;
defined ( 'ABSPATH' ) || exit ;
/**
2020-09-28 04:35:10 +00:00
* Coupon_Page_Moved class .
2020-06-16 02:30:41 +00:00
*/
2020-10-28 17:12:14 +00:00
class CouponPageMoved {
2020-06-16 02:30:41 +00:00
use NoteTraits , CouponsMovedTrait ;
const NOTE_NAME = 'wc-admin-coupon-page-moved' ;
/**
* Initialize our hooks .
*/
public function init () {
if ( ! wc_coupons_enabled () ) {
return ;
}
add_action ( 'admin_init' , [ $this , 'possibly_add_note' ] );
add_action ( 'admin_init' , [ $this , 'redirect_to_coupons' ] );
2021-12-06 15:21:08 +00:00
add_action ( 'woocommerce_admin_newly_installed' , [ $this , 'disable_legacy_menu_for_new_install' ] );
2020-06-16 02:30:41 +00:00
}
/**
* Checks if a note can and should be added .
*
* @ return bool
*/
public static function can_be_added () {
if ( ! wc_coupons_enabled () ) {
return false ;
}
2021-12-06 15:21:08 +00:00
// Don't add the notice if the legacy coupon menu is already disabled.
if ( ! self :: should_display_legacy_menu () ) {
return false ;
}
2020-06-16 02:30:41 +00:00
// Don't add the notice if it's been hidden by the user before.
if ( self :: has_dismissed_note () ) {
return false ;
}
// If we already have a notice, don't add a new one.
if ( self :: has_unactioned_note () ) {
return false ;
}
return isset ( $_GET [ self :: $query_key ] ) && ( bool ) $_GET [ self :: $query_key ]; // phpcs:ignore WordPress.Security.NonceVerification
}
/**
* Get the note object for this class .
*
2020-09-28 04:35:10 +00:00
* @ return Note
2020-06-16 02:30:41 +00:00
*/
public static function get_note () {
2020-09-28 04:35:10 +00:00
$note = new Note ();
2020-06-16 02:30:41 +00:00
$note -> set_title ( __ ( 'Coupon management has moved!' , 'woocommerce-admin' ) );
$note -> set_content ( __ ( 'Coupons can now be managed from Marketing > Coupons. Click the button below to remove the legacy WooCommerce > Coupons menu item.' , 'woocommerce-admin' ) );
2020-09-28 04:35:10 +00:00
$note -> set_type ( Note :: E_WC_ADMIN_NOTE_UPDATE );
2020-06-16 02:30:41 +00:00
$note -> set_name ( self :: NOTE_NAME );
$note -> set_content_data ( new stdClass () );
$note -> set_source ( 'woocommerce-admin' );
$note -> add_action (
'remove-legacy-coupon-menu' ,
__ ( 'Remove legacy coupon menu' , 'woocommerce-admin' ),
wc_admin_url ( '&action=remove-coupon-menu' ),
2020-09-28 04:35:10 +00:00
Note :: E_WC_ADMIN_NOTE_ACTIONED ,
2020-06-16 02:30:41 +00:00
true
);
return $note ;
}
/**
* Find notes that have not been actioned .
*
* @ return bool
*/
protected static function has_unactioned_note () {
2022-02-03 21:27:55 +00:00
$note = Notes :: get_note_by_name ( self :: NOTE_NAME );
if ( ! $note ) {
return false ;
}
2020-06-16 02:30:41 +00:00
2022-02-03 21:27:55 +00:00
return $note -> get_status () === 'unactioned' ;
2020-06-16 02:30:41 +00:00
}
/**
* Whether any notes have been dismissed by the user previously .
*
* @ return bool
*/
protected static function has_dismissed_note () {
2022-02-03 21:27:55 +00:00
$note = Notes :: get_note_by_name ( self :: NOTE_NAME );
if ( ! $note ) {
return false ;
}
2020-06-16 02:30:41 +00:00
2022-02-03 21:27:55 +00:00
return ! $note -> get_is_deleted ();
2020-06-16 02:30:41 +00:00
}
/**
* Get the data store object .
*
* @ return DataStore The data store object .
*/
protected static function get_data_store () {
return WC_Data_Store :: load ( 'admin-note' );
}
/**
* Safe redirect to the coupon page to force page refresh .
*/
public function redirect_to_coupons () {
/* phpcs:disable WordPress.Security.NonceVerification */
if (
! isset ( $_GET [ 'page' ] ) ||
'wc-admin' !== $_GET [ 'page' ] ||
! isset ( $_GET [ 'action' ] ) ||
'remove-coupon-menu' !== $_GET [ 'action' ] ||
! defined ( 'WC_ADMIN_PLUGIN_FILE' )
) {
return ;
}
/* phpcs:enable */
$this -> display_legacy_menu ( false );
wp_safe_redirect ( self :: get_management_url ( 'coupons' ) );
exit ;
}
2021-12-06 15:21:08 +00:00
/**
* Disable legacy coupon menu when installing for the first time .
*/
public function disable_legacy_menu_for_new_install () {
$this -> display_legacy_menu ( false );
}
2020-06-16 02:30:41 +00:00
}