Last active
August 5, 2020 15:50
-
-
Save uaibo/a65b47d07e754d1bbb1e2a37ad2d2d5d to your computer and use it in GitHub Desktop.
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
$total_free_reads = 3; | |
$cookie_name = 'article_reads'; | |
function check_update_cookie() { | |
global $cookie_name; | |
if( is_single() ){ | |
$expiry = strtotime('+1 month'); | |
if( ! isset($_COOKIE[$cookie_name]) ){ | |
setcookie($cookie_name, serialize([]), $expiry, '/'); | |
}else{ | |
$article_ids = unserialize($_COOKIE[$cookie_name]); | |
//reading new article? add its ID to the cookie | |
if( ! in_array( get_the_id(), $article_ids ) ){ | |
$article_ids[] = get_the_id(); | |
} | |
setcookie($cookie_name, serialize($article_ids), $expiry, '/'); | |
} | |
} | |
}; | |
function check_remove_block() { | |
global $total_free_reads, $cookie_name; | |
//if cookie is not set or limit not reached, deactivate plugin block | |
if( ! isset($_COOKIE[$cookie_name]) || sizeof(unserialize($_COOKIE[$cookie_name])) < ($total_free_reads-1) ){ | |
//unregister content filter | |
remove_filter('the_content', array('ARU_ReadMoreLogin\ContentLoader', 'FilterContent'), 99); | |
//remove the dead shortcode from content view | |
add_shortcode('rml_read_more', function( $atts, $content = "" ){ | |
return ''; | |
}); | |
} | |
} | |
//only run for guest users | |
if( ! is_user_logged_in() ){ | |
add_action('template_redirect', 'check_update_cookie'); | |
add_action('init', 'check_remove_block'); | |
} | |
//remove cookie if user is logged in | |
if( is_user_logged_in() && isset($_COOKIE[$cookie_name]) ){ | |
setcookie($cookie_name, '', (time() - 3600), '/'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment