Completed
Push — develop ( 5b00d7...84128b )
by
unknown
03:48 queued 12s
created

Jsonld   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 170
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C wl_after_get_jsonld() 0 38 12
A merge_video_data() 0 15 3
A wl_post_jsonld() 0 21 3
B get_videos_jsonld() 0 51 5
A is_associative_array() 0 7 2
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
		// check if we have @id in jsonld for first item.
54
		$id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : '';
55
56
		foreach ( $videos_jsonld as &$video_jsonld ) {
57
			if ( ! $id ) {
58
				continue;
59
			}
60
			if ( ! array_key_exists( 'mentions', $video_jsonld ) ) {
61
				$video_jsonld['mentions'] = array( '@id' => $id );
62
			} else {
63
				$video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) );
64
			}
65
		}
66
67
		return array_merge( $jsonld, $videos_jsonld );
68
	}
69
70
	/**
71
	 * @param $existing_video_data string | array associative or sequential array.
72
	 * @param $new_video_data array Sequential array.
73
	 *
74
	 * @return array
75
	 */
76
	private function merge_video_data( $existing_video_data, $new_video_data ) {
77
		if ( ! is_array( $existing_video_data ) ) {
78
			$new_video_data[] = $existing_video_data;
79
80
			return $new_video_data;
81
		}
82
83
		if ( $this->is_associative_array( $existing_video_data ) ) {
84
			$new_video_data[] = $existing_video_data;
85
86
			return $new_video_data;
87
		}
88
89
		return array_merge( $existing_video_data, $new_video_data );
90
	}
91
92
	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...
93
94
		$video_jsonld = $this->get_videos_jsonld( $post_id );
95
		if ( count( $video_jsonld ) === 0 ) {
96
			return $jsonld;
97
		}
98
		// Before adding the video jsonld check if the key
99
		// is present and additional data might be present,
100
		// if not present just add the data and return early.
101
		if ( ! array_key_exists( 'video', $jsonld ) ) {
102
			$jsonld['video'] = $video_jsonld;
103
104
			return $jsonld;
105
		}
106
107
		// since key exists, we need to merge the data based on type.
108
		$previous_video_data = $jsonld['video'];
109
		$jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
110
111
		return $jsonld;
112
	}
113
114
115
	/**
116
	 * @param $post_id int Post id.
117
	 *
118
	 * @return array
119
	 */
120
	public function get_videos_jsonld( $post_id ) {
121
122
		$videos = $this->video_storage->get_all_videos( $post_id );
123
124
		$jsonld = array();
125
126
		foreach ( $videos as $video ) {
127
			/**
128
			 * @var $video Video
129
			 */
130
			$description = $video->description;
131
			if ( ! $video->description ) {
132
				// If description is empty then use the video title as description
133
				$description = $video->name;
134
			}
135
			$single_jsonld = array(
136
				'@context'     => 'http://schema.org',
137
				'@type'        => 'VideoObject',
138
				'name'         => $video->name,
139
				'description'  => $description,
140
				'contentUrl'   => $video->content_url,
141
				'embedUrl'     => $video->embed_url,
142
				'uploadDate'   => $video->upload_date,
143
				'thumbnailUrl' => $video->thumbnail_urls,
144
				'duration'     => $video->duration,
145
			);
146
147
			if ( $video->views ) {
148
				$single_jsonld['interactionStatistic'] = array(
149
					'@type'                => 'InteractionCounter',
150
					'interactionType'      => array(
151
						'@type' => 'http://schema.org/WatchAction'
152
					),
153
					'userInteractionCount' => $video->views
154
				);
155
			}
156
157
			if ( $video->is_live_video ) {
158
				$single_jsonld['publication'] = array(
159
					'@type'           => 'BroadcastEvent',
160
					'isLiveBroadcast' => true,
161
					'startDate'       => $video->live_video_start_date,
162
					'endDate'         => $video->live_video_end_date
163
				);
164
			}
165
166
			$jsonld[] = $single_jsonld;
167
		}
168
169
		return $jsonld;
170
	}
171
172
173
	private function is_associative_array( $arr ) {
174
		if ( array() === $arr ) {
175
			return false;
176
		}
177
178
		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
179
	}
180
181
182
}
183