BlackFriday.php
8 months ago
EidSpecial.php
8 months ago
LoadResourceType.php
8 months ago
Review.php
8 months ago
SummerSale.php
8 months ago
BlackFriday.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Notice Controller class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers\Admin\Notice; |
| 9 | |
| 10 | // Do not allow directly accessing this file. |
| 11 | use RT\ThePostGrid\Helpers\Fns; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 'This script cannot be accessed directly.' ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Notice Controller class. |
| 19 | */ |
| 20 | class BlackFriday { |
| 21 | |
| 22 | /** |
| 23 | * Black friday notice. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | if ( ! Fns::is_black_friday_active() ) { |
| 29 | return; |
| 30 | } |
| 31 | add_action( 'admin_notices', [ __CLASS__, 'render_notice' ], 1 ); |
| 32 | add_action( 'admin_footer', [ __CLASS__, 'admin_footer_scripts' ] ); |
| 33 | add_action( 'wp_ajax_rttpg_dismiss_black_friday_notice', [ __CLASS__, 'dismiss_black_friday_notice' ] ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Black friday notice. |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public static function render_notice() { |
| 42 | $plugin_name = 'The Post Grid'; |
| 43 | $download_link = 'https://www.radiustheme.com/downloads/the-post-grid-pro-for-wordpress/'; |
| 44 | ?> |
| 45 | <div class="notice notice-info is-dismissible tpg-black-friday-notice" data-rttpg-dismissable="rttpg_dismiss_bf_notice" \ |
| 46 | style="padding: 0!important;" |
| 47 | > |
| 48 | <a href="<?php echo esc_url( $download_link ) ?>" style="display: block" target="_blank"> |
| 49 | <img alt="<?php echo esc_attr( $plugin_name ); ?>" |
| 50 | style="width: 100%;display: block;min-height: 30px;object-fit: cover" |
| 51 | src="<?php echo esc_url( rtTPG()->get_assets_uri( 'images/offer/black-friday.webp' ) ); ?>"/> |
| 52 | </a> |
| 53 | </div> |
| 54 | <?php |
| 55 | } |
| 56 | |
| 57 | public static function admin_footer_scripts() { |
| 58 | ?> |
| 59 | <script> |
| 60 | document.addEventListener('DOMContentLoaded', function () { |
| 61 | setTimeout(function () { |
| 62 | const dismissButtons = document.querySelectorAll('div[data-rttpg-dismissable] .notice-dismiss, div[data-rttpg-dismissable] .button-dismiss'); |
| 63 | |
| 64 | dismissButtons.forEach(function (button) { |
| 65 | button.addEventListener('click', function (e) { |
| 66 | e.preventDefault(); |
| 67 | |
| 68 | // Send AJAX request using fetch() |
| 69 | fetch(ajaxurl, { |
| 70 | method: 'POST', |
| 71 | headers: { |
| 72 | 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
| 73 | }, |
| 74 | body: new URLSearchParams({ |
| 75 | 'action': 'rttpg_dismiss_black_friday_notice', |
| 76 | 'nonce': <?php echo wp_json_encode( wp_create_nonce( 'rttpg-dismissible-notice' ) ); ?> |
| 77 | }), |
| 78 | }); |
| 79 | |
| 80 | // Remove the closest notice element |
| 81 | const notice = e.target.closest('.is-dismissible'); |
| 82 | if (notice) { |
| 83 | notice.remove(); |
| 84 | } |
| 85 | }); |
| 86 | }); |
| 87 | }, 1000); |
| 88 | }); |
| 89 | </script> |
| 90 | <?php |
| 91 | } |
| 92 | |
| 93 | public static function dismiss_black_friday_notice() { |
| 94 | if ( ! current_user_can( 'manage_options' ) ) { |
| 95 | wp_send_json_success( new \WP_Error( 'rttpg_block_user_permission', __( 'User permission error', 'the-post-grid' ) ) ); |
| 96 | } |
| 97 | check_ajax_referer( 'rttpg-dismissible-notice', 'nonce' ); |
| 98 | $currentYear = date( 'Y' ); |
| 99 | update_option( 'rttpg_dismiss_bf_notice_' . $currentYear, '1' ); |
| 100 | wp_die(); |
| 101 | } |
| 102 | |
| 103 | } |