admin-notices.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Custom CSS and JS
  4. *
  5. */
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly
  8. }
  9. /**
  10. * CustomCSSandJS_Notices
  11. */
  12. class CustomCSSandJS_Notices {
  13. var $prefix = 'ccj_';
  14. var $activation_time = '';
  15. var $version = '';
  16. var $dismiss_notice = '';
  17. var $expiration_days = 2;
  18. /**
  19. * Constructor
  20. */
  21. public function __construct() {
  22. $this->set_variables();
  23. if ( $this->dismiss_notice == 1 ) {
  24. return;
  25. }
  26. $p = $this->prefix;
  27. add_action( 'admin_notices', array( $this, 'admin_notices' ) );
  28. add_action( 'wp_ajax_'.$p.'_notice_dismiss', array( $this, 'notice_dismiss' ) );
  29. }
  30. /**
  31. * Hooked from 'admin_notices'
  32. */
  33. public function admin_notices() {
  34. $screen = get_current_screen();
  35. if ( !isset($screen->post_type) || $screen->post_type !== 'custom-css-js' )
  36. return;
  37. if ( ! $notice = $this->choose_notice() )
  38. return;
  39. if ( time() - $this->activation_time <= 3600 )
  40. return;
  41. $message = $this->get_message( $notice );
  42. $this->print_message( $notice, $message );
  43. }
  44. /**
  45. * Get the options from the database or set them on install or upgrade
  46. */
  47. public function set_variables() {
  48. $now = time();
  49. $p = $this->prefix;
  50. $this->activation_time = get_option( $p . '_activation_time', '' );
  51. $this->version = get_option( $p.'_version', '' );
  52. $this->dismiss_notice = get_option( $p.'_dismiss_notice', false );
  53. if ( empty( $this->activation_time ) || version_compare( $this->version, CCJ_VERSION, '<' ) ) {
  54. $this->activation_time = $now;
  55. update_option( $p.'_activation_time', $now );
  56. update_option( $p.'_version', CCJ_VERSION );
  57. update_option( $p.'_dismiss_notice', false );
  58. }
  59. }
  60. /**
  61. * Choose which notice to be shown
  62. */
  63. public function choose_notice() {
  64. $now = time();
  65. $days_passed = ceil( ( $now - $this->activation_time ) / 86400 );
  66. switch ( $days_passed ) {
  67. case 0 : return '1_day';
  68. case 1 : return '2_day';
  69. case 2 : break; //return '3_day';
  70. case 3 : break;
  71. case 4 : break;
  72. case 5 : break;
  73. case 6 : break; // return '7_day';
  74. case 7 : break;
  75. case 8 : break;
  76. case 9 : break;
  77. case 10 : break;
  78. case 11 : break; //return '12_day';
  79. }
  80. }
  81. /**
  82. * Get the text of the message
  83. */
  84. public function get_message( $notice ) {
  85. $message = '';
  86. $percentage = '30';
  87. $product_name = 'Simple Custom CSS and JS PRO';
  88. $expiration_date = $this->activation_time + ( $this->expiration_days * 86400 );
  89. $expiration_date = date( get_option( 'date_format') , $expiration_date );
  90. $expiration_period = date('j M', $this->activation_time - 3*86400 ) . ' - ' . date('j M', $this->activation_time + 2*86400 );
  91. if ( $notice == '12_days' ) {
  92. $link = 'https://www.silkypress.com/simple-custom-css-js-pro/?utm_source=wordpress&utm_campaign=ccj_free&utm_medium=banner';
  93. } else {
  94. $link = 'https://www.silkypress.com/simple-custom-css-js-pro/?a=' . $this->convert_numbers_letters( $this->activation_time ) . '&utm_source=wordpress&utm_campaign=ccj_free&utm_medium=banner';
  95. }
  96. $lower_part = sprintf( '<div style="margin-top: 7px;"><a href="%s" target="_blank">%s</a> | <a href="#" class="dismiss_notice" target="_parent">%s</a></div>', $link, __('Get your discount now', 'custom-css-js'), __('Dismiss this notice', 'custom-css-js') );
  97. switch ( $notice ) {
  98. case '1_day' :
  99. $message = '<div>Only between '. $expiration_period .': <b>'.$percentage.'% Off from <a href="'.$link.'" target="_blank">'.$product_name.'</a></b> for our WordPress.org users.</div>' . $lower_part;
  100. break;
  101. case '2_day' :
  102. $message = '<div><b>Limited offer ending today</b>. '.$percentage.'% Off from <a href="'.$link.'" target="_blank">'.$product_name.'</a> for our WordPress.org users. </div>' . $lower_part;
  103. break;
  104. case '3_day' :
  105. $message = '<div><b>Limited offer ending today</b>. '.$percentage.'% Off from '.$product_name.' for our WordPress.org users. </div>' . $lower_part;
  106. break;
  107. case '7_day' :
  108. $message = '';
  109. break;
  110. case '12_day' :
  111. $message = '<div><b>Special Offer</b>: 30% Off from '.$product_name.' for our WordPress.org users.</div>' . $lower_part;
  112. break;
  113. }
  114. return $message;
  115. }
  116. /**
  117. * Print the message
  118. */
  119. public function print_message( $option_name = '', $message = '' ) {
  120. if ( empty( $message ) || empty( $option_name ) )
  121. return;
  122. $p = $this->prefix;
  123. ?>
  124. <style type="text/css">
  125. .<?php echo $p; ?>_note{ color: #bc1117; }
  126. #<?php echo $p; ?>_notice { display: block; padding: }
  127. #<?php echo $p; ?>_notice b { color: #bc1117; }
  128. #<?php echo $p; ?>_notice a { text-decoration: none; font-weight: bold; }
  129. #<?php echo $p; ?>_notice a.dismiss_notice { font-weight: normal; }
  130. </style>
  131. <script type='text/javascript'>
  132. jQuery(function($){
  133. $(document).on( 'click', '.<?php echo $p; ?>_notice .dismiss_notice', function() {
  134. var data = {
  135. action: '<?php echo $p; ?>_notice_dismiss',
  136. option: '<?php echo $option_name; ?>',
  137. nonce: $(this).parent().parent().data('nonce'),
  138. };
  139. $.post(ajaxurl, data, function(response ) {
  140. $('#<?php echo $p; ?>_notice').fadeOut('slow');
  141. });
  142. });
  143. });
  144. </script>
  145. <div id="<?php echo $p; ?>_notice" class="updated notice <?php echo $p; ?>_notice is-dismissible" data-nonce="<?php echo wp_create_nonce( $this->prefix .'_notice'); ?>">
  146. <p><?php echo $message ?></p>
  147. <button type="button" class="notice-dismiss">
  148. <span class="screen-reader-text"><?php _e('Dismiss this notice'); ?></span>
  149. </button>
  150. </div>
  151. <?php
  152. }
  153. function convert_numbers_letters( $text, $from = 'numbers' ) {
  154. $alphabet = str_split('abcdefghij');
  155. $numbers = str_split('0123456789');
  156. if ( $from == 'numbers' ) {
  157. return str_replace( $numbers, $alphabet, $text );
  158. } else {
  159. return str_replace( $alphabet, $numbers, $text );
  160. }
  161. }
  162. /**
  163. * Ajax response for `notice_dismiss` action
  164. */
  165. function notice_dismiss() {
  166. $p = $this->prefix;
  167. check_ajax_referer( $p . '_notice', 'nonce' );
  168. update_option( $p.'_dismiss_notice', 1 );
  169. wp_die();
  170. }
  171. }
  172. return new CustomCSSandJS_Notices();