Skip to content

Commit

Permalink
Merge pull request #1539 from gocodebox/content-filter-conditions
Browse files Browse the repository at this point in the history
content filtering conditions WIP
  • Loading branch information
Thomas Patrick Levy authored Feb 22, 2021
2 parents 11ae011 + f2617b4 commit b0e4c18
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 81 deletions.
7 changes: 2 additions & 5 deletions includes/admin/class.llms.admin.builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package LifterLMS/Admin/Classes
*
* @since 3.13.0
* @version 4.16.0
* @version [version]
*/

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -318,6 +318,7 @@ private static function get_template( $template, $vars = array() ) {
* @since 3.19.2 Unknown.
* @since 4.16.0 Remove all filters/actions applied to the title/content when handling the ajax_save by deafault.
* This is specially to prevent plugin conflicts, see https://github.com/gocodebox/lifterlms/issues/1530.
* @since [version] Remove `remove_all_*` hooks added in version 4.16.0.
*
* @param array $request $_REQUEST
* @return array
Expand All @@ -334,10 +335,6 @@ public static function handle_ajax( $request ) {
case 'ajax_save':
if ( isset( $request['llms_builder'] ) ) {

// Remove filters/actions applied to the title and content.
remove_all_actions( 'the_title' );
remove_all_actions( 'the_content' );

$request['llms_builder'] = stripslashes( $request['llms_builder'] );
wp_send_json( self::heartbeat_received( array(), $request ) );

Expand Down
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
147 changes: 101 additions & 46 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,10 +15,11 @@
/**
* 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] Refactored.
*
* @param string $content WP_Post post_content.
* @return string
Expand All @@ -30,68 +31,122 @@ function llms_get_post_content( $content ) {
return $content;
}

$page_restricted = llms_page_restricted( $post->ID );
$before = '';
$template_before = '';
$after = '';
$template_after = '';
$restrictions = llms_page_restricted( $post->ID );

if ( 'course' === $post->post_type || 'llms_membership' === $post->post_type ) {
if ( in_array( $post->post_type, array( 'course', 'llms_membership', 'lesson', 'llms_quiz' ), true ) ) {

$sales_page = get_post_meta( $post->ID, '_llms_sales_page_content_type', true );
$post_type = str_replace( 'llms_', '', $post->post_type );
$template_before = 'single-' . $post_type . '-before';
$template_after = 'single-' . $post_type . '-after';

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 );
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 = 'no-access-before';
$template_after = 'no-access-after';
}
}

$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'] ) {
$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' );
}
} 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 );
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 );
load_template( llms_get_template_part_contents( 'content', $template_after ), false );
$after = ob_get_clean();

$content = do_shortcode( $before . $content . $after );

}

/**
* 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', $content, $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.
*
* @since [version]
*
* @param callable $callback Optional. Callback function to be added as a callback for the filter `the_content`. Default 'llms_get_post_content'.
* @param integer $priority Optional. Priority used when adding the filter. Default: 10.
* @return boolean Returns `true` if content filters are added and `false` if not.
*/
function llms_post_content_init( $callback = 'llms_get_post_content', $priority = 10 ) {

// Don't filter post content on the admin panel.
$should_filter = ( false === is_admin() );

/**
* Filters whether or not LifterLMS content filters should be applied.
*
* @since [version]
*
* @param boolean $should_filter Whether or not to filter the content.
* @param callable $callback Callback function to be added as a callback for the filter `the_content`.
*/
if ( apply_filters( 'llms_should_filter_post_content', $should_filter, $callback ) ) {
return add_filter( 'the_content', $callback, $priority );
}

return false;

}
add_filter( 'the_content', 'llms_get_post_content' );

llms_post_content_init();
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' );
15 changes: 12 additions & 3 deletions templates/quiz/meta-information.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
* @package LifterLMS/Templates
*
* @since 3.9.0
* @version 4.0.0-beta.2
* @since 4.0.0 Unknown.
* @since [version] Return early if accessed without a logged in user or the quiz can't be loaded from the `$post` global.
* @version [version]
*/

defined( 'ABSPATH' ) || exit;

global $post;

$quiz = llms_get_post( $post );
$student = llms_get_student();
if ( ! $student ) {
return;
}

$quiz = llms_get_post( $post );
if ( ! $quiz ) {
return;
}
$passing_percent = $quiz->get( 'passing_percent' );
$student = llms_get_student();
?>

<h2 class="llms-quiz-meta-title"><?php _e( 'Quiz Information', 'lifterlms' ); ?></h2>
Expand Down
9 changes: 7 additions & 2 deletions templates/quiz/results.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*
* @since 1.0.0
* @since 3.35.0 Access `$_GET` data via `llms_filter_input()`.
* @version 3.35.0
* @since [version] Return early if accessed without a logged in user.
* @version [version]
*
* @property LLMS_Quiz_Attempt $attempt Attempt object.
*/
Expand All @@ -19,7 +20,11 @@
return;
}

$student = llms_get_student();
$student = llms_get_student();
if ( ! $student ) {
return;
}

$attempts = $student->quizzes()->get_attempts_by_quiz(
$quiz->get( 'id' ),
array(
Expand Down
Loading

0 comments on commit b0e4c18

Please sign in to comment.