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

Meta_Storage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 51
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 8 1
A get_all_videos() 0 5 1
A remove_videos() 0 26 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
use Wordlift\Cache\Ttl_Cache;
11
12
class Meta_Storage implements Storage {
13
14
	const META_KEY = '_wl_video_object_videos';
15
16
	public function add_video( $post_id, $video ) {
17
		/**
18
		 * @since 3.31.0
19
		 * Fires when the video storage gets updated
20
		 */
21
		do_action( 'wordlift_videoobject_video_storage_updated' );
22
		add_post_meta( $post_id, self::META_KEY, $video );
23
	}
24
25
26
	public function get_all_videos( $post_id ) {
27
28
		return get_post_meta( $post_id, self::META_KEY );
29
30
	}
31
32
	public function remove_videos( $videos_to_be_removed, $post_id ) {
33
		/**
34
		 * @since 3.31.0
35
		 * Fires when the video storage gets updated
36
		 */
37
		do_action( 'wordlift_videoobject_video_storage_updated' );
38
		$videos_to_be_removed_ids = array_map( function ( $video ) {
39
			return $video->id;
40
		}, $videos_to_be_removed );
41
42
		$present_videos = $this->get_all_videos( $post_id );
43
44
		$filtered_videos = array_filter( $present_videos, function ( $video ) use ( $videos_to_be_removed_ids ) {
45
			return ! in_array( $video->id, $videos_to_be_removed_ids );
46
		} );
47
48
		// Remove all existing videos.
49
		$this->remove_all_videos( $post_id );
50
51
		// Save the remaining videos.
52
		foreach ( $filtered_videos as $video ) {
53
			$this->add_video( $post_id, $video );
54
		}
55
56
57
	}
58
59
	public function remove_all_videos( $post_id ) {
60
		delete_post_meta( $post_id, self::META_KEY );
61
	}
62
}