end_shutdown_function = false; } public function get_screen_ids($screen_ids) { $screen_ids[]='wpvivid-backup_page_wpvivid-export-import'; return $screen_ids; } public function get_toolbar_menus($toolbar_menus) { $admin_url = apply_filters('wpvivid_get_admin_url', ''); $menu['id']='wpvivid_admin_menu_export_import'; $menu['parent']='wpvivid_admin_menu'; $menu['title']=__('Export & Import', 'wpvivid-backuprestore'); $menu['tab']= 'admin.php?page=wpvivid-export-import'; $menu['href']=$admin_url . 'admin.php?page=wpvivid-export-import'; $menu['capability']='administrator'; $menu['index']=4; $toolbar_menus[$menu['parent']]['child'][$menu['id']]=$menu; return $toolbar_menus; } public function get_admin_menus($submenus) { $submenu['parent_slug']=WPVIVID_PLUGIN_SLUG; $submenu['page_title']= 'WPvivid Backup'; $submenu['menu_title']=__('Export & Import', 'wpvivid-backuprestore'); $submenu['capability']='administrator'; $submenu['menu_slug']='wpvivid-export-import'; $submenu['index']=4; $submenu['function']=array($this, 'init_page'); $submenus[$submenu['menu_slug']]=$submenu; return $submenus; } public function init_page() { ?>
|
'regular-text',
'show_option_all' => __('All Categories', 'wpvivid-backuprestore')
)
);
?>
|
|
|
get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = '$post_type'" );
wp_dropdown_users(
array(
'class' => 'regular-text',
'include' => $authors,
'name' => 'post_author',
'multi' => true,
'show_option_all' => __( 'All Authors', 'wpvivid-backuprestore' ),
'show' => 'display_name_with_login',
)
);
?>
|
|
|
|
|
|
|
|
|
|
|
Selected post(s):
.column-file_name { width:25% } .column-export_type { width:8% } .column-posts_count { width:8% } .column-media_size { width:8% } .column-import { width:8% } '; } public function get_list() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } if(!isset($_POST['post_type'])&&!isset($_POST['cat'])&&!isset($_POST['authors'])&&!isset($_POST['post_start_date'])&&!isset($_POST['post_end_date'])) { die(); } if(isset($_POST['post_ids'])&&!empty($_POST['post_ids'])) { $select_post_id=$_POST['post_ids']; $select_post_id = array_map( 'sanitize_key', $select_post_id ); $select_post_id=(int)$select_post_id; } else { $select_post_id=0; } if(isset($_POST['post_title'])&&!empty($_POST['post_title'])) { $post_title=sanitize_text_field($_POST['post_title']); } else { $post_title=''; } // $post_type=sanitize_text_field($_POST['post_type']); if(isset($_POST['cat'])) { $cat = (int)sanitize_key($_POST['cat']); } $author=(int)sanitize_key($_POST['authors']); $post_start_date=sanitize_text_field($_POST['post_start_date']); $post_end_date=sanitize_text_field($_POST['post_end_date']); global $wpdb; $where = $wpdb->prepare( "post_type =%s", $post_type); $join = ''; if(isset($_POST['cat'])) { if ($term = term_exists($cat, 'category')) { $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)"; $where .= $wpdb->prepare(" AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id']); } } if ( $author ) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $author ); } if ( $post_start_date ) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", gmdate( 'Y-m-d', strtotime( $post_start_date ) ) ); } if ( $post_end_date ) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", gmdate( 'Y-m-d', strtotime( '+1 month', strtotime( $post_end_date ) ) ) ); } if($select_post_id) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.ID = %d", $select_post_id ); } if($post_title) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title LIKE %s", '%' . $wpdb->esc_like($post_title) . '%' ); } $posts_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" ); asort($posts_ids); $list_cache=array(); foreach ($posts_ids as $id) { $post_id['id']=$id; $post_id['checked']=0; $list_cache[$id]=$post_id; } WPvivid_Setting::update_option('wpvivid_list_cache',$list_cache); $page=1; $arg['screen']=$post_type; $myListTable = new WPvivid_Post_List($arg); $myListTable->set_post_ids($list_cache,$page); $myListTable->prepare_items(); ob_start(); $myListTable->display(); $rows = ob_get_clean(); $ret['result']='success'; $ret['rows']=$rows; echo wp_json_encode($ret); die(); } public function get_export_list() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } $list = get_option('wpvivid_export_list',array()); $display_list=new WPvivid_Export_List(); $display_list->set_parent('wpvivid_import_list'); $display_list->set_list($list); $display_list->prepare_items(); ob_start(); $display_list->display(); $html = ob_get_clean(); $ret['result']='success'; $ret['html']=$html; echo wp_json_encode($ret); die(); } public function get_list_page() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } if(!isset($_POST['post_type'])&&!isset($_POST['page'])) { die(); } $list_cache=get_option('wpvivid_list_cache',array()); WPvivid_Setting::update_option('wpvivid_list_cache',$list_cache); $page=sanitize_key($_POST['page']); $post_type=sanitize_text_field($_POST['post_type']); $arg['screen']=$post_type; $myListTable = new WPvivid_Post_List($arg); $myListTable->set_post_ids($list_cache,$page); $myListTable->prepare_items(); ob_start(); $myListTable->display(); $rows = ob_get_clean(); $ret['result']='success'; $ret['rows']=$rows; echo wp_json_encode($ret); die(); } public function export_date_options($post_type = 'post') { global $wpdb, $wp_locale; $months = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM $wpdb->posts WHERE post_type = %s AND post_status != 'auto-draft' ORDER BY post_date DESC ", $post_type ) ); $month_count = count( $months ); if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) { return; } foreach ( $months as $date ) { if ( 0 == $date->year ) { continue; } $month = zeroise( $date->month, 2 ); echo ''; } } public function prepare_export_post() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } if(isset($_POST['post_type'])&&isset($_POST['export_data'])) { $post_type = sanitize_text_field($_POST['post_type']); $json_export = sanitize_text_field($_POST['export_data']); $json_export = stripslashes($json_export); $export_data = json_decode($json_export, true); $post_ids=array(); $posts_ids=array(); if(isset($export_data['post_ids']) && !empty($export_data['post_ids'])) { $post_ids=$export_data['post_ids']; } foreach ($post_ids as $id=>$checked) { if($checked) { $posts_ids[]=$id; } } if(empty($posts_ids)) { $ret['result']='failed'; $ret['error']=__('Empty post id', 'wpvivid-backuprestore'); echo wp_json_encode($ret); die(); } if(WPvivid_Exporter_taskmanager::is_tasks_running()) { $ret['result']='failed'; $ret['error']=__('A task is already running. Please wait until the running task is complete, and try again.', 'wpvivid-backuprestore'); echo wp_json_encode($ret); die(); } $export_task=new WPvivid_Exporter_task(); $options['post_ids']=$posts_ids; $options['post_type']=$post_type; $options['post_comment']=$export_data['post_comment']; $ret=$export_task->new_backup_task($options); echo wp_json_encode($ret); } die(); } public function export_now() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } try { if (!isset($_POST['task_id']) || empty($_POST['task_id']) || !is_string($_POST['task_id'])) { $ret['result'] = 'failed'; $ret['error'] = __('Error occurred while parsing the request data. Please try to run export task again.', 'wpvivid-backuprestore'); echo wp_json_encode($ret); die(); } $task_id = sanitize_key($_POST['task_id']); if(WPvivid_Exporter_taskmanager::is_tasks_running()) { $ret['result'] = 'failed'; $ret['error'] = __('A task is already running. Please wait until the running task is complete, and try again.', 'wpvivid-backuprestore'); echo wp_json_encode($ret); die(); } $this->export_post($task_id); } catch (Exception $error) { $message = 'An exception has occurred. class: '.get_class($error).';msg: '.$error->getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; error_log($message); echo wp_json_encode(array('result'=>'failed','error'=>$message)); die(); } die(); } public function export_post($task_id) { $this->end_shutdown_function = false; register_shutdown_function(array($this,'deal_shutdown_error'),$task_id); @ignore_user_abort(true); WPvivid_Exporter_taskmanager::update_backup_task_status($task_id,true,'running'); global $wpvivid_plugin; $wpvivid_plugin->wpvivid_log->OpenLogFile(WPvivid_Exporter_taskmanager::get_task_options($task_id,'log_file_name')); $wpvivid_plugin->wpvivid_log->WriteLog('Start export posts.','notice'); $wpvivid_plugin->wpvivid_log->WriteLogHander(); $this->flush($task_id); $export=new WPvivid_Exporter(); @set_time_limit(900); try { $ret = $export->export($task_id); if($ret['result']=='success') { do_action('wpvivid_handle_export_success', $task_id, true); } else { $wpvivid_plugin->wpvivid_log->WriteLog($ret['error'],'error'); WPvivid_Exporter_taskmanager::update_backup_task_status($task_id, false, 'error', false, false, $ret['error']); do_action('wpvivid_handle_export_failed', $task_id, true); } } catch (Exception $error) { $message = 'An error has occurred. class:'.get_class($error).';msg:'.$error->getMessage().';code:'.$error->getCode().';line:'.$error->getLine().';in_file:'.$error->getFile().';'; error_log($message); WPvivid_Exporter_taskmanager::update_backup_task_status($task_id,false,'error',false,false,$message); $wpvivid_plugin->wpvivid_log->WriteLog($message,'error'); $this->end_shutdown_function=true; die(); } echo wp_json_encode($ret); $this->end_shutdown_function=true; die(); } public function deal_shutdown_error($task_id) { if($this->end_shutdown_function===false) { global $wpvivid_plugin; $last_error = error_get_last(); if (!empty($last_error) && !in_array($last_error['type'], array(E_NOTICE,E_WARNING,E_USER_NOTICE,E_USER_WARNING,E_DEPRECATED), true)) { $error = $last_error; } else { $error = false; } if (WPvivid_Exporter_taskmanager::get_task($task_id) !== false) { if ($wpvivid_plugin->wpvivid_log->log_file_handle == false) { $wpvivid_plugin->wpvivid_log->OpenLogFile(WPvivid_Exporter_taskmanager::get_task_options($task_id, 'log_file_name')); } $status = WPvivid_Exporter_taskmanager::get_backup_task_status($task_id); $message='in shutdown'; if ($error !== false) { $message= 'type: '. $error['type'] . ', ' . $error['message'] . ' file:' . $error['file'] . ' line:' . $error['line']; } WPvivid_Exporter_taskmanager::update_backup_task_status($task_id, false, 'error', false, $status['resume_count'], $message); if ($wpvivid_plugin->wpvivid_log) $wpvivid_plugin->wpvivid_log->WriteLog($message, 'error'); } die(); } } public function handle_export_success($task_id) { global $wpvivid_plugin; WPvivid_Exporter_taskmanager::update_backup_task_status($task_id,false,'completed'); $wpvivid_plugin->wpvivid_log->WriteLog('Finished to export post','notice'); } public function handle_export_failed($task_id) { } public function list_tasks() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } $ret['result']='success'; $ret['show']=false; $tasks=WPvivid_Exporter_taskmanager::get_tasks(); foreach ($tasks as $task) { $this->task_monitor($task['id']); $task=WPvivid_Exporter_taskmanager::get_task($task['id']); $status=WPvivid_Exporter_taskmanager::get_backup_task_status($task['id']); $ret['show']=true; $ret['completed']=false; $ret['error']=false; if($status['str']=='running'||$status['str']=='no_responds'||$status['str']=='ready') { $ret['continue']=1; } else { $ret['continue']=0; $ret['show']=false; } $progress=WPvivid_Exporter_taskmanager::get_backup_tasks_progress($task['id']); $ret['percent']=''; $ret['doing']=$task['data']['doing']='export'; if($status['str']=='ready') { $ret['doing']=__('Ready to export. Progress: 0%, running time: 0second.','wpvivid-backuprestore'); } else if($status['str']=='running') { $ret['doing']= ' '.__('Progress: ', 'wpvivid-backuprestore') . $progress['descript'] . ', '.__('running time: ', 'wpvivid-backuprestore') . $progress['running_time']; } else if($status['str']=='wait_resume') { $ret['doing']='Task '.$task['id'].' timed out, the export task will retry in '.$task['data']['next_resume_time'].' seconds, retry times: '.$task['status']['resume_count'].'.'; } else if($status['str']=='no_responds') { $ret['doing']=__('The export task is not responding.','wpvivid-backuprestore'); } else if($status['str']=='completed') { $file_name = $task['data']['export']['export_info']['file_name']; $file_size = $task['data']['export']['export_info']['size']; if($task['options']['backup_options']['post_type'] === 'post'){ $post_type = 'posts'; } else{ $post_type = 'pages'; } $msg = '
'.__('Export error:', 'wpvivid-backuprestore').' '.$task['status']['error'].'