| 1 |
<?php |
| 2 |
/** |
| 3 |
* Promotion Service |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Services; |
| 10 |
|
| 11 |
/** |
| 12 |
* PromotionService Class |
| 13 |
* |
| 14 |
* Handles promotional banner display logic for Easy Invoice Pro upgrades |
| 15 |
*/ |
| 16 |
class PromotionService { |
| 17 |
|
| 18 |
/** |
| 19 |
* Promotion banner dismissed option key |
| 20 |
*/ |
| 21 |
const DISMISSED_OPTION = 'easy_invoice_promotion_dismissed'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Promotion banner last shown timestamp option key |
| 25 |
*/ |
| 26 |
const LAST_SHOWN_OPTION = 'easy_invoice_promotion_last_shown'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Promotion banner permanently hidden option key |
| 30 |
*/ |
| 31 |
const PERMANENTLY_HIDDEN_OPTION = 'easy_invoice_promotion_permanently_hidden'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Minimum invoices required to show promotion |
| 35 |
*/ |
| 36 |
const MIN_INVOICES = 1; |
| 37 |
|
| 38 |
/** |
| 39 |
* Days to wait before showing again if dismissed once |
| 40 |
*/ |
| 41 |
const RESHOW_AFTER_DAYS = 15; |
| 42 |
|
| 43 |
/** |
| 44 |
* Easy Invoice Pro pricing URL |
| 45 |
*/ |
| 46 |
const PRO_PRICING_URL = 'https://matrixaddons.com/plugins/easy-invoice#pricing'; |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize the promotion service |
| 50 |
*/ |
| 51 |
public static function init() { |
| 52 |
add_action('admin_notices', [__CLASS__, 'maybeShowPromotionBanner']); |
| 53 |
add_action('wp_ajax_easy_invoice_dismiss_promotion', [__CLASS__, 'handleDismissPromotion']); |
| 54 |
add_action('admin_enqueue_scripts', [__CLASS__, 'enqueueScripts']); |
| 55 |
|
| 56 |
// Add plugin row action |
| 57 |
add_filter('plugin_action_links_' . plugin_basename(EASY_INVOICE_PLUGIN_FILE), [__CLASS__, 'addPluginRowAction']); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Enqueue admin scripts for promotion functionality |
| 62 |
*/ |
| 63 |
public static function enqueueScripts() { |
| 64 |
wp_enqueue_script('easy-invoice-promotion', EASY_INVOICE_PLUGIN_URL . 'assets/js/promotion.js', ['jquery'], EASY_INVOICE_VERSION, true); |
| 65 |
wp_localize_script('easy-invoice-promotion', 'easyInvoicePromotion', [ |
| 66 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 67 |
'nonce' => wp_create_nonce('easy_invoice_promotion_nonce') |
| 68 |
]); |
| 69 |
|
| 70 |
// Add plain text CSS for plugin row action |
| 71 |
wp_add_inline_style('easy-invoice-promotion', ' |
| 72 |
.plugin-action-links a[href*="easy-invoice#pricing"] { |
| 73 |
color: #ff9500 !important; |
| 74 |
font-weight: 600 !important; |
| 75 |
text-decoration: none !important; |
| 76 |
font-size: 12px !important; |
| 77 |
transition: all 0.3s ease !important; |
| 78 |
} |
| 79 |
|
| 80 |
.plugin-action-links a[href*="easy-invoice#pricing"]:hover { |
| 81 |
color: #e67e00 !important; |
| 82 |
text-decoration: underline !important; |
| 83 |
} |
| 84 |
'); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if Easy Invoice Pro is active |
| 89 |
*/ |
| 90 |
public static function isProActive() { |
| 91 |
if (!function_exists('is_plugin_active')) { |
| 92 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 93 |
} |
| 94 |
return is_plugin_active('easy-invoice-pro/easy-invoice-pro.php'); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get the count of invoices created by the user |
| 99 |
*/ |
| 100 |
public static function getInvoiceCount() { |
| 101 |
$args = [ |
| 102 |
'post_type' => 'easy_invoice', |
| 103 |
'post_status' => ['publish', 'draft', 'pending'], |
| 104 |
'posts_per_page' => -1, |
| 105 |
'fields' => 'ids' |
| 106 |
]; |
| 107 |
|
| 108 |
$query = new \WP_Query($args); |
| 109 |
return $query->found_posts; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Check if promotion should be shown |
| 114 |
*/ |
| 115 |
public static function shouldShowPromotion() { |
| 116 |
// Don't show if Pro is active |
| 117 |
if (self::isProActive()) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
// Don't show if permanently hidden |
| 122 |
if (get_option(self::PERMANENTLY_HIDDEN_OPTION)) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
// Check if user has created enough invoices |
| 127 |
$invoice_count = self::getInvoiceCount(); |
| 128 |
if ($invoice_count < self::MIN_INVOICES) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
// Check if banner was dismissed |
| 133 |
$dismissed = get_option(self::DISMISSED_OPTION); |
| 134 |
if ($dismissed) { |
| 135 |
// Check if enough time has passed to show again |
| 136 |
$last_shown = get_option(self::LAST_SHOWN_OPTION); |
| 137 |
if ($last_shown) { |
| 138 |
$days_since_shown = (current_time('timestamp') - strtotime($last_shown)) / DAY_IN_SECONDS; |
| 139 |
if ($days_since_shown < self::RESHOW_AFTER_DAYS) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Display the promotion banner if conditions are met |
| 150 |
*/ |
| 151 |
public static function maybeShowPromotionBanner() { |
| 152 |
if (!self::shouldShowPromotion()) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
// Update last shown timestamp |
| 157 |
update_option(self::LAST_SHOWN_OPTION, current_time('mysql')); |
| 158 |
|
| 159 |
$invoice_count = self::getInvoiceCount(); |
| 160 |
?> |
| 161 |
<div id="easy-invoice-promotion-notice" class="notice is-dismissible" style="background: linear-gradient(135deg, #fdf6f0 0%, #f8f9fa 50%, #fff5ee 100%); border: 1px solid #f0e6d8; border-left: 4px solid #ff9500; border-radius: 6px; margin: 15px 0; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08); position: relative;"> |
| 162 |
<div style="position: absolute; top: 8px; right: 80px; background: #ff9500; color: white; padding: 2px 8px; border-radius: 10px; font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px;"> |
| 163 |
⚡ Limited Time |
| 164 |
</div> |
| 165 |
<div class="easy-invoice-promotion-content" style="padding: 18px;"> |
| 166 |
<div style="display: flex; align-items: flex-start; gap: 15px;"> |
| 167 |
<div style="flex: 1;"> |
| 168 |
<h3 style="margin: 0 0 10px 0; color: #2c3e50; font-size: 17px; font-weight: 600;"> |
| 169 |
🚀 Upgrade to Easy Invoice Pro - Save 20%! |
| 170 |
</h3> |
| 171 |
<p style="margin: 0 0 12px 0; font-size: 14px; line-height: 1.5; color: #495057;"> |
| 172 |
You've created <strong style="color: #ff9500;"><?php echo esc_html($invoice_count); ?></strong> invoice(s)! |
| 173 |
Get <strong style="color: #ff9500;">20% OFF</strong> today before prices increase very soon. Unlock recurring invoices, advanced reporting, multiple payment gateways, and priority support. |
| 174 |
</p> |
| 175 |
<div style="background: rgba(255, 149, 0, 0.08); padding: 10px; border-radius: 4px; margin-bottom: 15px; border-left: 3px solid #ff9500;"> |
| 176 |
<p style="margin: 0; font-size: 13px; color: #495057; font-weight: 600;"> |
| 177 |
⚠️ <strong>Price Increase Alert:</strong> Subscription prices will be increasing soon. Lock in current pricing with 20% discount! |
| 178 |
</p> |
| 179 |
</div> |
| 180 |
<div style="display: flex; align-items: center; gap: 12px;"> |
| 181 |
<a href="<?php echo esc_url(self::PRO_PRICING_URL); ?>" |
| 182 |
target="_blank" |
| 183 |
class="button button-primary" |
| 184 |
style="background-color: #ff9500; border-color: #ff9500; color: white; padding: 8px 20px; font-weight: 600; font-size: 14px; border-radius: 4px; text-decoration: none; box-shadow: 0 2px 6px rgba(255, 149, 0, 0.3); transition: all 0.3s ease;"> |
| 185 |
⚡ Get 20% Off - Upgrade Now |
| 186 |
</a> |
| 187 |
<a href="#" |
| 188 |
id="easy-invoice-promotion-dismiss" |
| 189 |
style="color: #6c757d; text-decoration: none; font-size: 13px; transition: color 0.3s ease;"> |
| 190 |
Maybe later |
| 191 |
</a> |
| 192 |
</div> |
| 193 |
</div> |
| 194 |
</div> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
|
| 198 |
<style> |
| 199 |
#easy-invoice-promotion-notice .button:hover { |
| 200 |
background-color: #e67e00 !important; |
| 201 |
border-color: #e67e00 !important; |
| 202 |
transform: translateY(-1px); |
| 203 |
box-shadow: 0 4px 10px rgba(255, 149, 0, 0.4) !important; |
| 204 |
} |
| 205 |
|
| 206 |
#easy-invoice-promotion-dismiss:hover { |
| 207 |
color: #495057 !important; |
| 208 |
} |
| 209 |
|
| 210 |
@media (max-width: 768px) { |
| 211 |
#easy-invoice-promotion-notice .easy-invoice-promotion-content { |
| 212 |
padding: 15px !important; |
| 213 |
} |
| 214 |
#easy-invoice-promotion-notice .easy-invoice-promotion-content > div { |
| 215 |
flex-direction: column !important; |
| 216 |
align-items: flex-start !important; |
| 217 |
} |
| 218 |
#easy-invoice-promotion-notice .button { |
| 219 |
width: 100% !important; |
| 220 |
text-align: center !important; |
| 221 |
} |
| 222 |
} |
| 223 |
</style> |
| 224 |
<?php |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Handle AJAX request to dismiss promotion |
| 229 |
*/ |
| 230 |
public static function handleDismissPromotion() { |
| 231 |
check_ajax_referer('easy_invoice_promotion_nonce', 'nonce'); |
| 232 |
|
| 233 |
if (!current_user_can('manage_options')) { |
| 234 |
wp_die(__('You do not have sufficient permissions to access this page.', 'easy-invoice')); |
| 235 |
} |
| 236 |
|
| 237 |
$dismiss_count = get_option('easy_invoice_promotion_dismiss_count', 0); |
| 238 |
$dismiss_count++; |
| 239 |
|
| 240 |
update_option('easy_invoice_promotion_dismiss_count', $dismiss_count); |
| 241 |
update_option(self::DISMISSED_OPTION, true); |
| 242 |
|
| 243 |
// If dismissed more than once, hide permanently |
| 244 |
if ($dismiss_count >= 2) { |
| 245 |
update_option(self::PERMANENTLY_HIDDEN_OPTION, true); |
| 246 |
} |
| 247 |
|
| 248 |
wp_send_json_success(); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Add "Upgrade to Pro" action in plugin row |
| 253 |
*/ |
| 254 |
public static function addPluginRowAction($links) { |
| 255 |
// Don't show if Pro is active |
| 256 |
if (self::isProActive()) { |
| 257 |
return $links; |
| 258 |
} |
| 259 |
|
| 260 |
$upgrade_link = sprintf( |
| 261 |
'<a href="%s" target="_blank" style="color: #ff9500; font-weight: 600; text-decoration: none; font-size: 12px; transition: all 0.3s ease;">%s</a>', |
| 262 |
esc_url(self::PRO_PRICING_URL), |
| 263 |
__('Upgrade to Pro', 'easy-invoice') |
| 264 |
); |
| 265 |
|
| 266 |
// Add to the beginning of the links array |
| 267 |
array_unshift($links, $upgrade_link); |
| 268 |
|
| 269 |
return $links; |
| 270 |
} |
| 271 |
} |
| 272 |
|