Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greenhornet79/2ddcd419ace8ecd4f4e423b5872b23fe to your computer and use it in GitHub Desktop.
Save greenhornet79/2ddcd419ace8ecd4f4e423b5872b23fe to your computer and use it in GitHub Desktop.
<?php
// leaky paywall restriction functionality for custom fields
function is_restricted_zeen101_post( $post_id ) {
if ( current_user_can( 'edit_posts' ) ) { // show content to contributors and up
return false;
}
$settings = get_leaky_paywall_settings();
$site = '';
$level_ids = leaky_paywall_subscriber_current_level_ids();
$restrictions = leaky_paywall_subscriber_restrictions();
foreach( $restrictions as $key => $restriction ) {
if ( is_singular( $restriction['post_type'] ) ) {
if ( 0 <= $restriction['allowed_value'] ) {
$post_type_id = $key;
$restricted_post_type = $restriction['post_type'];
break;
} else if ( -1 == $restriction['allowed_value'] ) {
return false; // content is not restricted because user has unlimted access to this post type
}
}
}
switch ( $settings['cookie_expiration_interval'] ) {
case 'hour':
$multiplier = 60 * 60; //seconds in an hour
break;
case 'day':
$multiplier = 60 * 60 * 24; //seconds in a day
break;
case 'week':
$multiplier = 60 * 60 * 24 * 7; //seconds in a week
break;
case 'month':
$multiplier = 60 * 60 * 24 * 7 * 4; //seconds in a month (4 weeks)
break;
case 'year':
$multiplier = 60 * 60 * 24 * 7 * 52; //seconds in a year (52 weeks)
break;
}
$expiration = time() + ( $settings['cookie_expiration'] * $multiplier );
if ( !empty( $_COOKIE['issuem_lp' . $site] ) )
$available_content = maybe_unserialize( stripslashes( $_COOKIE['issuem_lp' . $site] ) );
if ( empty( $available_content[$restricted_post_type] ) )
$available_content[$restricted_post_type] = array();
foreach ( $available_content[$restricted_post_type] as $key => $restriction ) {
if ( time() > $restriction || 7200 > $restriction ) {
//this post view has expired
//Or it is very old and based on the post ID rather than the expiration time
unset( $available_content[$restricted_post_type][$key] );
}
}
if( -1 != $restrictions[$post_type_id]['allowed_value'] ) { //-1 means unlimited
if ( $restrictions[$post_type_id]['allowed_value'] > count( $available_content[$restricted_post_type] ) ) {
if ( !array_key_exists( $post_id, $available_content[$restricted_post_type] ) ) {
$available_content[$restricted_post_type][$post_id] = $expiration;
}
} else {
if ( !array_key_exists( $post_id, $available_content[$restricted_post_type] ) ) {
return true; // content is restricted for this post
}
}
}
return false; // content is not restricted for this post
}
@celsowhite
Copy link

Thanks. I think there is an error in the code. After line 57 I had to put:

$available_content = json_decode($available_content, true);

$available_content would otherwise be stored as a string and not able to access its array elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment