",
'',
$current,
strlen( $total_pages )
);
}
$html_total_pages = sprintf( "%s", number_format_i18n( $total_pages ) );
$page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging', 'wpvivid-backuprestore' ), $html_current_page, $html_total_pages ) . $total_pages_after;
if ( $disable_next ) {
$page_links[] = '›';
} else {
$page_links[] = sprintf(
"%s%s
",
$current,
__( 'Next page', 'wpvivid-backuprestore' ),
'›'
);
}
if ( $disable_last ) {
$page_links[] = '»';
} else {
$page_links[] = sprintf(
"%s%s
",
__( 'Last page', 'wpvivid-backuprestore' ),
'»'
);
}
$pagination_links_class = 'pagination-links';
if ( ! empty( $infinite_scroll ) ) {
$pagination_links_class .= ' hide-if-js';
}
$output .= "\n';
if ( $total_pages ) {
$page_class = $total_pages < 2 ? ' one-page' : '';
} else {
$page_class = ' no-pages';
}
$this->_pagination = "$output
";
echo $this->_pagination;
}
/**
* Generate the table navigation above or below the table
*
* @since 3.1.0
* @param string $which
*/
protected function display_tablenav( $which ) {
$css_type = '';
if ( 'top' === $which ) {
wp_nonce_field( 'bulk-' . $this->_args['plural'] );
$css_type = 'margin: 0 0 10px 0';
}
else if( 'bottom' === $which ) {
$css_type = 'margin: 10px 0 0 0';
}
?>
'; ?>
extra_tablenav( $which );
$this->pagination( $which );
?>
0){
$running_time .= gmdate("G",$time_diff).'hour';
}
if(intval(gmdate("i",$time_diff)) > 0){
$running_time .= intval(gmdate("i",$time_diff)).'min';
}
if(intval(gmdate("s",$time_diff)) > 0){
$running_time .= intval(gmdate("s",$time_diff)).'second';
}
$ret['type']=$task['data']['doing'];
$ret['progress']=$task['data'][$ret['type']]['progress'];
$ret['doing']=$task['data'][$ret['type']]['doing'];
if(isset($task['data'][$ret['type']]['sub_job'][$ret['doing']]['progress']))
$ret['descript']=$task['data'][$ret['type']]['sub_job'][$ret['doing']]['progress'];
else
$ret['descript']='';
if(isset($task['data'][$ret['type']]['sub_job'][$ret['doing']]['upload_data']))
$ret['upload_data']=$task['data'][$ret['type']]['sub_job'][$ret['doing']]['upload_data'];
$task['data'][$ret['type']]['sub_job'][$ret['doing']]['upload_data']=false;
$ret['running_time']=$running_time;
$ret['running_stamp']=$time_diff;
return $ret;
}
else
{
return false;
}
}
}
class WPvivid_Exporter_task
{
private $task;
public function __construct($task_id=false,$task=false)
{
if($task_id!==false)
{
$this->task=WPvivid_Exporter_taskmanager::get_task($task_id);
}
if($task!==false)
{
$this->task=$task;
}
}
public function get_id()
{
return $this->task['id'];
}
public function new_backup_task($options)
{
$id=uniqid('wpvivid-');
$this->task=array();
$this->task['id']=$id;
$this->task['status']['start_time']=time();
$this->task['status']['run_time']=time();
$this->task['status']['timeout']=time();
$this->task['status']['str']='ready';
$this->task['status']['resume_count']=0;
if(isset($options['remote'])) {
if($options['remote']=='1') {
$this->task['options']['remote_options'] = isset($options['remote_options']) ? $options['remote_options'] : WPvivid_Setting::get_remote_options();
}
else {
$this->task['options']['remote_options']=false;
}
}
else {
$this->task['options']['remote_options']=false;
}
$this->task['options']['remote_options'] = apply_filters('wpvivid_set_remote_options', $this->task['options']['remote_options'],$options);
if(isset($options['local'])) {
$this->task['options']['save_local'] = $options['local']=='1' ? 1 : 0;
}
else {
$this->task['options']['save_local']=1;
}
$this->task['options']['post_comment'] = $options['post_comment'];
if(empty($backup_prefix))
$this->task['options']['file_prefix'] = $this->task['id'] . '_' . gmdate('Y-m-d-H-i', $this->task['status']['start_time']);
else
$this->task['options']['file_prefix'] = $backup_prefix . '_' . $this->task['id'] . '_' . gmdate('Y-m-d-H-i', $this->task['status']['start_time']);
$this->task['options']['log_file_name']=$id.'_export';
$log=new WPvivid_Log();
$log->CreateLogFile($this->task['options']['log_file_name'],'no_folder','export');
$this->task['options']['backup_options']['prefix']=$this->task['options']['file_prefix'];
$this->task['options']['backup_options']['compress']=WPvivid_Setting::get_option('wpvivid_compress_setting');
$this->task['options']['backup_options']['dir']=WPvivid_Setting::get_backupdir();
$this->task['options']['backup_options']['post_ids']=$options['post_ids'];
$this->task['options']['backup_options']['post_type']=$options['post_type'];
$export_data['json_info']['post_type']=$options['post_type'];
//$export_data['json_info']['post_ids']=$options['post_ids'];
$export_data['json_info']['post_comment']=$options['post_comment'];
$this->task['options']['backup_options']['backup'][$options['post_type']]=$export_data;
$this->task['data']['doing']='export';
$this->task['data']['export']['doing']='';
$this->task['data']['export']['finished']=0;
$this->task['data']['export']['progress']=0;
if(sizeof($options['post_ids'])>50) {
$this->task['data']['export']['pre_progress']=(50/sizeof($options['post_ids']))*100;
}
else {
$this->task['data']['export']['pre_progress']=100;
}
$this->task['data']['export']['job_data']=array();
$this->task['data']['export']['sub_job']=array();
$this->task['data']['export']['export_info']['post_count']=sizeof($options['post_ids']);
$this->task['data']['upload']['doing']='';
$this->task['data']['upload']['finished']=0;
$this->task['data']['upload']['progress']=0;
$this->task['data']['upload']['job_data']=array();
$this->task['data']['upload']['sub_job']=array();
WPvivid_Exporter_taskmanager::update_task($id,$this->task);
$ret['result']='success';
$ret['task_id']=$this->task['id'];
$log->CloseFile();
return $ret;
}
private function parse_url_all($url)
{
$parse = wp_parse_url($url);
$path=str_replace('/','_',$parse['path']);
return $parse['host'].$path;
}
public function update_sub_task_progress($key,$finished,$progress)
{
$this->task=WPvivid_Exporter_taskmanager::get_task($this->get_id());
$this->task['status']['run_time']=time();
$this->task['status']['str']='running';
$this->task['data']['doing']='export';
$sub_job_name=$key;
$this->task['data']['export']['doing']=$key;
$this->task['data']['export']['sub_job'][$sub_job_name]['finished']=$finished;
$this->task['data']['export']['sub_job'][$sub_job_name]['progress']=$progress;
if(!isset( $this->task['data']['export']['sub_job'][$sub_job_name]['job_data']))
{
$this->task['data']['export']['sub_job'][$sub_job_name]['job_data']=array();
}
WPvivid_Exporter_taskmanager::update_task($this->get_id(),$this->task);
}
public function get_next_posts()
{
asort($this->task['options']['backup_options']['post_ids']);
WPvivid_Exporter_taskmanager::update_task($this->get_id(),$this->task);
$post_ids=$this->task['options']['backup_options']['post_ids'];
if(empty($post_ids))
{
return false;
}
/*if(sizeof($post_ids)>50)
{
$next_post_ids = array_splice( $post_ids, 0, 50 );
}
else
{
$next_post_ids=$post_ids;
}*/
$next_post_ids=$post_ids;
$ret=$this->get_post_contain_attachment_ids($next_post_ids);
$next_post_ids = array_splice( $this->task['options']['backup_options']['post_ids'], 0, $ret['post_count'] );
$ret['next_post_ids']=$next_post_ids;
$post_type = $this->task['options']['backup_options']['post_type'];
$ret['json_info'] = $this->task['options']['backup_options']['backup'][$post_type]['json_info'];
$first=reset($next_post_ids);
$last=end($next_post_ids);
$post_comment = !empty($this->task['options']['post_comment']) ? $this->task['options']['post_comment'].'_' : '';
$ret['file_name']=$post_comment.self::get_id().'_'.gmdate('Y-m-d-H-i', $this->task['status']['start_time']);
$ret['export_type']=$this->task['options']['backup_options']['post_type'];
return $ret;
}
public function update_finished_posts($finished_posts)
{
$this->task=WPvivid_Exporter_taskmanager::get_task( $this->get_id());
array_splice( $this->task['options']['backup_options']['post_ids'], 0, $finished_posts['post_count'] );
$this->task['data']['export']['progress']=$this->task['data']['export']['progress']+$this->task['data']['export']['pre_progress'];
if($this->task['data']['export']['progress']>100)
{
$this->task['data']['export']['progress']=100;
}
WPvivid_Exporter_taskmanager::update_task($this->get_id(),$this->task);
}
public function update_export_files($file_data)
{
$this->task=WPvivid_Exporter_taskmanager::get_task( $this->get_id());
$this->task['data']['file_data'][]=$file_data;
$this->task['data']['export']['export_info']['file_name']=$file_data['file_name'];
$this->task['data']['export']['export_info']['size']=$file_data['size'];
WPvivid_Exporter_taskmanager::update_task($this->get_id(),$this->task);
}
public function get_export_files()
{
$this->task=WPvivid_Exporter_taskmanager::get_task( $this->get_id());
if(isset($this->task['data']['file_data']))
{
$file_data=$this->task['data']['file_data'];
return $file_data;
}
else
{
return array();
}
}
public function get_post_contain_attachment_ids($post_ids)
{
$max_size=1024*1024*100;
$current_size=0;
$count=0;
$sum_attachment_ids=array();
$attachment_added_ids=array();
$files=array();
foreach ($post_ids as $id)
{
$count++;
$attachment_ids=array();
$post = get_post( $id );
if (preg_match_all( '/
]+>/', $post->post_content, $matches ) )
{
foreach( $matches[0] as $image )
{
if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && ( $attachment_id = absint( $class_id[1] ) ) )
{
if(!in_array($attachment_id,$attachment_added_ids))
{
if(!is_null(get_post($attachment_id)))
{
$attachment_ids[] = $attachment_id;
$attachment_added_ids[]=$attachment_id;
}
else
{
$ret_attachment=$this->get_image_from_post_content($image);
$current_size+=$ret_attachment['size'];
$files=array_merge($files,$ret_attachment['files']);
}
}
}
else
{
$ret_attachment=$this->get_image_from_post_content($image);
$current_size+=$ret_attachment['size'];
$files=array_merge($files,$ret_attachment['files']);
}
}
}
$_elementor_meta=get_post_meta($id,'_elementor_data',true);
if($_elementor_meta!=false)
{
if ( is_string( $_elementor_meta ) && ! empty( $_elementor_meta ) )
{
$_elementor_meta = json_decode( $_elementor_meta, true );
}
if ( empty( $_elementor_meta ) )
{
$_elementor_meta = array();
}
$elements_data=$_elementor_meta;
foreach ( $elements_data as $element_data )
{
$element_image=$this->get_element_image($element_data,$attachment_added_ids);
$attachment_ids=array_merge($attachment_ids,$element_image);
}
}
//_thumbnail_id
$_thumbnail_id=get_post_meta($id,'_thumbnail_id',true);
if($_thumbnail_id!=false)
{
if(!in_array($_thumbnail_id,$attachment_added_ids))
{
if(!is_null(get_post($_thumbnail_id)))
{
$attachment_ids[] = $_thumbnail_id;
$attachment_added_ids[]=$_thumbnail_id;
}
}
}
$sum_attachment_ids=array_merge($sum_attachment_ids,$attachment_ids);
foreach ($attachment_ids as $attachment_id)
{
$ret_attachment=$this->get_attachment_size($attachment_id);
$current_size+=$ret_attachment['size'];
$files=array_merge($files,$ret_attachment['files']);
}
if($current_size>$max_size)
{
break;
}
}
$ret['attachment_ids']=$sum_attachment_ids;
$ret['post_count']=$count;
$ret['files']=$files;
return $ret;
}
public function get_image_from_post_content($image)
{
$ret['size']=0;
$ret['files']=array();
if(class_exists('DOMDocument'))
{
$doc = new DOMDocument();
$doc->loadHTML($image);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/@src)");
}
else
{
preg_match('/src="([^"]+)/i',$image, $src);
$src= str_ireplace( 'src="', '', $src[0]);
}
$src=str_replace('https://','',$src);
$src=str_replace('http://','',$src);
$upload=wp_upload_dir();
$upload['baseurl']=str_replace('https://','',$upload['baseurl']);
$upload['baseurl']=str_replace('http://','',$upload['baseurl']);
$path=str_replace($upload['baseurl'],$upload['basedir'],$src);
if(file_exists($path))
{
$ret['size']+=filesize($path);
$ret['files'][]=$path;
}
return $ret;
}
public function get_attachment_size($attachment_id)
{
$files=array();
global $wpdb;
$postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $attachment_id ) );
foreach ( $postmeta as $meta )
{
$upload_dir = wp_upload_dir();
if ( $upload_dir['error'] !== false )
{
continue;
}
$dir=$upload_dir['basedir'];
if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
continue;
}
if($meta->meta_key=='_wp_attached_file')
{
$bfound=false;
$name=$dir.DIRECTORY_SEPARATOR.$meta->meta_value;
if(!in_array($name,$files)&&file_exists($name))
{
$files[]=$name;
$bfound=true;
}
if($bfound)
{
$attach_meta = wp_get_attachment_metadata( $attachment_id );
if($attach_meta!=false)
{
if(isset($attach_meta['sizes']))
{
foreach ($attach_meta['sizes'] as $key=>$value)
{
$data=image_get_intermediate_size($attachment_id,$key);
$data['path']=ltrim($data['path'], './');
$name=$dir.DIRECTORY_SEPARATOR.$data['path'];
if(!in_array($name,$files)&&file_exists($name))
{
$files[]=$dir.DIRECTORY_SEPARATOR.$data['path'];
}
}
}
else
{
global $wpvivid_plugin;
$wpvivid_plugin->wpvivid_log->WriteLog('attach_meta size not found id:'.$attachment_id,'notice');
}
}
}
}
}
$size=0;
if(!empty($files))
{
foreach ($files as $file)
{
$size+=filesize($file);
}
}
$ret['size']=$size;
$ret['files']=$files;
return $ret;
}
public function get_element_image($element_data,&$attachment_added_ids)
{
$element_image=array();
if(!empty($element_data['settings']))
{
$settings=$element_data['settings'];
if(isset($settings['image']))
{
if(!in_array($settings['image']['id'],$attachment_added_ids))
{
$element_image[]=$settings['image']['id'];
$attachment_added_ids[]=$settings['image']['id'];
}
}
}
if(!empty($element_data['elements']))
{
foreach ($element_data['elements'] as $element)
{
$temp=$this->get_element_image($element,$attachment_added_ids);
$element_image=array_merge($element_image,$temp);
}
}
return $element_image;
}
public function add_new_export()
{
$files=$this->get_export_files();
$backup_data=array();
$status=WPvivid_Exporter_taskmanager::get_backup_task_status($this->task['id']);
$backup_data['create_time']=$status['start_time'];
global $wpvivid_plugin;
$backup_data['log']=$wpvivid_plugin->wpvivid_log->log_file;
$backup_data['export']=$files;
$backup_data['id']=$this->task['id'];
$list = get_option('wpvivid_export_list',array());
$list[$this->task['id']]=$backup_data;
WPvivid_Setting::update_option('wpvivid_export_list',$list);
}
}
class WPvivid_Exporter_Item{
private $config;
public function __construct($options){
$this->config=$options;
}
public function get_download_export_files(){
$files = isset($this->config['export']) ? $this->config['export'] : array();
if(empty($files)){
$ret['result'] = WPVIVID_FAILED;
$ret['error']='Failed to get export files.';
}
else{
$ret['result'] = WPVIVID_SUCCESS;
$ret['files']=$files;
}
return $ret;
}
public function get_download_progress($backup_id, $files){
$this->config['local']['path'] = 'wpvividbackups';
foreach ($files as $file){
$need_download = false;
$file_path = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.$this->config['local']['path'].DIRECTORY_SEPARATOR.$file;
$download_url = content_url().DIRECTORY_SEPARATOR.$this->config['local']['path'].DIRECTORY_SEPARATOR.$file;
if(file_exists($file_path)){
//need calc file size, then compare is need download
}
else{
$need_download = true;
}
if($need_download){
}
else{
$ret['result'] = WPVIVID_SUCCESS;
$ret['files'][$file]['status'] = 'completed';
$ret['files'][$file]['download_path'] = $file_path;
$ret['files'][$file]['download_url'] = $download_url;
ob_start();
?>
task=new WPvivid_Exporter_task($task_id);
}
else if($task!==false)
{
$this->task=new WPvivid_Exporter_task(false,$task);
}
else
{
$this->task=new WPvivid_Exporter_task();
}
}
public function init_options($task_id)
{
$this->task=new WPvivid_Exporter_task($task_id);
}
public function export($task_id)
{
$this->init_options($task_id);
global $wpvivid_plugin;
$next=$this->task->get_next_posts();
$ret['result']='success';
WPvivid_Exporter_taskmanager::update_main_task_progress($task_id, 'export', 5, 0);
while($next!==false)
{
@set_time_limit(900);
$wpvivid_plugin->wpvivid_log->WriteLog('Prepare to export post '.$next['file_name'],'notice');
$this->task->update_sub_task_progress($next['file_name'],0,'Start export file '.$next['file_name']);
$ret=$this->export_post_to_xml($next['next_post_ids'], $next['attachment_ids'],$next['file_name'],$next['export_type']);
$wpvivid_plugin->wpvivid_log->WriteLog('Finished to export post '.$next['file_name'],'notice');
if($ret['result']=='success')
{
$wpvivid_plugin->wpvivid_log->WriteLog('Prepare to zip file '.$next['file_name'],'notice');
$next['json_info']['posts_count']=sizeof($next['next_post_ids']);
$ret=$this->zip_media_files($ret['xml_file_name'],$next['files'],$next['file_name'],$next['export_type'],$next['json_info']);
$wpvivid_plugin->wpvivid_log->WriteLog('Finished to zip file '.$next['file_name'],'notice');
if($ret['result']!='success')
{
$wpvivid_plugin->wpvivid_log->WriteLog('Failed to zip post '.$next['file_name'].' '.wp_json_encode($ret),'notice');
return $ret;
}
$this->task->update_sub_task_progress($next['file_name'],1,'Backing up '.$next['file_name'].' finished');
$this->task->update_finished_posts($next);
$this->task->update_export_files($ret['file_data']);
}
else
{
$wpvivid_plugin->wpvivid_log->WriteLog('Failed to export post '.$next['file_name'].' '.wp_json_encode($ret),'notice');
return $ret;
}
$next=$this->task->get_next_posts();
}
WPvivid_Exporter_taskmanager::update_main_task_progress($task_id, 'export', 100, 1);
return $ret;
}
public function export_post_to_xml($posts_ids,$attachment_ids,$file_name,$export_type)
{
$all_ids=array_merge($posts_ids,$attachment_ids);
//$xml_file_name=$file_name.'.xml';
$xml_file_name=$file_name.'_'.$export_type.'.xml';
//$files=array();
$export_folder = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.WPVIVID_IMPORT_EXPORT_DIR;
if(!file_exists($export_folder)) {
@mkdir($export_folder);
}
$path=$export_folder.DIRECTORY_SEPARATOR.$xml_file_name;
$ret['xml_file_name']=$path;
if(file_exists($path))
{
@wp_delete_file($path);
}
$this->write_header_to_file($path);
$this->write_authors_list_to_file($path,$all_ids);
$this->write_cat_to_file($path,$posts_ids);
global $wp_query,$wpdb;
// Fake being in the loop.
$wp_query->in_the_loop = true;
$task_id = $this->task->get_id();
while ( $next_posts = array_splice( $posts_ids, 0, 20 ) )
{
$where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
// Begin Loop.
foreach ( $posts as $post )
{
$this->write_post_to_file($path,$post);
}
}
WPvivid_Exporter_taskmanager::update_main_task_progress($task_id, 'export', 25, 0);
while ( $next_posts = array_splice( $attachment_ids, 0, 20 ) )
{
$where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
// Begin Loop.
foreach ( $posts as $post )
{
$this->write_media_post_to_file($path,$post);
//$post_files=$this->write_media_post_to_file($path,$post);
//$files=array_merge($post_files,$files);
}
}
WPvivid_Exporter_taskmanager::update_main_task_progress($task_id, 'export', 50, 0);
$this->write_footer_to_file($path);
//$ret['files']=$files;
$ret['result']='success';
return $ret;
}
private function zip_media_files($xml_file,$files,$file_name,$export_type,$json_info=false)
{
if (!class_exists('WPvivid_PclZip'))
include_once WPVIVID_PLUGIN_DIR . '/includes/zip/class-wpvivid-pclzip.php';
$path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.WPVIVID_IMPORT_EXPORT_DIR.DIRECTORY_SEPARATOR.$file_name.'_export_'.$export_type.'.zip';
$options['compress']['no_compress']=1;
$options['compress']['use_temp_file']=1;
$options['compress']['use_temp_size']=16;
$options['root_flag']=WPVIVID_BACKUP_ROOT_WP_CONTENT;
if(file_exists($path))
@wp_delete_file($path);
$archive = new WPvivid_PclZip($path);
if($json_info!==false) {
$temp_path = dirname($path).DIRECTORY_SEPARATOR.'wpvivid_export_package_info.json';
if(file_exists($temp_path)) {
@wp_delete_file($temp_path);
}
$json_info['create_time']=time();
$json_info['xml_file']=basename($xml_file);
$json_info['media_size']=0;
foreach ($files as $file)
{
$json_info['media_size']+=@filesize($file);
}
file_put_contents($temp_path,print_r(wp_json_encode($json_info),true));
$archive -> add($temp_path,WPVIVID_PCLZIP_OPT_REMOVE_PATH,dirname($temp_path));
@wp_delete_file($temp_path);
}
$ret =$archive -> add($xml_file,WPVIVID_PCLZIP_OPT_REMOVE_PATH,dirname($xml_file));
@wp_delete_file($xml_file);
if(!$ret)
{
return array('result'=>WPVIVID_FAILED,'error'=>$archive->errorInfo(true));
}
if(!empty($files)) {
$ret = $archive->add($files, WPVIVID_PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR, WPVIVID_PCLZIP_OPT_TEMP_FILE_THRESHOLD, 16);
}
if(!$ret)
{
return array('result'=>WPVIVID_FAILED,'error'=>$archive->errorInfo(true));
}
$file_data = array();
$file_data['file_name'] = basename($path);
$file_data['size'] = filesize($path);
return array('result'=>WPVIVID_SUCCESS,'file_data'=>$file_data);
}
public function write_header_to_file($file)
{
$wxr_version=1.2;
$line='\n";
$line.='
';
$line.=apply_filters( 'the_generator', get_the_generator( 'export' ), 'export' ) . "\n";
$line.='
';
$line.='
'.apply_filters( 'bloginfo_rss', get_bloginfo_rss( 'name' ), 'name' ).'
'.apply_filters( 'bloginfo_rss', get_bloginfo_rss( 'url' ), 'url' ).'
'.apply_filters( 'bloginfo_rss', get_bloginfo_rss( 'description' ), 'description' ).'
'.gmdate( 'D, d M Y H:i:s +0000' ).'
'.apply_filters( 'bloginfo_rss', get_bloginfo_rss( 'language' ), 'language' ).'
'.$wxr_version.'
'.$this->wxr_site_url().'
'.apply_filters( 'bloginfo_rss', get_bloginfo_rss( 'url' ), 'url' ).'
';
file_put_contents($file,$line);
}
public function write_authors_list_to_file($file,$post_ids)
{
$line=$this->wxr_authors_list( $post_ids );
file_put_contents($file,$line,FILE_APPEND);
}
public function write_footer_to_file($file)
{
$line='
';
file_put_contents($file,$line,FILE_APPEND);
}
public function write_post_header_to_file($file,$post)
{
$is_sticky = is_sticky( $post->ID ) ? 1 : 0;
$post = get_post( $post );
$guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
$id = isset( $post->ID ) ? $post->ID : 0;
$guid= apply_filters( 'the_guid', $guid, $id );
$item_header_line='
-
'.apply_filters( 'the_title_rss', $post->post_title ).'
'.esc_url( apply_filters( 'the_permalink_rss', get_permalink($post->ID) ) ).'
'.mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true,$post ), false ).'
'.$this->wxr_cdata( get_the_author_meta( 'login' ) ).'
'.$guid.'
'.$this->wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) ).'
'.$this->wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) ).'
'.intval( $post->ID ).'
'.$this->wxr_cdata( $post->post_date ).'
'.$this->wxr_cdata( $post->post_date_gmt ).'
'.$this->wxr_cdata( $post->comment_status ).'
'.$this->wxr_cdata( $post->ping_status ).'
'.$this->wxr_cdata( $post->post_name ).'
'.$this->wxr_cdata( $post->post_status ).'
'.intval( $post->post_parent ).'
'.intval( $post->menu_order ).'
'.$this->wxr_cdata( $post->post_type ).'
'.$this->wxr_cdata( $post->post_password ).'
'.intval( $is_sticky ).'
';
if ( $post->post_type == 'attachment' )
$item_header_line.=''.$this->wxr_cdata( wp_get_attachment_url( $post->ID ) ).'';
file_put_contents($file,$item_header_line,FILE_APPEND);
$line=$this->wxr_post_taxonomy($post);
file_put_contents($file,$line,FILE_APPEND);
}
public function write_media_post_to_file($file,$post)
{
global $wpdb;
$postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
$post_meta_line='';
$added_meta_key=array();
foreach ( $postmeta as $meta )
{
if(in_array($meta->meta_key,$added_meta_key))
continue;
$added_meta_key[]=$meta->meta_key;
$post_meta_line.='
'.$this->wxr_cdata( $meta->meta_key ).'
'.$this->wxr_cdata( $meta->meta_value ).'
';
}
$this->write_post_header_to_file($file,$post);
file_put_contents($file,$post_meta_line,FILE_APPEND);
$_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
$comments = array_map( 'get_comment', $_comments );
$line='';
foreach ( $comments as $c )
{
$line.='
'.intval( $c->comment_ID ).'
'.$this->wxr_cdata( $c->comment_author ).'
'.$this->wxr_cdata( $c->comment_author_email ).'
'.esc_url_raw( $c->comment_author_url ).'
'.$this->wxr_cdata( $c->comment_author_IP ).'
'.$this->wxr_cdata( $c->comment_date ).'
'.$this->wxr_cdata( $c->comment_date_gmt ).'
'.$this->wxr_cdata( $c->comment_content ).'
'.$this->wxr_cdata( $c->comment_approved ).'
'.$this->wxr_cdata( $c->comment_type ).'
'.intval( $c->comment_parent ).'
'.intval( $c->user_id ).'';
$c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
foreach ( $c_meta as $meta )
{
if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) )
{
continue;
}
$line.='
'.$this->wxr_cdata( $meta->meta_key ).'
'.$this->wxr_cdata( $meta->meta_value ).'
';
}
$line.='
';
}
file_put_contents($file,$line,FILE_APPEND);
$line='
';
file_put_contents($file,$line,FILE_APPEND);
}
public function write_post_to_file($file,$post)
{
global $wpdb;
setup_postdata( $post );
$postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
$post_meta_line='';
$added_meta_key=array();
foreach ( $postmeta as $meta )
{
//if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
// continue;
//}
if(in_array($meta->meta_key,$added_meta_key))
continue;
$added_meta_key[]=$meta->meta_key;
$post_meta_line.='
'.$this->wxr_cdata( $meta->meta_key ).'
'.$this->wxr_cdata( $meta->meta_value ).'
';
}
$this->write_post_header_to_file($file,$post);
file_put_contents($file,$post_meta_line,FILE_APPEND);
$_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
$comments = array_map( 'get_comment', $_comments );
$line='';
foreach ( $comments as $c )
{
$line.='
'.intval( $c->comment_ID ).'
'.$this->wxr_cdata( $c->comment_author ).'
'.$this->wxr_cdata( $c->comment_author_email ).'
'.esc_url_raw( $c->comment_author_url ).'
'.$this->wxr_cdata( $c->comment_author_IP ).'
'.$this->wxr_cdata( $c->comment_date ).'
'.$this->wxr_cdata( $c->comment_date_gmt ).'
'.$this->wxr_cdata( $c->comment_content ).'
'.$this->wxr_cdata( $c->comment_approved ).'
'.$this->wxr_cdata( $c->comment_type ).'
'.intval( $c->comment_parent ).'
'.intval( $c->user_id ).'';
$c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
foreach ( $c_meta as $meta )
{
if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) )
{
continue;
}
$line.='
'.$this->wxr_cdata( $meta->meta_key ).'
'.$this->wxr_cdata( $meta->meta_value ).'
';
}
$line.='
';
}
file_put_contents($file,$line,FILE_APPEND);
$line='
';
file_put_contents($file,$line,FILE_APPEND);
return true;
}
public function write_cat_to_file($file,$post_ids)
{
$cats = $tags = $terms = array();
$categories = (array) get_categories( array( 'object_ids' => $post_ids ) );
$tags = (array) get_tags( array( 'object_ids' => $post_ids ) );
$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
$custom_terms = (array) get_terms( $custom_taxonomies,array( 'object_ids' => $post_ids ) );
// Put categories in order with no child going before its parent.
while ( $cat = array_shift( $categories ) ) {
if ( $cat->parent == 0 || isset( $cats[ $cat->parent ] ) ) {
$cats[ $cat->term_id ] = $cat;
} else {
$categories[] = $cat;
}
}
// Put terms in order with no child going before its parent.
while ( $t = array_shift( $custom_terms ) ) {
if ( $t->parent == 0 || isset( $terms[ $t->parent ] ) ) {
$terms[ $t->term_id ] = $t;
} else {
$custom_terms[] = $t;
}
}
unset( $categories, $custom_taxonomies, $custom_terms );
$line='';
foreach ($cats as $c)
{
$line.='
'.intval( $c->term_id ).'
'.$this->wxr_cdata( $c->slug ).'
'.$this->wxr_cdata( $c->parent ? $cats[ $c->parent ]->slug : '' ).'
'.$this->wxr_cat_name( $c ).'
'.$this->wxr_category_description( $c ).'
'.$this->wxr_term_meta( $c ).'
';
}
file_put_contents($file,$line,FILE_APPEND);
$line='';
foreach ( $tags as $t )
{
$line.='
'.intval( $t->term_id ).'
'.$this->wxr_cdata( $t->slug ).'
'.$this->wxr_tag_name( $t ).'
'.$this->wxr_tag_description( $t ).'
'.$this->wxr_term_meta( $t ).'
';
}
file_put_contents($file,$line,FILE_APPEND);
$line='';
foreach ( $terms as $t)
{
$line.='
'.$this->wxr_cdata( $t->term_id ).'
'.$this->wxr_cdata( $t->taxonomy ).'
'.$this->wxr_cdata( $t->slug ).'
'.$this->wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ).'
'.$this->wxr_term_name( $t ).'
'.$this->wxr_term_description( $t ).'
'.$this->wxr_term_meta( $t ).'
';
}
}
private function wxr_cat_name( $category )
{
if ( empty( $category->name ) )
{
return '';
}
return '' . $this->wxr_cdata( $category->name ) . "";
}
private function wxr_category_description( $category ) {
if ( empty( $category->description ) ) {
return '\n';
}
return '' . $this->wxr_cdata( $category->description ) . "";
}
private function wxr_tag_name( $tag ) {
if ( empty( $tag->name ) ) {
return '';
}
return '' . $this->wxr_cdata( $tag->name ) . "";
}
private function wxr_tag_description( $tag ) {
if ( empty( $tag->description ) ) {
return '';
}
return '' . $this->wxr_cdata( $tag->description ) . "";
}
private function wxr_term_name( $term ) {
if ( empty( $term->name ) ) {
return '';
}
return '' . $this->wxr_cdata( $term->name ) . "";
}
private function wxr_term_description( $term ) {
if ( empty( $term->description ) ) {
return '';
}
return "\t\t" . $this->wxr_cdata( $term->description ) . "";
}
private function wxr_term_meta( $term ) {
global $wpdb;
$termmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id ) );
$line='';
foreach ( $termmeta as $meta )
{
/**
* Filters whether to selectively skip term meta used for WXR exports.
*
* Returning a truthy value to the filter will skip the current meta
* object from being exported.
*
* @since 4.6.0
*
* @param bool $skip Whether to skip the current piece of term meta. Default false.
* @param string $meta_key Current meta key.
* @param object $meta Current meta object.
*/
if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) )
{
$line.="\t\t\n\t\t\t".$this->wxr_cdata( $meta->meta_key )."\n\t\t\t".$this->wxr_cdata( $meta->meta_value )."\n\t\t\n";
}
}
return $line;
}
private function wxr_cdata( $str )
{
if ( ! seems_utf8( $str ) ) {
$str = utf8_encode( $str );
}
// $str = ent2ncr(esc_html($str));
$str = '', ']]]]>', $str ) . ']]>';
return $str;
}
private function wxr_site_url() {
if ( is_multisite() ) {
// Multisite: the base URL.
return network_home_url();
} else {
// WordPress (single site): the blog URL.
return get_bloginfo_rss( 'url' );
}
}
private function wxr_authors_list( array $post_ids = array() )
{
global $wpdb;
if ( ! empty( $post_ids ) ) {
$post_ids = array_map( 'absint', $post_ids );
$and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
} else {
$and = '';
}
$authors = array();
$results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
foreach ( (array) $results as $result )
{
$authors[] = get_userdata( $result->post_author );
}
$authors = array_filter( $authors );
$line='';
foreach ( $authors as $author )
{
$line.= "\t";
$line.= '' . intval( $author->ID ) . '';
$line.= '' . $this->wxr_cdata( $author->user_login ) . '';
$line.= '' . $this->wxr_cdata( $author->user_email ) . '';
$line.= '' . $this->wxr_cdata( $author->display_name ) . '';
$line.= '' . $this->wxr_cdata( $author->first_name ) . '';
$line.= '' . $this->wxr_cdata( $author->last_name ) . '';
$line.= "\n";
}
return $line;
}
private function wxr_post_taxonomy($post)
{
$taxonomies = get_object_taxonomies( $post->post_type );
if ( empty( $taxonomies ) ) {
return;
}
$terms = wp_get_object_terms( $post->ID, $taxonomies );
$line='';
foreach ( (array) $terms as $term )
{
$line.= "\t\ttaxonomy}\" nicename=\"{$term->slug}\">" . $this->wxr_cdata( $term->name ) . "\n";
}
return $line;
}
}