Completed
Pull Request — develop (#1350)
by Naveen
04:17 queued 01:06
created

Classic_Editor_Parser::get_videos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Videoobject\Parser;
4
5
use Wordlift\Videoobject\Data\Embedded_Video\Embedded_Video;
6
use Wordlift\Videoobject\Data\Embedded_Video\Embedded_Video_Factory;
7
8
class Classic_Editor_Parser implements Parser {
9
10
	public function get_videos( $post_id ) {
11
12
		$post = get_post( $post_id );
13
14
		$content = $post->post_content;
15
16
		$line_matches      = array();
17
		$paragraph_matches = array();
18
19
		/**
20
		 * This replicates the wp autoembed function, instead of replacing we capture
21
		 * all the urls.
22
		 */
23
		if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) {
24
			// Find URLs on their own line.
25
			preg_match_all( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', $content, $line_matches );
26
			// Find URLs in their own paragraph.
27
			preg_match_all( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', $content, $paragraph_matches );
28
		}
29
30
		$matches = array_map( array( $this, 'get_url_from_match' ), array_merge( $line_matches, $paragraph_matches ) );
31
32
		$matches = array_values( array_unique( array_filter( $matches ) ) );
33
34
		return array_map( array( $this, 'url_to_embedded_video_object' ), $matches );
35
	}
36
37
38
	/**
39
	 * @param $url
40
	 *
41
	 * @return Embedded_Video
42
	 */
43
	private static function url_to_embedded_video_object( $url ) {
44
		return Embedded_Video_Factory::get_embedded_video( $url );
45
	}
46
47
	public static function get_url_from_match( $match ) {
48
		return array_key_exists( 0, $match ) ? $match[0] : false;
49
	}
50
51
}