| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- /**
- * WordPress eXtended RSS file parser implementations
- *
- * @package Importer
- */
- function vamtam_get_export_data( $file ) {
- $replaced_contents = file_get_contents( $file );
- return str_replace( '{WPV:SAMPLES_URI}', VAMTAM_SAMPLES_URI , $replaced_contents );
- }
- /**
- * WordPress Importer class for managing parsing of WXR files.
- */
- class VAMTAM_WXR_Parser {
- function parse( $file ) {
- // use regular expressions if nothing else available or this is bad XML
- $parser = new VAMTAM_WXR_Parser_Regex;
- return $parser->parse( $file );
- }
- }
- /**
- * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
- */
- class VAMTAM_WXR_Parser_Regex {
- var $authors = array();
- var $posts = array();
- var $categories = array();
- var $tags = array();
- var $terms = array();
- var $base_url = '';
- function __construct() {
- $this->has_gzip = is_callable( 'gzopen' );
- }
- function parse( $file ) {
- $wxr_version = $frontpage = $woocommerce_pages = false;
- $in_multiline = false;
- $multiline_content = '';
- $multiline_tags = array(
- 'wp:term' => array( 'terms', array( $this, 'process_term' ) ),
- 'wp:category' => array( 'categories', array( $this, 'process_category' ) ),
- 'wp:tag' => array( 'tags', array( $this, 'process_tag' ) ),
- 'item' => array( 'posts', array( $this, 'process_post' ) ),
- );
- $fp = $this->fopen( $file, 'r' );
- if ( $fp ) {
- while ( ! $this->feof( $fp ) ) {
- $importline = $this->fgets( $fp );
- $importline = str_replace( '{WPV:SAMPLES_URI}', VAMTAM_SAMPLES_URI , $importline );
- if ( ! $frontpage ) {
- preg_match( '|<wpv:frontpage.*?>(\d*?)</wpv:frontpage>|is', $importline, $frontpage );
- if ( isset( $frontpage[1] ) ) {
- $frontpage = $frontpage[1];
- }
- }
- if ( ! $woocommerce_pages ) {
- preg_match( '|<wpv:woocommerce_pages.*?><!\[CDATA\[(.*?)\]\]></wpv:woocommerce_pages>|is', $importline, $woocommerce_pages );
- if ( isset( $woocommerce_pages[1] ) ) {
- $woocommerce_pages = $woocommerce_pages[1];
- }
- }
- if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
- $wxr_version = $version[1];
- if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
- preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
- $this->base_url = $url[1];
- continue;
- }
- if ( false !== strpos( $importline, '<wp:author>' ) ) {
- preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
- $a = $this->process_author( $author[1] );
- $this->authors[$a['author_login']] = $a;
- continue;
- }
- foreach ( $multiline_tags as $tag => $handler ) {
- if ( false !== strpos( $importline, "<$tag>" ) ) {
- $multiline_content = '';
- $in_multiline = true;
- break;
- }
- if ( false !== strpos( $importline, "</$tag>" ) ) {
- $in_multiline = false;
- $this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content );
- break;
- }
- }
- if ( $in_multiline ) {
- $multiline_content .= $importline;
- }
- }
- $this->fclose( $fp );
- if ( class_exists( 'FLBuilderImporterDataFix' ) ) {
- // Try to fix any broken builder data.
- foreach ( $this->posts as $post_index => $post ) {
- if ( ! isset( $post['postmeta'] ) || ! is_array( $post['postmeta'] ) ) {
- continue;
- }
- foreach ( $post['postmeta'] as $postmeta_index => $postmeta ) {
- if ( stristr( $postmeta['key'], '_fl_builder_' ) ) {
- $this->posts[ $post_index ]['postmeta'][ $postmeta_index ]['value'] = Vamtam_Importers::fix_serialized( $postmeta['value'] );
- }
- }
- }
- }
- }
- if ( ! $wxr_version )
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
- return array(
- 'authors' => $this->authors,
- 'posts' => $this->posts,
- 'categories' => $this->categories,
- 'tags' => $this->tags,
- 'terms' => $this->terms,
- 'base_url' => $this->base_url,
- 'version' => $wxr_version,
- 'frontpage' => $frontpage,
- 'woocommerce_pages' => $woocommerce_pages,
- );
- }
- function get_tag( $string, $tag ) {
- preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
- if ( isset( $return[1] ) ) {
- if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
- if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
- preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
- $return = '';
- foreach( $matches[1] as $match )
- $return .= $match;
- } else {
- $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
- }
- } else {
- $return = $return[1];
- }
- } else {
- $return = '';
- }
- return $return;
- }
- function process_category( $c ) {
- return array(
- 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
- 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
- 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
- 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
- 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
- );
- }
- function process_tag( $t ) {
- return array(
- 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
- 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
- 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
- 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
- );
- }
- function process_term( $t ) {
- return array(
- 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
- 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
- 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
- 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
- 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
- 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
- );
- }
- function process_author( $a ) {
- return array(
- 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
- 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
- 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
- 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
- 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
- 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
- );
- }
- function process_post( $post ) {
- $post_id = $this->get_tag( $post, 'wp:post_id' );
- $post_title = $this->get_tag( $post, 'title' );
- $post_date = $this->get_tag( $post, 'wp:post_date' );
- $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
- $comment_status = $this->get_tag( $post, 'wp:comment_status' );
- $ping_status = $this->get_tag( $post, 'wp:ping_status' );
- $status = $this->get_tag( $post, 'wp:status' );
- $post_name = $this->get_tag( $post, 'wp:post_name' );
- $post_parent = $this->get_tag( $post, 'wp:post_parent' );
- $menu_order = $this->get_tag( $post, 'wp:menu_order' );
- $post_type = $this->get_tag( $post, 'wp:post_type' );
- $post_password = $this->get_tag( $post, 'wp:post_password' );
- $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
- $guid = $this->get_tag( $post, 'guid' );
- $post_author = $this->get_tag( $post, 'dc:creator' );
- $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
- $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
- $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
- $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
- $post_content = $this->get_tag( $post, 'content:encoded' );
- $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
- $post_content = str_replace( '<br>', '<br />', $post_content );
- $post_content = str_replace( '<hr>', '<hr />', $post_content );
- $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
- 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
- 'menu_order', 'post_type', 'post_password', 'is_sticky'
- );
- $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
- if ( $attachment_url )
- $postdata['attachment_url'] = $attachment_url;
- preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
- foreach ( $terms as $t ) {
- $post_terms[] = array(
- 'slug' => $t[2],
- 'domain' => $t[1],
- 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
- );
- }
- if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
- preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
- $comments = $comments[1];
- if ( $comments ) {
- foreach ( $comments as $comment ) {
- preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta );
- $commentmeta = $commentmeta[1];
- $c_meta = array();
- foreach ( $commentmeta as $m ) {
- $c_meta[] = array(
- 'key' => $this->get_tag( $m, 'wp:meta_key' ),
- 'value' => $this->get_tag( $m, 'wp:meta_value' ),
- );
- }
- $post_comments[] = array(
- 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
- 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
- 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
- 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
- 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
- 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
- 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
- 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
- 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
- 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
- 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
- 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
- 'commentmeta' => $c_meta,
- );
- }
- }
- if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments;
- preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta );
- $postmeta = $postmeta[1];
- if ( $postmeta ) {
- foreach ( $postmeta as $p ) {
- $post_postmeta[] = array(
- 'key' => $this->get_tag( $p, 'wp:meta_key' ),
- 'value' => $this->get_tag( $p, 'wp:meta_value' ),
- );
- }
- }
- if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta;
- return $postdata;
- }
- function _normalize_tag( $matches ) {
- return '<' . strtolower( $matches[1] );
- }
- function fopen( $filename, $mode = 'r' ) {
- if ( $this->has_gzip )
- return gzopen( $filename, $mode );
- return fopen( $filename, $mode );
- }
- function feof( $fp ) {
- if ( $this->has_gzip )
- return gzeof( $fp );
- return feof( $fp );
- }
- function fgets( $fp ) {
- if ( $this->has_gzip )
- return gzgets( $fp );
- return fgets( $fp );
- }
- function fclose( $fp ) {
- if ( $this->has_gzip )
- return gzclose( $fp );
- return fclose( $fp );
- }
- }
|