| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Core\Components; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use ImageOptimization\Classes\Client\Client; |
| 7 |
use ImageOptimization\Classes\Utils; |
| 8 |
use ImageOptimization\Plugin; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Renewal_Notice { |
| 15 |
const RENEWAL_NOTICE_SLUG = 'image-optimizer-renewal-notice'; |
| 16 |
|
| 17 |
public ?int $days_diff = null; |
| 18 |
|
| 19 |
public function date_diff_from_current( $date ) { |
| 20 |
$current_date = new DateTime(); // Current date |
| 21 |
$given_date = new DateTime( $date ); |
| 22 |
|
| 23 |
$interval = $current_date->diff( $given_date ); |
| 24 |
|
| 25 |
$this->days_diff = $interval->invert ? -$interval->days : $interval->days; |
| 26 |
|
| 27 |
return $this->days_diff; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_renewal_text(): array { |
| 31 |
if ( $this->days_diff <= 30 && $this->days_diff > 0 ) { |
| 32 |
return [ |
| 33 |
'title' => esc_html__( 'Image Optimizer Expires Soon!', 'image-optimization' ), |
| 34 |
'description' => esc_html__( 'Renew your subscription to maintain optimized images, faster website load times, and high performance.', 'image-optimization' ), |
| 35 |
'btn' => esc_html__( 'Turn Auto Renew On', 'image-optimization' ), |
| 36 |
'link' => esc_url( 'https://go.elementor.com/io-renew-30/' ), |
| 37 |
]; |
| 38 |
} |
| 39 |
if ( $this->days_diff <= 0 && $this->days_diff > -7 ) { |
| 40 |
return [ |
| 41 |
'title' => esc_html__( 'Image Optimizer Has Expired!', 'image-optimization' ), |
| 42 |
'description' => esc_html__( 'Renew your license today to continue enjoying premium image optimization and fast load times.', 'image-optimization' ), |
| 43 |
'btn' => esc_html__( 'Renew Now', 'image-optimization' ), |
| 44 |
'link' => esc_url( 'https://go.elementor.com/io-renew-expire/' ), |
| 45 |
]; |
| 46 |
} |
| 47 |
return [ |
| 48 |
'title' => esc_html__( 'It’s Not Too Late! Renew Image Optimizer', 'image-optimization' ), |
| 49 |
'description' => esc_html__( 'Reactivate your license to get back to top image performance.', 'image-optimization' ), |
| 50 |
'btn' => esc_html__( 'Renew and Optimize', 'image-optimization' ), |
| 51 |
'link' => esc_url( 'https://go.elementor.com/io-renew-post-expire/' ), |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
public function render_renewal_notice() { |
| 56 |
$text = $this->get_renewal_text(); |
| 57 |
?> |
| 58 |
<div class="notice notice-info is-dismissible image-optimizer__notice image-optimizer__notice--error image-optimizer__renewal-notice" |
| 59 |
data-notice-slug="<?php echo esc_attr( self::RENEWAL_NOTICE_SLUG ); ?>" style="display:none;"> |
| 60 |
<div class="image-optimizer__content-block"> |
| 61 |
<svg width="32" height="32" fill="none" role="presentation"> |
| 62 |
<rect width="32" height="32" fill="#232629" rx="16"/> |
| 63 |
<path fill="#fff" d="M10.508 4.135a.125.125 0 0 0-.236 0l-1.183 3.42a.125.125 0 0 1-.078.078L5.553 8.8a.125.125 0 0 0 0 .237l3.458 1.166a.125.125 0 0 1 .078.078l1.183 3.42a.125.125 0 0 0 .236 0l1.182-3.42a.125.125 0 0 1 .078-.078l3.458-1.166a.125.125 0 0 0 0-.237l-3.458-1.167a.125.125 0 0 1-.078-.077l-1.182-3.421ZM17.425 12.738v3.683l-4.073 4.582L26.495 9.598a.125.125 0 0 1 .207.094v14.851a.125.125 0 0 1-.125.125H5.874a.125.125 0 0 1-.09-.212l11.427-11.805a.125.125 0 0 1 .214.087Z"/> |
| 64 |
</svg> |
| 65 |
<p> |
| 66 |
<b> |
| 67 |
<?php echo esc_html( $text['title'] ); ?> |
| 68 |
</b> |
| 69 |
<span> |
| 70 |
<?php echo esc_html( $text['description'] ); ?> |
| 71 |
</span> |
| 72 |
</p> |
| 73 |
</div> |
| 74 |
<a href="<?php echo esc_url( $text['link'] ); ?>" target="_blank" rel="noopener noreferrer"> |
| 75 |
<?php echo esc_html( $text['btn'] ); ?> |
| 76 |
</a> |
| 77 |
</div> |
| 78 |
|
| 79 |
<script> |
| 80 |
jQuery( document ).ready( function( $ ) { |
| 81 |
setTimeout(() => { |
| 82 |
const $msInOneDay = 24 * 60 * 60 * 1000; |
| 83 |
const $time_dismissed = localStorage.getItem('<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>'); |
| 84 |
const $show_notice = !$time_dismissed || Date.now() - $time_dismissed >= $msInOneDay; |
| 85 |
|
| 86 |
const $notice = $( '[data-notice-slug="<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>"]' ); |
| 87 |
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>"] .notice-dismiss' ); |
| 88 |
|
| 89 |
if ($show_notice) { |
| 90 |
$notice.css('display', 'flex'); |
| 91 |
$closeButton.on( 'click', function () { |
| 92 |
localStorage.setItem('<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>', Date.now().toString()); |
| 93 |
} ); |
| 94 |
} else { |
| 95 |
$notice.remove(); |
| 96 |
} |
| 97 |
}, 0); |
| 98 |
} ); |
| 99 |
</script> |
| 100 |
<?php |
| 101 |
} |
| 102 |
|
| 103 |
public function add_media_menu_badge( $parent_file ) { |
| 104 |
global $menu; |
| 105 |
|
| 106 |
foreach ( $menu as &$item ) { |
| 107 |
if ( 'upload.php' === $item[2] ) { |
| 108 |
$item[0] .= ' <span class="update-plugins count-1"><span class="plugin-count">1</span></span>'; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return $parent_file; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public function __construct() { |
| 118 |
add_action('current_screen', function () { |
| 119 |
// @var ImageOptimizer/Modules/ConnectManager/Module |
| 120 |
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| 121 |
|
| 122 |
if ( ! $module->connect_instance->is_connected() || ! Utils::user_is_admin() ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$info = Client::get_subscription_info(); |
| 127 |
|
| 128 |
if ( empty( $info ) || $info->auto_renew || $this->date_diff_from_current( ( $info->date_next_payment ?? $info->date_end ) ) > 30 ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
add_filter( 'parent_file', [ $this, 'add_media_menu_badge' ] ); |
| 133 |
|
| 134 |
if ( |
| 135 |
Utils::is_media_page() || |
| 136 |
Utils::is_plugin_page() || |
| 137 |
Utils::is_single_attachment_page() || |
| 138 |
Utils::is_media_upload_page() || |
| 139 |
Utils::is_wp_dashboard_page() || |
| 140 |
Utils::is_wp_updates_page() |
| 141 |
) { |
| 142 |
add_action( 'admin_notices', [ $this, 'render_renewal_notice' ] ); |
| 143 |
} |
| 144 |
}); |
| 145 |
} |
| 146 |
} |
| 147 |
|