Completed
Pull Request — develop (#1388)
by Naveen
03:10
created
src/wordlift/videoobject/jsonld/class-jsonld.php 2 patches
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -11,164 +11,164 @@
 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 ( ! array_key_exists( 'mentions', $video_jsonld ) ) {
58
-				$video_jsonld['mentions'] = array( '@id' => $id );
59
-			} else {
60
-				$video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) );
61
-			}
62
-		}
63
-
64
-		return array_merge( $jsonld, $videos_jsonld );
65
-	}
66
-
67
-	/**
68
-	 * @param $existing_video_data string | array associative or sequential array.
69
-	 * @param $new_video_data array Sequential array.
70
-	 *
71
-	 * @return array
72
-	 */
73
-	private function merge_video_data( $existing_video_data, $new_video_data ) {
74
-		if ( ! is_array( $existing_video_data ) ) {
75
-			$new_video_data[] = $existing_video_data;
76
-
77
-			return $new_video_data;
78
-		}
79
-
80
-		if ( $this->is_associative_array( $existing_video_data ) ) {
81
-			$new_video_data[] = $existing_video_data;
82
-
83
-			return $new_video_data;
84
-		}
85
-
86
-		return array_merge( $existing_video_data, $new_video_data );
87
-	}
88
-
89
-	public function wl_post_jsonld( $jsonld, $post_id, $references ) {
90
-
91
-		$video_jsonld = $this->get_videos_jsonld( $post_id );
92
-		if ( count( $video_jsonld ) === 0 ) {
93
-			return $jsonld;
94
-		}
95
-		// Before adding the video jsonld check if the key
96
-		// is present and additional data might be present,
97
-		// if not present just add the data and return early.
98
-		if ( ! array_key_exists( 'video', $jsonld ) ) {
99
-			$jsonld['video'] = $video_jsonld;
100
-
101
-			return $jsonld;
102
-		}
103
-
104
-		// since key exists, we need to merge the data based on type.
105
-		$previous_video_data = $jsonld['video'];
106
-		$jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
107
-
108
-		return $jsonld;
109
-	}
110
-
111
-
112
-	/**
113
-	 * @param $post_id int Post id.
114
-	 *
115
-	 * @return array
116
-	 */
117
-	public function get_videos_jsonld( $post_id ) {
118
-
119
-		$videos = $this->video_storage->get_all_videos( $post_id );
120
-
121
-		$jsonld = array();
122
-
123
-		foreach ( $videos as $video ) {
124
-			/**
125
-			 * @var $video Video
126
-			 */
127
-			$single_jsonld = array(
128
-				'@context'     => 'http://schema.org',
129
-				'@type'        => 'VideoObject',
130
-				'name'         => $video->name,
131
-				'description'  => $video->description,
132
-				'contentUrl'   => $video->content_url,
133
-				'embedUrl'     => $video->embed_url,
134
-				'uploadDate'   => $video->upload_date,
135
-				'thumbnailUrl' => $video->thumbnail_urls,
136
-				'duration'     => $video->duration,
137
-			);
138
-
139
-			if ( $video->views ) {
140
-				$single_jsonld['interactionStatistic'] = array(
141
-					'@type'                => 'InteractionCounter',
142
-					'interactionType'      => array(
143
-						'@type' => 'http://schema.org/WatchAction'
144
-					),
145
-					'userInteractionCount' => $video->views
146
-				);
147
-			}
148
-
149
-			if ( $video->is_live_video ) {
150
-				$single_jsonld['publication'] = array(
151
-					'@type'           => 'BroadcastEvent',
152
-					'isLiveBroadcast' => true,
153
-					'startDate'       => $video->live_video_start_date,
154
-					'endDate'         => $video->live_video_end_date
155
-				);
156
-			}
157
-
158
-			$jsonld[] = $single_jsonld;
159
-		}
160
-
161
-		return $jsonld;
162
-	}
163
-
164
-
165
-	private function is_associative_array( $arr ) {
166
-		if ( array() === $arr ) {
167
-			return false;
168
-		}
169
-
170
-		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
171
-	}
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 ( ! array_key_exists( 'mentions', $video_jsonld ) ) {
58
+                $video_jsonld['mentions'] = array( '@id' => $id );
59
+            } else {
60
+                $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) );
61
+            }
62
+        }
63
+
64
+        return array_merge( $jsonld, $videos_jsonld );
65
+    }
66
+
67
+    /**
68
+     * @param $existing_video_data string | array associative or sequential array.
69
+     * @param $new_video_data array Sequential array.
70
+     *
71
+     * @return array
72
+     */
73
+    private function merge_video_data( $existing_video_data, $new_video_data ) {
74
+        if ( ! is_array( $existing_video_data ) ) {
75
+            $new_video_data[] = $existing_video_data;
76
+
77
+            return $new_video_data;
78
+        }
79
+
80
+        if ( $this->is_associative_array( $existing_video_data ) ) {
81
+            $new_video_data[] = $existing_video_data;
82
+
83
+            return $new_video_data;
84
+        }
85
+
86
+        return array_merge( $existing_video_data, $new_video_data );
87
+    }
88
+
89
+    public function wl_post_jsonld( $jsonld, $post_id, $references ) {
90
+
91
+        $video_jsonld = $this->get_videos_jsonld( $post_id );
92
+        if ( count( $video_jsonld ) === 0 ) {
93
+            return $jsonld;
94
+        }
95
+        // Before adding the video jsonld check if the key
96
+        // is present and additional data might be present,
97
+        // if not present just add the data and return early.
98
+        if ( ! array_key_exists( 'video', $jsonld ) ) {
99
+            $jsonld['video'] = $video_jsonld;
100
+
101
+            return $jsonld;
102
+        }
103
+
104
+        // since key exists, we need to merge the data based on type.
105
+        $previous_video_data = $jsonld['video'];
106
+        $jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
107
+
108
+        return $jsonld;
109
+    }
110
+
111
+
112
+    /**
113
+     * @param $post_id int Post id.
114
+     *
115
+     * @return array
116
+     */
117
+    public function get_videos_jsonld( $post_id ) {
118
+
119
+        $videos = $this->video_storage->get_all_videos( $post_id );
120
+
121
+        $jsonld = array();
122
+
123
+        foreach ( $videos as $video ) {
124
+            /**
125
+             * @var $video Video
126
+             */
127
+            $single_jsonld = array(
128
+                '@context'     => 'http://schema.org',
129
+                '@type'        => 'VideoObject',
130
+                'name'         => $video->name,
131
+                'description'  => $video->description,
132
+                'contentUrl'   => $video->content_url,
133
+                'embedUrl'     => $video->embed_url,
134
+                'uploadDate'   => $video->upload_date,
135
+                'thumbnailUrl' => $video->thumbnail_urls,
136
+                'duration'     => $video->duration,
137
+            );
138
+
139
+            if ( $video->views ) {
140
+                $single_jsonld['interactionStatistic'] = array(
141
+                    '@type'                => 'InteractionCounter',
142
+                    'interactionType'      => array(
143
+                        '@type' => 'http://schema.org/WatchAction'
144
+                    ),
145
+                    'userInteractionCount' => $video->views
146
+                );
147
+            }
148
+
149
+            if ( $video->is_live_video ) {
150
+                $single_jsonld['publication'] = array(
151
+                    '@type'           => 'BroadcastEvent',
152
+                    'isLiveBroadcast' => true,
153
+                    'startDate'       => $video->live_video_start_date,
154
+                    'endDate'         => $video->live_video_end_date
155
+                );
156
+            }
157
+
158
+            $jsonld[] = $single_jsonld;
159
+        }
160
+
161
+        return $jsonld;
162
+    }
163
+
164
+
165
+    private function is_associative_array( $arr ) {
166
+        if ( array() === $arr ) {
167
+            return false;
168
+        }
169
+
170
+        return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
171
+    }
172 172
 
