| 1 |
<?php |
| 2 |
/** |
| 3 |
* Common utilities for the plugin |
| 4 |
* |
| 5 |
*/ |
| 6 |
|
| 7 |
if( ! defined( 'ABSPATH' ) ) exit; |
| 8 |
|
| 9 |
class SRR_Utilities{ |
| 10 |
|
| 11 |
public static function init(){ |
| 12 |
|
| 13 |
add_filter( 'the_excerpt_rss', array( __CLASS__, 'insert_featured_image_rss_feed' ) ); |
| 14 |
add_filter( 'the_content_feed', array( __CLASS__, 'insert_featured_image_rss_feed' ) ); |
| 15 |
|
| 16 |
add_filter( 'wp_feed_options', array( __CLASS__, 'set_user_agent' ), 10, 2 ); |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
public static function insert_featured_image_rss_feed( $content ){ |
| 21 |
|
| 22 |
global $post; |
| 23 |
|
| 24 |
preg_match_all( '~<img.*?src=["\']+(.*?)["\']+~', $content, $image_urls ); |
| 25 |
|
| 26 |
if( empty( $image_urls[1] ) && has_post_thumbnail( $post->ID ) ) { |
| 27 |
$content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content; |
| 28 |
} |
| 29 |
|
| 30 |
return $content; |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
public static function set_user_agent( &$feed, $url ) { |
| 35 |
|
| 36 |
$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' ); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
public static function date_i18n( $format, $timestamp, $timezone ){ |
| 41 |
|
| 42 |
try{ |
| 43 |
$timezone = new DateTimeZone( trim( $timezone ) ); |
| 44 |
}catch( Exception $e ) { |
| 45 |
$timezone = new DateTimeZone( 'UTC' ); |
| 46 |
} |
| 47 |
|
| 48 |
$date = wp_date( $format, $timestamp, $timezone ); |
| 49 |
|
| 50 |
return $date; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
SRR_Utilities::init(); |
| 57 |
|
| 58 |
?> |