Completed
Pull Request — develop (#1388)
by Naveen
05:39 queued 02:11
created
src/wordlift/videoobject/jsonld/class-jsonld.php 2 patches
Indentation   +161 added lines, -161 removed lines patch added patch discarded remove patch
@@ -11,167 +11,167 @@
 block discarded – undo
11 11
 use Wordlift\Videoobject\Data\Video_Storage\Storage;
12 12
 
13 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 ) {
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 ) {
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
-			$single_jsonld = array(
131
-				'@context'     => 'http://schema.org',
132
-				'@type'        => 'VideoObject',
133
-				'name'         => $video->name,
134
-				'description'  => $video->description,
135
-				'contentUrl'   => $video->content_url,
136
-				'embedUrl'     => $video->embed_url,
137
-				'uploadDate'   => $video->upload_date,
138
-				'thumbnailUrl' => $video->thumbnail_urls,
139
-				'duration'     => $video->duration,
140
-			);
141
-
142
-			if ( $video->views ) {
143
-				$single_jsonld['interactionStatistic'] = array(
144
-					'@type'                => 'InteractionCounter',
145
-					'interactionType'      => array(
146
-						'@type' => 'http://schema.org/WatchAction'
147
-					),
148
-					'userInteractionCount' => $video->views
149
-				);
150
-			}
151
-
152
-			if ( $video->is_live_video ) {
153
-				$single_jsonld['publication'] = array(
154
-					'@type'           => 'BroadcastEvent',
155
-					'isLiveBroadcast' => true,
156
-					'startDate'       => $video->live_video_start_date,
157
-					'endDate'         => $video->live_video_end_date
158
-				);
159
-			}
160
-
161
-			$jsonld[] = $single_jsonld;
162
-		}
163
-
164
-		return $jsonld;
165
-	}
166
-
167
-
168
-	private function is_associative_array( $arr ) {
169
-		if ( array() === $arr ) {
170
-			return false;
171
-		}
172
-
173
-		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
174
-	}
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 ) {
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 ) {
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
+            $single_jsonld = array(
131
+                '@context'     => 'http://schema.org',
132
+                '@type'        => 'VideoObject',
133
+                'name'         => $video->name,
134
+                'description'  => $video->description,
135
+                'contentUrl'   => $video->content_url,
136
+                'embedUrl'     => $video->embed_url,
137
+                'uploadDate'   => $video->upload_date,
138
+                'thumbnailUrl' => $video->thumbnail_urls,
139
+                'duration'     => $video->duration,
140
+            );
141
+
142
+            if ( $video->views ) {
143
+                $single_jsonld['interactionStatistic'] = array(
144
+                    '@type'                => 'InteractionCounter',
145
+                    'interactionType'      => array(
146
+                        '@type' => 'http://schema.org/WatchAction'
147
+                    ),
148
+                    'userInteractionCount' => $video->views
149
+                );
150
+            }
151
+
152
+            if ( $video->is_live_video ) {
153
+                $single_jsonld['publication'] = array(
154
+                    '@type'           => 'BroadcastEvent',
155
+                    'isLiveBroadcast' => true,
156
+                    'startDate'       => $video->live_video_start_date,
157
+                    'endDate'         => $video->live_video_end_date
158
+                );
159
+            }
160
+
161
+            $jsonld[] = $single_jsonld;
162
+        }
163
+
164
+        return $jsonld;
165
+    }
166
+
167
+
168
+    private function is_associative_array( $arr ) {
169
+        if ( array() === $arr ) {
170
+            return false;
171
+        }
172
+
173
+        return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
174
+    }
175 175
 
176 176
 
177 177
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -21,50 +21,50 @@  discard block
 block discarded – undo
21 21
 	 *
22 22
 	 * @param $video_storage Storage
23 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 );
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 27
 		$this->video_storage = $video_storage;
28 28
 	}
29 29
 
30 30
 
