| @@ -12,8 +12,12 @@ | ||
| 12 | 12 | |
| 13 | 13 | add_filter( 'the_excerpt_rss', array( __CLASS__, 'insert_featured_image_rss_feed' ) ); |
| 14 | 14 | add_filter( 'the_content_feed', array( __CLASS__, 'insert_featured_image_rss_feed' ) ); |
| 15 | 15 | |
| 16 | + add_filter( 'commentrss2_item', array( __CLASS__, 'insert_avatar_comments_feed' ), 10, 2 ); | |
| 17 | + | |
| 18 | + add_filter( 'wp_feed_options', array( __CLASS__, 'set_user_agent' ), 10, 2 ); | |
| 19 | + | |
| 16 | 20 | } |
| 17 | 21 | |
| 18 | 22 | public static function insert_featured_image_rss_feed( $content ){ |
| 19 | 23 | |
| @@ -18,9 +22,9 @@ | ||
| 18 | 22 | public static function insert_featured_image_rss_feed( $content ){ |
| 19 | 23 | |
| 20 | 24 | global $post; |
| 21 | 25 | |
| 22 | - preg_match_all( '~<img.*?src=["\']+(.*?)["\']+~', $content, $image_urls ); | |
| 26 | + preg_match_all( '~<img.*?src=["\']+(.*?)["\']+~', $content ?? '', $image_urls ); | |
| 23 | 27 | |
| 24 | 28 | if( empty( $image_urls[1] ) && has_post_thumbnail( $post->ID ) ) { |
| 25 | 29 | $content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content; |
| 26 | 30 | } |
| @@ -25,8 +29,81 @@ | ||
| 25 | 29 | $content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content; |
| 26 | 30 | } |
| 27 | 31 | |
| 28 | 32 | return $content; |
| 33 | + | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static function insert_avatar_comments_feed( $comment_id, $comment_post_id ){ | |
| 37 | + | |
| 38 | + $comment = get_comment( $comment_id ); | |
| 39 | + $avatar_url = get_avatar_url( $comment ); | |
| 40 | + | |
| 41 | + if( $avatar_url ){ | |
| 42 | + echo '<media:thumbnail url="' . esc_attr( $avatar_url ) . '" />'; | |
| 43 | + } | |
| 44 | + | |
| 45 | + } | |
| 46 | + | |
| 47 | + public static function set_user_agent( &$feed, $url ) { | |
| 48 | + | |
| 49 | + $feed->set_useragent( 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' ); | |
| 50 | + | |
| 51 | + } | |
| 52 | + | |
| 53 | + public static function date_i18n( $format, $timestamp, $timezone ){ | |
| 54 | + | |
| 55 | + try{ | |
| 56 | + $timezone = new DateTimeZone( trim( $timezone ) ); | |
| 57 | + }catch( Exception $e ) { | |
| 58 | + $timezone = new DateTimeZone( 'UTC' ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $date = wp_date( $format, $timestamp, $timezone ); | |
| 62 | + | |
| 63 | + return $date; | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + public static function is_valid_image_url( $item, $url ){ | |
| 68 | + | |
| 69 | + // Does it begin with http? | |
| 70 | + if( strpos( $url, 'http' ) === 0 ){ | |
| 71 | + return true; | |
| 72 | + } | |
| 73 | + | |
| 74 | + // If it does not begin with http, then check if the RSS feed is of the current domain | |
| 75 | + $wp_domain = parse_url( get_site_url(), PHP_URL_HOST ); | |
| 76 | + return strpos( $item->get_link(), $wp_domain ) !== false; | |
| 77 | + | |
| 78 | + } | |
| 79 | + | |
| 80 | + public static function parse_image_url( $item, $content ){ | |
| 81 | + | |
| 82 | + $attributes = array( | |
| 83 | + 'src' => '~<img.*?src=["\']+(.*?)["\']+~', | |
| 84 | + 'srcset' => '~<img.*?srcset=["\']+(.*?)["\']+~' | |
| 85 | + ); | |
| 86 | + | |
| 87 | + foreach( $attributes as $attribute => $regex ){ | |
| 88 | + preg_match( $regex, $content ?? '', $urls ); | |
| 89 | + | |
| 90 | + if( empty( $urls ) ){ | |
| 91 | + continue; | |
| 92 | + } | |
| 93 | + $parsed_url = $urls[1]; | |
| 94 | + | |
| 95 | + if( $attribute == 'srcset' ){ | |
| 96 | + $srcset_urls = explode( ',' , $parsed_url ); | |
| 97 | + $parsed_url = explode( ' ', $srcset_urls[0] )[0]; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if( self::is_valid_image_url( $item, $parsed_url ) ){ | |
| 101 | + return $parsed_url; | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + return ''; | |
| 29 | 106 | |
| 30 | 107 | } |
| 31 | 108 | |
| 32 | 109 | } |