PHP code example of wp-php-toolkit / blockparser
1. Go to this page and download the library: Download wp-php-toolkit/blockparser 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/ */
wp-php-toolkit / blockparser example snippets
ument = "<!-- wp:heading {\"level\":2} -->\n<h2>Welcome</h2>\n<!-- /wp:heading -->\n\n"
. "<!-- wp:paragraph -->\n<p>Hello from the block editor.</p>\n<!-- /wp:paragraph -->";
$blocks = ( new WP_Block_Parser() )->parse( $document );
foreach ( $blocks as $block ) {
if ( null === $block['blockName'] ) {
continue;
}
echo $block['blockName'] . ': ' . trim( strip_tags( $block['innerHTML'] ) ) . "\n";
}
ument = "<!-- wp:group --><div class=\"wp-block-group\">"
. "<!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->"
. "<!-- wp:paragraph --><p>One.</p><!-- /wp:paragraph -->"
. "<!-- wp:paragraph --><p>Two.</p><!-- /wp:paragraph -->"
. "<!-- wp:image {\"id\":1} --><figure><img src=\"a.jpg\"/></figure><!-- /wp:image -->"
. "</div><!-- /wp:group -->";
$blocks = ( new WP_Block_Parser() )->parse( $document );
$counts = array();
$queue = $blocks;
while ( ! empty( $queue ) ) {
$block = array_shift( $queue );
if ( null !== $block['blockName'] ) {
$name = $block['blockName'];
$counts[ $name ] = isset( $counts[ $name ] ) ? $counts[ $name ] + 1 : 1;
}
foreach ( $block['innerBlocks'] as $inner_block ) {
$queue[] = $inner_block;
}
}
arsort( $counts );
foreach ( $counts as $name => $n ) {
echo str_pad( (string) $n, 4, ' ', STR_PAD_LEFT ) . ' ' . $name . "\n";
}
ument = "<!-- wp:group --><div class=\"wp-block-group\">"
. "<!-- wp:buttons --><div class=\"wp-block-buttons\">"
. "<!-- wp:button --><div class=\"wp-block-button\"><a>Buy now</a></div><!-- /wp:button -->"
. "</div><!-- /wp:buttons -->"
. "</div><!-- /wp:group -->";
$blocks = ( new WP_Block_Parser() )->parse( $document );
function post_has_block( $blocks, $name ) {
$queue = $blocks;
while ( ! empty( $queue ) ) {
$block = array_shift( $queue );
if ( $name === $block['blockName'] ) {
return true;
}
foreach ( $block['innerBlocks'] as $inner_block ) {
$queue[] = $inner_block;
}
}
return false;
}
echo post_has_block( $blocks, 'core/button' ) ? "has button\n" : "missing button\n";
echo post_has_block( $blocks, 'core/gallery' ) ? "has gallery\n" : "missing gallery\n";
ument = "<!-- wp:heading -->\n<h2>Intro</h2>\n<!-- /wp:heading -->"
. "<!-- wp:heading {\"level\":4} -->\n<h4>Subsection</h4>\n<!-- /wp:heading -->"
. "<!-- wp:heading {\"level\":3} -->\n<h3>Body</h3>\n<!-- /wp:heading -->";
$blocks = ( new WP_Block_Parser() )->parse( $document );
function collect_headings( $blocks, &$headings ) {
foreach ( $blocks as $block ) {
if ( 'core/heading' === $block['blockName'] ) {
$headings[] = array(
'level' => isset( $block['attrs']['level'] ) ? (int) $block['attrs']['level'] : 2,
'text' => trim( strip_tags( $block['innerHTML'] ) ),
);
}
collect_headings( $block['innerBlocks'], $headings );
}
}
$headings = array();
collect_headings( $blocks, $headings );
$last = 1;
foreach ( $headings as $heading ) {
$level = $heading['level'];
$label = $heading['text'];
if ( $level > $last + 1 ) {
echo "WARN {$label}: jumped from H{$last} to H{$level}\n";
} else {
echo "ok {$label}: H{$level}\n";
}
$last = $level;
}
ument = "<!-- wp:paragraph --><p>Reviews</p><!-- /wp:paragraph -->"
. "<!-- wp:my-plugin/testimonial {\"author\":\"Jane\",\"rating\":5} -->"
. "<blockquote>Loved it.</blockquote>"
. "<!-- /wp:my-plugin/testimonial -->"
. "<!-- wp:my-plugin/testimonial {\"author\":\"Joe\",\"rating\":4} -->"
. "<blockquote>Pretty good.</blockquote>"
. "<!-- /wp:my-plugin/testimonial -->";
$blocks = ( new WP_Block_Parser() )->parse( $document );
function find_blocks_by_name( $blocks, $name, &$matches ) {
foreach ( $blocks as $block ) {
if ( $name === $block['blockName'] ) {
$matches[] = $block;
}
find_blocks_by_name( $block['innerBlocks'], $name, $matches );
}
}
$testimonials = array();
find_blocks_by_name( $blocks, 'my-plugin/testimonial', $testimonials );
foreach ( $testimonials as $i => $b ) {
echo ( $i + 1 ) . '. ' . $b['attrs']['author'] . ' (' . $b['attrs']['rating'] . '/5): '
. trim( strip_tags( $b['innerHTML'] ) ) . "\n";
}
ument = <<<'HTML'
<!-- wp:embed {"url":"https://twitter.com/wordpress/status/1","providerNameSlug":"twitter"} /-->
<!-- wp:embed {"url":"https://youtube.com/watch?v=abc","providerNameSlug":"youtube"} /-->
<!-- wp:embed {"url":"https://vine.co/v/xyz","providerNameSlug":"vine"} /-->
HTML;
$retired = array( 'vine.co', 'plus.google.com' );
foreach ( ( new WP_Block_Parser() )->parse( $document ) as $b ) {
if ( 'core/embed' !== $b['blockName'] ) {
continue;
}
$url = isset( $b['attrs']['url'] ) ? $b['attrs']['url'] : '';
$host = parse_url( $url, PHP_URL_HOST );
$bad = $host && in_array( $host, $retired, true );
echo ( $bad ? 'STALE ' : 'ok ' ) . $url . "\n";
}