PHP code example of alleyinteractive / wp-match-blocks
1. Go to this page and download the library: Download alleyinteractive/wp-match-blocks library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alleyinteractive / wp-match-blocks example snippets
$grafs = \Alley\WP\match_blocks( $post, [ 'name' => 'core/paragraph' ] );
$grafs = \Alley\WP\match_blocks(
$post,
[
'flatten' => true,
'name' => 'core/paragraph',
]
);
$count = \Alley\WP\match_blocks(
$post,
[
'count' => true,
'name' => 'core/paragraph',
]
);
$blocks = parse_blocks( '<!-- wp:group --><div class="wp-block-group"><!-- wp:paragraph -->…<!-- wp:group /-->' );
$count = \Alley\WP\match_blocks(
$blocks[0],
[
'count' => true,
'name' => 'core/paragraph',
]
);
$blocks = \Alley\WP\match_blocks(
'<!-- wp:paragraph -->…',
[
'name' => [ 'core/heading', 'core/paragraph' ],
]
);
$blocks = \Alley\WP\match_blocks(
[ /* blocks */ ],
[
'name' => 'core/paragraph',
'with_attrs' => 'align',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'attrs' => [
[
'key' => 'align',
'value' => 'right',
],
],
'name' => 'core/paragraph',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'attrs' => [
[
'key' => 'align',
'value' => 'right',
'operator' => '!==',
],
],
'name' => 'core/paragraph',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'attrs' => [
[
'key' => 'align',
'value' => [ 'left', 'right' ],
'operator' => 'IN',
],
],
'name' => 'core/paragraph',
]
);
$images = \Alley\WP\match_blocks(
$post,
[
'attrs' => [
[
'key' => 'credit',
'value' => '/(The )?Associated Press/i',
'operator' => 'REGEX',
],
[
'key' => 'credit',
'value' => 'AP',
],
'relation' => 'OR',
],
'name' => 'core/image',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => 'core/shortcode',
'with_innerhtml' => '[bc_video',
]
);
final class YouTube_Video_Exists extends \Alley\WP\Validator\Block_Validator {
// ...
}
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => 'core/embed',
'attrs' => [
[
'key' => 'providerNameSlug',
'value' => 'youtube',
],
],
'is_valid' => new \Alley\Validator\Not( new YouTube_Video_Exists(), '…' ),
],
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'limit' => 3,
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'limit' => 3,
'name' => 'core/paragraph',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => 'core/paragraph',
'nth_of_type' => 3,
]
);
// Or, skip straight to the parsed block:
$block = \Alley\WP\match_block(
$post,
[
'name' => 'core/paragraph',
'nth_of_type' => '3n',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => 'core/paragraph',
'nth_of_type' => '3n',
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => 'core/paragraph',
'nth_of_type' => [ 'n+3', '-n+8' ],
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => 'core/paragraph',
'position' => 3,
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'position' => [ -1, -2 ],
]
);
$blocks = \Alley\WP\match_blocks( $post );
$blocks = \Alley\WP\match_blocks(
$post,
[
'name' => null,
'skip_empty_blocks' => false,
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'has_innerblocks' => true,
]
);
$blocks = \Alley\WP\match_blocks(
$post,
[
'has_innerblocks' => false,
]
);
// '<!-- wp:media-text {"mediaId":617,"mediaType":"image","isStackedOnMobile":false,"className":"alignwide"} -->';
$valid = new Alley\WP\Validator\Block_Attribute(
[
'key' => 'mediaType',
'value' => 'image',
],
);
$valid = new Alley\WP\Validator\Block_Attribute(
[
'key' => [ 'mediaType', 'mediaId' ],
'key_operator' => 'IN',
],
);
$valid = new Alley\WP\Validator\Block_Attribute(
[
'key' => '/^media/',
'key_operator' => 'REGEX',
'value' => [ 'image', 'video' ],
'operator' => 'IN',
],
);
$valid = new Alley\WP\Validator\Block_Attribute(
[
'key' => '/^media/',
'key_operator' => 'REGEX',
'value' => [ 'audio', 'document' ],
'operator' => 'NOT IN',
],
);
// '
// <!-- wp:paragraph -->
// <p>The goal of this new editor is to make adding rich content to WordPress simple and enjoyable.</p>
// <!-- /wp:paragraph -->
// '
$valid = new Alley\WP\Validator\Block_InnerHTML(
[
'content' => 'wordpress',
'operator' => 'LIKE',
],
);
$valid = new Alley\WP\Validator\Block_InnerHTML(
[
'content' => 'WordPress',
'operator' => 'CONTAINS',
],
);
$valid = new Alley\WP\Validator\Block_InnerHTML(
[
'content' => '/^\s*<p>\s*</p>/',
'operator' => 'NOT REGEX',
],
);
$valid = new Alley\WP\Validator\Block_Name(
[
'name' => 'core/paragraph',
]
);
$valid = new Alley\WP\Validator\Block_Name(
[
'name' => [ 'core/gallery', 'jetpack/slideshow', 'jetpack/tiled-gallery' ],
]
);
$blocks = parse_blocks(
<<<HTML
<!-- wp:paragraph --><p>Hello, world!</p><!-- /wp:paragraph -->
<!-- wp:archives {\"displayAsDropdown\":true,\"showPostCounts\":true} /-->
<!-- wp:media-text {\"mediaId\":617,\"mediaType\":\"image\",\"isStackedOnMobile\":false,\"className\":\"alignwide\"} -->
HTML
);
$valid = new Alley\WP\Validator\Block_Offset(
[
'blocks' => $blocks,
'offset' => 1,
],
);
$valid->isValid( $blocks[2] ); // true
$valid = new Alley\WP\Validator\Block_Offset(
[
'blocks' => $blocks,
'offset' => [ 4 ],
'skip_empty_blocks' => false,
],
);
$valid->isValid( $blocks[4] ); // true
$valid = new Alley\WP\Validator\Block_Offset(
[
'blocks' => $blocks,
'offset' => -2,
],
);
$valid->isValid( $blocks[2] ); // true
$blocks = parse_blocks(
<<<HTML
<!-- wp:foo -->
<!-- wp:bar -->
<!-- wp:baz /-->
<!-- /wp:bar -->
<!-- /wp:foo -->
HTML
);
$valid = new \Alley\WP\Validator\Block_InnerBlocks_Count(
[
'count' => 1,
'operator' => '===',
]
);
$valid->isValid( $blocks[0] ); // true
$valid = new \Alley\WP\Validator\Block_InnerBlocks_Count(
[
'count' => 0,
'operator' => '>',
]
);
$valid->isValid( $blocks[0] ); // true
$valid = new \Alley\WP\Validator\Block_InnerBlocks_Count(
[
'count' => 42,
'operator' => '<=',
]
);
$valid->isValid( $blocks[0] ); // true
$blocks = parse_blocks( "\n" );
$valid = new \Alley\WP\Validator\Nonempty_Block();
$valid->isValid( $blocks[0] ); // false