Dismiss.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PriyoMukul\WPNotice; |
| 4 | |
| 5 | use PriyoMukul\WPNotice\Utils\Base; |
| 6 | use PriyoMukul\WPNotice\Utils\Helper; |
| 7 | use PriyoMukul\WPNotice\Utils\Storage; |
| 8 | |
| 9 | class Dismiss extends Base { |
| 10 | use Helper; |
| 11 | |
| 12 | private $id; |
| 13 | private $scope = 'user'; |
| 14 | private $app = null; |
| 15 | private $hook = null; |
| 16 | |
| 17 | public function __construct( $id, $options, $app ){ |
| 18 | $this->id = $id; |
| 19 | $this->app = $app; |
| 20 | |
| 21 | if( ! empty( $options ) ) { |
| 22 | foreach( $options as $key => $_value ) { |
| 23 | $this->{$key} = $_value; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | $this->hook = $this->app->app .'_wpnotice_dismiss_notice'; |
| 28 | |
| 29 | add_action( 'wp_ajax_'. $this->hook, [ $this, 'ajax_maybe_dismiss_notice' ] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Print the script for dismissing the notice. |
| 34 | * |
| 35 | * @access public |
| 36 | * @since 1.0 |
| 37 | * @return void |
| 38 | */ |
| 39 | public function print_script() { |
| 40 | $nonce = wp_create_nonce( 'wpnotice_dismiss_notice_' . $this->id ); |
| 41 | $_id = '#wpnotice-' . esc_attr( $this->app->app ) . '-' . esc_attr( $this->id ); |
| 42 | ?> |
| 43 | <script> |
| 44 | window.addEventListener( 'load', function() { |
| 45 | var dismissBtn = document.querySelector( '<?php echo $_id ?> .notice-dismiss' ); |
| 46 | var extraDismissBtn = document.querySelectorAll( '<?php echo $_id ?> .dismiss-btn' ); |
| 47 | |
| 48 | function wpNoticeDismissFunc( event ) { |
| 49 | event.preventDefault(); |
| 50 | |
| 51 | var httpRequest = new XMLHttpRequest(), |
| 52 | postData = '', |
| 53 | dismiss = event.target.dataset?.hasOwnProperty('dismiss') && event.target.dataset.dismiss || false, |
| 54 | later = event.target.dataset?.hasOwnProperty('later') && event.target.dataset.later || false; |
| 55 | |
| 56 | if( dismiss || later ) { |
| 57 | jQuery(event.target.offsetParent).slideUp(200); |
| 58 | } |
| 59 | |
| 60 | // Data has to be formatted as a string here. |
| 61 | postData += 'id=<?php echo esc_attr( rawurlencode( $this->id ) ); ?>'; |
| 62 | postData += '&action=<?php echo esc_attr( $this->hook ); ?>'; |
| 63 | if( dismiss ) { |
| 64 | postData += '&dismiss=' + dismiss; |
| 65 | } |
| 66 | if( later ) { |
| 67 | postData += '&later=' + later; |
| 68 | } |
| 69 | |
| 70 | postData += '&nonce=<?php echo esc_html( $nonce ); ?>'; |
| 71 | |
| 72 | httpRequest.open( 'POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>' ); |
| 73 | httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); |
| 74 | httpRequest.send( postData ); |
| 75 | } |
| 76 | |
| 77 | // Add an event listener to the dismiss button. |
| 78 | dismissBtn && dismissBtn.addEventListener( 'click', wpNoticeDismissFunc); |
| 79 | if( extraDismissBtn.length > 0 ) { |
| 80 | extraDismissBtn.forEach( btn => btn.addEventListener( 'click', wpNoticeDismissFunc)) |
| 81 | } |
| 82 | }); |
| 83 | </script> |
| 84 | <?php |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Run check to see if we need to dismiss the notice. |
| 90 | * If all tests are successful then call the dismiss_notice() method. |
| 91 | * |
| 92 | * @access public |
| 93 | * @since 1.0 |
| 94 | * @return void |
| 95 | */ |
| 96 | public function ajax_maybe_dismiss_notice() { |
| 97 | // Sanity check: Early exit if we're not on a wptrt_dismiss_notice action. |
| 98 | if ( ! isset( $_POST['action'] ) || $this->hook !== $_POST['action'] ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Sanity check: Early exit if the ID of the notice is not the one from this object. |
| 103 | if ( ! isset( $_POST['id'] ) || $this->id !== $_POST['id'] ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | // Security check: Make sure nonce is OK. |
| 108 | check_ajax_referer( 'wpnotice_dismiss_notice_' . $this->id, 'nonce', true ); |
| 109 | |
| 110 | if( isset( $_POST['later'] ) ) { |
| 111 | $_recurrence = intval( $this->recurrence ) || 15; |
| 112 | $_queue = $this->app->storage()->get(); |
| 113 | $_queue[ $this->id ]['start'] = $this->strtotime( "+$_recurrence days" ); |
| 114 | $_queue[ $this->id ]['expire'] = $this->strtotime( "+". ($_recurrence + 3) ." days" ); |
| 115 | $this->app->storage()->save( $_queue ); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | // If we got this far, we need to dismiss the notice. |
| 120 | $this->dismiss_notice(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Actually dismisses the notice. |
| 125 | * |
| 126 | * @access private |
| 127 | * @since 1.0 |
| 128 | * @return void |
| 129 | */ |
| 130 | public function dismiss_notice() { |
| 131 | if ( 'user' === $this->scope ) { |
| 132 | return $this->app->storage()->save_meta( $this->id ); |
| 133 | } |
| 134 | |
| 135 | $_key = $this->app->app . '_' . $this->id . '_notice_dismissed'; |
| 136 | return $this->app->storage()->save( $_key ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Check if is dismissed or not |
| 141 | * @return boolean |
| 142 | */ |
| 143 | public function is_dismissed(){ |
| 144 | if ( 'user' === $this->scope ) { |
| 145 | return $this->app->storage()->get_meta( $this->id ); |
| 146 | } |
| 147 | |
| 148 | $_key = $this->app->app . '_' . $this->id . '_notice_dismissed'; |
| 149 | return $this->app->storage()->get( $_key ); |
| 150 | } |
| 151 | } |