Skip to content

Instantly share code, notes, and snippets.

@glaubersilva
Last active April 10, 2020 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaubersilva/bb89d29e0d9c36499a6865303ba057ce to your computer and use it in GitHub Desktop.
Save glaubersilva/bb89d29e0d9c36499a6865303ba057ce to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Hustle] - Shows popup in a specifics time range.
* Plugin URI: https://premium.wpmudev.org/
* Description: Show pop-up every day from 20:00 to 8:00 the second day. To activing this to a specific pop-up go: Hustle > Pop-ups > YOUR POP-UP > Behavior > Pop-up Trigger > Click > Active the option "Click on existing element" > and in the input "CSS selector" insert: #time_range_popup_opener
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
* Adptation of: https://gist.github.com/wpmudev-sls/03fd7d24cfe6de0ee2e516722c1018e6
*
* @package Hustle_Popup_By_Time_Range
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WPMUDEV_Hustle_Popup_By_Time_Range' ) ) {
/**
* Main class of the plugin.
*/
class WPMUDEV_Hustle_Popup_By_Time_Range {
/**
* Start time of the range - eg. "10:00 AM".
*
* @var Property
*/
private $start_time = '08:00 PM';
/**
* End time of the range - eg. "10:00 PM".
*
* @var Property
*/
private $end_time = '08:00 AM';
/**
* Enter your popup opener id or class - eg. "#popup_opener".
*
* @var Property
*/
private $opener_id = '#time_range_popup_opener';
/**
* Stores the main instance of the class.
*
* @var Property
*/
private static $instance = null;
/**
* Returns the main instance of the class.
*
* @return WPMUDEV_Hustle_Popup_By_Time_Range
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new WPMUDEV_Hustle_Popup_By_Time_Range();
}
return self::$instance;
}
/**
* Constructor of the class.
*/
private function __construct() {
$this->init();
}
/**
* Loads the functions of the class in the apropriete hooks.
*/
public function init() {
add_action( 'wp_footer', array( $this, 'hustle_popup_opener_add_to_footer' ) );
}
/**
* Checks if the $id is a class or id and returns a proper value.
*
* @param string $id The ID (or class) of the invisible link that trigger the popup display.
*/
public function hustle_popup_opener_trigger( $id ) {
$selector_name = substr( trim( $id ), 1 );
$ret = '';
if ( strpos( $id, '#' ) !== false ) {
$ret = 'id="' . $selector_name . '"';
} elseif ( strpos( $id, '.' ) !== false ) {
$ret = 'class="' . $selector_name . '"';
}
return $ret;
}
/**
* Inserts the js to control the popup behavior on footer.
*/
public function hustle_popup_opener_add_to_footer() {
?>
<a style="display:none;" href="#" <?php echo $this->hustle_popup_opener_trigger( $this->opener_id ); // phpcs:ignore ?>></a>
<script>
(function($){
$( document ).ready(function() {
$( window ).load(function() {
var startTime = '<?php echo $this->start_time ; // phpcs:ignore ?>';
var endTime = '<?php echo $this->end_time ; // phpcs:ignore ?>';
var curr_time = get_current_time();
if ( get24Hr( curr_time ) >= get24Hr( startTime ) && get24Hr( curr_time ) <= get24Hr(endTime) ) {
//in between these two times
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
//console.log(time);
$('<?php echo $this->opener_id; // phpcs:ignore ?>').trigger('click');
}
});
// Return time in 24 hours format
function get24Hr( time ){
var hours = Number(time.match(/^(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var minutes = Number(time.match(/:(\d+)/)[1]);
hours = hours*100+minutes;
//console.log(time +" - "+hours);
return hours;
}
// Return current time in 12 hours format
function get_current_time() {
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if ( minutes < 10 ) minutes = "0" + minutes;
var suffix = "AM";
if ( hours >= 12 ) {
suffix = "PM";
hours = hours - 12;
}
if ( hours == 0 ) {
hours = 12;
}
var current_time = hours + ":" + minutes + " " + suffix;
return current_time;
}
});
})(jQuery);
</script>
<?php
}
}
add_action(
'plugins_loaded',
function() {
return WPMUDEV_Hustle_Popup_By_Time_Range::get_instance();
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment