Last active
February 8, 2016 15:20
-
-
Save alisspers/709fe3d7ae97e00fa770 to your computer and use it in GitHub Desktop.
Plugin to import from MyNewsDesk - full plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: MyNewsDesk Importer | |
Description: Imports news via API from NyNewsDesk as post type for news | |
Version: 1.0 | |
Author: Webbgaraget | |
Author URI: http://www.webbgaraget.se | |
*/ | |
class MND_News_Importer | |
{ | |
// URL for MyNewsDesk API | |
const MND_URL = 'http://www.mynewsdesk.com/services/pressroom/list/[unique_key]/?type_of_media=news&format=json'; | |
// Name of WP cron job | |
const CRON_NAME = 'mnd-import-api'; | |
// Meta key where posts feed id will be stored | |
const META_KEY = 'mnd-id'; | |
protected static $instance = null; | |
/** | |
* Access to the working instance of this class | |
* | |
* @wp-hook plugins_loaded | |
* @return Ngn_Pr_Importer | |
*/ | |
public static function get_instance() | |
{ | |
null === self::$instance and self::$instance = new self; | |
return self::$instance; | |
} | |
/** | |
* Constructor. Intentionally left empty and public. | |
* | |
* @see plugin_setup() | |
*/ | |
public function __construct() {} | |
/** | |
* Initalizes the plugin | |
* @wp-hook plugins_loaded | |
*/ | |
public function plugin_setup() | |
{ | |
$this->_schedule_import(); | |
add_action( self::CRON_NAME, array( $this, 'add_new_entries' ) ); | |
// If you add '/?mnd-news-force-import' to the url you force the import of news | |
if ( isset( $_GET['mnd-news-force-import'] ) ) | |
add_action( 'wp', array( $this, 'add_and_update_entries' ) ); | |
} | |
/** | |
* Schedule the import hook to be run hourly | |
*/ | |
private function _schedule_import() | |
{ | |
// If the schedule isn't set | |
if ( !wp_next_scheduled( self::CRON_NAME ) ) | |
{ | |
// Use the built in function wp_schedule event to run the function every hour | |
wp_schedule_event( time(), 'hourly', self::CRON_NAME ); | |
} | |
} | |
/** | |
* Import the entries | |
*/ | |
public function add_and_update_entries() | |
{ | |
// Get the news (see previous gist) | |
$entries = $this->_get_entries(); | |
// Create an empty array where we will save post information | |
$entry_info = []; | |
// This avoids an error being produced in WPML when doing wp_insert_post() | |
unset( $_GET['doing_wp_cron'] ); | |
// Loop through the news | |
foreach( $entries->items->item as $item ) | |
{ | |
// Save the information for the post creation | |
$id = $item->id; | |
$title = $item->header; | |
$excerpt = $item->summary; | |
$content = $item->body; | |
$dateTime = DateTime::createFromFormat( 'Y-m-d H:i:s', $item->updated_at ); | |
$date = $dateTime->format( "F j, Y" ); | |
$postdate = $dateTime->format( "Y-m-d H:i:s" ); | |
// Get the terms for the custom post type, in this case news-cat | |
$existing_terms = get_terms( 'news-cat' ); | |
// Create empty variables for saving the rest of the information you want | |
$image = ''; | |
$categories = ''; | |
$contacts = ''; | |
$boilerplate = ''; | |
$lang = ''; | |
// If the information you want i set, save it in the variable | |
if(isset($item->image)) | |
$image = $item->image; //string | |
if(isset($item->subjects) && is_object($item->subjects) ) | |
$categories = $item->subjects; //object | |
if(isset($item->contact_people) && is_object($item->contact_people) ) | |
$contacts = $item->contact_people; //object | |
if(isset($item->boilerplate)) | |
$boilerplate = $item->boilerplate; //string | |
if(isset($item->language)) | |
$lang = $item->language; //string | |
// Query existing posts of the cpt to see if there | |
// is a post with the current ID saved in a metabox | |
$args = array( | |
'meta_key' => self::META_KEY, | |
'meta_value' => $id, | |
'post_type' => 'cpt_news' | |
); | |
$query = new WP_Query( $args ); | |
// If post doesn't exist, create it | |
if ( $query->post_count < 1 ) | |
{ | |
// Setup post data | |
// Important, do not add the ID, then it will try to | |
// update a post with that ID (which does not exist) | |
$post_data = array( | |
// 'post_author' => 1, | |
'post_title' => $title, | |
'post_content' => $content, | |
'post_excerpt' => $excerpt, | |
'post_date' => $postdate, | |
'post_status' => 'publish', | |
'post_type' => 'cpt_news', | |
); | |
// Create post | |
$post_id = wp_insert_post( $post_data ); | |
// Add the news ID from MyNewsDesk as meta | |
add_post_meta( $post_id, self::META_KEY, $id ); | |
// Set was_created to true | |
$was_created = true; | |
// Save title, date, id and if it was new in our empty array | |
$entry_info[] = array( | |
'title' => $title, | |
'date' => $date, | |
'id' => $id, | |
'new' => $was_created, | |
); | |
// If language is set, populate meta box news-language | |
if( isset( $lang ) && $lang !== '' ) { | |
add_post_meta( $post_id, 'news-language', $lang ); | |
} | |
// If there is an image, populate meta box news-featured-image | |
if( isset( $image ) && $image !== '' ) { | |
add_post_meta( $post_id, 'news-featured-image', $image ); | |
} | |
// If there are categories, add the categories to the post | |
if( isset( $categories ) && $categories !== '' ) { | |
// If there is only one category it is saved as a string | |
if( is_string( $categories->subject ) ) | |
{ | |
// If the category doesn't exist, we create it | |
if( !in_array( $categories->subject , $existing_terms ) ) | |
{ | |
wp_insert_term( $categories->subject, 'news-cat' ); | |
} | |
$cat_slug = get_term_by( 'name', $categories->subject, 'news-cat' ); | |
$cat_slug = $cat_slug->slug; | |
wp_set_object_terms( $post_id, $cat_slug, 'news-cat', true ); | |
} | |
// If there are multiple categories it is saved as an array | |
elseif( is_array( $categories->subject ) ) | |
{ | |
$previous_cats = array(); | |
foreach ($categories->subject as $cat) | |
{ | |
// If the category doesn't exist, we create it | |
if( !in_array( $cat, $existing_terms ) ) | |
{ | |
wp_insert_term( $cat, 'news-cat' ); | |
} | |
$cat_slug = get_term_by( 'name', $cat, 'news-cat' ); | |
$cat_slug = $cat_slug->slug; | |
wp_set_object_terms( $post_id, $cat_slug, 'news-cat', true ); | |
} | |
} | |
} | |
// If there are contact persons, add name, title, image and contact information to the meta boxes | |
if( isset( $contacts ) && $contacts !== '' ) { | |
foreach( $contacts as $person ) | |
{ | |
$contact_name = $person->name; | |
$contact_title = $person->title; | |
$contact_phone = $person->phone; | |
$contact_phone_alt = $person->phone_alternative; | |
$contact_mail = $person->email; | |
$contact_image = $person->image; | |
} | |
add_post_meta( $post_id, 'contact-name', $contact_name ); | |
add_post_meta( $post_id, 'contact-title', $contact_title ); | |
add_post_meta( $post_id, 'contact-phone', $contact_phone ); | |
add_post_meta( $post_id, 'contact-phone-alt', $contact_phone_alt ); | |
add_post_meta( $post_id, 'contact-mail', $contact_mail ); | |
add_post_meta( $post_id, 'contact-image', $contact_image ); | |
} | |
// If there is an boilerplate, populate meta box news-boilerplate | |
if( isset( $boilerplate ) && $boilerplate !== '' ) | |
{ | |
add_post_meta( $post_id, 'news-boilerplate', $boilerplate ); | |
} | |
} | |
else | |
{ | |
// Update existing post | |
// Get the post that already exists | |
$args = array( | |
'meta_key' => self::META_KEY, | |
'meta_value' => $id, | |
'post_type' => 'cpt_news' | |
); | |
$query = new WP_Query( $args ); | |
// Get the post ID | |
$post_id = $query->posts[0]->ID; | |
$post_data = array( | |
'ID' => $post_id, | |
'post_title' => $title, | |
'post_content' => $content, | |
'post_excerpt' => $excerpt, | |
); | |
// Update the existing posts content and excerpt | |
wp_update_post( $post_data ); | |
// If language is set, populate meta box news-language | |
if( isset( $lang ) && $lang !== '' ) { | |
add_post_meta( $post_id, 'news-language', $lang ) || update_post_meta( $post_id, 'news-language', $lang ); | |
} | |
// If there is an image, populate meta box news-featured-image | |
if( isset( $image ) && $image !== '' ) { | |
add_post_meta( $post_id, 'news-featured-image', $image ) || update_post_meta( $post_id, 'news-featured-image', $image ); | |
} | |
// If there are categories, add the categories to the post | |
if( isset( $categories ) && $categories !== '' ) { | |
if( is_string( $categories->subject ) ) | |
{ | |
// If the category doesn't exist, we create it | |
if( !in_array( $categories->subject , $existing_terms ) ) | |
{ | |
wp_insert_term( $categories->subject, 'news-cat' ); | |
} | |
$cat_slug = get_term_by( 'name', $categories->subject, 'news-cat' ); | |
$cat_slug = $cat_slug->slug; | |
wp_set_object_terms( $post_id, $cat_slug, 'news-cat', true ); | |
} | |
elseif( is_array( $categories->subject ) ) | |
{ | |
$previous_cats = array(); | |
foreach ($categories->subject as $cat) | |
{ | |
// If the category doesn't exist, we create it | |
if( !in_array( $cat, $existing_terms ) ) | |
{ | |
wp_insert_term( $cat, 'news-cat' ); | |
} | |
$cat_slug = get_term_by( 'name', $cat, 'news-cat' ); | |
$cat_slug = $cat_slug->slug; | |
wp_set_object_terms( $post_id, $cat_slug, 'news-cat', true ); | |
} | |
} | |
} | |
// If there are contact persons, update name, title, image and contact information | |
if( isset( $contacts ) && $contacts !== '' ) { | |
foreach( $contacts as $person ) | |
{ | |
$contact_name = $person->name; | |
$contact_title = $person->title; | |
$contact_phone = $person->phone; | |
$contact_phone_alt = $person->phone_alternative; | |
$contact_mail = $person->email; | |
$contact_image = $person->image; | |
} | |
add_post_meta( $post_id, 'contact-name', $contact_name ) || update_post_meta( $post_id, 'contact-name', $contact_name ); | |
add_post_meta( $post_id, 'contact-title', $contact_title ) || update_post_meta( $post_id, 'contact-title', $contact_title ); | |
add_post_meta( $post_id, 'contact-phone', $contact_phone ) || update_post_meta( $post_id, 'contact-phone', $contact_phone ); | |
add_post_meta( $post_id, 'contact-phone-alt', $contact_phone_alt ) || update_post_meta( $post_id, 'contact-phone-alt', $contact_phone_alt ); | |
add_post_meta( $post_id, 'contact-mail', $contact_mail ) || update_post_meta( $post_id, 'contact-mail', $contact_mail ); | |
add_post_meta( $post_id, 'contact-image', $contact_image ) || update_post_meta( $post_id, 'contact-image', $contact_image ); | |
} | |
// If there is a boilerplate, update it | |
if( isset( $boilerplate ) && $boilerplate !== '' ) | |
{ | |
add_post_meta( $post_id, 'news-boilerplate', $boilerplate ) || update_post_meta( $post_id, 'news-boilerplate', $boilerplate ); | |
} | |
// The post was updated, not created | |
$was_created = false; | |
} | |
} | |
exit(); | |
} | |
/** | |
* Retrieve feed with news | |
* @return SimpleXMLElement The feed | |
*/ | |
private function _get_entries() | |
{ | |
$url = self::MND_URL; | |
// Initialize session | |
$process = curl_init( $url ); | |
// Set options for transfer | |
// (seconds the cURL functions can execute and return the transfer as string) | |
curl_setopt( $process, CURLOPT_TIMEOUT, 30 ); | |
curl_setopt( $process, CURLOPT_RETURNTRANSFER, TRUE ); | |
// Perform session | |
$feed = curl_exec( $process ); | |
// Close session | |
curl_close( $process ); | |
// Parse the feed | |
$entries = json_decode( $feed ); | |
// Return the results | |
return $entries; | |
} | |
} | |
add_action( | |
'after_setup_theme', | |
array( MND_News_Importer::get_instance(), 'plugin_setup' ) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment