'; echo '

'.__('Import LiveInternet.ru').'

'; } function footer() { echo ''; } function unhtmlentities($string) { // From php.net for < 4.3 compat $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } function greet() { echo '
'; echo '

'.__('Howdy! Upload your LiveInternet.ru XML export file and we’ll import the posts into this blog.').'

'; echo '

'.__('Choose a LiveInternet.ru XML file to upload, then click Upload file and import.').'

'; wp_import_upload_form("admin.php?import=liru&step=1"); echo '
'; } function import_posts() { global $wpdb, $current_user; set_magic_quotes_runtime(0); $importdata = file($this->file); // Read the file into an array $importdata = implode('', $importdata); // squish it $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); preg_match_all('|(.*?)|is', $importdata, $posts); $posts = $posts[1]; unset($importdata); echo '
    '; foreach ($posts as $post) { preg_match('|(.*?)|is', $post, $post_title); $post_title = str_replace(array (''), '', trim($post_title[1])); $post_title = $wpdb->escape(trim($post_title)); if ( empty($post_title) ) { preg_match('|(.*?)|is', $post, $post_title); $post_title = $wpdb->escape(trim($post_title[1])); } $post_title = iconv("windows-1251","utf-8",$post_title); preg_match('|(.*?)|is', $post, $post_date); $post_date = $post_date[1]; $post_date=str_replace(array (''), '', trim($post_date)); $post_date = strtotime($post_date); $post_date = date('Y-m-d H:i:s', $post_date); preg_match('|(.*?)|is', $post, $post_content); $post_content = str_replace(array (''), '', trim($post_content[1])); $post_content = $this->unhtmlentities($post_content); $post_content = iconv("windows-1251","utf-8",$post_content); // Clean up content $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 = $wpdb->escape($post_content); $post_author = $current_user->ID; $post_status = 'publish'; $offset = 0; $match_count = 0; $tags_input = ''; while(preg_match('|(.*?)|is', $post, $matches, PREG_OFFSET_CAPTURE, $offset)) { $match_count++; $match_start = $matches[0][1]; $match_length = strlen($matches[0][0]); $matches[0][0]=str_replace(array (''), '', trim($matches[0][0])); $tags_input = $tags_input.$matches[0][0].','; $offset = $match_start + $match_length; } $tags_input = iconv("windows-1251","utf-8",$tags_input); echo '
  1. '; if ($post_id = post_exists($post_title, $post_content, $post_date)) { printf(__('Post %s already exists.'), stripslashes($post_title)); } else { printf(__('Importing post %s...'), stripslashes($post_title)); $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status', 'tags_input'); $post_id = wp_insert_post($postdata); if ( is_wp_error( $post_id ) ) return $post_id; if (!$post_id) { _e("Couldn't get post ID"); echo '
  2. '; break; } } } } function import() { $file = wp_import_handle_upload(); if ( isset($file['error']) ) { echo $file['error']; return; } $this->file = $file['file']; $result = $this->import_posts(); if ( is_wp_error( $result ) ) return $result; wp_import_cleanup($file['id']); do_action('import_done', 'LiveInternet.ru'); echo '

    '; printf(__('All done. Have fun!'), get_option('home')); echo '

    '; } function dispatch() { if (empty ($_GET['step'])) $step = 0; else $step = (int) $_GET['step']; $this->header(); switch ($step) { case 0 : $this->greet(); break; case 1 : check_admin_referer('import-upload'); $result = $this->import(); if ( is_wp_error( $result ) ) echo $result->get_error_message(); break; } $this->footer(); } function liru_Import() { // Nothing. } } } add_action( 'init', 'liru_Import' ); $liveinternetru_import = new liru_Import(); register_importer('liru', __('LiveInternet.ru'), __('Import posts from a LiveInternet.ru XML export file.'), array ($liveinternetru_import, 'dispatch')); ?>