31
-	public function wl_after_get_jsonld( $jsonld, $post_id, $context ) {
32
-		if ( 0 === count( $jsonld ) ) {
31
+	public function wl_after_get_jsonld($jsonld, $post_id, $context) {
32
+		if (0 === count($jsonld)) {
33 33
 			return $jsonld;
34 34
 		}
35 35
 		$current_item = $jsonld[0];
36 36
 
37
-		if ( ! array_key_exists( '@type', $current_item ) ) {
37
+		if ( ! array_key_exists('@type', $current_item)) {
38 38
 			// Cant determine type return early.
39 39
 			return $jsonld;
40 40
 		}
41 41
 
42 42
 		$type = $current_item['@type'];
43
-		if ( ( is_string( $type ) && $type === 'Article' ) ||
44
-		     ( is_array( $type ) && in_array( 'Article', $type ) ) ) {
43
+		if ((is_string($type) && $type === 'Article') ||
44
+		     (is_array($type) && in_array('Article', $type))) {
45 45
 			return $jsonld;
46 46
 		}
47 47
 
48
-		$videos_jsonld = $this->get_videos_jsonld( $post_id );
49
-		if ( 0 === count( $videos_jsonld ) ) {
48
+		$videos_jsonld = $this->get_videos_jsonld($post_id);
49
+		if (0 === count($videos_jsonld)) {
50 50
 			return $jsonld;
51 51
 		}
52 52
 
53 53
 		// check if we have @id in jsonld for first item.
54
-		$id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : '';
54
+		$id = array_key_exists('@id', $current_item) ? $current_item['@id'] : '';
55 55
 
56
-		foreach ( $videos_jsonld as &$video_jsonld ) {
56
+		foreach ($videos_jsonld as &$video_jsonld) {
57 57
 			if ( ! $id) {
58 58
 				continue;
59 59
 			}
60
-			if ( ! array_key_exists( 'mentions', $video_jsonld ) ) {
61
-				$video_jsonld['mentions'] = array( '@id' => $id );
60
+			if ( ! array_key_exists('mentions', $video_jsonld)) {
61
+				$video_jsonld['mentions'] = array('@id' => $id);
62 62
 			} else {
63
-				$video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) );
63
+				$video_jsonld['mentions'] = array_merge($video_jsonld['mentions'], array('@id' => $id));
64 64
 			}
65 65
 		}
66 66
 
67
-		return array_merge( $jsonld, $videos_jsonld );
67
+		return array_merge($jsonld, $videos_jsonld);
68 68
 	}
69 69
 
70 70
 	/**
@@ -73,32 +73,32 @@  discard block
 block discarded – undo
73 73
 	 *
74 74
 	 * @return array
75 75
 	 */
76
-	private function merge_video_data( $existing_video_data, $new_video_data ) {
77
-		if ( ! is_array( $existing_video_data ) ) {
76
+	private function merge_video_data($existing_video_data, $new_video_data) {
77
+		if ( ! is_array($existing_video_data)) {
78 78
 			$new_video_data[] = $existing_video_data;
79 79
 
80 80
 			return $new_video_data;
81 81
 		}
82 82
 
83
-		if ( $this->is_associative_array( $existing_video_data ) ) {
83
+		if ($this->is_associative_array($existing_video_data)) {
84 84
 			$new_video_data[] = $existing_video_data;
85 85
 
86 86
 			return $new_video_data;
87 87
 		}
88 88
 
89
-		return array_merge( $existing_video_data, $new_video_data );
89
+		return array_merge($existing_video_data, $new_video_data);
90 90
 	}
91 91
 
92
-	public function wl_post_jsonld( $jsonld, $post_id, $references ) {
92
+	public function wl_post_jsonld($jsonld, $post_id, $references) {
93 93
 
94
-		$video_jsonld = $this->get_videos_jsonld( $post_id );
95
-		if ( count( $video_jsonld ) === 0 ) {
94
+		$video_jsonld = $this->get_videos_jsonld($post_id);
95
+		if (count($video_jsonld) === 0) {
96 96
 			return $jsonld;
97 97
 		}
98 98
 		// Before adding the video jsonld check if the key
99 99
 		// is present and additional data might be present,
100 100
 		// if not present just add the data and return early.
101
-		if ( ! array_key_exists( 'video', $jsonld ) ) {
101
+		if ( ! array_key_exists('video', $jsonld)) {
102 102
 			$jsonld['video'] = $video_jsonld;
103 103
 
104 104
 			return $jsonld;
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 
107 107
 		// since key exists, we need to merge the data based on type.
108 108
 		$previous_video_data = $jsonld['video'];
109
-		$jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
109
+		$jsonld['video']     = $this->merge_video_data($previous_video_data, $video_jsonld);
110 110
 
111 111
 		return $jsonld;
112 112
 	}
@@ -117,13 +117,13 @@  discard block
 block discarded – undo
117 117
 	 *
118 118
 	 * @return array
119 119
 	 */
120
-	public function get_videos_jsonld( $post_id ) {
120
+	public function get_videos_jsonld($post_id) {
121 121
 
122
-		$videos = $this->video_storage->get_all_videos( $post_id );
122
+		$videos = $this->video_storage->get_all_videos($post_id);
123 123
 
124 124
 		$jsonld = array();
125 125
 
126
-		foreach ( $videos as $video ) {
126
+		foreach ($videos as $video) {
127 127
 			/**
128 128
 			 * @var $video Video
129 129
 			 */
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 				'duration'     => $video->duration,
140 140
 			);
141 141
 
142
-			if ( $video->views ) {
142
+			if ($video->views) {
143 143
 				$single_jsonld['interactionStatistic'] = array(
144 144
 					'@type'                => 'InteractionCounter',
145 145
 					'interactionType'      => array(
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
 				);
150 150
 			}
151 151
 
152
-			if ( $video->is_live_video ) {
152
+			if ($video->is_live_video) {
153 153
 				$single_jsonld['publication'] = array(
154 154
 					'@type'           => 'BroadcastEvent',
155 155
 					'isLiveBroadcast' => true,
@@ -165,12 +165,12 @@  discard block
 block discarded – undo
165 165
 	}
166 166
 
167 167
 
168
-	private function is_associative_array( $arr ) {
169
-		if ( array() === $arr ) {
168
+	private function is_associative_array($arr) {
169
+		if (array() === $arr) {
170 170
 			return false;
171 171
 		}
172 172
 
173
-		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
173
+		return array_keys($arr) !== range(0, count($arr) - 1);
174 174
 	}
175 175
 
176 176
 
Please login to merge, or discard this patch.