Last active
August 24, 2017 16:46
-
-
Save wpmudev-sls/c98f2fa4cc238a755082481f179d08b5 to your computer and use it in GitHub Desktop.
[Membership] - Dripped memberships countdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: [Membership] - Dripped memberships countdown | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Adds a countdown for dripped memberships | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'MS_Dripped_Countdown' ) ) { | |
class MS_Dripped_Countdown | |
{ | |
private static $_instance = null; | |
public static function get_instance() | |
{ | |
if (is_null(self::$_instance)) { | |
self::$_instance = new MS_Dripped_Countdown(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() | |
{ | |
add_filter( 'query_vars', array( $this, 'query_vars' ), 10 ); | |
add_filter( 'ms_model_settings_get_protection_message', array( $this, 'inject_countdown' ), 10, 3 ); | |
add_action( 'wp_head', array( $this, 'countdown_style' ), 10 ); | |
} | |
public function query_vars( $query_vars ){ | |
$query_vars[] = 'redirect_to'; | |
return $query_vars; | |
} | |
public function inject_countdown( $msg, $type, $MS_Settings_Model ){ | |
$page_url = get_query_var( 'redirect_to' ); | |
if( $post_id = url_to_postid( $page_url ) ){ | |
$All_Memberships = MS_Model_Membership::get_memberships(); | |
foreach ( $All_Memberships as $membership ) { | |
if( $membership->is_dripped() ){ | |
foreach ( $membership->rule_values as $post_type => $protected_items_array ) { | |
foreach( $protected_items_array as $protected_items ){ | |
if( in_array( $post_id, $protected_items ) ){ | |
$MS_Rule = new MS_Rule( $membership->id ); | |
$avail_date = $MS_Rule->get_dripped_avail_date( $post_id ) . ' 00:00:00'; | |
//$avail_date = strtotime( $avail_date ); | |
$msg = self::show_countdown( $avail_date ); | |
} | |
} | |
} | |
} | |
} | |
} | |
return $msg; | |
} | |
public static function show_countdown( $avail_date ){ | |
ob_start(); | |
?> | |
<h1><?php _e( 'Content will be available in:' ) ?></h1> | |
<div id="wpmudev-countdown-wrap"> | |
<div> | |
<span class="days"></span> | |
<div class="smalltext">Days</div> | |
</div> | |
<div> | |
<span class="hours"></span> | |
<div class="smalltext">Hours</div> | |
</div> | |
<div> | |
<span class="minutes"></span> | |
<div class="smalltext">Minutes</div> | |
</div> | |
<div> | |
<span class="seconds"></span> | |
<div class="smalltext">Seconds</div> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
function getTimeRemaining(endtime) { | |
var t = Date.parse(endtime) - Date.parse(new Date()); | |
var seconds = Math.floor((t / 1000) % 60); | |
var minutes = Math.floor((t / 1000 / 60) % 60); | |
var hours = Math.floor((t / (1000 * 60 * 60)) % 24); | |
var days = Math.floor(t / (1000 * 60 * 60 * 24)); | |
return { | |
'total': t, | |
'days': days, | |
'hours': hours, | |
'minutes': minutes, | |
'seconds': seconds | |
}; | |
} | |
function initializeClock(id, endtime) { | |
var clock = document.getElementById(id); | |
var daysSpan = clock.querySelector('.days'); | |
var hoursSpan = clock.querySelector('.hours'); | |
var minutesSpan = clock.querySelector('.minutes'); | |
var secondsSpan = clock.querySelector('.seconds'); | |
function updateClock() { | |
var t = getTimeRemaining(endtime); | |
daysSpan.innerHTML = t.days; | |
hoursSpan.innerHTML = ('0' + t.hours).slice(-2); | |
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); | |
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); | |
if (t.total <= 0) { | |
clearInterval(timeinterval); | |
} | |
} | |
updateClock(); | |
var timeinterval = setInterval(updateClock, 1000); | |
} | |
var deadline = new Date( Date.parse('<?php echo $avail_date; ?>' ) + 1 * 24 * 60 * 60 * 1000 ); | |
initializeClock('wpmudev-countdown-wrap', deadline); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
return ob_get_clean(); | |
} | |
public function countdown_style(){ | |
global $post; | |
//if( ! $post instanceof WP_Post || ! MS_Model_Pages::is_membership_page( $post->ID, MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT ) ){ | |
if( ! $post instanceof WP_Post || MS_Model_Pages::get_page_type( $post->ID ) != MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT ){ | |
return; | |
} | |
?> | |
<style type="text/css"> | |
#wpmudev-countdown-wrap{ | |
font-family: sans-serif; | |
color: #fff; | |
display: inline-block; | |
font-weight: 100; | |
text-align: center; | |
font-size: 30px; | |
} | |
#wpmudev-countdown-wrap > div{ | |
padding: 10px; | |
border-radius: 3px; | |
background: #219cd8; | |
display: inline-block; | |
} | |
#wpmudev-countdown-wrap div > span{ | |
padding: 15px; | |
border-radius: 3px; | |
background: #5eb1e1; | |
display: inline-block; | |
} | |
#wpmudev-countdown-wrap .smalltext{ | |
padding-top: 5px; | |
font-size: 16px; | |
} | |
</style> | |
<?php | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['MS_Dripped_Countdown'] = MS_Dripped_Countdown::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment