@@ -197,11 +197,11 @@ public function enqueue_styles() {
197197 /**
198198 * Add attributes to $item_array.
199199 *
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.
205205 * @return mixed
206206 * @since 1.0.0
207207 * @access public
@@ -216,29 +216,92 @@ public function add_data_to_item( $item_array, $item, $sc = null, $index = null,
216216 $ item_array ['item ' ] = $ item ;
217217 $ item_array ['item_index ' ] = $ item_index ;
218218
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+
219287 return $ item_array ;
220288 }
221289
222290 /**
223291 * Retrieve the categories.
224292 *
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.
227295 *
228296 * @return string
229- * @since ?
230297 * @access public
231298 */
232299 public function retrieve_categories ( $ dumb , $ item ) {
233300 $ cats = array ();
234301 $ categories = $ item ->get_categories ();
235302 if ( $ categories ) {
236303 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 ();
242305 }
243306 }
244307
0 commit comments