Skip to content

Commit

Permalink
Process [heading] shortcode in getSubsections (fix #1403) (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Zimmerman committed Oct 12, 2018
1 parent d41ddde commit f0c0af7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 4 additions & 2 deletions inc/class-book.php
Expand Up @@ -517,12 +517,14 @@ static function getSubsections( $id ) {
if ( empty( $parent ) ) {
return false;
}
if ( stripos( $parent->post_content, '<h1' ) === false ) {
$has_shortcode = has_shortcode( $parent->post_content, 'heading' );
if ( stripos( $parent->post_content, '<h1' ) === false && $has_shortcode === false ) { // No <h1> or [heading] shortcode
return false;
}

$type = $parent->post_type;
$content = strip_tags( $parent->post_content, '<h1>' ); // Strip everything except h1 to speed up load time
$content = ( $has_shortcode ) ? apply_filters( 'the_content', $parent->post_content ) : $parent->post_content; // Only render shortcodes if we have to
$content = strip_tags( $content, '<h1>' ); // Strip everything except h1 to speed up load time
$output = [];
$s = 1;

Expand Down
8 changes: 7 additions & 1 deletion tests/test-book.php
Expand Up @@ -145,6 +145,7 @@ public function test_wordCount() {

public function test_getSubsections() {
$this->_book();
$this->_shortcodes();
$book = \Pressbooks\Book::getInstance();

$result = $book::getSubsections( 0 );
Expand All @@ -158,12 +159,17 @@ public function test_getSubsections() {
$this->assertEquals( 'Hi there!', $result["front-matter-{$id}-section-1"] );

$test = "<H1 style='font-size:small;'>Hi there! Hope you're doing good.<B></B></H1><P>How are you?</P>"; // ALL CAPS, texturized
$id = $book::getBookStructure()['front-matter'][0]['ID'];
$this->factory()->post->update_object( $id, [ 'post_content' => $test ] );
$result = $book::getSubsections( $id );
$this->assertArrayHasKey( "front-matter-{$id}-section-1", $result );
$this->assertEquals( 'Hi there! Hope you&#8217;re doing good.', $result["front-matter-{$id}-section-1"] );

$test = '[heading]Whoa, a shortcode![/heading]<p>Some other stuff.</p>'; // A [heading] shortcode
$this->factory()->post->update_object( $id, [ 'post_content' => $test ] );
$result = $book::getSubsections( $id );
$this->assertArrayHasKey( "front-matter-{$id}-section-1", $result );
$this->assertEquals( 'Whoa, a shortcode!', $result["front-matter-{$id}-section-1"] );

$test = "<h2>Hi there! Hope you're doing good.<b></b></h2><p>How are you?</p>"; // H2
$this->factory()->post->update_object( $id, [ 'post_content' => $test ] );
$result = $book::getSubsections( $id );
Expand Down
19 changes: 18 additions & 1 deletion tests/utils-trait.php
Expand Up @@ -124,7 +124,7 @@ private function _createChapter( $post_parent = 0, $with_media = false ) {
$content .= "
[video]{$video_url}[/video]
{$thumbnail_html}
{$thumbnail_html}
";
}

Expand Down Expand Up @@ -256,4 +256,21 @@ public function _allowIframes( $allowed, $context ) {
return $allowed;
}

/**
* Set up shortcodes.
* @see hooks.php
*/
public function _shortcodes() {
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 12 ); // execute wpautop after shortcode processing

\Pressbooks\Shortcodes\Footnotes\Footnotes::init();
\Pressbooks\Shortcodes\Attributions\Attachments::init();
\Pressbooks\Shortcodes\Glossary\Glossary::init();
\Pressbooks\Shortcodes\Complex\Complex::init();
\Pressbooks\Shortcodes\Generics\Generics::init();
\Pressbooks\Shortcodes\WikiPublisher\Glyphs::init();
\Pressbooks\Shortcodes\TablePress::init();
}

}

0 comments on commit f0c0af7

Please sign in to comment.