Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active April 23, 2020 20:17
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 wpmudev-sls/5e2ca3e062bfcd8d13858bf42765adad to your computer and use it in GitHub Desktop.
Save wpmudev-sls/5e2ca3e062bfcd8d13858bf42765adad to your computer and use it in GitHub Desktop.
[Hustle Pro] - Add countdown to a slide in
<?php
/**
* Plugin Name: [Hustle Pro] - Add countdown to a slide in
* Plugin URI: https://premium.wpmudev.org/
* Description: Add countdown to a slide in (as of 4.1.1)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/1135022585412927/1172433930968078
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// No need to do anything if the request is via WP-CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Hustle_Slidein_Countdown' ) ) {
class WPMUDEV_Hustle_Slidein_Countdown {
// User defined settings
private $module_id = 10;
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Hustle_Slidein_Countdown();
}
return self::$_instance;
}
private function __construct() {
if ( ! defined( 'Opt_In::VERSION' ) || Opt_In::VERSION < '4.1' ) {
return;
}
$this->init();
}
public function init(){
add_filter( 'hustle_render_module_markup', array( $this, 'wpmudev_hustle_render_module_markup' ), 10, 2 );
}
public function wpmudev_hustle_render_module_markup( $html, $object ){
// Module
$module = $object->module;
$module_id = $module->id;
if( $module->module_type !== 'slidein' && $module_id != $this->module_id ){
return $html;
}
// Settings
$content = $module->content;
$design = $module->design;
$settings = $module->settings;
$trigger = $module->triggers;
$module_type = $module->module_type;
// Timer
$markup = '';
// Text direction based on slidein's position
if( in_array( $settings->display_position, array( 'ne', 'e', 'se' ) ) ){
$style = 'text-align: right; padding-right: 9px;';
}elseif( in_array( $settings->display_position, array( 's', 'n' ) ) ){
$style = 'text-align: center;';
}else{
$style = 'text-align: left; padding-left: 9px;';
}
$markup = '<p class="hustle-close-timer" style="' . $style . ' line-height:30px; margin: 0; font-size: 12px;">' . __( 'This module will auto close in', 'hustle' ) . ' <span></span></p>';
$html = str_replace( '</div></div></div>', '</div></div>' . $markup . '</div>', $html );
add_action( 'wp_footer', function() use ( $module_id ) { ?>
<script type="text/javascript">
(function($){
$( document ).on( 'hustle:module:displayed', function(){
// Slide-in
let id = <?php echo $module_id; ?>;
let module = $( '.hustle-slidein[data-id="' + id + '"]' );
// Check if slider exists on page
if( ! module.length ) return;
// Init countdown
let c = module.attr( 'data-close-delay' ) / 1000;
let interval = setInterval( function() {
c--;
module.find( '.hustle-close-timer > span' ).html( c );
if ( ! c ) clearInterval(interval);
}, 1000 );
});
})(jQuery);
</script>
<?php }, 100 );
return $html;
}
}
add_action( 'plugins_loaded', function(){
return WPMUDEV_Hustle_Slidein_Countdown::get_instance();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment