@@ -197,11 +197,11 @@ public function enqueue_styles() {
197
197
/**
198
198
* Add attributes to $item_array.
199
199
*
200
- * @param array $item_array The item attributes array.
201
- * @param object $item The feed item.
202
- * @param array $sc The shortcode attributes array.
203
- * @param int $index The item number.
204
- * @param int $item_index The real index of this item in the feed.
200
+ * @param array $item_array The item attributes array.
201
+ * @param SimplePie\Item $item The feed item.
202
+ * @param array $sc The shortcode attributes array.
203
+ * @param int $index The item number.
204
+ * @param int $item_index The real index of this item in the feed.
205
205
* @return mixed
206
206
* @since 1.0.0
207
207
* @access public
@@ -216,29 +216,92 @@ public function add_data_to_item( $item_array, $item, $sc = null, $index = null,
216
216
$ item_array ['item ' ] = $ item ;
217
217
$ item_array ['item_index ' ] = $ item_index ;
218
218
219
+ $ item_array = $ this ->handle_youtube_content ( $ item_array , $ item , $ sc );
220
+
221
+ return $ item_array ;
222
+ }
223
+
224
+ /**
225
+ * Fetches additional information for each item.
226
+ *
227
+ * @param array<string, string > $item_array The item attributes array.
228
+ * @param SimplePie\Item $item The feed item.
229
+ * @param array<string, mixed> $sc The shortcode attributes array. This will be empty through the block editor.
230
+ *
231
+ * @return array<string, string>
232
+ */
233
+ private function handle_youtube_content ( $ item_array , $ item , $ sc ) {
234
+ $ url = '' ;
235
+ if ( array_key_exists ( 'item_url ' , $ item_array ) ) {
236
+ $ url = $ item_array ['item_url ' ];
237
+ } elseif ( $ item ) {
238
+ $ url = $ item ->get_permalink ();
239
+ }
240
+
241
+ if ( empty ( $ url ) ) {
242
+ return $ item_array ;
243
+ }
244
+
245
+ $ host = wp_parse_url ( $ url , PHP_URL_HOST );
246
+
247
+ // Remove all dots in the hostname so that shortforms such as youtu.be can also be resolved.
248
+ $ host = str_replace ( array ( '. ' , 'www ' ), '' , $ host );
249
+
250
+ if ( ! in_array ( $ host , array ( 'youtubecom ' , 'youtube ' ), true ) ) {
251
+ // Not a YouTube link, return the item array as is.
252
+ return $ item_array ;
253
+ }
254
+
255
+ $ tags = $ item ->get_item_tags ( \SimplePie \SimplePie::NAMESPACE_MEDIARSS , 'group ' );
256
+ $ desc = '' ;
257
+ if ( $ tags ) {
258
+ $ desc_tag = $ tags [0 ]['child ' ][ \SimplePie \SimplePie::NAMESPACE_MEDIARSS ]['description ' ];
259
+ if ( $ desc_tag ) {
260
+ $ desc = $ desc_tag [0 ]['data ' ];
261
+ }
262
+ }
263
+
264
+ if ( ! empty ( $ desc ) ) {
265
+ if ( empty ( $ item_array ['item_content ' ] ) ) {
266
+ $ item_array ['item_content ' ] = $ desc ;
267
+ }
268
+
269
+ if (
270
+ ( empty ( $ sc ) || 'yes ' === $ sc ['summary ' ] ) &&
271
+ empty ( $ item_array ['item_description ' ] )
272
+ ) {
273
+ if (
274
+ is_numeric ( $ sc ['summarylength ' ] ) &&
275
+ strlen ( $ desc ) > $ sc ['summarylength ' ]
276
+ ) {
277
+ $ desc = preg_replace ( '/\s+?(\S+)?$/ ' , '' , substr ( $ desc , 0 , $ sc ['summarylength ' ] ) ) . ' […] ' ;
278
+ }
279
+ $ item_array ['item_description ' ] = $ desc ;
280
+ }
281
+ }
282
+
283
+ $ embed_video_shortcode = '[embed] ' . $ url . '[/embed] ' ;
284
+ $ should_overwrite = str_contains ( $ item_array ['item_content ' ], 'Post Content ' );
285
+ $ item_array ['item_content ' ] = ( $ should_overwrite ? '' : $ item_array ['item_content ' ] ) . $ embed_video_shortcode ;
286
+
219
287
return $ item_array ;
220
288
}
221
289
222
290
/**
223
291
* Retrieve the categories.
224
292
*
225
- * @param string $dumb The initial categories (only a placeholder argument for the filter).
226
- * @param object $item The feed item.
293
+ * @param string $dumb The initial categories (only a placeholder argument for the filter).
294
+ * @param SimplePie\Item $item The feed item.
227
295
*
228
296
* @return string
229
- * @since ?
230
297
* @access public
231
298
*/
232
299
public function retrieve_categories ( $ dumb , $ item ) {
233
300
$ cats = array ();
234
301
$ categories = $ item ->get_categories ();
235
302
if ( $ categories ) {
236
303
foreach ( $ categories as $ category ) {
237
- if ( is_string ( $ category ) ) {
238
- $ cats [] = $ category ;
239
- } else {
240
- $ cats [] = $ category ->get_label ();
241
- }
304
+ $ cats [] = $ category ->get_label ();
242
305
}
243
306
}
244
307
0 commit comments