1371 | | wp_widget_rss_output( $args['url'], $args ); |
1372 | | echo "</div>"; |
| 1374 | |
| 1375 | $rss = fetch_feed( $args['url'] ); |
| 1376 | |
| 1377 | if ( is_wp_error( $rss ) ) { |
| 1378 | if ( is_admin() || current_user_can( 'manage_options' ) ) { |
| 1379 | echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>'; |
| 1380 | } |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | $default_args = array( |
| 1385 | 'show_author' => 0, |
| 1386 | 'show_date' => 0, |
| 1387 | 'show_summary' => 0, |
| 1388 | 'items' => 0, |
| 1389 | ); |
| 1390 | $args = wp_parse_args( $args, $default_args ); |
| 1391 | |
| 1392 | $items = (int) $args['items']; |
| 1393 | if ( $items < 1 || 20 < $items ) { |
| 1394 | $items = 10; |
| 1395 | } |
| 1396 | $show_summary = (int) $args['show_summary']; |
| 1397 | $show_author = (int) $args['show_author']; |
| 1398 | $show_date = (int) $args['show_date']; |
| 1399 | |
| 1400 | if ( ! $rss->get_item_quantity() ) { |
| 1401 | echo '<ul><li>' . esc_html__( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; |
| 1402 | $rss->__destruct(); |
| 1403 | unset( $rss ); |
| 1404 | return; |
| 1405 | } |
| 1406 | |
| 1407 | $maxitems = $rss->get_item_quantity(); |
| 1408 | $rss_items = $rss->get_items( 0, $maxitems ); |
| 1409 | |
| 1410 | $count = 0; |
| 1411 | |
| 1412 | echo '<ul>'; |
| 1413 | foreach ( $rss_items as $item ) { |
| 1414 | $link = $item->get_link(); |
| 1415 | while ( stristr( $link, 'http' ) !== $link ) { |
| 1416 | $link = substr( $link, 1 ); |
| 1417 | } |
| 1418 | $link = esc_url( strip_tags( $link ) ); |
| 1419 | |
| 1420 | // Skip this item if the link was already displayed. |
| 1421 | if ( in_array( $link, $displayed_links, true ) ) { |
| 1422 | continue; |
| 1423 | } |
| 1424 | |
| 1425 | $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); |
| 1426 | if ( empty( $title ) ) { |
| 1427 | $title = __( 'Untitled' ); |
| 1428 | } |
| 1429 | |
| 1430 | $desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); |
| 1431 | $desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) ); |
| 1432 | |
| 1433 | $summary = ''; |
| 1434 | if ( $show_summary ) { |
| 1435 | $summary = $desc; |
| 1436 | |
| 1437 | // Change existing [...] to […]. |
| 1438 | if ( '[...]' === substr( $summary, -5 ) ) { |
| 1439 | $summary = substr( $summary, 0, -5 ) . '[…]'; |
| 1440 | } |
| 1441 | |
| 1442 | $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>'; |
| 1443 | } |
| 1444 | |
| 1445 | $date = ''; |
| 1446 | if ( $show_date ) { |
| 1447 | $date = $item->get_date( 'U' ); |
| 1448 | |
| 1449 | if ( $date ) { |
| 1450 | $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>'; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | $author = ''; |
| 1455 | if ( $show_author ) { |
| 1456 | $author = $item->get_author(); |
| 1457 | if ( is_object( $author ) ) { |
| 1458 | $author = $author->get_name(); |
| 1459 | $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>'; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | if ( '' === $link ) { |
| 1464 | echo "<li>$title{$date}{$summary}{$author}</li>"; |
| 1465 | } elseif ( $show_summary ) { |
| 1466 | echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>"; |
| 1467 | } else { |
| 1468 | echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>"; |
| 1469 | } |
| 1470 | |
| 1471 | $displayed_links[] = $link; |
| 1472 | $count++; |
| 1473 | |
| 1474 | // Stop if we displayed the requested number of items. |
| 1475 | if ( $count === $items ) { |
| 1476 | break; |
| 1477 | } |
| 1478 | |
| 1479 | } |
| 1480 | echo '</ul>'; |
| 1481 | $rss->__destruct(); |
| 1482 | unset( $rss ); |
| 1483 | |
| 1484 | echo '</div>'; |