Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Limesharp Newsreach Integration
- Plugin URI: http://google.com
- Description: Integrated a newsreach API feed into Wordpress, posting for you
- Version: 0.1
- Author: Limesharp
- Author URI: http://limesharp.net
- License: LNI1
- */
- /* Copyright 2012 LIMESHARP (email : SUPPORT@LIMESHARP.NET)
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License, version 2, as
- published by the Free Software Foundation.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- require_once( dirname (__FILE__) . '/newsReachAPI.php' );
- class newsReach extends newsReachAPI {
- public function __construct() {
- add_filter('cron_schedules', array( $this, 'add_cs_cron_fn' ) );
- register_activation_hook( __FILE__, array($this, 'run_on_activate') );
- register_deactivation_hook( __FILE__, array($this, 'run_on_deactivate') );
- add_action ('content_scheduler_notify', array( $this, 'insert_post') );
- }
- function insert_post() {
- $newsFeed = $this->returnFeed();
- foreach ($newsFeed as $article) {
- //foreach ($article->categories as $cats)
- // error_log($cats);
- $opts = array('post_status' => 'publish', 'post_type' => 'post');
- $allPosts = get_posts($opts);
- $post = array(
- 'post_author' => 3,
- 'post_category' => array(3,4),
- 'post_content' => $article->content,
- 'post_status' => 'publish',
- 'post_title' => $article->heading,
- );
- global $wpdb;
- global $wpdb;
- $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_name = %s', sanitize_title_with_dashes($article->heading));
- $cID = $wpdb->get_var( $query );
- if ( empty($cID) ) {
- // do nothing
- } else {
- $newId = wp_insert_post($post, $wp_error);
- }
- // insert featured image
- if( !class_exists( 'WP_Http' ) )
- include_once( ABSPATH . WPINC. '/class-http.php' );
- $photo = new WP_Http();
- $photo = $photo->request( $article->largeURL );
- $attachment = wp_upload_bits( $photo_name . '.jpg', null, $photo['body'], date("Y-m", strtotime( $photo['headers']['last-modified'] ) ) );
- $filetype = wp_check_filetype( basename( $attachment['file'] ), null );
- $postinfo = array(
- 'post_mime_type' => $filetype['type'],
- 'post_title' => $article->heading . ' ',
- 'post_content' => '',
- 'post_status' => 'inherit',
- );
- $filename = $attachment['file'];
- $attach_id = wp_insert_attachment( $postinfo, $filename, $newId );
- if( !function_exists( 'wp_generate_attachment_data' ) )
- require_once(ABSPATH . "wp-admin" . '/includes/image.php');
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- error_log(set_post_thumbnail($newId,$attach_id));
- }
- //$body = 'this is an email from newsreach';
- }
- function run_on_activate()
- {
- // for notifications
- if( !wp_next_scheduled( 'content_scheduler_notify' ) )
- {
- wp_schedule_event( time(), 'myMinute', 'content_scheduler_notify' );
- }
- } // end run_on_activate()
- function add_cs_cron_fn( $array )
- {
- // set our 6 hours, units in seconds
- $period = 60;
- // use that for 'interval' below.
- // 'quarterday' is a unique name for our custom period
- $array['myMinute'] = array(
- 'interval' => $period,
- 'display' => 'Every 1 minute'
- );
- return $array;
- } // end add_cs_cron_fn()
- function run_on_deactivate()
- {
- // for notifications
- wp_clear_scheduled_hook('content_scheduler_notify');
- } // end run_on_activate()
- }
- /**
- * @global
- * Start up
- **/
- $newsInt = new newsReach();
- ?>
- /* EOF */
- <?php
- class article {
- var $heading = "";
- var $content = "";
- var $largeURL = "";
- var $smallURL = "";
- var $categories = "";
- public function __construct($heading,$content,$largeURL,$smallURL,$categories) {
- $this->id = $postId;
- $this->heading = $heading;
- $this->content = $content;
- $this->largeURL = $largeURL;
- $this->smallURL = $smallURL;
- $this->categories = $categories;
- }
- }
- class newsReachAPI {
- function getFeed($xmlUrl) {
- $xml = simplexml_load_file($xmlUrl);
- $articles = array();
- foreach ($xml->Article as $item) {
- $catList = array();
- foreach ($item->Categories->children() as $child)
- array_push($catList,$this->categoryMatching($child));
- $newArticle = new article($item->Heading,$item->Contents,$item->Picture->Large->URL,$item->Picture->Small->URL,$catList);
- array_push($articles,$newArticle);
- }
- return $articles;
- }
- function categoryMatching($categoryIn) {
- $allCategories = get_categories();
- foreach ($allCategories as $currentCategory) {
- if ($currentCategory->name == $categoryIn) {
- return $currentCategory->term_id;
- }
- }
- return $categoryIn;
- }
- public function returnFeed() {
- $articlesIn = $this->getFeed('http://feeds.directnews.co.uk/?e4f740aa-c1f2-43cd-9ce1-09a65379142e');
- return $articlesIn;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement