Skip to content

Commit

Permalink
Split out WP_Filesystem_To_Post_Hierarchy to enable turning each file…
Browse files Browse the repository at this point in the history
… into a stream of entities
  • Loading branch information
adamziel committed Dec 19, 2024
1 parent 1cb383a commit 273fb8f
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/playground/data-liberation/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
require_once __DIR__ . '/src/entity-readers/WP_EPub_Entity_Reader.php';
require_once __DIR__ . '/src/entity-readers/WP_WXR_Entity_Reader.php';
require_once __DIR__ . '/src/entity-readers/WP_Directory_Tree_Entity_Reader.php';
require_once __DIR__ . '/src/entity-readers/WP_Filesystem_To_Post_Hierarchy.php';

require_once __DIR__ . '/src/xml-api/WP_XML_Decoder.php';
require_once __DIR__ . '/src/xml-api/WP_XML_Processor.php';
Expand Down
1 change: 1 addition & 0 deletions packages/playground/data-liberation/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<file>tests/WPHTMLEntityReaderTests.php</file>
<file>tests/WPEPubEntityReaderTests.php</file>
<file>tests/WPDirectoryTreeEntityReaderTests.php</file>
<file>tests/WPFilesystemToPostHierarchyTests.php</file>
<file>tests/WPURLInTextProcessorTests.php</file>
<file>tests/WPBlockMarkupProcessorTests.php</file>
<file>tests/WPBlockMarkupUrlProcessorTests.php</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ public function get_last_error(): ?string {
}

public function skip_and_get_block_inner_html() {
if('#block-comment' !== $this->get_token_type()) {
if ( '#block-comment' !== $this->get_token_type() ) {
return false;
}

if($this->is_block_closer()) {
if ( $this->is_block_closer() ) {
return false;
}

if(false === WP_HTML_Tag_Processor::set_bookmark('block-start')) {
if ( false === WP_HTML_Tag_Processor::set_bookmark( 'block-start' ) ) {
return false;
}

$starting_block_depth = $this->get_block_depth();
while($this->next_token()) {
if(
while ( $this->next_token() ) {
if (
$this->get_token_type() === '#block-comment' &&
$this->is_block_closer() &&
$this->get_block_depth() === $starting_block_depth - 1
Expand All @@ -68,16 +68,16 @@ public function skip_and_get_block_inner_html() {
}
}

if(false === WP_HTML_Tag_Processor::set_bookmark('block-end')) {
WP_HTML_Tag_Processor::release_bookmark('block-start');
if ( false === WP_HTML_Tag_Processor::set_bookmark( 'block-end' ) ) {
WP_HTML_Tag_Processor::release_bookmark( 'block-start' );
return false;
}

$inner_html_start = $this->bookmarks['block-start']->start + $this->bookmarks['block-start']->length;
$inner_html_end = $this->bookmarks['block-end']->start - $inner_html_start;
$inner_html_end = $this->bookmarks['block-end']->start - $inner_html_start;

WP_HTML_Tag_Processor::release_bookmark('block-start');
WP_HTML_Tag_Processor::release_bookmark('block-end');
WP_HTML_Tag_Processor::release_bookmark( 'block-start' );
WP_HTML_Tag_Processor::release_bookmark( 'block-end' );

return substr(
$this->html,
Expand All @@ -87,7 +87,7 @@ public function skip_and_get_block_inner_html() {
}

public function get_block_depth() {
return count($this->stack_of_open_blocks);
return count( $this->stack_of_open_blocks );
}

public function get_block_breadcrumbs() {
Expand Down Expand Up @@ -115,12 +115,12 @@ public function get_block_attributes() {
return $this->block_attributes;
}

public function get_block_attribute($attribute_name) {
public function get_block_attribute( $attribute_name ) {
if ( null === $this->block_attributes ) {
return false;
}

return $this->block_attributes[$attribute_name] ?? false;
return $this->block_attributes[ $attribute_name ] ?? false;
}

public function is_block_closer() {
Expand All @@ -135,7 +135,7 @@ public function is_self_closing_block() {
public function next_token(): bool {
// Prevent running next_token() logic twice when the parent method
// makes recursive calls to itself.
if($this->in_next_token) {
if ( $this->in_next_token ) {
return parent::next_token();
}
$this->in_next_token = true;
Expand Down Expand Up @@ -229,18 +229,18 @@ public function next_token(): bool {
// The rest of the comment can only consist of block attributes
// and an optional solidus character.
$rest = trim( substr( $text, $at ) );
$at = strlen( $text );
$at = strlen( $text );

// Inspect our potential JSON for the self-closing solidus (`/`) character.
$json_maybe = $rest;
if ( substr( $json_maybe, -1 ) === '/' ) {
// Self-closing block (<!-- wp:image /-->)
$this->self_closing_flag = true;
$json_maybe = substr( $json_maybe, 0, -1 );
$json_maybe = substr( $json_maybe, 0, -1 );
}

// Let's try to parse attributes as JSON.
if( strlen( $json_maybe ) > 0 ) {
if ( strlen( $json_maybe ) > 0 ) {
$attributes = json_decode( $json_maybe, true );
if ( null === $attributes || ! is_array( $attributes ) ) {
// This comment looked like a block comment, but the attributes didn't
Expand All @@ -256,14 +256,14 @@ public function next_token(): bool {
$this->block_name = $name;
$this->block_attributes = $attributes;

if($this->block_closer) {
$popped = array_pop($this->stack_of_open_blocks);
if($popped !== $name) {
$this->last_block_error = sprintf('Block closer %s does not match the last opened block %s.', $name, $popped);
if ( $this->block_closer ) {
$popped = array_pop( $this->stack_of_open_blocks );
if ( $popped !== $name ) {
$this->last_block_error = sprintf( 'Block closer %s does not match the last opened block %s.', $name, $popped );
return false;
}
} else if (!$this->self_closing_flag) {
array_push($this->stack_of_open_blocks, $name);
} elseif ( ! $this->self_closing_flag ) {
array_push( $this->stack_of_open_blocks, $name );
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WP_Block_Markup_Url_Processor extends WP_Block_Markup_Processor {
private $inspected_url_attribute_idx = - 1;

public static function create_from_html( $html, $base_url_string = null ) {
$processor = static::create_fragment( $html );
$processor = static::create_fragment( $html );
$processor->base_url_string = $base_url_string;
$processor->base_url_object = $base_url_string ? WP_URL::parse( $base_url_string ) : null;
return $processor;
Expand Down
Loading

0 comments on commit 273fb8f

Please sign in to comment.