← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Announcements.php
+394
-209
3.10.15
→
4.0.9
View file →
| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | namespace ThemeisleSDK\Modules; |
| 15 | 15 | |
| 16 | +use DateTime; | |
| 16 | 17 | use ThemeisleSDK\Common\Abstract_Module; |
| 17 | 18 | use ThemeisleSDK\Loader; |
| 18 | 19 | use ThemeisleSDK\Product; |
| 19 | 20 | |
| @@ -21,36 +22,31 @@ | ||
| 21 | 22 | * Announcement module for the ThemeIsle SDK. |
| 22 | 23 | */ |
| 23 | 24 | class Announcements extends Abstract_Module { |
| 24 | 25 | |
| 26 | + const SALE_DURATION_BLACK_FRIDAY = '+7 days'; // DateTime modifier. (Include Cyber Monday) | |
| 27 | + const MINIMUM_INSTALL_AGE = 3 * DAY_IN_SECONDS; | |
| 28 | + | |
| 25 | 29 | /** |
| 26 | - * Holds the timeline for the announcements. | |
| 30 | + * Mark if the notice was already loaded. | |
| 27 | 31 | * |
| 28 | - * @var array | |
| 32 | + * @var boolean | |
| 29 | 33 | */ |
| 30 | - private static $timeline = array( | |
| 31 | - 'black_friday' => array( | |
| 32 | - 'start' => '2024-11-25 00:00:00', | |
| 33 | - 'end' => '2024-12-03 23:59:59', | |
| 34 | - 'rendered' => false, | |
| 35 | - ), | |
| 36 | - ); | |
| 34 | + private static $notice_loaded = false; | |
| 37 | 35 | |
| 38 | 36 | /** |
| 39 | - * Holds the option prefix for the announcements. | |
| 37 | + * Mark if the plugin meta link was already loaded. | |
| 40 | 38 | * |
| 41 | - * This is used to store the dismiss date for each announcement. | |
| 42 | - * | |
| 43 | - * @var string | |
| 39 | + * @var boolean | |
| 44 | 40 | */ |
| 45 | - public $option_prefix = 'themeisle_sdk_announcement_'; | |
| 41 | + private static $meta_link_loaded = false; | |
| 46 | 42 | |
| 47 | 43 | /** |
| 48 | - * Holds the time for the current request. | |
| 44 | + * The product to be used. | |
| 49 | 45 | * |
| 50 | 46 | * @var string |
| 51 | 47 | */ |
| 52 | - public $time = ''; | |
| 48 | + private static $current_product = ''; | |
| 53 | 49 | |
| 54 | 50 | /** |
| 55 | 51 | * Check if the module can be loaded. |
| 56 | 52 | * |
| @@ -73,17 +69,19 @@ | ||
| 73 | 69 | * |
| 74 | 70 | * @return void |
| 75 | 71 | */ |
| 76 | 72 | public function load( $product ) { |
| 77 | - if ( ! current_user_can( 'install_plugins' ) ) { | |
| 78 | - return; | |
| 79 | - } | |
| 73 | + $this->product = $product; | |
| 80 | 74 | |
| 81 | - $this->product = $product; | |
| 75 | + add_filter( | |
| 76 | + 'themeisle_sdk_is_black_friday_sale', | |
| 77 | + function( $is_black_friday ) { | |
| 78 | + return $this->is_black_friday_sale( $this->get_current_date() ); | |
| 79 | + } | |
| 80 | + ); | |
| 82 | 81 | |
| 83 | - add_action( 'admin_init', array( $this, 'load_announcements' ) ); | |
| 84 | - add_filter( 'themeisle_sdk_active_announcements', array( $this, 'get_active_announcements' ) ); | |
| 85 | - add_filter( 'themeisle_sdk_announcements', array( $this, 'get_announcements_for_plugins' ) ); | |
| 82 | + add_action( 'admin_menu', array( $this, 'load_announcements' ), 9 ); | |
| 83 | + add_action( 'wp_ajax_themeisle_sdk_dismiss_black_friday_notice', array( $this, 'disable_notification_ajax' ) ); | |
| 86 | 84 | } |
| 87 | 85 | |
| 88 | 86 | /** |
| 89 | 87 | * Load all valid announcements. |
| @@ -90,272 +88,459 @@ | ||
| 90 | 88 | * |
| 91 | 89 | * @return void |
| 92 | 90 | */ |
| 93 | 91 | public function load_announcements() { |
| 94 | - $active = $this->get_active_announcements(); | |
| 92 | + $current_date = $this->get_current_date(); | |
| 93 | + if ( ! $this->is_black_friday_sale( $current_date ) ) { | |
| 94 | + return; | |
| 95 | + } | |
| 95 | 96 | |
| 96 | - if ( empty( $active ) ) { | |
| 97 | + if ( self::MINIMUM_INSTALL_AGE > ( $current_date->getTimestamp() - $this->product->get_install_time() ) ) { | |
| 97 | 98 | return; |
| 98 | 99 | } |
| 99 | 100 | |
| 100 | - foreach ( $active as $announcement ) { | |
| 101 | + add_action( 'admin_notices', array( $this, 'black_friday_notice_render' ) ); | |
| 101 | 102 | |
| 102 | - $method = $announcement . '_notice_render'; | |
| 103 | + add_action( | |
| 104 | + 'themeisle_internal_page', | |
| 105 | + function( $plugin, $page_slug ) { | |
| 106 | + self::$current_product = $plugin; | |
| 107 | + }, | |
| 108 | + 10, | |
| 109 | + 2 | |
| 110 | + ); | |
| 103 | 111 | |
| 104 | - if ( method_exists( $this, $method ) ) { | |
| 105 | - add_action( 'admin_notices', array( $this, $method ) ); | |
| 106 | - } | |
| 107 | - } | |
| 112 | + add_filter( 'plugin_row_meta', array( $this, 'add_plugin_meta_links' ), 10, 2 ); | |
| 113 | + add_filter( $this->product->get_key() . '_about_us_metadata', array( $this, 'override_about_us_metadata' ), 100 ); | |
| 114 | + } | |
| 108 | 115 | |
| 109 | - // Load the ajax handler. | |
| 110 | - add_action( 'wp_ajax_themeisle_sdk_dismiss_announcement', array( $this, 'disable_notification_ajax' ) ); | |
| 116 | + /** | |
| 117 | + * Get the remaining time for the event in a human-readable format. | |
| 118 | + * | |
| 119 | + * @param DateTime $end_date The end date for event. | |
| 120 | + * | |
| 121 | + * @return string Remaining time for the event. | |
| 122 | + */ | |
| 123 | + public function get_remaining_time_for_event( $end_date ) { | |
| 124 | + return human_time_diff( $this->get_current_date()->getTimestamp(), $end_date->getTimestamp() ); | |
| 111 | 125 | } |
| 112 | 126 | |
| 113 | 127 | /** |
| 114 | - * Get all active announcements. | |
| 128 | + * Check if the announcement can be shown. | |
| 115 | 129 | * |
| 116 | - * @return array List of active announcements. | |
| 130 | + * @param DateTime $current_date The announcement to check. | |
| 131 | + * @param int $user_id The user id to show the notice. | |
| 132 | + * | |
| 133 | + * @return bool | |
| 117 | 134 | */ |
| 118 | - public function get_active_announcements() { | |
| 119 | - $active = array(); | |
| 135 | + public function can_show_notice( $current_date, $user_id ) { | |
| 136 | + $current_year = $current_date->format( 'Y' ); | |
| 137 | + $user_notice_dismiss_timestamp = get_user_meta( $user_id, 'themeisle_sdk_dismissed_notice_black_friday', true ); | |
| 120 | 138 | |
| 121 | - foreach ( self::$timeline as $announcement_slug => $dates ) { | |
| 122 | - if ( $this->is_active( $dates ) && $this->can_show( $announcement_slug, $dates ) ) { | |
| 123 | - $active[] = $announcement_slug; | |
| 124 | - } | |
| 139 | + if ( empty( $user_notice_dismiss_timestamp ) ) { | |
| 140 | + return true; | |
| 125 | 141 | } |
| 126 | 142 | |
| 127 | - return $active; | |
| 143 | + $dismissed_year = wp_date( 'Y', $user_notice_dismiss_timestamp ); | |
| 144 | + | |
| 145 | + return $current_year !== $dismissed_year; | |
| 128 | 146 | } |
| 129 | 147 | |
| 130 | 148 | /** |
| 131 | - * Get all announcements along with plugin specific data. | |
| 149 | + * Calculate the start date for Black Friday based on the year of the given date. | |
| 132 | 150 | * |
| 133 | - * @return array List of announcements. | |
| 151 | + * Black Friday is the day after the Thanksgiving and the sale starts on the Monday of that week. | |
| 152 | + * | |
| 153 | + * @param DateTime $date The current date object, used to determine the year. | |
| 154 | + * @return DateTime The start date of Black Friday for the given year. | |
| 134 | 155 | */ |
| 135 | - public function get_announcements_for_plugins() { | |
| 156 | + public function get_start_date( $date ) { | |
| 157 | + $year = $date->format( 'Y' ); | |
| 158 | + $black_friday = new DateTime( "last friday of november {$year}" ); | |
| 136 | 159 | |
| 137 | - $announcements = array(); | |
| 160 | + $sale_start = clone $black_friday; | |
| 161 | + $sale_start->modify( 'monday this week' ); | |
| 162 | + $sale_start->setTime( 0, 0 ); | |
| 138 | 163 | |
| 139 | - foreach ( self::$timeline as $announcement => $dates ) { | |
| 140 | - $announcements[ $announcement ] = $dates; | |
| 164 | + return $sale_start; | |
| 165 | + } | |
| 141 | 166 | |
| 142 | - if ( false !== strpos( $announcement, 'black_friday' ) ) { | |
| 143 | - $announcements[ $announcement ]['active'] = $this->is_active( $dates ); | |
| 144 | - | |
| 145 | - // Dashboard banners URLs. | |
| 146 | - $announcements[ $announcement ]['feedzy_dashboard_url'] = tsdk_utmify( 'https://themeisle.com/plugins/feedzy-rss-feeds/blackfriday/', 'bfcm24', 'dashboard' ); | |
| 147 | - $announcements[ $announcement ]['neve_dashboard_url'] = tsdk_utmify( 'https://themeisle.com/themes/neve/blackfriday/', 'bfcm24', 'dashboard' ); | |
| 148 | - $announcements[ $announcement ]['otter_dashboard_url'] = tsdk_utmify( 'https://themeisle.com/plugins/otter-blocks/blackfriday/', 'bfcm24', 'dashboard' ); | |
| 149 | - | |
| 150 | - // Customizer banners URLs. | |
| 151 | - $announcements[ $announcement ]['hestia_customizer_url'] = tsdk_utmify( 'https://themeisle.com/black-friday/', 'bfcm24', 'hestiacustomizer' ); | |
| 152 | - $announcements[ $announcement ]['neve_customizer_url'] = tsdk_utmify( 'https://themeisle.com/black-friday/', 'bfcm24', 'nevecustomizer' ); | |
| 153 | - | |
| 154 | - // Banners urgency text. | |
| 155 | - $remaining_time = $this->get_remaining_time_for_event( $dates['end'] ); | |
| 156 | - $announcements[ $announcement ]['remaining_time'] = $remaining_time; | |
| 157 | - $announcements[ $announcement ]['urgency_text'] = ! empty( $remaining_time ) ? sprintf( Loader::$labels['announcements']['hurry_up'], $remaining_time ) : ''; | |
| 158 | - } | |
| 159 | - } | |
| 160 | - | |
| 161 | - return apply_filters( 'themeisle_sdk_announcements_data', $announcements ); | |
| 167 | + /** | |
| 168 | + * Calculate the event end date. | |
| 169 | + * | |
| 170 | + * @param DateTime $start_date The start date. | |
| 171 | + * @return DateTime The end date. | |
| 172 | + */ | |
| 173 | + public function get_end_date( $start_date ) { | |
| 174 | + $black_friday_end = clone $start_date; | |
| 175 | + $black_friday_end->modify( self::SALE_DURATION_BLACK_FRIDAY ); | |
| 176 | + $black_friday_end->setTime( 23, 59, 59 ); | |
| 177 | + return $black_friday_end; | |
| 162 | 178 | } |
| 163 | 179 | |
| 164 | 180 | /** |
| 165 | - * Get the announcement data. | |
| 181 | + * Check if the current date falls within the Black Friday sale period. | |
| 166 | 182 | * |
| 167 | - * @param string $announcement The announcement to get the data for. | |
| 168 | - * | |
| 169 | - * @return array | |
| 183 | + * @param DateTime $current_date The date to check. | |
| 184 | + * @return bool True if the date is within the Black Friday sale period, false otherwise. | |
| 170 | 185 | */ |
| 171 | - public function get_announcement_data( $announcement ) { | |
| 172 | - return ! empty( $announcement ) && is_string( $announcement ) && isset( self::$timeline[ $announcement ] ) ? self::$timeline[ $announcement ] : array(); | |
| 186 | + public function is_black_friday_sale( $current_date ) { | |
| 187 | + $black_friday_start_date = $this->get_start_date( $current_date ); | |
| 188 | + $black_friday_end = $this->get_end_date( $black_friday_start_date ); | |
| 189 | + return $black_friday_start_date <= $current_date && $current_date <= $black_friday_end; | |
| 173 | 190 | } |
| 174 | 191 | |
| 175 | 192 | /** |
| 176 | - * Check if the announcement has an active timeline. | |
| 193 | + * Get the notice data. | |
| 177 | 194 | * |
| 178 | - * @param array $dates The announcement to check. | |
| 179 | - * | |
| 180 | - * @return bool | |
| 195 | + * @return array The notice data. | |
| 181 | 196 | */ |
| 182 | - public function is_active( $dates ) { | |
| 197 | + public function get_notice_data() { | |
| 198 | + $time_left_label = $this->get_remaining_time_for_event( $this->get_end_date( $this->get_start_date( $this->get_current_date() ) ) ); | |
| 199 | + $time_left_label = sprintf( Loader::$labels['announcements']['time_left'], $time_left_label ); | |
| 183 | 200 | |
| 184 | - if ( empty( $this->time ) ) { | |
| 185 | - $this->time = current_time( 'Y-m-d' ); | |
| 201 | + $utm_location = 'globalnotice'; | |
| 202 | + if ( ! empty( $this->product ) ) { | |
| 203 | + $utm_location = $this->product->get_friendly_name(); | |
| 186 | 204 | } |
| 187 | 205 | |
| 188 | - $start = isset( $dates['start'] ) ? $dates['start'] : null; | |
| 189 | - $end = isset( $dates['end'] ) ? $dates['end'] : null; | |
| 206 | + $sale_title = Loader::$labels['announcements']['black_friday']; | |
| 207 | + $sale_url = tsdk_translate_link( tsdk_utmify( 'https://themeisle.com/blackfriday/', 'bfcm26', $utm_location ) ); | |
| 190 | 208 | |
| 191 | - if ( $start && $end ) { | |
| 192 | - return $start <= $this->time && $this->time <= $end; | |
| 193 | - } elseif ( $start ) { | |
| 194 | - return $this->time >= $start; | |
| 195 | - } elseif ( $end ) { | |
| 196 | - return $this->time <= $end; | |
| 197 | - } | |
| 209 | + $current_year = $this->get_current_date()->format( 'Y' ); | |
| 210 | + $sale_message = sprintf( Loader::$labels['announcements']['max_savings'], $current_year ); | |
| 198 | 211 | |
| 199 | - return false; | |
| 212 | + return array( | |
| 213 | + 'title' => $sale_title, | |
| 214 | + 'sale_url' => $sale_url, | |
| 215 | + 'message' => $sale_message, | |
| 216 | + 'time_left' => $time_left_label, | |
| 217 | + ); | |
| 200 | 218 | } |
| 201 | 219 | |
| 202 | 220 | /** |
| 203 | - * Get the remaining time for the event in a human readable format. | |
| 221 | + * Render the Black Friday notice. | |
| 204 | 222 | * |
| 205 | - * @param string $end_date The end date for event. | |
| 206 | - * | |
| 207 | - * @return string Remaining time for the event. | |
| 223 | + * @return void | |
| 208 | 224 | */ |
| 209 | - public function get_remaining_time_for_event( $end_date ) { | |
| 210 | - if ( empty( $end_date ) || ! is_string( $end_date ) ) { | |
| 211 | - return ''; | |
| 225 | + public function black_friday_notice_render() { | |
| 226 | + | |
| 227 | + // Prevent the notice from being rendered twice. | |
| 228 | + if ( self::$notice_loaded ) { | |
| 229 | + return; | |
| 212 | 230 | } |
| 231 | + self::$notice_loaded = true; | |
| 213 | 232 | |
| 214 | - return human_time_diff( time(), strtotime( $end_date ) ); | |
| 233 | + $current_user_id = get_current_user_id(); | |
| 215 | 234 | |
| 235 | + if ( ! $this->can_show_notice( $this->get_current_date(), $current_user_id ) ) { | |
| 236 | + return; | |
| 237 | + } | |
| 238 | + | |
| 239 | + $all_configs = apply_filters( 'themeisle_sdk_blackfriday_data', array( 'default' => $this->get_notice_data() ) ); | |
| 240 | + | |
| 241 | + if ( empty( $all_configs ) || ! is_array( $all_configs ) ) { | |
| 242 | + return; | |
| 243 | + } | |
| 244 | + | |
| 245 | + $data = isset( $all_configs['default'] ) ? $all_configs['default'] : $this->get_notice_data(); | |
| 246 | + $products = Loader::get_products(); | |
| 247 | + $current_time = $this->get_current_date()->getTimestamp(); | |
| 248 | + $can_show = false; | |
| 249 | + | |
| 250 | + // Check if we have products that are eligible to show the notice with the default data. If the product provide its own config, use it. | |
| 251 | + foreach ( $products as $product ) { | |
| 252 | + $slug = $product->get_slug(); | |
| 253 | + | |
| 254 | + if ( self::MINIMUM_INSTALL_AGE < ( $current_time - $product->get_install_time() ) ) { | |
| 255 | + $can_show = true; | |
| 256 | + | |
| 257 | + if ( isset( $all_configs[ $slug ] ) && ! empty( $all_configs[ $slug ] ) && is_array( $all_configs[ $slug ] ) ) { | |
| 258 | + $data = $all_configs[ $slug ]; | |
| 259 | + | |
| 260 | + if ( self::$current_product === $slug ) { | |
| 261 | + $data = $all_configs[ $slug ]; | |
| 262 | + break; | |
| 263 | + } | |
| 264 | + } | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | + if ( ! $can_show ) { | |
| 269 | + return; | |
| 270 | + } | |
| 271 | + | |
| 272 | + $displayed_on_internal_page = 0 < did_action( 'themeisle_internal_page' ); | |
| 273 | + | |
| 274 | + $title = ! empty( $data['title'] ) ? $data['title'] : Loader::$labels['announcements']['black_friday']; | |
| 275 | + $time_left_label = ! empty( $data['time_left'] ) ? $data['time_left'] : ''; | |
| 276 | + $message = ! empty( $data['message'] ) ? $data['message'] : ''; | |
| 277 | + $logo_url = ! empty( $data['logo_url'] ) ? $data['logo_url'] : $this->get_sdk_uri() . 'assets/images/themeisle-logo.png'; | |
| 278 | + $cta_label = ! empty( $data['cta_label'] ) ? $data['cta_label'] : Loader::$labels['announcements']['notice_link_label']; | |
| 279 | + $sale_url = ! empty( $data['sale_url'] ) ? $data['sale_url'] : ''; | |
| 280 | + $hide_other_notices = ! empty( $data['hide_other_notices'] ) ? $data['hide_other_notices'] : $displayed_on_internal_page; | |
| 281 | + $dismiss_notice_url = wp_nonce_url( | |
| 282 | + add_query_arg( | |
| 283 | + array( 'action' => 'themeisle_sdk_dismiss_black_friday_notice' ), | |
| 284 | + admin_url( 'admin-ajax.php' ) | |
| 285 | + ), | |
| 286 | + 'dismiss_themeisle_event_notice' | |
| 287 | + ); | |
| 288 | + | |
| 289 | + if ( empty( $sale_url ) ) { | |
| 290 | + return; | |
| 291 | + } | |
| 292 | + | |
| 293 | + if ( ! current_user_can( 'install_plugins' ) ) { | |
| 294 | + $sale_url = remove_query_arg( 'lkey', $sale_url ); | |
| 295 | + } | |
| 296 | + | |
| 297 | + ?> | |
| 298 | + <style> | |
| 299 | + .themeisle-sale { | |
| 300 | + border-left-color: #0466CB; | |
| 301 | + } | |
| 302 | + .themeisle-sale :is(.themeisle-sale-title, p) { | |
| 303 | + margin: 0; | |
| 304 | + } | |
| 305 | + .themeisle-sale-container { | |
| 306 | + display: flex; | |
| 307 | + align-items: center; | |
| 308 | + padding: 0.5rem 0; | |
| 309 | + gap: 0.5rem; | |
| 310 | + padding-right: 10px; | |
| 311 | + } | |
| 312 | + .themeisle-sale-content { | |
| 313 | + display: flex; | |
| 314 | + flex-direction: column; | |
| 315 | + gap: 0.2rem; | |
| 316 | + } | |
| 317 | + .themeisle-sale a { | |
| 318 | + text-decoration: none; | |
| 319 | + } | |
| 320 | + .themeisle-sale p a { | |
| 321 | + margin-left: 1rem; | |
| 322 | + padding: 7px 12px; | |
| 323 | + border-radius: 4px; | |
| 324 | + background: #0466CB; | |
| 325 | + color: white; | |
| 326 | + font-weight: 700; | |
| 327 | + } | |
| 328 | + .themeisle-sale-dismiss { | |
| 329 | + padding-top: 5px; | |
| 330 | + } | |
| 331 | + .themeisle-sale-dismiss span { | |
| 332 | + color: #787c82; | |
| 333 | + font-size: 16px; | |
| 334 | + } | |
| 335 | + .notice.themeisle-sale { | |
| 336 | + padding: 0; | |
| 337 | + } | |
| 338 | + .themeisle-sale-logo { | |
| 339 | + display: flex; | |
| 340 | + justify-content: center; | |
| 341 | + align-items: center; | |
| 342 | + margin-left: 5px; | |
| 343 | + } | |
| 344 | + .themeisle-sale-time-left { | |
| 345 | + margin-left: 5px; | |
| 346 | + padding: 3px 5px; | |
| 347 | + border-radius: 4px; | |
| 348 | + background-color: #dfdfdf; | |
| 349 | + font-weight: 600; | |
| 350 | + font-size: x-small; | |
| 351 | + line-height: 1; | |
| 352 | + } | |
| 353 | + .themeisle-sale-title { | |
| 354 | + font-size: 14px; | |
| 355 | + display: flex; | |
| 356 | + align-items: center; | |
| 357 | + } | |
| 358 | + .themeisle-sale-action { | |
| 359 | + flex-grow: 1; | |
| 360 | + display: flex; | |
| 361 | + justify-content: flex-end; | |
| 362 | + } | |
| 363 | + <?php if ( $hide_other_notices ) : ?> | |
| 364 | + .notice:not(.themeisle-sale) { | |
| 365 | + display: none; | |
| 366 | + } | |
| 367 | + <?php endif; ?> | |
| 368 | + </style> | |
| 369 | + <div class="themeisle-sale notice notice-info" data-event-slug="black_friday"> | |
| 370 | + <div class="themeisle-sale-container"> | |
| 371 | + <div class="themeisle-sale-logo"> | |
| 372 | + <img | |
| 373 | + width="45" | |
| 374 | + src="<?php echo esc_url( $logo_url ); ?>" | |
| 375 | + /> | |
| 376 | + </div> | |
| 377 | + <div class="themeisle-sale-content"> | |
| 378 | + <h4 class="themeisle-sale-title"> | |
| 379 | + <?php echo esc_html( $title ); ?> | |
| 380 | + <span class="themeisle-sale-time-left"> | |
| 381 | + <?php echo esc_html( $time_left_label ); ?> | |
| 382 | + </span> | |
| 383 | + </h4> | |
| 384 | + <p> | |
| 385 | + <?php echo wp_kses_post( $message ); ?> | |
| 386 | + </p> | |
| 387 | + </div> | |
| 388 | + <div class="themeisle-sale-action"> | |
| 389 | + <a | |
| 390 | + href="<?php echo esc_url( $sale_url ); ?>" | |
| 391 | + target="_blank" | |
| 392 | + class="button button-primary themeisle-sale-button" | |
| 393 | + > | |
| 394 | + <?php echo esc_html( $cta_label ); ?> | |
| 395 | + </a> | |
| 396 | + </div> | |
| 397 | + <a href="<?php echo esc_url( $dismiss_notice_url ); ?>" class="themeisle-sale-dismiss"> | |
| 398 | + <span class="dashicons dashicons-dismiss"></span> | |
| 399 | + </a> | |
| 400 | + </div> | |
| 401 | + </div> | |
| 402 | + <script> | |
| 403 | + // Note: Some plugins use React and the content is ready after the `DOMContentLoaded` event. Use this function to reposition the notice after components have rendered. | |
| 404 | + window.tsdk_reposition_notice = function() { | |
| 405 | + const bannerRoot = document.getElementById('tsdk_banner'); | |
| 406 | + const saleNotice = document.querySelector('.themeisle-sale'); | |
| 407 | + if ( ! bannerRoot || ! saleNotice ) { | |
| 408 | + return; | |
| 409 | + } | |
| 410 | + | |
| 411 | + bannerRoot.appendChild(saleNotice); | |
| 412 | + }; | |
| 413 | + | |
| 414 | + document.addEventListener( 'DOMContentLoaded', function() { | |
| 415 | + window.tsdk_reposition_notice(); | |
| 416 | + } ); | |
| 417 | + </script> | |
| 418 | + <?php | |
| 216 | 419 | } |
| 217 | 420 | |
| 218 | 421 | /** |
| 219 | - * Check if the announcement can be shown. | |
| 422 | + * Disable the notification via ajax. | |
| 220 | 423 | * |
| 221 | - * @param string $announcement_slug The announcement to check. | |
| 222 | - * @param array $dates The announcement to check. | |
| 223 | - * | |
| 224 | - * @return bool | |
| 424 | + * @return void | |
| 225 | 425 | */ |
| 226 | - public function can_show( $announcement_slug, $dates ) { | |
| 227 | - $dismiss_date = get_option( $this->option_prefix . $announcement_slug, false ); | |
| 426 | + public function disable_notification_ajax() { | |
| 427 | + check_ajax_referer( 'dismiss_themeisle_event_notice' ); | |
| 228 | 428 | |
| 229 | - if ( false === $dismiss_date ) { | |
| 230 | - return true; | |
| 231 | - } | |
| 429 | + update_user_meta( get_current_user_id(), 'themeisle_sdk_dismissed_notice_black_friday', $this->get_current_date()->getTimestamp() ); | |
| 232 | 430 | |
| 233 | - // If the start date is after the dismiss date, show the notice. | |
| 234 | - $start = isset( $dates['start'] ) ? $dates['start'] : null; | |
| 235 | - if ( $start && $dismiss_date < $start ) { | |
| 236 | - return true; | |
| 431 | + $return_page_url = wp_get_referer(); | |
| 432 | + if ( empty( $return_page_url ) ) { | |
| 433 | + $return_page_url = admin_url(); | |
| 237 | 434 | } |
| 238 | 435 | |
| 239 | - return false; | |
| 436 | + wp_safe_redirect( $return_page_url ); | |
| 437 | + exit; | |
| 240 | 438 | } |
| 241 | 439 | |
| 242 | 440 | /** |
| 243 | - * Disable the notification via ajax. | |
| 441 | + * Add the plugin meta links. | |
| 244 | 442 | * |
| 245 | - * @return void | |
| 443 | + * @param array<string, string> $links The plugin meta links. | |
| 444 | + * @param string $plugin_file The plugin file. | |
| 445 | + * @return array<string, string> The plugin meta links. | |
| 246 | 446 | */ |
| 247 | - public function disable_notification_ajax() { | |
| 248 | - if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'dismiss_themeisle_event_notice' ) ) { | |
| 249 | - wp_die( 'Invalid nonce! Refresh the page and try again.' ); | |
| 447 | + public function add_plugin_meta_links( $links, $plugin_file ) { | |
| 448 | + if ( self::$meta_link_loaded ) { | |
| 449 | + return $links; | |
| 250 | 450 | } |
| 251 | 451 | |
| 252 | - if ( ! isset( $_POST['announcement'] ) || ! is_string( $_POST['announcement'] ) ) { | |
| 253 | - wp_die( 'Invalid announcement! Refresh the page and try again.' ); | |
| 452 | + if ( $plugin_file !== plugin_basename( $this->product->get_basefile() ) ) { | |
| 453 | + return $links; | |
| 254 | 454 | } |
| 255 | 455 | |
| 256 | - $announcement = sanitize_key( $_POST['announcement'] ); | |
| 456 | + $configs = apply_filters( 'themeisle_sdk_blackfriday_data', array( 'default' => $this->get_notice_data() ) ); | |
| 257 | 457 | |
| 258 | - update_option( $this->option_prefix . $announcement, current_time( 'Y-m-d' ) ); | |
| 259 | - wp_die( 'success' ); | |
| 458 | + if ( empty( $configs ) || ! is_array( $configs ) ) { | |
| 459 | + return $links; | |
| 460 | + } | |
| 461 | + | |
| 462 | + $current_slug = $this->product->get_slug(); | |
| 463 | + $data = isset( $configs[ $current_slug ] ) && ! empty( $configs[ $current_slug ] ) && is_array( $configs[ $current_slug ] ) ? $configs[ $current_slug ] : array(); | |
| 464 | + | |
| 465 | + $plugin_meta_message = ''; | |
| 466 | + $plugin_meta_url = ''; | |
| 467 | + | |
| 468 | + if ( isset( $data['plugin_meta_targets'] ) && ! empty( $data['plugin_meta_targets'] ) && ! in_array( $current_slug, $data['plugin_meta_targets'] ) ) { | |
| 469 | + return $links; // The current configuration is for another plugins. | |
| 470 | + } | |
| 471 | + | |
| 472 | + $plugin_meta_message = ! empty( $data['plugin_meta_message'] ) ? $data['plugin_meta_message'] : ''; | |
| 473 | + $plugin_meta_url = ! empty( $data['sale_url'] ) ? $data['sale_url'] : ''; | |
| 474 | + | |
| 475 | + if ( empty( $plugin_meta_url ) || empty( $plugin_meta_message ) ) { | |
| 476 | + | |
| 477 | + // Check if a configuration is in another plugin. | |
| 478 | + $products = Loader::get_products(); | |
| 479 | + foreach ( $products as $product ) { | |
| 480 | + $slug = $product->get_slug(); | |
| 481 | + | |
| 482 | + if ( $slug === $current_slug || ! isset( $configs[ $slug ] ) || empty( $configs[ $slug ] ) || ! is_array( $configs[ $slug ] ) ) { | |
| 483 | + continue; | |
| 484 | + } | |
| 485 | + | |
| 486 | + if ( ! empty( $configs[ $slug ]['plugin_meta_targets'] ) && in_array( $current_slug, $configs[ $slug ]['plugin_meta_targets'] ) ) { | |
| 487 | + $plugin_meta_message = ! empty( $configs[ $slug ]['plugin_meta_message'] ) ? $configs[ $slug ]['plugin_meta_message'] : ''; | |
| 488 | + $plugin_meta_url = ! empty( $configs[ $slug ]['sale_url'] ) ? $configs[ $slug ]['sale_url'] : ''; | |
| 489 | + break; | |
| 490 | + } | |
| 491 | + } | |
| 492 | + } | |
| 493 | + | |
| 494 | + if ( empty( $plugin_meta_url ) || empty( $plugin_meta_message ) ) { | |
| 495 | + return $links; | |
| 496 | + } | |
| 497 | + | |
| 498 | + $links[] = sprintf( '<a class="themeisle-sale-plugin-meta-link" style="color: red;" href="%s" target="_blank">%s</a>', esc_url( $plugin_meta_url ), esc_html( $plugin_meta_message ) ); | |
| 499 | + | |
| 500 | + self::$meta_link_loaded = true; | |
| 501 | + | |
| 502 | + return $links; | |
| 260 | 503 | } |
| 261 | 504 | |
| 262 | 505 | /** |
| 263 | - * Render the Black Friday notice. | |
| 506 | + * Override the About Us upgrade menu during Black Friday. | |
| 264 | 507 | * |
| 265 | - * @return void | |
| 508 | + * Registered dynamically during admin_menu when sale is active. | |
| 509 | + * Only applies if About_Us module is loaded for the product. | |
| 510 | + * | |
| 511 | + * @param array<string, mixed> $about_data About Us metadata. | |
| 512 | + * | |
| 513 | + * @return array<string, mixed> | |
| 266 | 514 | */ |
| 267 | - public function black_friday_notice_render() { | |
| 515 | + public function override_about_us_metadata( $about_data ) { | |
| 516 | + if ( ! $this->is_black_friday_sale( $this->get_current_date() ) ) { | |
| 517 | + return $about_data; | |
| 518 | + } | |
| 268 | 519 | |
| 269 | - // Prevent the notice from being rendered twice. | |
| 270 | - if ( self::$timeline['black_friday']['rendered'] ) { | |
| 271 | - return; | |
| 520 | + if ( empty( $about_data ) || ! is_array( $about_data ) ) { | |
| 521 | + return $about_data; | |
| 272 | 522 | } |
| 273 | - self::$timeline['black_friday']['rendered'] = true; | |
| 274 | 523 | |
| 275 | - $product_names = array(); | |
| 524 | + if ( empty( $about_data['has_upgrade_menu'] ) || true !== $about_data['has_upgrade_menu'] ) { | |
| 525 | + return $about_data; | |
| 526 | + } | |
| 276 | 527 | |
| 277 | - foreach ( Loader::get_products() as $product ) { | |
| 278 | - $slug = $product->get_slug(); | |
| 528 | + $configs = apply_filters( 'themeisle_sdk_blackfriday_data', array( 'default' => $this->get_notice_data() ) ); | |
| 279 | 529 | |
| 280 | - // Do not add if the contains the string 'pro'. | |
| 281 | - if ( strpos( $slug, 'pro' ) !== false ) { | |
| 282 | - continue; | |
| 283 | - } | |
| 284 | - | |
| 285 | - $product_names[] = $product->get_name(); | |
| 530 | + $current_slug = $this->product->get_slug(); | |
| 531 | + if ( ! isset( $configs[ $current_slug ] ) || empty( $configs[ $current_slug ] ) || ! is_array( $configs[ $current_slug ] ) ) { | |
| 532 | + return $about_data; | |
| 286 | 533 | } |
| 287 | 534 | |
| 288 | - // Randomize the products and get only 4. | |
| 289 | - shuffle( $product_names ); | |
| 290 | - $product_names = array_slice( $product_names, 0, 4 ); | |
| 535 | + $config = $configs[ $current_slug ]; | |
| 291 | 536 | |
| 292 | - ?> | |
| 293 | - <style> | |
| 294 | - .themeisle-sale { | |
| 295 | - display: flex; | |
| 296 | - } | |
| 297 | - </style> | |
| 298 | - <div class="themeisle-sale notice notice-info is-dismissible" data-announcement="black_friday"> | |
| 299 | - <img src="<?php echo esc_url_raw( $this->get_sdk_uri() . 'assets/images/themeisle-logo.svg' ); ?>"/> | |
| 300 | - <p> | |
| 301 | - <strong><?php echo esc_html( Loader::$labels['announcements']['sale_live'] ); ?> ></strong> | |
| 302 | - - <?php echo sprintf( esc_html( Loader::$labels['announcements']['max_savings'] ), esc_html( implode( ', ', $product_names ) ) ); ?> | |
| 303 | - . | |
| 304 | - <a href="<?php echo esc_url_raw( tsdk_utmify( 'https://themeisle.com/blackfriday/', 'bfcm24', 'globalnotice' ) ); ?>" | |
| 305 | - target="_blank"><?php echo esc_html( Loader::$labels['announcements']['learn_more'] ); ?>></a> | |
| 306 | - <span class="themeisle-sale-error"></span> | |
| 307 | - </p> | |
| 308 | - </div> | |
| 309 | - <script type="text/javascript" data-origin="themeisle-sdk"> | |
| 310 | - window.document.addEventListener('DOMContentLoaded', () => { | |
| 311 | - const observer = new MutationObserver((mutationsList, observer) => { | |
| 312 | - for (let mutation of mutationsList) { | |
| 313 | - if (mutation.type === 'childList') { | |
| 314 | - const container = document.querySelector('.themeisle-sale.notice'); | |
| 315 | - const button = container?.querySelector('button'); | |
| 316 | - if (button) { | |
| 317 | - button.addEventListener('click', e => { | |
| 318 | - e.preventDefault(); | |
| 319 | - fetch('<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', { | |
| 320 | - method: 'POST', | |
| 321 | - headers: { | |
| 322 | - 'Content-Type': 'application/x-www-form-urlencoded' | |
| 323 | - }, | |
| 324 | - body: new URLSearchParams({ | |
| 325 | - action: 'themeisle_sdk_dismiss_announcement', | |
| 326 | - nonce: '<?php echo esc_attr( wp_create_nonce( 'dismiss_themeisle_event_notice' ) ); ?>', | |
| 327 | - announcement: container.dataset.announcement | |
| 328 | - }) | |
| 329 | - }) | |
| 330 | - .then(response => response.text()) | |
| 331 | - .then(response => { | |
| 332 | - if (!response?.includes('success')) { | |
| 333 | - document.querySelector('.themeisle-sale-error').innerHTML = response; | |
| 334 | - return; | |
| 335 | - } | |
| 537 | + if ( empty( $config['upgrade_menu_text'] ) || empty( $config['sale_url'] ) ) { | |
| 538 | + return $about_data; | |
| 539 | + } | |
| 336 | 540 | |
| 337 | - document.querySelectorAll('.themeisle-sale.notice').forEach(el => { | |
| 338 | - el.classList.add('hidden'); | |
| 339 | - setTimeout(() => { | |
| 340 | - el.remove(); | |
| 341 | - }, 800); | |
| 342 | - }); | |
| 343 | - }) | |
| 344 | - .catch(error => { | |
| 345 | - console.error('Error:', error); | |
| 346 | - document.querySelector('.themeisle-sale-error').innerHTML = error; | |
| 347 | - }); | |
| 348 | - }); | |
| 349 | - observer.disconnect(); | |
| 350 | - break; | |
| 351 | - } | |
| 352 | - } | |
| 353 | - } | |
| 354 | - }); | |
| 541 | + $about_data['upgrade_text'] = $config['upgrade_menu_text']; | |
| 542 | + $about_data['upgrade_link'] = $config['sale_url']; | |
| 355 | 543 | |
| 356 | - observer.observe(document.body, {childList: true, subtree: true}); | |
| 357 | - }); | |
| 358 | - </script> | |
| 359 | - <?php | |
| 544 | + return $about_data; | |
| 360 | 545 | } |
| 361 | 546 | } |