Completed
Pull Request — develop (#1350)
by Naveen
03:35
created

Block_Editor_Parser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_videos() 0 6 1
A block_to_video() 0 3 1
A filter_blocks() 0 10 6
1
<?php
2
3
namespace Wordlift\Videoobject\Parser;
4
use Wordlift\Videoobject\Data\Embedded_Video\Embedded_Video;
5
use Wordlift\Videoobject\Data\Embedded_Video\Embedded_Video_Factory;
6
7
/**
8
 * @since 3.31.0
9
 * @author Naveen Muthusamy <[email protected]>
10
 */
11
class Block_Editor_Parser implements Parser {
12
13
	public function get_videos( $post_id ) {
14
		$post            = get_post( $post_id );
15
		$content         = $post->post_content;
16
		$video_blocks    = array_filter( parse_blocks( $content ), array( $this, 'filter_blocks' ) );
17
		return array_map( array( $this, 'block_to_video' ), $video_blocks );
18
	}
19
20
	/**
21
	 * @param $block
22
	 *
23
	 * @return Embedded_Video
24
	 */
25
	public function block_to_video( $block ) {
26
		return Embedded_Video_Factory::get_embedded_video( $block['attrs']['url'] );
27
	}
28
29
	public function filter_blocks( $block ) {
30
		return array_key_exists( 'blockName', $block )
31
		       && $block['blockName'] === 'core/embed'
32
		       // Check if attributes present
33
		       && array_key_exists( 'attrs', $block )
34
		       && is_array( $block['attrs'] )
35
		       // check if valid url present.
36
		       && array_key_exists( 'url', $block['attrs'] )
37
		       && is_string( $block['attrs']['url'] );
38
	}
39
40
}