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() {
?>
scheduled content importer sample
Content importer is scheduled!
$cron ) {
if ( isset( $cron['importersample_sched_hook'] ) ) {
echo 'Time now:'." \t\t\t".date(get_option('date_format'))." ".date("H:i:s")."
";
echo 'Schedule will be triggered:'." \t".date(get_option('date_format'),$timestamp)." ".date("H:i:s",$timestamp)."
";
}
}
?>refresh
0) {
?>
Import schedule was triggered
times.
Content Importer Sample is NOT scheduled!
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 = ''.$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('
', '
', $post_content);
$post_content = str_replace('
', '
', $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);
}
}
}
}
?>