| 1 |
<?php |
| 2 |
namespace WPIDE\App\Classes; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use const WPIDE\Constants\AUTHOR_URL; |
| 6 |
use const WPIDE\Constants\NAME; |
| 7 |
use const WPIDE\Constants\SLUG; |
| 8 |
use const WPIDE\Constants\VERSION; |
| 9 |
use const WPIDE\Constants\ASSETS_URL; |
| 10 |
|
| 11 |
use DateTime; |
| 12 |
use DateInterval; |
| 13 |
use WPIDE\App\App; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class that takes care of adding promo notice if available based on current date |
| 17 |
*/ |
| 18 |
|
| 19 |
class PromoNotice { |
| 20 |
|
| 21 |
/** |
| 22 |
* Dismissed option key |
| 23 |
*/ |
| 24 |
public static $dismissed_option = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* Today date |
| 28 |
*/ |
| 29 |
public static $today = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* @throws Exception |
| 33 |
*/ |
| 34 |
public static function init() { |
| 35 |
|
| 36 |
if(!empty(self::$today)) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
self::$dismissed_option = App::instance()->prefix('promos_dismissed'); |
| 41 |
self::$today = new DateTime(); |
| 42 |
|
| 43 |
// Add Ajax Events |
| 44 |
add_action("wp_ajax_".SLUG."_promo_dismiss_action", [__CLASS__, 'ajaxPromoDismissAction']); |
| 45 |
add_action('admin_enqueue_scripts', [__CLASS__, 'enqueueAdminAssets'], 1); |
| 46 |
|
| 47 |
if(!self::enabled()) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// Init Promo Notice |
| 52 |
add_action( 'admin_notices', [ __CLASS__, 'addPromoNotice' ] ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @throws Exception |
| 58 |
*/ |
| 59 |
public static function enabled(): bool |
| 60 |
{ |
| 61 |
|
| 62 |
if(!Freemius::showSubmenus()) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
try { |
| 67 |
$promo = self::getActivePromo(); |
| 68 |
return $promo && !self::isPromoDismissed($promo->id); |
| 69 |
}catch (Exception $error) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @throws Exception |
| 76 |
*/ |
| 77 |
public static function getPromos(): array |
| 78 |
{ |
| 79 |
|
| 80 |
$year = self::$today->format('Y'); |
| 81 |
|
| 82 |
return array_map(function ($promo) { |
| 83 |
|
| 84 |
$promo['id'] = md5($promo['name'] . $promo['start'] . $promo['end']); |
| 85 |
$promo['start'] = new DateTime($promo['start']); |
| 86 |
//end date end of day |
| 87 |
$promo['end'] = (new DateTime($promo['end']))->add(new DateInterval('P1D'))->sub(new DateInterval('PT1S')); |
| 88 |
|
| 89 |
return (object)$promo; |
| 90 |
}, [ |
| 91 |
[ |
| 92 |
"name" => "Valentine's Day Sale", |
| 93 |
"discount" => "15%", |
| 94 |
"coupon" => "VALENTINE", |
| 95 |
"start" => "14-02-" . $year, |
| 96 |
"end" => "14-02-" . $year |
| 97 |
], |
| 98 |
[ |
| 99 |
"name" => "Mother’s Day Sale", |
| 100 |
"discount" => "15%", |
| 101 |
"coupon" => "MOTHERS", |
| 102 |
"start" => "second Sunday of May " . $year, |
| 103 |
"end" => "second Sunday of May " . $year |
| 104 |
], |
| 105 |
[ |
| 106 |
"name" => "Father’s Day Sale", |
| 107 |
"discount" => "15%", |
| 108 |
"coupon" => "FATHERS", |
| 109 |
"start" => "third Sunday of June " . $year, |
| 110 |
"end" => "third Sunday of June " . $year |
| 111 |
], |
| 112 |
[ |
| 113 |
"name" => "Halloween Sale", |
| 114 |
"discount" => "15%", |
| 115 |
"coupon" => "HALLOWEEN", |
| 116 |
"start" => "28-10-" . $year, |
| 117 |
"end" => "31-10-" . $year |
| 118 |
], |
| 119 |
[ |
| 120 |
"name" => "Black Friday + Cyber Monday Sale", |
| 121 |
"discount" => "40%", |
| 122 |
"coupon" => "BFCM", |
| 123 |
"start" => "24-11-" . $year, |
| 124 |
"end" => "01-12-" . $year |
| 125 |
], |
| 126 |
[ |
| 127 |
"name" => "Christmas Sale", |
| 128 |
"discount" => "25%", |
| 129 |
"coupon" => "XMAS", |
| 130 |
"start" => "24-12-" . $year, |
| 131 |
"end" => "27-12-" . $year |
| 132 |
] |
| 133 |
]); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @throws Exception |
| 138 |
*/ |
| 139 |
public static function getPromoById($id): ?object |
| 140 |
{ |
| 141 |
|
| 142 |
$promos = self::getPromos(); |
| 143 |
|
| 144 |
$results = array_filter($promos, function($promo) use($id) { |
| 145 |
return $promo->id === $id; |
| 146 |
}); |
| 147 |
|
| 148 |
return !empty($results) ? array_shift($results) : null; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @throws Exception |
| 153 |
*/ |
| 154 |
public static function promoExists($id): bool |
| 155 |
{ |
| 156 |
return !empty(self::getPromoById($id)); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @throws Exception |
| 161 |
*/ |
| 162 |
public static function getActivePromo(): ?object |
| 163 |
{ |
| 164 |
|
| 165 |
$promos = self::getPromos(); |
| 166 |
|
| 167 |
foreach($promos as $promo) { |
| 168 |
|
| 169 |
if(self::$today >= $promo->start && self::$today <= $promo->end) { |
| 170 |
|
| 171 |
return $promo; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return null; |
| 176 |
} |
| 177 |
|
| 178 |
public static function getDismissedPromos(): array |
| 179 |
{ |
| 180 |
|
| 181 |
$dismissed = get_option(self::$dismissed_option); |
| 182 |
|
| 183 |
return is_array($dismissed) ? $dismissed : []; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @throws Exception |
| 188 |
*/ |
| 189 |
public static function cleanDismissedPromos($dismissed) |
| 190 |
{ |
| 191 |
foreach($dismissed as $key => $id) { |
| 192 |
if(!self::promoExists($id)) { |
| 193 |
unset($dismissed[$key]); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return $dismissed; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @throws Exception |
| 202 |
*/ |
| 203 |
public static function dismissPromo($id): bool |
| 204 |
{ |
| 205 |
|
| 206 |
$dismissed = self::getDismissedPromos(); |
| 207 |
|
| 208 |
if(!in_array($id, $dismissed)) { |
| 209 |
$dismissed = self::cleanDismissedPromos($dismissed); |
| 210 |
$dismissed[] = $id; |
| 211 |
return update_option(self::$dismissed_option, $dismissed); |
| 212 |
} |
| 213 |
|
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
public static function isPromoDismissed($id): bool |
| 218 |
{ |
| 219 |
$dismissed = self::getDismissedPromos(); |
| 220 |
|
| 221 |
return in_array($id, $dismissed); |
| 222 |
} |
| 223 |
|
| 224 |
public static function ajaxPromoDismissAction() { |
| 225 |
|
| 226 |
// Continue only if the nonce is correct |
| 227 |
$nonce = sanitize_text_field($_REQUEST['_nonce']); |
| 228 |
|
| 229 |
if ( ! wp_verify_nonce( $nonce, App::instance()->prefix('promo_dismiss_nonce') ) ) { |
| 230 |
wp_send_json_error(); |
| 231 |
} |
| 232 |
|
| 233 |
$id = sanitize_text_field($_POST['params']['id']); |
| 234 |
|
| 235 |
$dismissed = self::dismissPromo($id); |
| 236 |
|
| 237 |
if($dismissed) { |
| 238 |
wp_send_json_success(); |
| 239 |
}else { |
| 240 |
wp_send_json_error(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @throws Exception |
| 246 |
*/ |
| 247 |
public static function addPromoNotice() { |
| 248 |
|
| 249 |
$current_user = wp_get_current_user(); |
| 250 |
$first_name = ucfirst(strtolower(!empty($current_user->user_firstname) ? $current_user->user_firstname : $current_user->display_name)); |
| 251 |
|
| 252 |
$promo = self::getActivePromo(); |
| 253 |
$startYear = $promo->start->format('Y'); |
| 254 |
$endYear = $promo->end->format('Y'); |
| 255 |
|
| 256 |
$promoDays = $promo->end->diff($promo->start)->days; |
| 257 |
|
| 258 |
if($promoDays === 0) { |
| 259 |
|
| 260 |
$endDate = esc_html__('the end of the day', 'wpide'); |
| 261 |
|
| 262 |
}else if($promoDays === 1) { |
| 263 |
|
| 264 |
$endDate = esc_html__('tomorrow', 'wpide'); |
| 265 |
|
| 266 |
}else{ |
| 267 |
|
| 268 |
$endDateFormat = 'M jS'; |
| 269 |
|
| 270 |
if($startYear !== $endYear) { |
| 271 |
$endDateFormat .= ', Y'; |
| 272 |
} |
| 273 |
$endDate = $promo->end->format($endDateFormat); |
| 274 |
} |
| 275 |
|
| 276 |
$action = SLUG."_promo_dismiss_action"; |
| 277 |
|
| 278 |
$params = [ |
| 279 |
'id' => $promo->id |
| 280 |
]; |
| 281 |
|
| 282 |
$dataParams = htmlspecialchars(json_encode($params), ENT_QUOTES, 'UTF-8'); |
| 283 |
|
| 284 |
?> |
| 285 |
<div class="notice notice-info wpide-notice" data-slug="<?php echo esc_attr(SLUG); ?>"> |
| 286 |
<p><?php echo sprintf(esc_html__("Hey %s, %s is LIVE until %s!", "wpide"), $first_name, '<strong>' . $promo->name . '</strong>', '<strong>' . $endDate . '</strong>'); ?> |
| 287 |
<p><?php echo sprintf(esc_html__('Get %s OFF on a %s license using this coupon: %s', "wpide"), "<strong>$promo->discount</strong>", "<strong>".NAME."</strong>", "<strong>$promo->coupon</strong>"); ?></p> |
| 288 |
<ul class="wpide-notice-action" data-action="<?php echo esc_attr($action);?>" data-nonce="<?php echo wp_create_nonce( App::instance()->prefix('promo_dismiss_nonce') ) ?>"> |
| 289 |
<li><span class="dashicons dashicons-thumbs-up"></span> <a href="<?php echo esc_url(Freemius::sdk()->get_upgrade_url());?>"><?php echo esc_html__( 'Upgrade', 'wpide' ) ?></a></li> |
| 290 |
<li><span class="dashicons dashicons-products"></span> <a target="_blank" href="<?php echo esc_url(App::instance()->getExternalUrl('promo-notice', AUTHOR_URL.'/plugins'));?>"><?php echo esc_html__( 'More Products', 'wpide' ) ?></a></li> |
| 291 |
<li><span class="dashicons dashicons-dismiss"></span> <a data-action="dismiss" data-params="<?php echo $dataParams;?>" href="#"><?php echo esc_html__( 'Dismiss', 'wpide' ) ?></a></li> |
| 292 |
</ul> |
| 293 |
</div> |
| 294 |
<?php |
| 295 |
} |
| 296 |
|
| 297 |
public static function enqueueAdminAssets() |
| 298 |
{ |
| 299 |
|
| 300 |
wp_enqueue_script(SLUG.'-notice', ASSETS_URL.'global/js/notice-min.js', [], VERSION); |
| 301 |
wp_enqueue_style(SLUG.'-notice', ASSETS_URL.'global/css/notice.css', [], VERSION); |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|