Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active May 1, 2020 12:58
Show Gist options
  • Save JarrydLong/f8c1f835b29c2e698bd57d4733df748d to your computer and use it in GitHub Desktop.
Save JarrydLong/f8c1f835b29c2e698bd57d4733df748d to your computer and use it in GitHub Desktop.
<?php
/**
* This recipe will track post views for members that have bought a membership.
* Members' views will then be tracked, and if views have exceeded based on their respective
* membership level, they'll be redirected away from the post.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpro_lpv_adjusted() {
global $current_user;
if ( function_exists( 'pmpro_has_membership_access' ) && function_exists( 'pmprolpv_init' ) ) {
/*
If we're viewing a page that the user doesn't have access to...
Could add extra checks here.
*/
if ( pmpro_has_membership_access() ) {
/**
* Filter which post types should be tracked by LPV
*
* @since .4
*/
$pmprolpv_post_types = apply_filters( 'pmprolpv_post_types', array( 'post' ) );
$queried_object = get_queried_object();
if ( empty( $queried_object ) || empty( $queried_object->post_type ) || ! in_array( $queried_object->post_type, $pmprolpv_post_types, true ) ) {
return;
}
$hasaccess = apply_filters( 'pmprolpv_has_membership_access', true, $queried_object );
if ( false === $hasaccess ) {
pmpro_lpv_redirect();
}
// if we're using javascript, just give them access and let JS redirect them.
if ( defined( 'PMPRO_LPV_USE_JAVASCRIPT' ) && PMPRO_LPV_USE_JAVASCRIPT ) {
wp_enqueue_script( 'wp-utils', includes_url( '/js/utils.js' ) );
add_action( 'wp_footer', 'pmpro_lpv_wp_footer' );
add_filter( 'pmpro_has_membership_access_filter', '__return_true' );
return;
}
// PHP is going to handle cookie check and redirect.
$thismonth = date( 'n', current_time( 'timestamp' ) );
$level = pmpro_getMembershipLevelForUser( $current_user->ID );
if ( ! empty( $level->id ) ) {
$level_id = $level->id;
} else {
$level_id = null;
}
// check for past views.
if ( ! empty( $_COOKIE['pmpro_lpv_count'] ) ) {
$month = $thismonth;
$parts = explode( ';', sanitize_text_field( $_COOKIE['pmpro_lpv_count'] ) );
if ( count( $parts ) > 1 ) { // just in case.
$month = $parts[1];
} else { // for one-time cookie format migration.
$parts[0] = '0,0';
}
$limitparts = explode( ',', $parts[0] );
$levellimits = array();
$length = count( $limitparts );
for ( $i = 0; $i < $length; $i++ ) {
if ( $i % 2 === 1 ) {
$levellimits[ $limitparts[ $i -1 ] ] = $limitparts[ $i ];
}
}
if ( $month == $thismonth && array_key_exists( $level_id, $levellimits ) ) {
$count = $levellimits[ $level_id ] + 1; // same month as other views.
$levellimits[ $level_id ]++;
} elseif ( $month == $thismonth ) { // same month, but we haven't ticked yet.
$count = 1;
$levellimits[ $level_id ] = 1;
} else {
$count = 1; // new month.
$levellimits = array();
$levellimits[ $level_id ] = 1;
$month = $thismonth;
}
} else {
// new user.
$count = 1;
$levellimits = array();
$levellimits[ $level_id ] = 1;
$month = $thismonth;
}
// if count is above limit, redirect, otherwise update cookie.
if ( defined( 'PMPRO_LPV_LIMIT' ) && $count > PMPRO_LPV_LIMIT ) {
pmpro_lpv_redirect();
} else {
// give them access and track the view.
add_filter( 'pmpro_has_membership_access_filter', '__return_true' );
if ( defined( 'PMPRO_LPV_LIMIT_PERIOD' ) ) {
switch ( PMPRO_LPV_LIMIT_PERIOD ) {
case 'hour':
$expires = current_time( 'timestamp' ) + HOUR_IN_SECONDS;
break;
case 'day':
$expires = current_time( 'timestamp' ) + DAY_IN_SECONDS;
break;
case 'week':
$expires = current_time( 'timestamp' ) + WEEK_IN_SECONDS;
break;
case 'month':
$expires = current_time( 'timestamp' ) + ( DAY_IN_SECONDS * 30 );
}
} else {
$expires = current_time( 'timestamp' ) + ( DAY_IN_SECONDS * 30 );
}
// put the cookie string back together with updated values.
$cookiestr = '';
foreach ( $levellimits as $curlev => $curviews ) {
$cookiestr .= "$curlev,$curviews";
}
setcookie( 'pmpro_lpv_count', $cookiestr . ';' . $month, $expires, '/' );
}
}
}
}
add_action( 'wp', 'mypmpro_lpv_adjusted' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment