Skip to content

Commit

Permalink
Add back support for pmprolpv_has_membership_access filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dparker1005 committed Jun 6, 2024
1 parent 9a43abb commit 7285616
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
23 changes: 19 additions & 4 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ function pmprolpv_get_level_limit( $level_id ) {
}

/**
* Check if we want to allow free views for a given post type.
* Check if we want to allow free views for a given post.
*
* @param string $post_type The post type to check.
* @param WP_POST $post The post to check.
* @return bool True if we want to allow free views for this post type, false otherwise.
*/
function pmprolpv_allow_free_views_for_post_type( $post_type ) {
function pmprolpv_allow_free_views_for_post( $post ) {
// If $post is not a WP_Post object, return false.
if ( ! is_a( $post, 'WP_Post' ) ) {
return false;
}

/**
* Filter which post types should be tracked by LPV.
*
Expand All @@ -29,5 +34,15 @@ function pmprolpv_allow_free_views_for_post_type( $post_type ) {
* @param array $allowed_post_types Array of post types to track.
*/
$allowed_post_types = apply_filters( 'pmprolpv_post_types', array( 'post' ) );
return is_string( $post_type ) && in_array( $post_type, $allowed_post_types, true );
if ( empty( $post->post_type ) || ! in_array( $post->post_type, $allowed_post_types, true ) ) {
return false;
}

/**
* Filter whether LPV should allow free views for a given post.
*
* @param bool $allow_free_views True if we want to allow free views for this post type, false otherwise.
* @param WP_POST $post The post to check.
*/
return apply_filters( 'pmprolpv_has_membership_access', true, $post );
}
14 changes: 7 additions & 7 deletions includes/restriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* Give all users access to all posts. LPV will redirect away if the user runs out of free views.
*
* @since TBD
* @since 1.0
*
* @param bool $has_access Whether the user has access to the post.
* @param WP_Post $post The post being checked.
* @return bool $has_access True if the user has access to the post.
*/
function pmprolpv_has_membership_access_filter( $has_access, $post ) {
// Check if we want to allow free views for this post type.
if ( empty( $post->post_type ) || ! pmprolpv_allow_free_views_for_post_type( $post->post_type ) ) {
if ( ! pmprolpv_allow_free_views_for_post( $post ) ) {
return $has_access;
}
return true;
Expand All @@ -21,7 +21,7 @@ function pmprolpv_has_membership_access_filter( $has_access, $post ) {
/**
* Enqueue frontend script to restrict content when needed.
*
* @since TBD
* @since 1.0
*/
function pmprolpv_wp_enqueue_scripts() {
wp_register_script( 'pmprolpv', plugins_url( 'js/pmprolpv.js', PMPROLPV_BASE_FILE ), array( 'jquery' ), PMPROLPV_VERSION );
Expand All @@ -33,7 +33,7 @@ function pmprolpv_wp_enqueue_scripts() {
/**
* Handler for thepmprolpv_get_restriction_js AJAX endpoint.
*
* @since TBD
* @since 1.0
*/
function pmprolpv_get_restriction_js() {
// Check parameters.
Expand All @@ -49,7 +49,7 @@ function pmprolpv_get_restriction_js() {

// Check if we want to allow free views for this post type.
$post = get_post( $post_id );
if ( empty( $post ) || ! pmprolpv_allow_free_views_for_post_type( $post->post_type ) ) {
if ( ! pmprolpv_allow_free_views_for_post( $post ) ) {
wp_send_json_success( 'return;' );
}

Expand Down Expand Up @@ -131,7 +131,7 @@ function pmprolpv_get_restriction_js() {
* Filter the JavaScript to run when LPV grants access to a post.
* For example, this can be used to show a popup or a banner with the remaining view count.
*
* @since TBD
* @since 1.0
*
* @param string $notification_js JavaScript to run when LPV grants access to a post.
* @param int $views_remaining Number of views remaining.
Expand All @@ -151,7 +151,7 @@ function pmprolpv_get_restriction_js() {
* Filter the JavaScript to run when LPV denies access to a post.
* For example, this could be used to blur the page and show a message or redirect.
*
* @since TBD
* @since 1.0
*
* @param string $restriction_js JavaScript to run when LPV denies access to a post.
* @param int $level_views Number of views allowed for the user's level.
Expand Down

0 comments on commit 7285616

Please sign in to comment.