Advertisement
Guest User

Untitled

a guest
Nov 15th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Limesharp Newsreach Integration
  4. Plugin URI: http://google.com
  5. Description: Integrated a newsreach API feed into Wordpress, posting for you
  6. Version: 0.1
  7. Author: Limesharp
  8. Author URI: http://limesharp.net
  9. License: LNI1
  10. */
  11.  
  12. /* Copyright 2012 LIMESHARP (email : SUPPORT@LIMESHARP.NET)
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License, version 2, as
  16. published by the Free Software Foundation.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27.  
  28. require_once( dirname (__FILE__) . '/newsReachAPI.php' );
  29.  
  30. class newsReach extends newsReachAPI {
  31.  
  32. public function __construct() {
  33.  
  34. add_filter('cron_schedules', array( $this, 'add_cs_cron_fn' ) );
  35. register_activation_hook( __FILE__, array($this, 'run_on_activate') );
  36. register_deactivation_hook( __FILE__, array($this, 'run_on_deactivate') );
  37. add_action ('content_scheduler_notify', array( $this, 'insert_post') );
  38.  
  39. }
  40. function insert_post() {
  41.  
  42. $newsFeed = $this->returnFeed();
  43. foreach ($newsFeed as $article) {
  44. //foreach ($article->categories as $cats)
  45. // error_log($cats);
  46. $opts = array('post_status' => 'publish', 'post_type' => 'post');
  47. $allPosts = get_posts($opts);
  48. $post = array(
  49. 'post_author' => 3,
  50. 'post_category' => array(3,4),
  51. 'post_content' => $article->content,
  52. 'post_status' => 'publish',
  53. 'post_title' => $article->heading,
  54. );
  55.  
  56. global $wpdb;
  57. global $wpdb;
  58. $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_name = %s', sanitize_title_with_dashes($article->heading));
  59. $cID = $wpdb->get_var( $query );
  60.  
  61. if ( empty($cID) ) {
  62. // do nothing
  63. } else {
  64. $newId = wp_insert_post($post, $wp_error);
  65. }
  66.  
  67. // insert featured image
  68. if( !class_exists( 'WP_Http' ) )
  69. include_once( ABSPATH . WPINC. '/class-http.php' );
  70.  
  71. $photo = new WP_Http();
  72. $photo = $photo->request( $article->largeURL );
  73. $attachment = wp_upload_bits( $photo_name . '.jpg', null, $photo['body'], date("Y-m", strtotime( $photo['headers']['last-modified'] ) ) );
  74.  
  75. $filetype = wp_check_filetype( basename( $attachment['file'] ), null );
  76.  
  77. $postinfo = array(
  78. 'post_mime_type' => $filetype['type'],
  79. 'post_title' => $article->heading . ' ',
  80. 'post_content' => '',
  81. 'post_status' => 'inherit',
  82. );
  83. $filename = $attachment['file'];
  84. $attach_id = wp_insert_attachment( $postinfo, $filename, $newId );
  85. if( !function_exists( 'wp_generate_attachment_data' ) )
  86. require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  87. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  88. wp_update_attachment_metadata( $attach_id, $attach_data );
  89. error_log(set_post_thumbnail($newId,$attach_id));
  90. }
  91. //$body = 'this is an email from newsreach';
  92. }
  93.  
  94. function run_on_activate()
  95. {
  96. // for notifications
  97. if( !wp_next_scheduled( 'content_scheduler_notify' ) )
  98. {
  99. wp_schedule_event( time(), 'myMinute', 'content_scheduler_notify' );
  100. }
  101.  
  102. } // end run_on_activate()
  103. function add_cs_cron_fn( $array )
  104. {
  105. // set our 6 hours, units in seconds
  106. $period = 60;
  107. // use that for 'interval' below.
  108. // 'quarterday' is a unique name for our custom period
  109. $array['myMinute'] = array(
  110. 'interval' => $period,
  111. 'display' => 'Every 1 minute'
  112. );
  113. return $array;
  114. } // end add_cs_cron_fn()
  115. function run_on_deactivate()
  116. {
  117. // for notifications
  118. wp_clear_scheduled_hook('content_scheduler_notify');
  119. } // end run_on_activate()
  120. }
  121.  
  122. /**
  123. * @global
  124. * Start up
  125. **/
  126.  
  127. $newsInt = new newsReach();
  128. ?>
  129.  
  130.  
  131. /* EOF */
  132.  
  133.  
  134. <?php
  135. class article {
  136. var $heading = "";
  137. var $content = "";
  138. var $largeURL = "";
  139. var $smallURL = "";
  140. var $categories = "";
  141.  
  142. public function __construct($heading,$content,$largeURL,$smallURL,$categories) {
  143. $this->id = $postId;
  144. $this->heading = $heading;
  145. $this->content = $content;
  146. $this->largeURL = $largeURL;
  147. $this->smallURL = $smallURL;
  148. $this->categories = $categories;
  149. }
  150. }
  151. class newsReachAPI {
  152. function getFeed($xmlUrl) {
  153. $xml = simplexml_load_file($xmlUrl);
  154. $articles = array();
  155. foreach ($xml->Article as $item) {
  156.  
  157. $catList = array();
  158.  
  159. foreach ($item->Categories->children() as $child)
  160. array_push($catList,$this->categoryMatching($child));
  161.  
  162. $newArticle = new article($item->Heading,$item->Contents,$item->Picture->Large->URL,$item->Picture->Small->URL,$catList);
  163. array_push($articles,$newArticle);
  164. }
  165. return $articles;
  166. }
  167. function categoryMatching($categoryIn) {
  168. $allCategories = get_categories();
  169. foreach ($allCategories as $currentCategory) {
  170. if ($currentCategory->name == $categoryIn) {
  171. return $currentCategory->term_id;
  172. }
  173. }
  174. return $categoryIn;
  175. }
  176. public function returnFeed() {
  177. $articlesIn = $this->getFeed('http://feeds.directnews.co.uk/?e4f740aa-c1f2-43cd-9ce1-09a65379142e');
  178. return $articlesIn;
  179. }
  180. }
  181. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement