Skip to content

Commit b6c7599

Browse files
authored
Merge pull request #19 from alleyinteractive/revert-18-feature/xpath
Revert "Add experimental support for matching blocks with XPath"
2 parents 2d3a2c8 + 1263017 commit b6c7599

File tree

9 files changed

+23
-676
lines changed

9 files changed

+23
-676
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C
66

77
Nothing yet.
88

9-
## 4.1.0
9+
## 4.2.0
10+
11+
### Removed
12+
13+
- Reverted changes from version 4.1 to avoid conflicts with the Alleyvate plugin.
14+
15+
## 4.1.0 [YANKED]
1016

1117
### Added
1218

README.md

Lines changed: 0 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Blocks can be matched by:
1111
* Whether the block represents only space (`skip_empty_blocks`)
1212
* Whether the block has inner blocks (`has_innerblocks`)
1313
* Custom validation classes (`is_valid`)
14-
* Xpath queries (`__experimental_xpath`) ([huh?](#matching-blocks-with-xpath))
1514

1615
Passing matching parameters is optional; all non-empty blocks match by default.
1716

@@ -659,206 +658,6 @@ $valid = new \Alley\WP\Validator\Nonempty_Block();
659658
$valid->isValid( $blocks[0] ); // false
660659
```
661660

662-
## Matching blocks with XPath
663-
664-
`match_blocks()` has **experimental** support for matching blocks with XPath queries. These are made possible by converting the source blocks to a custom XML structure.
665-
666-
**This feature may be changed without backwards compatibility in future releases.**
667-
668-
### Basic usage
669-
670-
Find all paragraph blocks that are inner blocks of a cover block:
671-
672-
```php
673-
<?php
674-
675-
$grafs = \Alley\WP\match_blocks(
676-
$post,
677-
[
678-
'__experimental_xpath' => '//block[blockName="core/cover"]/innerBlocks/block[blockName="core/paragraph"]',
679-
],
680-
);
681-
```
682-
683-
Find list blocks with zero or one list items:
684-
685-
```php
686-
<?php
687-
688-
$lists = \Alley\WP\match_blocks(
689-
$post,
690-
[
691-
'__experimental_xpath' => '//block[blockName="core/list" and count(innerBlocks/block[blockName="core/list-item"]) <= 1]',
692-
],
693-
);
694-
```
695-
696-
Find the second paragraph block:
697-
698-
```php
699-
<?php
700-
701-
$graf = \Alley\WP\match_block(
702-
$post,
703-
[
704-
'__experimental_xpath' => '//block[blockName="core/paragraph"][2]',
705-
],
706-
);
707-
```
708-
709-
Find full-width images:
710-
711-
```php
712-
<?php
713-
714-
$images = \Alley\WP\match_blocks(
715-
$post,
716-
[
717-
'__experimental_xpath' => '//block[blockName="core/image"][attrs/sizeSlug="full"]',
718-
],
719-
);
720-
```
721-
722-
The XML document currently has the following structure:
723-
724-
```xml
725-
<blocks>
726-
<block>
727-
<blockName />
728-
<attrs />
729-
<innerBlocks />
730-
<innerHTML />
731-
</block>
732-
</blocks>
733-
```
734-
735-
For example, this block HTML:
736-
737-
```html
738-
<!-- wp:paragraph -->
739-
<p>The Common category includes the following blocks: <em>Paragraph, image, headings, list, gallery, quote, audio, cover, video.</em></p>
740-
<!-- /wp:paragraph -->
741-
742-
<!-- wp:paragraph {"align":"right"} -->
743-
<p class="has-text-align-right"><em>This italic paragraph is right aligned.</em></p>
744-
<!-- /wp:paragraph -->
745-
746-
<!-- wp:image {"id":968,"sizeSlug":"full","className":"is-style-circle-mask"} -->
747-
<figure class="wp-block-image size-full is-style-circle-mask"><img src="https://example.com/wp-content/uploads/2013/03/image-alignment-150x150-13.jpg" alt="Image Alignment 150x150" class="wp-image-968"/></figure>
748-
<!-- /wp:image -->
749-
750-
<!-- wp:cover {"url":"https://example.com/wp-content/uploads/2008/06/dsc04563-12.jpg","id":759,"minHeight":274} -->
751-
<div class="wp-block-cover has-background-dim" style="background-image:url(https://example.com/wp-content/uploads/2008/06/dsc04563-12.jpg);min-height:274px">
752-
<div class="wp-block-cover__inner-container">
753-
<!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
754-
<p class="has-text-align-center has-large-font-size">Cover block with background image</p>
755-
<!-- /wp:paragraph -->
756-
</div>
757-
</div>
758-
<!-- /wp:cover -->
759-
```
760-
761-
will be converted to this XML:
762-
763-
```xml
764-
<blocks>
765-
<block>
766-
<blockName>core/paragraph</blockName>
767-
<attrs/>
768-
<innerBlocks/>
769-
<innerHTML><![CDATA[
770-
<p>The Common category includes the following blocks: <em>Paragraph, image, headings, list, gallery, quote, audio, cover, video.</em></p>
771-
]]></innerHTML>
772-
</block>
773-
774-
<block>
775-
<blockName>core/paragraph</blockName>
776-
<attrs>
777-
<align>right</align>
778-
</attrs>
779-
<innerBlocks/>
780-
<innerHTML><![CDATA[
781-
<p class="has-text-align-right"><em>This italic paragraph is right aligned.</em></p>
782-
]]></innerHTML>
783-
</block>
784-
785-
<block>
786-
<blockName>core/image</blockName>
787-
<attrs>
788-
<id>968</id>
789-
<sizeSlug>full</sizeSlug>
790-
<className>is-style-circle-mask</className>
791-
</attrs>
792-
<innerBlocks/>
793-
<innerHTML><![CDATA[
794-
<figure class="wp-block-image size-full is-style-circle-mask"><img src="https://example.com/wp-content/uploads/2013/03/image-alignment-150x150-13.jpg" alt="Image Alignment 150x150" class="wp-image-968"/></figure>
795-
]]></innerHTML>
796-
</block>
797-
798-
<block>
799-
<blockName>core/cover</blockName>
800-
<attrs>
801-
<url>https://example.com/wp-content/uploads/2008/06/dsc04563-12.jpg</url>
802-
<id>759</id>
803-
<minHeight>274</minHeight>
804-
</attrs>
805-
<innerBlocks>
806-
<block>
807-
<blockName>core/paragraph</blockName>
808-
<attrs>
809-
<align>center</align>
810-
<placeholder>Write title&#x2026;</placeholder>
811-
<fontSize>large</fontSize>
812-
</attrs>
813-
<innerBlocks/>
814-
<innerHTML><![CDATA[
815-
<p class="has-text-align-center has-large-font-size">Cover block with background image</p>
816-
]]></innerHTML>
817-
</block>
818-
</innerBlocks>
819-
<innerHTML><![CDATA[
820-
<div class="wp-block-cover has-background-dim" style="background-image:url(https://example.com/wp-content/uploads/2008/06/dsc04563-12.jpg);min-height:274px">
821-
<div class="wp-block-cover__inner-container">
822-
823-
</div>
824-
</div>
825-
]]></innerHTML>
826-
</block>
827-
</blocks>
828-
```
829-
830-
### Limitations
831-
832-
Although it's possible to use XPath queries in conjunction with other `match_blocks()` arguments, the results with some arguments might be unexpected.
833-
834-
Typically, `match_blocks()` returns the blocks that match all the arguments. But when the `__experimental_xpath` argument is used, the set of source blocks will be first reduced to the blocks that match the XPath query, and then the remaining arguments will be applied.
835-
836-
For example, compare these sets of arguments:
837-
838-
```php
839-
<?php
840-
841-
$blocks = \Alley\WP\match_blocks(
842-
$post,
843-
[
844-
'name' => 'core/paragraph',
845-
'position' => 3,
846-
],
847-
);
848-
849-
$blocks = \Alley\WP\match_blocks(
850-
$post,
851-
[
852-
'__experimental_xpath' => '//block[blockName="core/paragraph"]',
853-
'position' => 3,
854-
],
855-
);
856-
```
857-
858-
In the top example, the third block in the set of blocks will be returned, but only if it's a paragraph.
859-
860-
In the bottom example, the XPath query will match all paragraphs in the document, regardless of their depth, and then the third paragraph out of that set will be returned.
861-
862661
## About
863662

864663
### License

composer.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
],
1212
"require": {
1313
"php": "^8.2",
14-
"ext-simplexml": "*",
15-
"alleyinteractive/composer-wordpress-autoloader": "^1.0",
16-
"alleyinteractive/laminas-validator-extensions": "^2.0",
17-
"alleyinteractive/wp-type-extensions": "^4.0",
18-
"symfony/serializer": "^7.2"
14+
"alleyinteractive/composer-wordpress-autoloader": "^1.0.0",
15+
"alleyinteractive/laminas-validator-extensions": "^2.0.0"
1916
},
2017
"require-dev": {
2118
"alleyinteractive/alley-coding-standards": "^2.0",
@@ -72,4 +69,4 @@
7269
],
7370
"tidy": "[ $COMPOSER_DEV_MODE -eq 0 ] || composer normalize"
7471
}
75-
}
72+
}

src/blocks/class-matched-blocks.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)