| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Settings\Banners; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Core\Components\Pointers; |
| 6 |
use Throwable; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Sale_Banner |
| 14 |
*/ |
| 15 |
class Sale_Banner { |
| 16 |
const BANNER_POINTER_NAME = 'image_optimizer_sale_bf_2025_banner'; |
| 17 |
const POINTER_ACTION = 'image_optimizer_pointer_dismissed'; |
| 18 |
const POINTER_NONCE_KEY = 'image-optimization-pointer-dismissed'; |
| 19 |
|
| 20 |
public static function is_sale_time(): bool { |
| 21 |
$sale_start_time = strtotime( '2025-11-25 13:00:00 UTC' ); |
| 22 |
$sale_end_time = strtotime( '2025-12-04 04:59:00 UTC' ); |
| 23 |
|
| 24 |
$now_time = time(); |
| 25 |
|
| 26 |
return $now_time >= $sale_start_time && $now_time <= $sale_end_time; |
| 27 |
} |
| 28 |
|
| 29 |
public static function user_viewed_banner(): bool { |
| 30 |
return Pointers::is_dismissed( self::BANNER_POINTER_NAME ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get banner markup |
| 35 |
* @throws Throwable |
| 36 |
*/ |
| 37 |
public static function get_banner( string $link ) { |
| 38 |
if ( ! self::is_sale_time() || self::user_viewed_banner() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
$img = plugins_url( '/images/bf-banner-io-2025.png', __FILE__ ); |
| 42 |
$url = admin_url( 'admin-ajax.php' ); |
| 43 |
$nonce = wp_create_nonce( self::POINTER_NONCE_KEY ); |
| 44 |
|
| 45 |
?> |
| 46 |
<div class="elementor-black-friday-banner"> |
| 47 |
<div class="elementor-black-friday-banner-container"> |
| 48 |
<img src="<?php echo esc_url( $img ); ?>" alt="Black Friday banner image"> |
| 49 |
<a href="<?php echo esc_url( $link ); ?>" target="_blank"> |
| 50 |
Get discount |
| 51 |
</a> |
| 52 |
<button> |
| 53 |
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 54 |
<path fill-rule="evenodd" clip-rule="evenodd" d="M13.2803 1.28033C13.5732 0.987437 13.5732 0.512563 13.2803 0.21967C12.9874 -0.0732233 12.5126 -0.0732233 12.2197 0.21967L6.75 5.68934L1.28033 0.21967C0.987437 -0.0732233 0.512563 -0.0732233 0.21967 0.21967C-0.0732233 0.512563 -0.0732233 0.987437 0.21967 1.28033L5.68934 6.75L0.21967 12.2197C-0.0732233 12.5126 -0.0732233 12.9874 0.21967 13.2803C0.512563 13.5732 0.987437 13.5732 1.28033 13.2803L6.75 7.81066L12.2197 13.2803C12.5126 13.5732 12.9874 13.5732 13.2803 13.2803C13.5732 12.9874 13.5732 12.5126 13.2803 12.2197L7.81066 6.75L13.2803 1.28033Z" fill="white"/> |
| 55 |
</svg> |
| 56 |
</button> |
| 57 |
</div> |
| 58 |
</div> |
| 59 |
<style> |
| 60 |
.elementor-black-friday-banner { |
| 61 |
overflow: hidden; |
| 62 |
margin-left: -20px; |
| 63 |
background: #000; |
| 64 |
} |
| 65 |
.elementor-black-friday-banner-container { |
| 66 |
position: relative; |
| 67 |
max-width: 1600px; |
| 68 |
margin: 0 auto; |
| 69 |
display: flex; |
| 70 |
justify-content: end; |
| 71 |
align-items: center; |
| 72 |
direction: ltr; |
| 73 |
height: 80px; |
| 74 |
} |
| 75 |
.elementor-black-friday-banner img { |
| 76 |
position: absolute; |
| 77 |
left: 0; |
| 78 |
top: 50%; |
| 79 |
transform: translateY(-50%); |
| 80 |
width: 100%; |
| 81 |
} |
| 82 |
.elementor-black-friday-banner a { |
| 83 |
position: relative; |
| 84 |
display: inline-block; |
| 85 |
padding: 12px 24px; |
| 86 |
font-size: 18px; |
| 87 |
color: #000; |
| 88 |
background-color: #ff7be5; |
| 89 |
text-decoration: none; |
| 90 |
z-index: 2; |
| 91 |
} |
| 92 |
.elementor-black-friday-banner button { |
| 93 |
position: relative; |
| 94 |
border: none; |
| 95 |
background: none; |
| 96 |
padding: 12px; |
| 97 |
margin: 0 24px; |
| 98 |
cursor: pointer; |
| 99 |
z-index: 2; |
| 100 |
} |
| 101 |
@media (max-width: 768px) { |
| 102 |
.elementor-black-friday-banner a { |
| 103 |
padding: 6px 12px; |
| 104 |
font-size: 14px; |
| 105 |
} |
| 106 |
.elementor-black-friday-banner button { |
| 107 |
margin: 0 12px; |
| 108 |
} |
| 109 |
} |
| 110 |
</style> |
| 111 |
<script> |
| 112 |
document.addEventListener('DOMContentLoaded', function () { |
| 113 |
const banner = document.querySelector('.elementor-black-friday-banner'); |
| 114 |
const button = document.querySelector('.elementor-black-friday-banner button'); |
| 115 |
|
| 116 |
const requestData = { |
| 117 |
action: "<?php echo esc_js( self::POINTER_ACTION ); ?>", |
| 118 |
nonce: "<?php echo esc_js( $nonce ); ?>", |
| 119 |
data: { |
| 120 |
pointer: "<?php echo esc_js( self::BANNER_POINTER_NAME ); ?>", |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
if (button) { |
| 125 |
button.addEventListener('click', function () { |
| 126 |
jQuery.ajax( |
| 127 |
{ |
| 128 |
url: '<?php echo esc_js( $url ); ?>', |
| 129 |
method: 'POST', |
| 130 |
data: requestData, |
| 131 |
success: () => banner.remove(), |
| 132 |
error: (error) => console.error('Error:', error), |
| 133 |
} |
| 134 |
); |
| 135 |
}); |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
</script> |
| 140 |
<?php |
| 141 |
} |
| 142 |
} |
| 143 |
|