Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content filtering conditions #1539

Merged
merged 12 commits into from
Feb 22, 2021
7 changes: 4 additions & 3 deletions includes/class.llms.post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package LifterLMS\Classes
*
* @since 1.0.0
* @version 4.5.1
* @version [version]
*/

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -344,6 +344,7 @@ public static function register_post_type( $name, $data ) {
* @since 3.33.0 `llms_question` post type is not publicly queryable anymore.
* @since 3.37.12 Added 'revisions' support to course, lesson, and llms_mebership post types.
* @since 4.5.1 Removed "excerpt" support for the course post type.
* @since [version] Add "llms-sales-page" feature to course and membership post types.
*
* @return void
*/
Expand Down Expand Up @@ -385,7 +386,7 @@ public static function register_post_types() {
'feeds' => true,
),
'query_var' => true,
'supports' => array( 'title', 'author', 'editor', 'thumbnail', 'comments', 'custom-fields', 'page-attributes', 'revisions', 'llms-clone-post', 'llms-export-post' ),
'supports' => array( 'title', 'author', 'editor', 'thumbnail', 'comments', 'custom-fields', 'page-attributes', 'revisions', 'llms-clone-post', 'llms-export-post', 'llms-sales-page' ),
'has_archive' => ( $catalog_id && get_page( $catalog_id ) ) ? get_page_uri( $catalog_id ) : _x( 'courses', 'course archive url slug', 'lifterlms' ),
'show_in_nav_menus' => true,
'menu_position' => 52,
Expand Down Expand Up @@ -580,7 +581,7 @@ public static function register_post_types() {
'feeds' => true,
),
'query_var' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'custom-fields', 'page-attributes', 'revisions' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'custom-fields', 'page-attributes', 'revisions', 'llms-sales-page' ),
'has_archive' => ( $membership_page_id && get_page( $membership_page_id ) ) ? get_page_uri( $membership_page_id ) : _x( 'memberships', 'membership archive url slug', 'lifterlms' ),
'show_in_nav_menus' => true,
'menu_position' => 52,
Expand Down
152 changes: 99 additions & 53 deletions includes/functions/llms-functions-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package LifterLMS/Functions
*
* @since 3.25.1
* @version 3.25.2
* @version [version]
*/

defined( 'ABSPATH' ) || exit;
Expand All @@ -15,83 +15,129 @@
/**
* Post Template Include
*
* Appends LLMS content above and below post content.
* Adds LifterLMS template content before and after the post's default content.
*
* @since 1.0.0
* @since 3.25.2 Unknown.
* @since [version] TODO.
*
* @param string $content WP_Post post_content.
* @return string
*/
function llms_get_post_content( $content ) {

global $post;
if ( ! $post instanceof WP_Post ) {
if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, array( 'course', 'llms_membership', 'lesson', 'llms_quiz' ), true ) ) {
return $content;
thomasplevy marked this conversation as resolved.
Show resolved Hide resolved
}

$page_restricted = llms_page_restricted( $post->ID );
$before = '';
$template_before = '';
$after = '';
$template_after = '';
$restrictions = llms_page_restricted( $post->ID );
$post_type = str_replace( 'llms_', '', $post->post_type );
$template_before = 'single-' . $post_type . '-before';
$template_after = 'single-' . $post_type . '-after';

if ( 'course' === $post->post_type || 'llms_membership' === $post->post_type ) {

$sales_page = get_post_meta( $post->ID, '_llms_sales_page_content_type', true );

if ( $page_restricted['is_restricted'] && ( '' === $sales_page || 'content' === $sales_page ) ) {

add_filter( 'the_excerpt', array( $GLOBALS['wp_embed'], 'autoembed' ), 9 );
if ( $post->post_excerpt ) {
$content = llms_get_excerpt( $post->ID );
}
}

$template_name = str_replace( 'llms_', '', $post->post_type );
$template_before = llms_get_template_part_contents( 'content', 'single-' . $template_name . '-before' );
$template_after = llms_get_template_part_contents( 'content', 'single-' . $template_name . '-after' );

} elseif ( 'lesson' === $post->post_type ) {

if ( $page_restricted['is_restricted'] ) {
if ( $restrictions['is_restricted'] ) {
$content = llms_get_post_sales_page_content( $post, $content );
if ( in_array( $post->post_type, array( 'lesson', 'llms_quiz' ), true ) ) {
$content = '';
$template_before = llms_get_template_part_contents( 'content', 'no-access-before' );
$template_after = llms_get_template_part_contents( 'content', 'no-access-after' );
} else {
$template_before = llms_get_template_part_contents( 'content', 'single-lesson-before' );
$template_after = llms_get_template_part_contents( 'content', 'single-lesson-after' );
$template_before = 'no-access-before';
$template_after = 'no-access-after';
}
} elseif ( 'llms_quiz' === $post->post_type ) {

$template_before = llms_get_template_part_contents( 'content', 'single-quiz-before' );
$template_after = llms_get_template_part_contents( 'content', 'single-quiz-after' );

}

if ( $template_before ) {
ob_start();
load_template( $template_before, false );
$before = ob_get_clean();
}
ob_start();
load_template( llms_get_template_part_contents( 'content', $template_before ), false );
$before = ob_get_clean();

if ( $template_after ) {
ob_start();
load_template( $template_after, false );
$after = ob_get_clean();
}
ob_start();
load_template( llms_get_template_part_contents( 'content', $template_after ), false );
$after = ob_get_clean();

/**
* Filter the post_content of a LifterLMS post type.
*
* @since Unknown.
* @since Unknown
*
* @param string $content Post content.
* @param WP_Post $post Post object.
* @param array $page_restricted Result from `llms_page_restricted()` for the current post.
* @param string $content Post content.
* @param WP_Post $post Post object.
* @param array $restrictions Result from `llms_page_restricted()` for the current post.
*/
return apply_filters( 'llms_get_post_content', do_shortcode( $before . $content . $after ), $post, $page_restricted );
return apply_filters( 'llms_get_post_content', do_shortcode( $before . $content . $after ), $post, $restrictions );

}
}

