Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Last active August 10, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/f0161a132bf133b13768e83b75edfd1d to your computer and use it in GitHub Desktop.
Save panoslyrakis/f0161a132bf133b13768e83b75edfd1d to your computer and use it in GitHub Desktop.
It will try fetch feeds with file_get_contents() instead of using simplepie
<?php
/*
Plugin Name: Custom Feed Importer
Plugin URI: https://premium.wpmudev.org/
Description: It will try fetch feeds with file_get_contents
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Custom_Feed_Importer' ) ) {
class WPMUDEV_Custom_Feed_Importer {
private static $_instance = null;
var $feed_urls = array();
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Custom_Feed_Importer();
}
return self::$_instance;
}
private function __construct() {
$this->feed_urls = array(
'http://siteone.com/feed/',
'https://sitetwo.com/feed'
);
add_action( 'wp', array( $this, 'maybe_shcedule_job' ) );
add_action( 'wpmudev_feed_schedule', array( $this, 'fetch_feeds' ) );
}
public function maybe_shcedule_job(){
if ( !wp_next_scheduled( 'wpmudev_feed_schedule' ) ) {
wp_schedule_event( time(), 'daily', 'wpmudev_feed_schedule' );
}
}
public function fetch_feeds(){
foreach( $this->feed_urls as $feed_url ){
self::fetch_feed( $feed_url );
}
}
public static function fetch_feed( $feed_url = null ){
if( is_null( $feed_url ) ){
return;
}
$feed_content = file_get_contents( $feed_url );
self::import_feed_content( $feed_content );
}
public static function import_feed_content( $feed_content = null ){
if( is_null( $feed_content ) ){
return;
}
$x = new SimpleXmlElement( $feed_content );
foreach( $x->channel->item as $entry ) {
if( get_page_by_title( $entry->title, OBJECT, 'post' ) ){
continue;
}
$new_post = array(
'post_title' => $entry->title,
'post_content' => $entry->description,
'post_status' => 'publish',
'post_type' => 'post'
);
$post_id = wp_insert_post( $new_post );
if( $img = $entry->enclosure->attributes()->{'url'} ){
if( self::is_valid_image( $img ) ){
$attachment_id = self::import_image( $img );
if( $attachment_id ){
set_post_thumbnail( $post_id, $attachment_id );
}
}
}
}
}
public static function is_valid_image( $img_url = null ){
if( is_null( $img_url ) ){
return false;
}
list($width, $height, $type, $attr) = getimagesize( $img_url );
$allowed_image_types = array( IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF );
return isset( $type ) && in_array( $type, $allowed_image_types );
}
public static function import_image( $img_url = null ){
if( is_null( $img_url ) ){
return false;
}
$filename = @end( explode( '/', $img_url ) );
$uploaddir = wp_upload_dir();
$uploadfile = $uploaddir['path'] . '/' . $filename;
$contents= file_get_contents( $img_url );
$savefile = fopen( $uploadfile, 'w' );
fwrite( $savefile, $contents );
fclose( $savefile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => $filename,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
$imagenew = get_post( $attach_id );
$fullsizepath = get_attached_file( $imagenew->ID );
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id, $attach_data );
return $attach_id;
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_Custom_Feed_Importer'] = WPMUDEV_Custom_Feed_Importer::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment