llms_is_post_restricted_by_sitewide_membership( int $post_id, int|null $user_id = null )

Determine if a post should bypass sitewide membership restrictions.


Description Description

If sitewide membership restriction is disabled, this will always return false. This function replaces the now deprecated site_restricted_by_membership() (and has slightly different functionality).


Top ↑

Parameters Parameters

$post_id

(int) (Required) WP Post ID.

$user_id

(int|null) (Optional) WP User ID (will use get_current_user_id() if none supplied). Default null.

Default value: null


Top ↑

Return Return

(bool|int) If the post is not restricted (or there are not sitewide membership restrictions) returns false. If the post is restricted, returns the membership id required.


Top ↑

Source Source

File: includes/functions/llms.functions.access.php

function llms_is_post_restricted_by_sitewide_membership( $post_id, $user_id = null ) {

	$membership_id = absint( get_option( 'lifterlms_membership_required', '' ) );

	// site is restricted to a membership.
	if ( ! empty( $membership_id ) ) {

		$membership = new LLMS_Membership( $membership_id );

		if ( ! $membership || ! is_a( $membership, 'LLMS_Membership' ) ) {
			return false;
		}

		// Restricted contents redirection page id, if any.
		$redirect_page_id = 'page' === $membership->get( 'restriction_redirect_type' ) ? absint( $membership->get( 'redirect_page_id' ) ) : 0;

		/**
		 * Pages that can be bypassed when sitewide restrictions are enabled.
		 */
		$allowed = apply_filters(
			'lifterlms_sitewide_restriction_bypass_ids',
			array_filter(
				array(
					absint( $membership_id ), // the membership page the site is restricted to.
					absint( get_option( 'lifterlms_terms_page_id' ) ), // terms and conditions.
					llms_get_page_id( 'memberships' ), // membership archives.
					llms_get_page_id( 'myaccount' ), // lifterlms account page.
					llms_get_page_id( 'checkout' ), // lifterlms checkout page.
					absint( get_option( 'wp_page_for_privacy_policy' ) ), // wp privacy policy page.
					$redirect_page_id, // Restricted contents redirection page id.
				)
			)
		);

		if ( in_array( $post_id, $allowed, true ) ) {
			return false;
		}

		return $membership_id;

	} else {

		return false;

	}

}


Top ↑

Changelog Changelog

Changelog
Version Description
3.37.10 Do not apply membership restrictions on the page set as membership's restriction redirect page. Exclude the privacy policy from the sitewide restriction. Call in_array() with strict comparison.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.