Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/helpers
File: blocks-helper.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Helpers;
[2] Fix | Delete
[3] Fix | Delete
use WP_Block_Parser_Block;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* A helper object for blocks.
[7] Fix | Delete
*/
[8] Fix | Delete
class Blocks_Helper {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Holds the Post_Helper instance.
[12] Fix | Delete
*
[13] Fix | Delete
* @var Post_Helper
[14] Fix | Delete
*/
[15] Fix | Delete
private $post;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Constructs a Blocks_Helper instance.
[19] Fix | Delete
*
[20] Fix | Delete
* @codeCoverageIgnore It handles dependencies.
[21] Fix | Delete
*
[22] Fix | Delete
* @param Post_Helper $post The post helper.
[23] Fix | Delete
*/
[24] Fix | Delete
public function __construct( Post_Helper $post ) {
[25] Fix | Delete
$this->post = $post;
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Returns all blocks in a given post.
[30] Fix | Delete
*
[31] Fix | Delete
* @param int $post_id The post id.
[32] Fix | Delete
*
[33] Fix | Delete
* @return array The blocks in a block-type => WP_Block_Parser_Block[] format.
[34] Fix | Delete
*/
[35] Fix | Delete
public function get_all_blocks_from_post( $post_id ) {
[36] Fix | Delete
if ( ! $this->has_blocks_support() ) {
[37] Fix | Delete
return [];
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
$post = $this->post->get_post( $post_id );
[41] Fix | Delete
return $this->get_all_blocks_from_content( $post->post_content );
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Returns all blocks in a given content.
[46] Fix | Delete
*
[47] Fix | Delete
* @param string $content The content.
[48] Fix | Delete
*
[49] Fix | Delete
* @return array The blocks in a block-type => WP_Block_Parser_Block[] format.
[50] Fix | Delete
*/
[51] Fix | Delete
public function get_all_blocks_from_content( $content ) {
[52] Fix | Delete
if ( ! $this->has_blocks_support() ) {
[53] Fix | Delete
return [];
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
$collection = [];
[57] Fix | Delete
$blocks = \parse_blocks( $content );
[58] Fix | Delete
return $this->collect_blocks( $blocks, $collection );
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Checks if the installation has blocks support.
[63] Fix | Delete
*
[64] Fix | Delete
* @codeCoverageIgnore It only checks if a WordPress function exists.
[65] Fix | Delete
*
[66] Fix | Delete
* @return bool True when function parse_blocks exists.
[67] Fix | Delete
*/
[68] Fix | Delete
protected function has_blocks_support() {
[69] Fix | Delete
return \function_exists( 'parse_blocks' );
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Collects an array of blocks into an organised collection.
[74] Fix | Delete
*
[75] Fix | Delete
* @param WP_Block_Parser_Block[] $blocks The blocks.
[76] Fix | Delete
* @param array $collection The collection.
[77] Fix | Delete
*
[78] Fix | Delete
* @return array The blocks in a block-type => WP_Block_Parser_Block[] format.
[79] Fix | Delete
*/
[80] Fix | Delete
private function collect_blocks( $blocks, $collection ) {
[81] Fix | Delete
foreach ( $blocks as $block ) {
[82] Fix | Delete
if ( ! isset( $collection[ $block['blockName'] ] ) || ! \is_array( $collection[ $block['blockName'] ] ) ) {
[83] Fix | Delete
$collection[ $block['blockName'] ] = [];
[84] Fix | Delete
}
[85] Fix | Delete
$collection[ $block['blockName'] ][] = $block;
[86] Fix | Delete
[87] Fix | Delete
if ( isset( $block['innerBlocks'] ) ) {
[88] Fix | Delete
$collection = $this->collect_blocks( $block['innerBlocks'], $collection );
[89] Fix | Delete
}
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
return $collection;
[93] Fix | Delete
}
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function