Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Sample scheduled content importer
- Plugin URI:
- Description: A sample wordpress plugin that pulls articles from the API and stores them in the database.
- Version: 0.6
- Author:
- Author URI:
- */
- /* options are deleted in case of plugin deactivation */
- require_once(ABSPATH . 'wp-admin/includes/admin.php');
- require_once(ABSPATH . 'wp-includes/post.php');
- include_once 'SampleAPIClientLibrary/ApiHandler.php';
- $feedSettings;
- add_action('deactivate_SampleWordpressPlugin/SampleWordpressPlugin.php', 'importersample_sched_deactivate');
- function importersample_sched_deactivate() {
- delete_option("importersample_sched_url");
- delete_option("importersample_sched_inseconds");
- delete_option("importersample_sched_recc");
- delete_option("importersample_sched_triggercount");
- delete_option("importersample_sched_API_KEY");
- }
- /* Admin options page display function is called */
- add_action('admin_menu', 'importersample_sched_add_admin_pages');
- function importersample_sched_add_admin_pages() {
- add_options_page('Sample - Content Import Scheduler', 'Sample - Content Import Scheduler', 10, __FILE__, 'importersample_sched_options_page');
- }
- /* Options sent by the options form are set here */
- /* Schedules are activated and deactivated */
- add_action('init', 'importersample_sched_setoptions');
- function importersample_sched_setoptions() {
- global $feedSettings;
- if(!empty($_POST['importersample_sched_url'])) {
- update_option("importersample_sched_url",$_POST['importersample_sched_url']);
- }
- if(!empty($_POST['importersample_sched_API_KEY'])) {
- update_option("importersample_sched_API_KEY",$_POST['importersample_sched_API_KEY']);
- }
- $feedSettings = array("url" => get_option("importersample_sched_url"), "API_Key" => get_option("importersample_sched_API_KEY"));
- if(!empty($_POST['importersample_sched_stop'])) {
- $timestamp = wp_next_scheduled('importersample_sched_hook', $feedSettings);
- /* This is where the event gets unscheduled */
- wp_unschedule_event($timestamp, "importersample_sched_hook", $feedSettings);
- }
- if(!empty($_POST['importersample_sched_inseconds'])) {
- update_option("importersample_sched_inseconds",$_POST['importersample_sched_inseconds']);
- /* This is where the actual recurring event is scheduled */
- if (!wp_next_scheduled('importersample_sched_hook', $feedSettings)) {
- wp_schedule_event(time()+$_POST['importersample_sched_inseconds'], "importersample_sched_recc", "importersample_sched_hook", $feedSettings);
- importersample_sched_trigger_schedule($feedSettings['url'],$feedSettings['API_Key']);
- }
- }
- }
- /* a reccurence has to be added to the cron_schedules array */
- add_filter('cron_schedules', 'importersample_sched_more_reccurences');
- function importersample_sched_more_reccurences($recc) {
- $recc['importersample_sched_recc'] = array('interval' => get_option("importersample_sched_inseconds"), 'display' => 'Sample XML Import Schedule');
- return $recc;
- }
- /* This is the scheduling hook for our plugin that is triggered by cron */
- add_action('importersample_sched_hook','importersample_sched_trigger_schedule',10,2);
- function importersample_sched_trigger_schedule($url, $API_Key) {
- importersample_sched_load_articles($url, $API_Key);
- update_option("importersample_sched_triggercount",get_option("importersample_sched_triggercount")+1);
- }
- /* The options page display */
- function importersample_sched_options_page() {
- ?>
- <div class=wrap>
- <h2>scheduled content importer sample</h2>
- <p></p>
- <div style="padding: 10px; border: 1px solid #cccccc;">
- <?php
- global $feedSettings;
- if (wp_next_scheduled('importersample_sched_hook', $feedSettings)) {
- ?>
- <p><b>Content importer is scheduled!</b></p>
- <pre><?php
- $crons = _get_cron_array();
- foreach ( $crons as $timestamp => $cron ) {
- if ( isset( $cron['importersample_sched_hook'] ) ) {
- echo 'Time now:'." \t\t\t".date(get_option('date_format'))." ".date("H:i:s")."<br />";
- echo 'Schedule will be triggered:'." \t".date(get_option('date_format'),$timestamp)." ".date("H:i:s",$timestamp)."<br />";
- }
- }
- ?><a href="<?php bloginfo('wpurl') ?>/wp-admin/options-general.php?page=SampleWordpressPlugin/SampleWordpressPlugin.php">refresh</a><br />
- </pre>
- <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
- <input type="submit" name="importersample_sched_stop" id="importersample_sched_stop" value="To turn off importer schedules" />
- </form>
- <?php
- if(get_option("importersample_sched_triggercount") > 0) {
- ?>
- <p>Import schedule was triggered
- <?php echo get_option("importersample_sched_triggercount");?> times.</p>
- <?php
- }
- } else {
- ?>
- <p>Content Importer Sample is NOT scheduled!</p>
- <?php //importersample_sched_load_articles(get_option("importersample_sched_url")); ?>
- <?php
- }
- ?>
- </div>
- <?php
- if (!wp_next_scheduled('importersample_sched_hook', $feedSettings)) {
- ?>
- <br />
- <form style="padding: 10px; border: 1px solid #cccccc;" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
- <p><b>Set up a new import schedule</b></p><br />
- API Base URL <input type="text" name="importersample_sched_url" value="<?php echo get_option("importersample_sched_url"); ?>" />
- API Key <input type="text" name="importersample_sched_API_KEY" value="<?php echo get_option("importersample_sched_API_KEY"); ?>" /><br />
- Seconds from now until this schedule should be triggered:<br />
- <br>
- <b>Option for use of categories or tags</b><br>
- <br>
- Tags <input type="radio" name="radio" value="1" />
- Category <input type="radio" name="radio" value="2" checked=""/>
- <br>
- <br>
- <input type="text" name="importersample_sched_inseconds" value="<?php echo get_option("importersample_sched_inseconds"); ?>" />seconds<br />
- <input type="submit" name="importersample_sched_submit" id="importersample_sched_submit" value="Set Import Schedule" />
- </form>
- <?php
- }
- ?>
- </div>
- <?php
- }
- function importersample_sched_load_articles($url, $API_Key) {
- global $wpdb, $post;
- $option=$_POST['radio']; //get value from radio button
- $fh = new ApiHandler($API_Key, $url);
- $articles = $fh->getNewsHTML();
- set_magic_quotes_runtime(0);
- $wpArticles = array();
- foreach ($articles as $a) {
- /* @var $a NewsItem */
- $date = $a->getPublishDate();
- $post_title = $a->getHeadline();
- $post_content = $a->getText();
- $photos = $a->getPhotos();
- //Check if large picture exists
- if(!empty($photos)){
- $large = $photos[0]->getLarge();
- if(!empty($large)){
- $post_image = $large->getUrl();
- $post_image_caption = $photos[0]->getAlt();
- }
- }
- $post_date;
- $post_date_gmt;
- $post_author = 1;
- $post_status = 'publish';
- $guid = $API_Key;
- $categories = array();
- $categoriesId = array();
- $tags_input = array();
- $CatColl = $a->getCategories();
- $cat_index = 0;
- foreach ($CatColl as $c){
- $trans_tbl = get_html_translation_table(HTML_ENTITIES);
- $trans_tbl = array_flip($trans_tbl);
- $categoryID = $wpdb->escape($c->getID());
- $categories[$cat_index] = $wpdb->escape($c->getName());
- if($option==1){
- $tags_input[$cat_index] = $wpdb->escape($c->getName());
- }
- $cat_index++;
- }
- //add picture if available
- if($post_image != ""){
- $post_content = '<img src="'.$post_image.'" alt="'.$post_image_caption.'" align="right">'.$post_content;
- }
- //Do some formatting
- $post_date_gmt = strtotime($date);
- $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
- $post_date = get_date_from_gmt( $post_date_gmt );
- $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
- $post_content = str_replace('<br>', '<br />', $post_content);
- $post_content = str_replace('<hr>', '<hr />', $post_content);
- //$post_content = str_replace("'", "''", $post_content);
- //$post_title = str_replace("'", "''", $post_title);
- //Save the article to the articles array
- if($option==1){ //tags
- $wpArticles[$i] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'tags_input');
- }else if($option!=1){ //categories
- $wpArticles[$i] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'categories');
- }
- $i++;
- }
- //import into wordpress
- foreach ($wpArticles as $wpArticle) {
- extract($wpArticle);
- if ($post_id = post_exists($post_title, "", $post_date)) {
- //TODO: update story
- } else {
- //insert new story
- $post_id = wp_insert_post($wpArticle);
- if ( is_wp_error( $post_id ) )
- return $post_id;
- if (!$post_id) {
- return;
- }
- if($option==2){ //if categories
- if (0 != count($categories))
- wp_create_categories($categories, $post_id);
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement