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

Meta_Storage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add_video() 0 4 1
A get_all_videos() 0 5 1
A remove_videos() 0 22 2
A remove_all_videos() 0 3 1
1
<?php
2
/**
3
 * @since 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Data\Video_Storage;
8
9
10
class Meta_Storage implements Storage {
11
12
	const META_KEY = '_wl_video_object_videos';
13
14
	public function add_video( $post_id, $video ) {
15
16
		add_post_meta( $post_id, self::META_KEY, $video );
17
	}
18
19
20
	public function get_all_videos( $post_id ) {
21
22
		return get_post_meta( $post_id, self::META_KEY );
23
24
	}
25
26
	public function remove_videos( $videos_to_be_removed, $post_id ) {
27
28
		$videos_to_be_removed_ids = array_map( function ( $video ) {
29
			return $video->id;
30
		}, $videos_to_be_removed );
31
32
		$present_videos = $this->get_all_videos( $post_id );
33
34
		$filtered_videos = array_filter( $present_videos, function ( $video ) use ( $videos_to_be_removed_ids ) {
35
			return ! in_array( $video->id, $videos_to_be_removed_ids );
36
		} );
37
38
		// Remove all existing videos.
39
		$this->remove_all_videos( $post_id );
40
41
		// Save the videos.
42
		foreach ( $filtered_videos as $video ) {
43
			$this->add_video( $post_id, $video );
44
		}
45
46
47
	}
48
49
	public function remove_all_videos( $post_id ) {
50
		delete_post_meta( $post_id, self::META_KEY );
51
	}
52
}