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
41 changes: 38 additions & 3 deletions includes/functions/llms-functions-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function llms_get_post_content( $content ) {
$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' );

Expand All @@ -84,7 +83,7 @@ function llms_get_post_content( $content ) {
/**
* Filter the post_content of a LifterLMS post type.
*
* @since Unknown.
* @since [version]
thomasplevy marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $content Post content.
* @param WP_Post $post Post object.
Expand All @@ -94,4 +93,40 @@ function llms_get_post_content( $content ) {

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

/**
* 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_action( 'init', 'llms_post_content_init' );
thomasplevy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php
/**
* Tests for LifterLMS User Postmeta functions
* @group functions
* @group content_functions
* @since 3.25.1
* @version 3.25.1
*
* @package LifterLMS/Tests
*
* @group functions
* @group content_functions
*
* @since 3.25.1
*/
class LLMS_Test_Functions_Content extends LLMS_UnitTestCase {

Expand All @@ -14,6 +17,8 @@ public function get_post_content( $post ) {

public function test_llms_get_post_content() {

llms_post_content_init();

$content = '<p>Lorem ipsum dolor sit amet.</p>';
$post_types = array( 'llms_membership', 'course', 'lesson', 'post', 'page' );
foreach ( $post_types as $post_type ) {
Expand All @@ -34,4 +39,76 @@ public function test_llms_get_post_content() {

}

/**
* Test that llms_get_post_content() will return early if the `$post` global is not set.
*
* @since [version]
*
* @return void
*/
public function test_llms_get_post_content_no_global() {

llms_post_content_init();

$input = 'whatever';
$this->assertEquals( $input, llms_get_post_content( $input ) );

}

/**
* Test llms_post_content_init() when filters should be applied
*
* @since [version]
*
* @return void
*/
public function test_llms_post_content_init() {

remove_filter( 'the_content', 'llms_get_post_content' );

$this->assertTrue( llms_post_content_init() );
$this->assertEquals( 10, has_filter( 'the_content', 'llms_get_post_content' ) );

}

/**
* Test llms_post_content_init() when on the admin panel
*
* @since [version]
*
* @return void
*/
public function test_llms_post_content_init_is_admin() {

remove_filter( 'the_content', 'llms_get_post_content' );

set_current_screen( 'admin.php' );

$this->assertFalse( llms_post_content_init() );
$this->assertFalse( has_filter( 'the_content', 'llms_get_post_content' ) );

set_current_screen( 'front' ); // Reset.

}

/**
* Test llms_post_content_init() during REST requests
*
* @since [version]
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
*/
public function test_llms_post_content_init_is_rest() {

remove_filter( 'the_content', 'llms_get_post_content' );

define( 'REST_REQUEST', true );
$this->assertFalse( llms_post_content_init() );
$this->assertFalse( has_filter( 'the_content', 'llms_get_post_content' ) );

}

}