/**
* Retrieve the sales page content for a course or membership
*
* By default only courses and memberships support sales pages, the meta property
* must be set to `content` or an empty string, and the post must have a `post_excerpt`
* property value.
*
* @since [version]
*
* @param WP_Post $post The post object.
* @param string $default Optional. Default content to use when no override content can be found.
* @return string
*/
function llms_get_post_sales_page_content( $post, $default = '' ) {

$content = $default;

if ( post_type_supports( $post->post_type, 'llms-sales-page' ) ) {
$sales_page = get_post_meta( $post->ID, '_llms_sales_page_content_type', true );
if ( $post->post_excerpt && ( '' === $sales_page || 'content' === $sales_page ) ) {
add_filter( 'the_excerpt', array( $GLOBALS['wp_embed'], 'autoembed' ), 9 );
$content = llms_get_excerpt( $post->ID );
}
}

/**
* Filters the HTML content of a LifterLMS post type's sales page content
*
* @since [version]
*
* @param string $content HTML content of the sales page.
* @param WP_Post $content Post object.
* @param string $default Default content used when no override content can be found.
*/
return apply_filters( 'llms_post_sales_page_content', $content, $post, $default );

}

/**
* Initialize LifterLMS post type content filters
*
* This method is used to determine whether or `llms_get_post_content()` should automatically
* be added as a filter callback for the WP core `the_content` filter.
*
* When working with posts on the admin panel (during course building, importing) we don't want
* other plugins that may desire running `apply_filters( 'the_content', $content )` to apply our
* plugin's filters.
*
* Additionally, we don't want to return template information when processing REST requests.
*
* @since [version]
*
* @return boolean Returns `true` if content filters are added and `false` if not.
*/
function llms_post_content_init() {

// Don't filter any requests on the admin panel or when processing REST requests.
$should_filter = ( ! is_admin() && ! llms_is_rest() );

/**
* Filters whether or not LifterLMS content filters should be applied.
*
* @since [version]
*
* @param boolean $should_filter Whether or not to filter the content.
*/
if ( apply_filters( 'llms_should_filter_post_content', $should_filter ) ) {
return add_filter( 'the_content', 'llms_get_post_content' );
}

return false;

}
add_filter( 'the_content', 'llms_get_post_content' );
add_action( 'init', 'llms_post_content_init' );
thomasplevy marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 12 additions & 9 deletions templates/content-no-access-after.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php
/**
* The Template for displaying all single courses.
* Template displayed after content on a restricted page
*
* @author codeBOX
* @package lifterLMS/Templates
* @package LifterLMS/Templates
*
* @since 1.0.0
* @since [version] Removed redundant notice output call and replaced duplicated hook with a new hook.
* @version [version]
*/

defined( 'ABSPATH' ) || exit;

global $post;
llms_print_notices();

?>
<?php
do_action( 'lifterlms_no_access_main_content' );
/**
* Action triggered after the main content of a restricted page is rendered
*
* @since [version]
*/
do_action( 'lifterlms_no_access_after' );
17 changes: 11 additions & 6 deletions templates/content-no-access-before.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<?php
/**
* The Template for displaying all single courses.
* Template displayed before the main content on a restricted page
*
* @author codeBOX
* @package lifterLMS/Templates
* @package LifterLMS/Templates
*
* @since 1.0.0
* @since [version] Removed redundant notice output call and replaced duplicated hook with a new hook.
* @version [version]
*/

defined( 'ABSPATH' ) || exit;

global $post;
llms_print_notices();

?>
<?php
/**
* Action triggered before the main content of a restricted page is rendered
*
* @since [version]
*/
do_action( 'lifterlms_no_access_main_content' );
Loading