173 173
 
174 174
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -21,47 +21,47 @@  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 ) {
57
-			if ( ! array_key_exists( 'mentions', $video_jsonld ) ) {
58
-				$video_jsonld['mentions'] = array( '@id' => $id );
56
+		foreach ($videos_jsonld as &$video_jsonld) {
57
+			if ( ! array_key_exists('mentions', $video_jsonld)) {
58
+				$video_jsonld['mentions'] = array('@id' => $id);
59 59
 			} else {
60
-				$video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) );
60
+				$video_jsonld['mentions'] = array_merge($video_jsonld['mentions'], array('@id' => $id));
61 61
 			}
62 62
 		}
63 63
 
64
-		return array_merge( $jsonld, $videos_jsonld );
64
+		return array_merge($jsonld, $videos_jsonld);
65 65
 	}
66 66
 
67 67
 	/**
@@ -70,32 +70,32 @@  discard block
 block discarded – undo
70 70
 	 *
71 71
 	 * @return array
72 72
 	 */
73
-	private function merge_video_data( $existing_video_data, $new_video_data ) {
74
-		if ( ! is_array( $existing_video_data ) ) {
73
+	private function merge_video_data($existing_video_data, $new_video_data) {
74
+		if ( ! is_array($existing_video_data)) {
75 75
 			$new_video_data[] = $existing_video_data;
76 76
 
77 77
 			return $new_video_data;
78 78
 		}
79 79
 
80
-		if ( $this->is_associative_array( $existing_video_data ) ) {
80
+		if ($this->is_associative_array($existing_video_data)) {
81 81
 			$new_video_data[] = $existing_video_data;
82 82
 
83 83
 			return $new_video_data;
84 84
 		}
85 85
 
86
-		return array_merge( $existing_video_data, $new_video_data );
86
+		return array_merge($existing_video_data, $new_video_data);
87 87
 	}
88 88
 
89
-	public function wl_post_jsonld( $jsonld, $post_id, $references ) {
89
+	public function wl_post_jsonld($jsonld, $post_id, $references) {
90 90
 
91
-		$video_jsonld = $this->get_videos_jsonld( $post_id );
92
-		if ( count( $video_jsonld ) === 0 ) {
91
+		$video_jsonld = $this->get_videos_jsonld($post_id);
92
+		if (count($video_jsonld) === 0) {
93 93
 			return $jsonld;
94 94
 		}
95 95
 		// Before adding the video jsonld check if the key
96 96
 		// is present and additional data might be present,
97 97
 		// if not present just add the data and return early.
98
-		if ( ! array_key_exists( 'video', $jsonld ) ) {
98
+		if ( ! array_key_exists('video', $jsonld)) {
99 99
 			$jsonld['video'] = $video_jsonld;
100 100
 
101 101
 			return $jsonld;
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
 
104 104
 		// since key exists, we need to merge the data based on type.
105 105
 		$previous_video_data = $jsonld['video'];
106
-		$jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
106
+		$jsonld['video']     = $this->merge_video_data($previous_video_data, $video_jsonld);
107 107
 
108 108
 		return $jsonld;
109 109
 	}
@@ -114,13 +114,13 @@  discard block
 block discarded – undo
114 114
 	 *
115 115
 	 * @return array
116 116
 	 */
117
-	public function get_videos_jsonld( $post_id ) {
117
+	public function get_videos_jsonld($post_id) {
118 118
 
119
-		$videos = $this->video_storage->get_all_videos( $post_id );
119
+		$videos = $this->video_storage->get_all_videos($post_id);
120 120
 
121 121
 		$jsonld = array();
122 122
 
123
-		foreach ( $videos as $video ) {
123
+		foreach ($videos as $video) {
124 124
 			/**
125 125
 			 * @var $video Video
126 126
 			 */
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 				'duration'     => $video->duration,
137 137
 			);
138 138
 
139
-			if ( $video->views ) {
139
+			if ($video->views) {
140 140
 				$single_jsonld['interactionStatistic'] = array(
141 141
 					'@type'                => 'InteractionCounter',
142 142
 					'interactionType'      => array(
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 				);
147 147
 			}
148 148
 
149
-			if ( $video->is_live_video ) {
149
+			if ($video->is_live_video) {
150 150
 				$single_jsonld['publication'] = array(
151 151
 					'@type'           => 'BroadcastEvent',
152 152
 					'isLiveBroadcast' => true,
@@ -162,12 +162,12 @@  discard block
 block discarded – undo
162 162
 	}
163 163
 
164 164
 
165
-	private function is_associative_array( $arr ) {
166
-		if ( array() === $arr ) {
165
+	private function is_associative_array($arr) {
166
+		if (array() === $arr) {
167 167
 			return false;
168 168
 		}
169 169
 
170
-		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
170
+		return array_keys($arr) !== range(0, count($arr) - 1);
171 171
 	}
172 172
 
173 173
 
Please login to merge, or discard this patch.