Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NewsML to WP database
/*
* Inserts the news from the array with NewsML_Objects into the database
*/
public function insert_news_to_db() {
global $wpdb;
$result = get_option( $this->option_name );
// Do we want to use FTP or HTTP
if ( $result['enable_ftp'] == 'yes' ) {
$access = new FTP_File_Access( $result['url_newsml'], $result['ftp_user'], $result['ftp_pass'] );
} else {
$access = new HTTP_File_Access( $result['url_newsml'], '', '' );
}
$access->establish_connection();
// Get the filelist from the database to see which files we already read
$files = get_option( 'newsml-filelist' );
$files = json_decode( $files );
// Determine the new files we want to add
if ( ! empty( $files ) ) {
$diff = array_diff( $access->file_list(), $files );
} else {
$diff = $access->file_list();
}
$diff = array_values( $diff );
$access->save_files( $diff );
// Update the filelist to the new one
update_option( 'newsml-filelist', json_encode( $access->file_list() ) );
$news_to_add = array();
// Load all the files with their associated parser and create a NewsML_Object
foreach ( $diff as $df ) {
$file = $access->open_file( $df );
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->loadXML( $file );
$chooser = new Parser_Chooser();
$parser = $chooser->choose_parser( $xml );
if ( $parser ) {
$obj = $parser->parse( $xml );
}
$news_to_add[] = $obj;
}
// Iterate through our news objects and save them to the database
foreach ( $news_to_add as $object ) {
$query_check_exists = $wpdb->prepare(
"SELECT m.post_id, m.meta_value, p.post_date FROM $wpdb->postmeta m
LEFT JOIN $wpdb->posts p ON p.ID = m.post_id
WHERE meta_key = 'newsml_meta_guid' AND meta_value =%s",
$object->get_guid()
);
$res_check_exists = $wpdb->query( $query_check_exists );
if ( $res_check_exists <= 0 ) {
$new_post_id = wp_insert_post( array(
'post_type' => 'newsml_post',
'post_name' => sanitize_title( $object->get_title() ),
'post_title' => $object->get_title(),
'post_content' => $object->get_content(),
'post_status' => 'publish',
'post_date' => date( 'Y-m-d H:i:s', $object->get_timestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $object->get_timestamp() ),
) );
// Just try changing the mediatopics if the post was successfully created
if ( $new_post_id ) {
$tax_ids = array();
if ( ! empty( $object->get_mediatopics() ) ) {
foreach ( $object->get_mediatopics() as $topic ) {
$res_parent = $wpdb->get_row( "SELECT option_name, option_value FROM $wpdb->options WHERE option_value LIKE '%s:5:\"qcode\";s:15:\"" . $topic['qcode'] . "%'", ARRAY_A );
if ( ! empty( $res_parent ) ) {
// Get the ID of the parent mediatopic that is saved in term_taxonomy
$splitted_taxonomy_meta = explode( '_', $res_parent['option_name'] );
// Get the acutal parent term
$parent_term = get_term( $splitted_taxonomy_meta[2], 'newsml_mediatopic', ARRAY_A );
// Update the child mediatopic, now with the correct parent ID
$tax_ids[] = $parent_term['term_id'];
}
}
}
$tax_ids = array_map( 'intval', $tax_ids );
$tax_ids = array_unique( $tax_ids );
$term_tax_ids = wp_set_object_terms( $new_post_id, $tax_ids, 'newsml_mediatopic' );
// Insert different post meta
if ( ! empty( $object->get_subtitle() ) ) {
add_post_meta( $new_post_id, 'newsml_meta_subtitle', $object->get_subtitle() );
}
if ( ! empty( $object->get_locations() ) ) {
$locations = array();
foreach ( $object->get_locations() as $loc ) {
$locations[] = $loc['name'];
}
add_post_meta( $new_post_id, 'newsml_meta_location', implode( ', ', $locations ) );
}
$access->save_media_files( get_home_path() . $result['image_dir'], $object->get_multimedia() );
foreach ( $object->get_multimedia() as $file ) {
$image = media_sideload_image( home_url() . '/' . $result['image_dir'] . '/' . $file['href'], $new_post_id, 'image for ' . $object->get_title() );
}
add_post_meta( $new_post_id, 'newsml_meta_guid', $object->get_guid() );
add_post_meta( $new_post_id, 'newsml_meta_copyrightholder', $object->get_copyrightholder() );
add_post_meta( $new_post_id, 'newsml_meta_copyrightnotice', $object->get_copyrightnotice() );
} elseif ( is_wp_error( $new_post_id ) ) {
$error_string = $new_post_id->get_error_message();
}
} else {
}
}
// Remove the temp and newsml-images directories
$access->recursive_rmdir();
$access->recursive_rmdir( get_home_path() . $result['image_dir'] );
// Recreate the newsml-images directory
if ( ! file_exists( get_home_path() . $result['image_dir'] ) ) {
mkdir( get_home_path() . $result['image_dir'], 0755 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.