|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Videoobject; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift\Videoobject\Data\Embedded_Video\Embedded_Video; |
|
6
|
|
|
use Wordlift\Videoobject\Data\Video\Video; |
|
7
|
|
|
use Wordlift\Videoobject\Data\Video_Storage\Storage; |
|
8
|
|
|
use Wordlift\Videoobject\Data\Video_Storage\Video_Storage_Factory; |
|
9
|
|
|
use Wordlift\Videoobject\Parser\Parser_Factory; |
|
10
|
|
|
use Wordlift\Videoobject\Provider\Provider_Factory; |
|
11
|
|
|
|
|
12
|
|
|
class Video_Processor { |
|
13
|
|
|
|
|
14
|
|
|
private function get_data_for_videos( $embedded_videos ) { |
|
15
|
|
|
|
|
16
|
|
|
$youtube_videos = $this->get_youtube_videos( $embedded_videos ); |
|
17
|
|
|
|
|
18
|
|
|
$youtube_provider = Provider_Factory::get_provider( Provider_Factory::YOUTUBE ); |
|
19
|
|
|
$youtube_videos = $youtube_provider->get_videos_data( $youtube_videos ); |
|
20
|
|
|
|
|
21
|
|
|
$vimeo_videos = $this->get_vimeo_videos( $embedded_videos ); |
|
22
|
|
|
|
|
23
|
|
|
$vimeo_provider = Provider_Factory::get_provider( Provider_Factory::VIMEO ); |
|
24
|
|
|
$vimeo_videos = $vimeo_provider->get_videos_data( $vimeo_videos ); |
|
25
|
|
|
|
|
26
|
|
|
return array_merge( $youtube_videos, $vimeo_videos ); |
|
27
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $storage Storage |
|
32
|
|
|
* @param $post_id int |
|
33
|
|
|
* @param $embedded_videos array<Embedded_Video> |
|
34
|
|
|
*/ |
|
35
|
|
|
private function remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ) { |
|
36
|
|
|
|
|
37
|
|
|
$videos_to_be_deleted = $this->get_videos_to_be_deleted( $storage, $post_id, $embedded_videos ); |
|
38
|
|
|
$storage->remove_videos( $videos_to_be_deleted, $post_id ); |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Storage $storage |
|
44
|
|
|
* @param $post_id |
|
45
|
|
|
* @param array $embedded_videos |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
private function get_videos_to_be_deleted( Storage $storage, $post_id, array $embedded_videos ) { |
|
|
|
|
|
|
50
|
|
|
$videos_in_store = $storage->get_all_videos( $post_id ); |
|
51
|
|
|
$embedded_video_urls = array_map( function ( $embedded_video ) { |
|
52
|
|
|
/** |
|
53
|
|
|
* @var $embedded_video Embedded_Video |
|
54
|
|
|
*/ |
|
55
|
|
|
return $embedded_video->get_url(); |
|
56
|
|
|
}, $embedded_videos ); |
|
57
|
|
|
|
|
58
|
|
|
return array_filter( $videos_in_store, function ( $video ) use ( $embedded_video_urls ) { |
|
59
|
|
|
/** |
|
60
|
|
|
* @var $video Video |
|
61
|
|
|
*/ |
|
62
|
|
|
return ! in_array( $video->id, $embedded_video_urls ); |
|
63
|
|
|
} ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \WP_Post $post |
|
68
|
|
|
* @param $post_id |
|
69
|
|
|
*/ |
|
70
|
|
|
public function process_video_urls( \WP_Post $post, $post_id ) { |
|
71
|
|
|
|
|
72
|
|
|
$parser = Parser_Factory::get_parser_from_content( $post->post_content ); |
|
73
|
|
|
|
|
74
|
|
|
$embedded_videos = $parser->get_videos( $post_id ); |
|
75
|
|
|
|
|
76
|
|
|
$storage = Video_Storage_Factory::get_storage(); |
|
77
|
|
|
|
|
78
|
|
|
// Before sending api requests we need to check if there are any videos in |
|
79
|
|
|
// store which is not present on post content, remove them if there are |
|
80
|
|
|
// any |
|
81
|
|
|
$this->remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ); |
|
82
|
|
|
|
|
83
|
|
|
$embedded_videos = $this->get_videos_without_existing_data( $storage, $post_id, $embedded_videos ); |
|
84
|
|
|
|
|
85
|
|
|
// Return early after removing all the videos. |
|
86
|
|
|
if ( ! $embedded_videos ) { |
|
|
|
|
|
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$videos = $this->get_data_for_videos( $embedded_videos ); |
|
91
|
|
|
|
|
92
|
|
|
if ( ! $videos ) { |
|
|
|
|
|
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach ( $videos as $video ) { |
|
97
|
|
|
$storage->add_video( $post_id, $video ); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $storage Storage |
|
103
|
|
|
* @param $post_id int |
|
104
|
|
|
* @param $embedded_videos array<Embedded_Video> |
|
105
|
|
|
* |
|
106
|
|
|
* @return array<Embedded_Video> Return array of embedded videos which are not in store. |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
private function get_videos_without_existing_data( $storage, $post_id, $embedded_videos ) { |
|
|
|
|
|
|
109
|
|
|
$videos_in_store = $storage->get_all_videos( $post_id ); |
|
110
|
|
|
$video_ids_in_store = array_map( function ( $video ) { |
|
111
|
|
|
return $video->id; |
|
112
|
|
|
}, $videos_in_store ); |
|
113
|
|
|
|
|
114
|
|
|
return array_filter( $embedded_videos, function ( $embedded_video ) use ( $video_ids_in_store ) { |
|
115
|
|
|
return ! in_array( $embedded_video->get_url(), $video_ids_in_store ); |
|
116
|
|
|
} ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $embedded_videos |
|
121
|
|
|
* |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
private function get_youtube_videos( $embedded_videos ) { |
|
125
|
|
|
return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
126
|
|
|
/** |
|
127
|
|
|
* it should have youtube.com or youtu.be in the url |
|
128
|
|
|
* |
|
129
|
|
|
* @param $embedded_video Embedded_Video |
|
130
|
|
|
*/ |
|
131
|
|
|
$video_url = $embedded_video->get_url(); |
|
132
|
|
|
|
|
133
|
|
|
return strpos( $video_url, "youtube.com" ) !== false || |
|
134
|
|
|
strpos( $video_url, "youtu.be" ) !== false; |
|
135
|
|
|
} ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $embedded_videos |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
private function get_vimeo_videos( $embedded_videos ) { |
|
144
|
|
|
return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
145
|
|
|
/** |
|
146
|
|
|
* it should have vimeo.com in the url |
|
147
|
|
|
* |
|
148
|
|
|
* @param $embedded_video Embedded_Video |
|
149
|
|
|
*/ |
|
150
|
|
|
$video_url = $embedded_video->get_url(); |
|
151
|
|
|
|
|
152
|
|
|
return strpos( $video_url, "vimeo.com" ) !== false; |
|
153
|
|
|
} ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.