| 1 |
<?php |
| 2 |
/** |
| 3 |
* Notice main class. |
| 4 |
* |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
class Merchant_Notice { |
| 12 |
|
| 13 |
/** |
| 14 |
* The notice ID. |
| 15 |
* |
| 16 |
*/ |
| 17 |
public $id = ''; |
| 18 |
|
| 19 |
/** |
| 20 |
* Display conditions. |
| 21 |
* |
| 22 |
*/ |
| 23 |
public $display_conditions = array( |
| 24 |
'woocommerce_page_wc-settings', |
| 25 |
'index.php', |
| 26 |
'plugins.php', |
| 27 |
'edit.php', |
| 28 |
'plugin-install.php', |
| 29 |
); |
| 30 |
|
| 31 |
/** |
| 32 |
* Control if notice is only for free version. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public $only_free = false; |
| 36 |
|
| 37 |
/** |
| 38 |
* End date target. |
| 39 |
* |
| 40 |
*/ |
| 41 |
public $end_date_target = ''; |
| 42 |
|
| 43 |
/** |
| 44 |
* The single class instance. |
| 45 |
* |
| 46 |
*/ |
| 47 |
private static $instance = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* Instance. |
| 51 |
* |
| 52 |
* @return Merchant_Notice |
| 53 |
*/ |
| 54 |
public static function instance() { |
| 55 |
if ( is_null( self::$instance ) ) { |
| 56 |
self::$instance = new self(); |
| 57 |
} |
| 58 |
return self::$instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Constructor |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 68 |
add_action( 'admin_notices', array( $this, 'notice' ), 20 ); |
| 69 |
add_action( 'admin_init', array( $this, 'dimiss_notice' ), 0 ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Enqueue admin scripts |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function admin_enqueue_scripts() { |
| 78 |
wp_enqueue_style( 'merchant-notices', MERCHANT_URI . '/assets/css/admin/notices.min.css', array(), MERCHANT_VERSION, 'all' ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get notice dismiss status. |
| 83 |
* |
| 84 |
*/ |
| 85 |
public function is_notice_dismissed() { |
| 86 |
$user_id = get_current_user_id(); |
| 87 |
|
| 88 |
return get_user_meta( $user_id, $this->id . '_dismiss', true ) ? true : false; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Has end date passed. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function has_end_date_passed() { |
| 97 |
if ( empty( $this->end_date_target ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
$end_date = strtotime( $this->end_date_target ); |
| 102 |
$current_date = time(); |
| 103 |
|
| 104 |
return $current_date > $end_date; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Notice. |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public function notice() { |
| 113 |
$dismissed_notice = $this->is_notice_dismissed(); |
| 114 |
$has_end_date_passed = $this->has_end_date_passed(); |
| 115 |
|
| 116 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if( $this->only_free && defined( 'MERCHANT_PRO_VERSION' ) ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if ( $dismissed_notice || $has_end_date_passed ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
// Display Conditions |
| 129 |
global $hook_suffix; |
| 130 |
|
| 131 |
if( ! in_array( $hook_suffix, $this->display_conditions, true ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 136 |
if( $hook_suffix === 'edit.php' && ! isset( $_GET[ 'post_type' ] ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 141 |
if( $hook_suffix === 'edit.php' && ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] !== 'product' ) ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$this->notice_markup(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Notice HTML markup. |
| 150 |
* |
| 151 |
*/ |
| 152 |
public function notice_markup() {} |
| 153 |
|
| 154 |
/** |
| 155 |
* Dismiss notice permanently |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function dimiss_notice() { |
| 160 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 161 |
if ( isset( $_GET[$this->id . '_dismiss'] ) && '1' === $_GET[$this->id . '_dismiss'] ) { |
| 162 |
add_user_meta( get_current_user_id(), $this->id . '_dismiss', 'true', true ); |
| 163 |
} |
| 164 |
} |
| 165 |
} |