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

Jsonld::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Jsonld;
8
9
10
use Wordlift\Videoobject\Data\Video\Video;
11
use Wordlift\Videoobject\Data\Video_Storage\Storage;
12
13
class Jsonld {
14
	/**
15
	 * @var Storage
16
	 */
17
	private $video_storage;
18
19
	/**
20
	 * Jsonld constructor.
21
	 *
22
	 * @param $video_storage Storage
23
	 */
24
	public function __construct( $video_storage ) {
25
		add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 );
26
		add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 );
27
		$this->video_storage = $video_storage;
28
	}
29
30
31
	public function wl_after_get_jsonld( $jsonld, $post_id, $context ) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
		if ( 0 === count( $jsonld ) ) {
33
			return $jsonld;
34
		}
35
		$current_item = $jsonld[0];
36
37
		if ( ! array_key_exists( '@type', $current_item ) ) {
38
			// Cant determine type return early.
39
			return $jsonld;
40
		}
41
42
		$type = $current_item['@type'];
43
		if ( ( is_string( $type ) && $type === 'Article' ) ||
44
		     ( is_array( $type ) && in_array( 'Article', $type ) ) ) {
45
			return $jsonld;
46
		}
47
48
		$videos_jsonld = $this->get_videos_jsonld( $post_id );
49
		if ( 0 === count( $videos_jsonld ) ) {
50
			return $jsonld;
51
		}
52
53
		return array_merge( $jsonld, $videos_jsonld );
54
	}
55
56
	/**
57
	 * @param $existing_video_data string | array associative or sequential array.
58
	 * @param $new_video_data array Sequential array.
59
	 *
60
	 * @return array
61
	 */
62
	private function merge_video_data( $existing_video_data, $new_video_data ) {
63
		if ( ! is_array( $existing_video_data ) ) {
64
			$new_video_data[] = $existing_video_data;
65
66
			return $new_video_data;
67
		}
68
69
		if ( $this->is_associative_array( $existing_video_data ) ) {
70
			$new_video_data[] = $existing_video_data;
71
72
			return $new_video_data;
73
		}
74
75
		return array_merge( $existing_video_data, $new_video_data );
76
	}
77
78
	public function wl_post_jsonld( $jsonld, $post_id, $references ) {
0 ignored issues
show
Unused Code introduced by
The parameter $references is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
80
		$video_jsonld = $this->get_videos_jsonld( $post_id );
81
		if ( count( $video_jsonld ) === 0 ) {
82
			return $jsonld;
83
		}
84
		// Before adding the video jsonld check if the key
85
		// is present and additional data might be present,
86
		// if not present just add the data and return early.
87
		if ( ! array_key_exists( 'video', $jsonld ) ) {
88
			$jsonld['video'] = $video_jsonld;
89
90
			return $jsonld;
91
		}
92
93
		// since key exists, we need to merge the data based on type.
94
		$previous_video_data = $jsonld['video'];
95
		$jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
96
97
		return $jsonld;
98
	}
99
100
101
	/**
102
	 * @param $post_id int Post id.
103
	 *
104
	 * @return array
105
	 */
106
	public function get_videos_jsonld( $post_id ) {
107
108
		$videos = $this->video_storage->get_all_videos( $post_id );
109
110
		$jsonld = array();
111
112
		foreach ( $videos as $video ) {
113
			/**
114
			 * @var $video Video
115
			 */
116
			$single_jsonld = array(
117
				'@context'     => 'http://schema.org',
118
				'@type'        => 'VideoObject',
119
				'name'         => $video->name,
120
				'description'  => $video->description,
121
				'contentUrl'   => $video->content_url,
122
				'embedUrl'     => $video->embed_url,
123
				'uploadDate'   => $video->upload_date,
124
				'thumbnailUrl' => $video->thumbnail_urls,
125
				'duration'     => $video->duration,
126
			);
127
128
			if ( $video->views ) {
129
				$single_jsonld['interactionStatistic'] = array(
130
					'@type'                => 'InteractionCounter',
131
					'interactionType'      => array(
132
						'@type' => 'http://schema.org/WatchAction'
133
					),
134
					'userInteractionCount' => $video->views
135
				);
136
			}
137
138
			if ( $video->is_live_video ) {
139
				$single_jsonld['publication'] = array(
140
					'@type'           => 'BroadcastEvent',
141
					'isLiveBroadcast' => true,
142
					'startDate'       => $video->live_video_start_date,
143
					'endDate'         => $video->live_video_end_date
144
				);
145
			}
146
147
			$jsonld[] = $single_jsonld;
148
		}
149
150
		return $jsonld;
151
	}
152
153
154
	private function is_associative_array( $arr ) {
155
		if ( array() === $arr ) {
156
			return false;
157
		}
158
159
		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
160
	}
161
162
163
}
164