'logs',
'singular' => 'log',
'ajax' => true,
) );
$this->log_folder = get_site_option( 'backwpup_cfg_logfolder' );
$this->log_folder = BackWPup_File::get_absolute_path( $this->log_folder );
$this->log_folder = untrailingslashit( $this->log_folder );
}
/**
* User can
*
* @return bool True if user has the right capabilities, false otherwise
*/
function ajax_user_can() {
return current_user_can( 'backwpup_logs' );
}
/**
* Prepare Items
*
* @return void
*/
function prepare_items() {
$this->job_types = BackWPup::get_job_types();
$per_page = $this->get_items_per_page( 'backwpuplogs_per_page' );
if ( empty( $per_page ) || $per_page < 1 ) {
$per_page = 20;
}
// Load logs.
$logfiles = array();
if ( is_readable( $this->log_folder ) ) {
$dir = new BackWPup_Directory( $this->log_folder );
foreach ( $dir as $file ) {
if ( $file->isFile() && $file->isReadable() && strpos( $file->getFilename(), 'backwpup_log_' ) !== false && strpos( $file->getFilename(), '.html' ) !== false ) {
$logfiles[$file->getMTime()] = $file->getFilename();
}
}
}
// Ordering.
$order = isset( $_GET['order'] ) ? $_GET['order'] : 'desc';
$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : 'time';
if ( $orderby == 'time' ) {
if ( $order == 'asc' ) {
ksort( $logfiles );
} else {
krsort( $logfiles );
}
}
// By page.
$start = intval( ( $this->get_pagenum() - 1 ) * $per_page );
$end = $start + $per_page;
if ( $end > count( $logfiles ) ) {
$end = count( $logfiles );
}
$this->items = array();
$i = - 1;
foreach ( $logfiles as $mtime => $logfile ) {
$i ++;
if ( $i < $start ) {
continue;
}
if ( $i >= $end ) {
break;
}
$this->items[ $mtime ] = BackWPup_Job::read_logheader( $this->log_folder . '/' . $logfile );
$this->items[ $mtime ]['file'] = $logfile;
}
$this->set_pagination_args( array(
'total_items' => count( $logfiles ),
'per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
) );
}
/**
* @inheritdoc
*/
function get_sortable_columns() {
return array(
'time' => array( 'time', false ),
);
}
/**
*
*/
function no_items() {
_e( 'No Logs.', 'backwpup' );
}
/**
* @inheritdoc
*/
function get_bulk_actions() {
if ( ! $this->has_items() ) {
return array();
}
$actions = array();
$actions['delete'] = __( 'Delete', 'backwpup' );
return $actions;
}
/**
* @inheritdoc
*/
function get_columns() {
$posts_columns = array();
$posts_columns['cb'] = '';
$posts_columns['time'] = __( 'Time', 'backwpup' );
$posts_columns['job'] = __( 'Job', 'backwpup' );
$posts_columns['status'] = __( 'Status', 'backwpup' );
$posts_columns['type'] = __( 'Type', 'backwpup' );
$posts_columns['size'] = __( 'Size', 'backwpup' );
$posts_columns['runtime'] = __( 'Runtime', 'backwpup' );
return $posts_columns;
}
/**
* @inheritdoc
*/
function column_cb( $item ) {
return '';
}
/**
* The job id Column
*
* @param $item
*
* @return string
*/
function column_time( $item ) {
$r = sprintf( __( '%1$s at %2$s', 'backwpup' ), date_i18n( get_option( 'date_format' ), $item['logtime'], true ), date_i18n( get_option( 'time_format' ), $item['logtime'], true ) );
return $r;
}
/**
* The type Column
*
* @param $item
*
* @return string
*/
function column_type( $item ) {
$r = '';
if ( $types = explode( '+', $item['type'] ) ) {
foreach ( $types as $type ) {
if ( isset( $this->job_types[ $type ] ) ) {
$r .= $this->job_types[ $type ]->info['name'] . '
';
} else {
$r .= $type . '
';
}
}
}
return $r;
}
/**
* The log Column
*
* @param $item
*
* @return string
*/
function column_job( $item ) {
$log_name = str_replace( array( '.html', '.gz' ), '', basename( $item['file'] ) );
$r = "" . esc_html( ! empty( $item['name'] ) ? $item['name'] : $item['file'] ) . "";
$actions = array();
$actions['view'] = '' . __( 'View', 'backwpup' ) . '';
if ( current_user_can( 'backwpup_logs_delete' ) ) {
$actions['delete'] = "get_pagenum() . '&logfiles[]=' . $item['file'], 'bulk-logs' ) . "\" onclick=\"return showNotice.warn();\">" . __( 'Delete', 'backwpup' ) . "";
}
$actions['download'] = "" . __( 'Download', 'backwpup' ) . "";
$r .= $this->row_actions( $actions );
return $r;
}
/**
* The status Column
*
* @param $item
*
* @return string
*/
function column_status( $item ) {
$r = '';
if ( $item['errors'] ) {
$r .= sprintf( '' . _n( "1 ERROR", "%d ERRORS", $item['errors'], 'backwpup' ) . '
', $item['errors'] );
}
if ( $item['warnings'] ) {
$r .= sprintf( '' . _n( "1 WARNING", "%d WARNINGS", $item['warnings'], 'backwpup' ) . '
', $item['warnings'] );
}
if ( ! $item['errors'] && ! $item['warnings'] ) {
$r .= '' . __( 'O.K.', 'backwpup' ) . '';
}
return $r;
}
/**
* The size Column
*
* @param $item
*
* @return string
*/
function column_size( $item ) {
if ( ! empty( $item['backupfilesize'] ) ) {
return size_format( $item['backupfilesize'], 2 );
} else {
return __( 'Log only', 'backwpup' );
}
}
/**
* The runtime Column
*
* @param $item
*
* @return string
*/
function column_runtime( $item ) {
return $item['runtime'] . ' ' . __( 'seconds', 'backwpup' );
}
/**
* Load
*
* @return void
*/
public static function load() {
//Create Table
self::$listtable = new BackWPup_Page_Logs;
switch ( self::$listtable->current_action() ) {
// Delete Log
case 'delete':
if ( ! current_user_can( 'backwpup_logs_delete' ) ) {
break;
}
if ( is_array( $_GET['logfiles'] ) ) {
check_admin_referer( 'bulk-logs' );
foreach ( $_GET['logfiles'] as $logfile ) {
$logfile = basename( $logfile );
if ( is_writeable( self::$listtable->log_folder . '/' . $logfile ) && ! is_dir( self::$listtable->log_folder . '/' . $logfile ) && ! is_link( self::$listtable->log_folder . '/' . $logfile ) ) {
unlink( self::$listtable->log_folder . '/' . $logfile );
}
}
}
break;
// Download Log
case 'download':
$log_file = trailingslashit( self::$listtable->log_folder ) . basename( trim( $_GET['file'] ) );
$log_file = realpath( BackWPup_Sanitize_Path::sanitize_path( $log_file ) );
if ( ! $log_file
|| ! is_readable( $log_file )
|| is_dir( $log_file )
|| is_link( $log_file )
) {
header( 'HTTP/1.0 404 Not Found' );
die();
}
$capability = 'backwpup_logs';
$download_handler = new BackWpup_Download_Handler(
new BackWPup_Download_File(
$log_file,
function ( \BackWPup_Download_File_Interface $obj ) {
$obj->clean_ob()
->headers();
// phpcs:ignore
echo backwpup_wpfilesystem()->get_contents( $obj->filepath() );
die();
},
$capability
),
'download_backwpup_logs',
$capability,
'download'
);
$download_handler->handle();
break;
}
//Save per page
if ( isset( $_POST['screen-options-apply'] )
&& isset( $_POST['wp_screen_options']['option'] )
&& isset( $_POST['wp_screen_options']['value'] )
&& $_POST['wp_screen_options']['option'] == 'backwpuplogs_per_page'
) {
check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
global $current_user;
if ( $_POST['wp_screen_options']['value'] > 0 && $_POST['wp_screen_options']['value'] < 1000 ) {
update_user_option( $current_user->ID, 'backwpuplogs_per_page', (int) $_POST['wp_screen_options']['value'] );
wp_redirect( remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() ) );
exit;
}
}
add_screen_option( 'per_page', array(
'label' => __( 'Logs', 'backwpup' ),
'default' => 20,
'option' => 'backwpuplogs_per_page',
) );
self::$listtable->prepare_items();
}
/**
* Output css
*
* @return void
*/
public static function admin_print_styles() {
?>