Jetpack_Holiday_Snow_OptIn

Jetpack_Holiday_Snow_OptIn class.

Defined (1)

The class is defined in the following location(s).

/jetpack-holiday-snow-opt-in.php  
  1. class Jetpack_Holiday_Snow_OptIn { 
  2.  
  3. protected $version = '0.1.5'; 
  4. protected $plugin_slug = 'jetpack-holiday-snow-opt-in'; 
  5. protected static $instance = null; 
  6. protected $plugin_screen_hook_suffix = null; 
  7. protected $plugin_path = null; 
  8. public $snowing; 
  9.  
  10. /** 
  11. * Initialize the plugin by setting localization, filters, and administration functions. 
  12. * @since 0.1.1 
  13. */ 
  14. private function __construct() { 
  15.  
  16. // Get plugin path. 
  17. $this->plugin_path = dirname( plugin_dir_path( __FILE__ ) ); 
  18.  
  19. // Initialize snow status. 
  20. $this->snowing = false; 
  21. $this->jetpack = false; 
  22.  
  23. add_action( 'plugins_loaded', array( $this, 'jetpack_active_check' ), 15 ); 
  24.  
  25. // Load the plugin if we're in season. 
  26. add_action( 'init', array( $this, 'run_if_in_season' ), 1 ); 
  27.  
  28. // Check for snow cookies (yum!) or snow control requests. 
  29. add_action( 'init', array( $this, 'process_snow_opt_in' ), 2 ); 
  30.  
  31. /** 
  32. * Return an instance of this class. 
  33. * @since 0.1.1 
  34. * @return object A single instance of this class. 
  35. */ 
  36. public static function get_instance() { 
  37.  
  38. // If the single instance hasn't been set, set it now. 
  39. if ( null === self::$instance ) { 
  40. self::$instance = new self; 
  41.  
  42. return self::$instance; 
  43.  
  44. public function jetpack_active_check() { 
  45. $this->jetpack = false; 
  46.  
  47. // Confirm Jetpack is loaded and the holiday-snow.php file is loaded. 
  48. if ( class_exists( 'Jetpack' ) && function_exists( 'jetpack_is_holiday_snow_season' ) ) { 
  49. $this->jetpack = true; 
  50. return $this->jetpack; 
  51.  
  52. return $this->jetpack; 
  53.  
  54. public function run_if_in_season() { 
  55. if ( ! $this->jetpack ) { 
  56. return; 
  57.  
  58. if ( ! jetpack_is_holiday_snow_season() ) { 
  59. return; 
  60.  
  61. // Add a filter so we can control the snow. 
  62. add_filter( 'jetpack_holiday_chance_of_snow', array( $this, 'should_it_snow' ) ); 
  63.  
  64. // Add our snow control and styles. 
  65. add_action( 'wp_head', array( $this, 'add_snow_css' ) ); 
  66. add_action( 'wp_footer', array( $this, 'add_snow_opt_in' ) ); 
  67.  
  68. public function should_it_snow( $snow ) { 
  69. return $this->snowing; 
  70.  
  71. public function add_snow_css() { 
  72. // Check if the snow option is enabled, if not, bail. 
  73. if ( ! get_option( jetpack_holiday_snow_option_name() ) ) { 
  74. return; 
  75.  
  76. ?> 
  77. <style type="text/css"> 
  78. #jetpack-holiday-snow-opt-in { 
  79. position: absolute; 
  80. z-index: 9999; 
  81. width: 30px; 
  82. height: 30px; 
  83. top: 0; 
  84. right: 10%; 
  85. background-color: #000000; 
  86. background-color: rgba(0, 0, 0, 0.7); 
  87. border-bottom-right-radius: 5px; 
  88. border-bottom-left-radius: 5px; 
  89. cursor: hand; 
  90. #jetpack-holiday-snow-opt-in * { 
  91. color: #fff; 
  92. #jetpack-holiday-snow-opt-in a { 
  93. display: block; 
  94. padding: 2px 7px; 
  95. text-decoration: none; 
  96. body.admin-bar #jetpack-holiday-snow-opt-in { 
  97. top: 28px; 
  98. </style> 
  99. <?php 
  100.  
  101. public function add_snow_opt_in() { 
  102. // Check if the snow option is enabled, if not, bail. 
  103. if ( ! get_option( jetpack_holiday_snow_option_name() ) ) { 
  104. return; 
  105.  
  106. // Change our link depending on the weather. 
  107. if ( $this->snowing ) { 
  108. $show_snow = '0'; 
  109. $title = __( 'Click to stop the snow', 'jetpack-holiday-snow-opt-in' ); 
  110. } else { 
  111. $show_snow = '1'; 
  112. $title = __( 'Click here to make it snow!', 'jetpack-holiday-snow-opt-in' ); 
  113.  
  114. echo '<div id="jetpack-holiday-snow-opt-in">'; 
  115. echo '<a href="' . esc_url( add_query_arg( 'show_snow', $show_snow ) ) . '" title="' . esc_attr( $title ) . '"><span>*</span></a>'; 
  116. echo '</div>'; 
  117.  
  118. public function has_snow_cookie() { 
  119. if ( isset( $_COOKIE['show_me_the_snow'] ) // Input var okay. 
  120. && 1 === absint( $_COOKIE['show_me_the_snow'] ) ) { 
  121. return true; 
  122.  
  123. return false; 
  124.  
  125. public function process_snow_opt_in() { 
  126. if ( ! $this->jetpack ) { 
  127. return; 
  128.  
  129. // Check if the snow option is enabled, if not, bail. 
  130. if ( ! get_option( jetpack_holiday_snow_option_name() ) || ! jetpack_is_holiday_snow_season() ) { 
  131. return; 
  132.  
  133. // If we want snow and haven't asked it to change. 
  134. if ( $this->has_snow_cookie() && ! isset( $_GET['show_snow'] ) ) { // Input var okay. 
  135. $this->snowing = true; 
  136. return; 
  137.  
  138. // If we haven't yet asked for snow, and haven't asked it to change. 
  139. if ( ! $this->has_snow_cookie() && ! isset( $_GET['show_snow'] ) ) { // Input var okay. 
  140. $this->snowing = false; 
  141. return; 
  142.  
  143. // Only continue if we're changing snow states. 
  144. $show_snow = absint( $_GET['show_snow'] ); // Input var okay. 
  145.  
  146. // If user wants snow, set the cookie saying so 
  147. // If not, remove the cookie. 
  148. if ( 1 === $show_snow ) { 
  149. setcookie( 'show_me_the_snow', '1', time() + 3600 * 24 * 30, COOKIEPATH, COOKIE_DOMAIN, false ); 
  150. $this->snowing = true; 
  151. } else { 
  152. setcookie( 'show_me_the_snow', '1', time() - 3600 * 24 * 30, COOKIEPATH, COOKIE_DOMAIN, false ); 
  153. $this->snowing = false; 
  154. } // end class Jetpack_Holiday_Snow_OptIn