Advertisement
Guest User

content-importer

a guest
Jun 18th, 2010
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.95 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Sample scheduled content importer
  4. Plugin URI:
  5. Description: A sample wordpress plugin that pulls articles from the API and stores them in the database.
  6. Version: 0.6
  7. Author:
  8. Author URI:
  9. */
  10. /* options are deleted in case of plugin deactivation */
  11. require_once(ABSPATH . 'wp-admin/includes/admin.php');
  12. require_once(ABSPATH . 'wp-includes/post.php');
  13. include_once 'SampleAPIClientLibrary/ApiHandler.php';
  14.  
  15. $feedSettings;
  16.  
  17.  
  18.  
  19.  
  20. add_action('deactivate_SampleWordpressPlugin/SampleWordpressPlugin.php', 'importersample_sched_deactivate');
  21. function importersample_sched_deactivate() {
  22.     delete_option("importersample_sched_url");
  23.     delete_option("importersample_sched_inseconds");
  24.     delete_option("importersample_sched_recc");
  25.     delete_option("importersample_sched_triggercount");
  26.     delete_option("importersample_sched_API_KEY");
  27. }
  28.  
  29. /* Admin options page display function is called */
  30. add_action('admin_menu', 'importersample_sched_add_admin_pages');
  31. function importersample_sched_add_admin_pages() {
  32.     add_options_page('Sample - Content Import Scheduler', 'Sample - Content Import Scheduler', 10, __FILE__, 'importersample_sched_options_page');
  33. }
  34.  
  35. /* Options sent by the options form are set here */
  36. /* Schedules are activated and deactivated */
  37. add_action('init', 'importersample_sched_setoptions');
  38. function importersample_sched_setoptions() {
  39.     global $feedSettings;
  40.     if(!empty($_POST['importersample_sched_url'])) {
  41.         update_option("importersample_sched_url",$_POST['importersample_sched_url']);
  42.     }
  43.     if(!empty($_POST['importersample_sched_API_KEY'])) {
  44.         update_option("importersample_sched_API_KEY",$_POST['importersample_sched_API_KEY']);
  45.     }
  46.     $feedSettings = array("url" => get_option("importersample_sched_url"), "API_Key" => get_option("importersample_sched_API_KEY"));
  47.     if(!empty($_POST['importersample_sched_stop'])) {
  48.         $timestamp = wp_next_scheduled('importersample_sched_hook', $feedSettings);
  49.         /* This is where the event gets unscheduled */
  50.         wp_unschedule_event($timestamp, "importersample_sched_hook", $feedSettings);
  51.     }
  52.     if(!empty($_POST['importersample_sched_inseconds'])) {
  53.         update_option("importersample_sched_inseconds",$_POST['importersample_sched_inseconds']);
  54.         /* This is where the actual recurring event is scheduled */
  55.         if (!wp_next_scheduled('importersample_sched_hook', $feedSettings)) {
  56.             wp_schedule_event(time()+$_POST['importersample_sched_inseconds'], "importersample_sched_recc", "importersample_sched_hook", $feedSettings);
  57.             importersample_sched_trigger_schedule($feedSettings['url'],$feedSettings['API_Key']);
  58.         }
  59.     }
  60. }
  61. /* a reccurence has to be added to the cron_schedules array */
  62. add_filter('cron_schedules', 'importersample_sched_more_reccurences');
  63. function importersample_sched_more_reccurences($recc) {
  64.     $recc['importersample_sched_recc'] = array('interval' => get_option("importersample_sched_inseconds"), 'display' => 'Sample XML Import Schedule');
  65.     return $recc;
  66. }
  67. /* This is the scheduling hook for our plugin that is triggered by cron */
  68. add_action('importersample_sched_hook','importersample_sched_trigger_schedule',10,2);
  69. function importersample_sched_trigger_schedule($url, $API_Key) {
  70.     importersample_sched_load_articles($url, $API_Key);
  71.     update_option("importersample_sched_triggercount",get_option("importersample_sched_triggercount")+1);
  72. }
  73.  
  74. /* The options page display */
  75. function importersample_sched_options_page() {
  76.     ?>
  77.     <div class=wrap>
  78.         <h2>scheduled content importer sample</h2>
  79.         <p></p>
  80.         <div style="padding: 10px; border: 1px solid #cccccc;">
  81.         <?php
  82.         global $feedSettings;
  83.         if (wp_next_scheduled('importersample_sched_hook', $feedSettings)) {
  84.             ?>
  85.             <p><b>Content importer is scheduled!</b></p>
  86.             <pre><?php
  87.             $crons = _get_cron_array();
  88.              foreach ( $crons as $timestamp => $cron ) {
  89.                  if ( isset( $cron['importersample_sched_hook'] ) ) {
  90.                     echo 'Time now:'." \t\t\t".date(get_option('date_format'))." ".date("H:i:s")."<br />";
  91.                     echo 'Schedule will be triggered:'." \t".date(get_option('date_format'),$timestamp)." ".date("H:i:s",$timestamp)."<br />";
  92.                  }
  93.              }
  94.             ?><a href="<?php bloginfo('wpurl') ?>/wp-admin/options-general.php?page=SampleWordpressPlugin/SampleWordpressPlugin.php">refresh</a><br />
  95.             </pre>
  96.             <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
  97.             <input type="submit" name="importersample_sched_stop" id="importersample_sched_stop" value="To turn off importer schedules" />
  98.             </form>
  99.             <?php
  100.             if(get_option("importersample_sched_triggercount") > 0) {
  101.             ?>
  102.             <p>Import schedule was triggered
  103.             <?php echo get_option("importersample_sched_triggercount");?> times.</p>
  104.             <?php
  105.             }
  106.         } else {
  107.             ?>
  108.             <p>Content Importer Sample is NOT scheduled!</p>
  109.       <?php //importersample_sched_load_articles(get_option("importersample_sched_url")); ?>
  110.             <?php
  111.         }
  112.  
  113.         ?>
  114.         </div>
  115.         <?php
  116.         if (!wp_next_scheduled('importersample_sched_hook', $feedSettings)) {
  117.         ?>
  118.         <br />
  119.         <form style="padding: 10px; border: 1px solid #cccccc;" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
  120.         <p><b>Set up a new import schedule</b></p><br />
  121.         API Base URL <input type="text" name="importersample_sched_url" value="<?php echo get_option("importersample_sched_url"); ?>" />
  122.         API Key <input type="text" name="importersample_sched_API_KEY" value="<?php echo get_option("importersample_sched_API_KEY"); ?>" /><br />
  123.         Seconds from now until this schedule should be triggered:<br />
  124.  
  125.                 <br>
  126.  
  127.                 <b>Option for use of categories or tags</b><br>
  128.  
  129.                 <br>
  130.                 Tags <input type="radio" name="radio" value="1" />
  131.                 Category <input type="radio" name="radio" value="2" checked=""/>
  132.                 <br>
  133.  
  134.  
  135.                 <br>
  136.  
  137.         <input type="text" name="importersample_sched_inseconds" value="<?php echo get_option("importersample_sched_inseconds"); ?>" />seconds<br />
  138.         <input type="submit" name="importersample_sched_submit" id="importersample_sched_submit" value="Set Import Schedule" />
  139.  
  140.  
  141.         </form>
  142.         <?php
  143.         }
  144.         ?>
  145.     </div>
  146.     <?php
  147. }
  148.  
  149.  
  150.  
  151. function importersample_sched_load_articles($url, $API_Key) {
  152.     global $wpdb, $post;
  153.  
  154.  
  155.     $option=$_POST['radio']; //get value from radio button
  156.  
  157.  
  158.  
  159.     $fh = new ApiHandler($API_Key, $url);
  160.     $articles = $fh->getNewsHTML();
  161.     set_magic_quotes_runtime(0);
  162.     $wpArticles = array();
  163.  
  164.     foreach ($articles as $a) {
  165.         /* @var $a NewsItem */
  166.         $date = $a->getPublishDate();
  167.         $post_title = $a->getHeadline();
  168.         $post_content = $a->getText();
  169.         $photos = $a->getPhotos();
  170.  
  171.         //Check if large picture exists
  172.         if(!empty($photos)){
  173.             $large = $photos[0]->getLarge();
  174.             if(!empty($large)){
  175.                 $post_image = $large->getUrl();
  176.                 $post_image_caption = $photos[0]->getAlt();
  177.             }
  178.         }
  179.  
  180.         $post_date;
  181.         $post_date_gmt;
  182.         $post_author = 1;
  183.         $post_status = 'publish';
  184.         $guid = $API_Key;
  185.         $categories = array();
  186.         $categoriesId = array();
  187.         $tags_input = array();
  188.  
  189.         $CatColl = $a->getCategories();
  190.         $cat_index = 0;
  191.  
  192.         foreach ($CatColl as $c){
  193.  
  194.             $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  195.             $trans_tbl = array_flip($trans_tbl);
  196.             $categoryID = $wpdb->escape($c->getID());    
  197.             $categories[$cat_index] = $wpdb->escape($c->getName());
  198.  
  199.             if($option==1){
  200.                 $tags_input[$cat_index] = $wpdb->escape($c->getName());
  201.             }
  202.  
  203.             $cat_index++;
  204.  
  205.         }
  206.  
  207.     //add picture if available
  208.     if($post_image != ""){
  209.       $post_content = '<img src="'.$post_image.'" alt="'.$post_image_caption.'" align="right">'.$post_content;
  210.     }
  211.         //Do some formatting
  212.  
  213.         $post_date_gmt = strtotime($date);
  214.         $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
  215.         $post_date = get_date_from_gmt( $post_date_gmt );
  216.         $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
  217.         $post_content = str_replace('<br>', '<br />', $post_content);
  218.         $post_content = str_replace('<hr>', '<hr />', $post_content);
  219.         //$post_content = str_replace("'", "''", $post_content);
  220.         //$post_title = str_replace("'", "''", $post_title);
  221.  
  222.         //Save the article to the articles array
  223.  
  224.         if($option==1){ //tags
  225.  
  226.         $wpArticles[$i] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'tags_input');
  227.  
  228.         }else if($option!=1){ //categories
  229.  
  230.         $wpArticles[$i] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'categories');
  231.  
  232.         }
  233.  
  234.         $i++;
  235.     }
  236.     //import into wordpress
  237.     foreach ($wpArticles as $wpArticle) {
  238.         extract($wpArticle);
  239.         if ($post_id = post_exists($post_title, "", $post_date)) {
  240.       //TODO: update story
  241.         } else {
  242.       //insert new story
  243.             $post_id = wp_insert_post($wpArticle);
  244.             if ( is_wp_error( $post_id ) )
  245.                 return $post_id;
  246.             if (!$post_id) {
  247.                 return;
  248.             }
  249.             if($option==2){ //if categories
  250.                 if (0 != count($categories))                   
  251.                     wp_create_categories($categories, $post_id);
  252.             }  
  253.  
  254.         }
  255.     }
  256. }
  257. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement