log_file_handle=false; } public function fopen($filename,$mode) { return @fopen($filename,$mode); } public function fwrite($handle,$text) { if($handle) { return @fwrite($handle,$text ); } else { return false; } } public function CreateLogFile($file_name='') { if(empty($file_name)) { $offset=get_option('gmt_offset'); $localtime = time() + $offset * 60 * 60; $file_name='compressx_'.gmdate('Ymd',$localtime).'_log.txt'; } $this->log_file=$this->GetSaveLogFolder().$file_name; $this->log_file_handle = $this->fopen($this->log_file, 'a'); if($this->log_file_handle===false) { return false; } $text="====================================================\n"; $time =gmdate("Y-m-d H:i:s",time()); $text.='open log file: '.$time."\n"; $this->fwrite($this->log_file_handle,$text); return $this->log_file; } public function OpenLogFile($file_name='') { if(empty($file_name)) { $offset=get_option('gmt_offset'); $localtime = time() + $offset * 60 * 60; $file_name='compressx_'.gmdate('Ymd',$localtime).'_log.txt'; } $this->log_file=$this->GetSaveLogFolder().$file_name; $this->log_file_handle = $this->fopen($this->log_file, 'a'); return $this->log_file; } public function WriteLog($log,$type) { if ($this->log_file_handle) { $time =gmdate("Y-m-d H:i:s",time()); $text='['.$time.']'.'['.$type.']'.$log."\n"; $this->fwrite($this->log_file_handle,$text ); } } public function GetlastLog() { if(empty($file_name)) { $offset=get_option('gmt_offset'); $localtime = time() + $offset * 60 * 60; $file_name='compressx_'.gmdate('Ymd',$localtime).'_log.txt'; } $this->log_file=$this->GetSaveLogFolder().$file_name; $file = file($this->log_file); $text=''; for ($i = max(0, count($file)-1); $i < count($file); $i++) { $text.= $file[$i] . "\n"; } return $text; } public function GetSaveLogFolder() { $path=WP_CONTENT_DIR.'/compressx/'.'log'; if(!is_dir($path)) { wp_mkdir_p($path); $this->fopen($path.DIRECTORY_SEPARATOR.'index.html', 'x'); $tempfile=$this->fopen($path.DIRECTORY_SEPARATOR.'.htaccess', 'x'); if($tempfile) { $text="deny from all"; $this->fwrite($tempfile,$text ); } } return $path.'/'; } public function get_logs() { $dir=$this->GetSaveLogFolder(); $files = scandir($dir, SCANDIR_SORT_DESCENDING); $log_files=array(); $regex='#^compressx.*_log.txt#'; foreach ($files as $filename) { if(preg_match($regex,$filename)) { $log_files[] = $dir.DIRECTORY_SEPARATOR.$filename; } if(sizeof($log_files)>=5) { break; } } return $log_files; } } class CompressX_Log_Ex { private static $instance = null; public $log_file = ''; public $log_file_handle = false; private function __construct() {} public static function get_instance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } public function fopen($filename,$mode) { return @fopen($filename,$mode); } public function fwrite($handle,$text) { if($handle) { return @fwrite($handle,$text ); } else { return false; } } public function CreateLogFile($file_name = '') { if ($this->log_file_handle && !empty($this->log_file)) { return $this->log_file; } if (empty($file_name)) { $offset = get_option('gmt_offset'); $localtime = time() + $offset * 3600; $file_name = 'compressx_' . gmdate('Ymd', $localtime) . '_log.txt'; } $this->log_file = $this->GetSaveLogFolder() . $file_name; $this->log_file_handle = $this->fopen($this->log_file, 'a'); if ($this->log_file_handle === false) { return false; } $this->write_open_header(); return $this->log_file; } public function OpenLogFile($file_name = '') { if (empty($file_name)) { $offset = get_option('gmt_offset'); $localtime = time() + $offset * 3600; $file_name = 'compressx_' . gmdate('Ymd', $localtime) . '_log.txt'; } $this->log_file = $this->GetSaveLogFolder() . $file_name; $this->log_file_handle = $this->fopen($this->log_file, 'a'); return $this->log_file; } public function use_log_file($file_name) { $this->log_file = $this->GetSaveLogFolder() . $file_name; if (!$this->log_file_handle) { $this->log_file_handle = $this->fopen($this->log_file, 'a'); $this->write_open_header(); } return $this->log_file; } public function WriteLog($log, $type) { if (!$this->ensure_handle_open()) { return; } $time = gmdate("Y-m-d H:i:s", time()); $text = '[' . $time . '][' . $type . '] ' . $log . "\n"; $this->fwrite($this->log_file_handle, $text); } public function GetlastLog() { if (empty($this->log_file)) { return ''; } if (!file_exists($this->log_file)) { return ''; } $file = file($this->log_file); $text = $file[count($file) - 1] ?? ''; return $text; } public function get_log_content($offset) { $file = $this->log_file; if (!file_exists($file)) { return [ 'result' => 'success', 'offset' => 0, 'content' => '' ]; } $fp = $this->fopen($file, 'r'); if (!$fp) { return [ 'result' => 'success', 'offset' => 0, 'content' => '' ]; } if ($offset > 0) { fseek($fp, $offset); } $content = ''; while (!feof($fp)) { $content .= fgets($fp); } $new_offset = ftell($fp); fclose($fp); return [ 'result' => 'success', 'offset' => $new_offset, 'content' => $content ]; } public function GetSaveLogFolder() { $path = WP_CONTENT_DIR . '/compressx/log'; if (!is_dir($path)) { wp_mkdir_p($path); @file_put_contents($path . '/index.html', ''); @file_put_contents($path . '/.htaccess', "deny from all"); } return $path . '/'; } private function ensure_handle_open() { if ($this->log_file_handle) { return true; } if (!empty($this->log_file)) { $this->log_file_handle = $this->fopen($this->log_file, 'a'); return (bool)$this->log_file_handle; } $this->OpenLogFile(); return (bool)$this->log_file_handle; } private function write_open_header() { $text = "====================================================\n"; $time = gmdate("Y-m-d H:i:s", time()); $text .= "open log file: {$time}\n"; $this->fwrite($this->log_file_handle, $text); } public function get_logs() { $dir = $this->GetSaveLogFolder(); $files = scandir($dir, SCANDIR_SORT_DESCENDING); $log_files = []; $regex = '#^compressx.*_log.txt#'; foreach ($files as $filename) { if (preg_match($regex, $filename)) { $log_files[] = $dir . '/' . $filename; } if (count($log_files) >= 5) { break; } } return $log_files; } } if ( ! class_exists( 'WP_List_Table' ) ) { require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); } class CompressX_Log_List extends WP_List_Table { public $page_num; public $log_list; public function __construct( $args = array() ) { parent::__construct( array( 'plural' => 'log', 'screen' => 'log' ) ); } public function get_columns() { $columns = array(); $columns['cx_date'] = __('Data and Time','compressx'); $columns['cx_log_file_name'] =__( 'Log File Name', 'compressx' ); $columns['cx_size'] = __( 'Log Size', 'compressx' ); $columns['cx_log_actions'] = __( 'Actions', 'compressx' ); return $columns; } public function set_log_list($log_list, $page_num=1) { $this->log_list=$log_list; $this->page_num=$page_num; } public function get_pagenum() { if($this->page_num=='first') { $this->page_num=1; } else if($this->page_num=='last') { $this->page_num=$this->_pagination_args['total_pages']; } $pagenum = $this->page_num ? $this->page_num : 0; if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) { $pagenum = $this->_pagination_args['total_pages']; } return max( 1, $pagenum ); } public function prepare_items() { $columns = $this->get_columns(); $hidden = array(); $sortable = array(); $this->_column_headers = array($columns, $hidden, $sortable); $total_items =sizeof($this->log_list); $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => 30, ) ); } public function has_items() { return !empty($this->log_list); } public function _column_cx_date( $log ) { echo ''.esc_html($log['date']).''; } public function _column_cx_log_file_name( $log ) { echo ''.esc_html($log['file_name']).''; } public function _column_cx_size( $log ) { echo ''.esc_html(size_format($log['size'],2)).''; } public function _column_cx_log_actions( $log ) { ?> | | _display_rows( $this->log_list ); } private function _display_rows($log_list) { $page=$this->get_pagenum(); $page_log_list=array(); $count=0; while ( $count<$page ) { $page_log_list = array_splice( $log_list, 0, 30); $count++; } foreach ( $page_log_list as $log) { $this->single_row($log); } } public function single_row($log) { ?> single_row_columns( $log ); ?> _args['plural'] ); $css_type = 'padding:0 0 1em 0;'; } else if( 'bottom' === $which ) { $css_type = 'display: none;'; } $total_pages = $this->_pagination_args['total_pages']; if ( $total_pages >1) { ?>
to
extra_tablenav( $which ); $this->pagination( $which ); ?>
display_tablenav( 'top' ); $this->screen->render_screen_reader_content( 'heading_list' ); $total_pages = $this->_pagination_args['total_pages']; if ( $total_pages <= 1) { ?>
to
print_column_headers(); ?> display_rows_or_placeholder(); ?> print_column_headers(); ?>
display_tablenav( 'bottom' ); } public function print_column_headers( $with_id = true ) { list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); if(isset($_SERVER['HTTP_HOST'])) $HTTP_HOST=sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])); else $HTTP_HOST=""; if(isset($_SERVER['REQUEST_URI'])) $REQUEST_URI=sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])); else $REQUEST_URI=""; $current_url = set_url_scheme( 'http://' .$HTTP_HOST . $REQUEST_URI ); $current_url = remove_query_arg( 'paged', $current_url ); // When users click on a column header to sort by other columns. if ( isset( $_GET['orderby'] ) ) { $current_orderby = $_GET['orderby']; // In the initial view there's no orderby parameter. } else { $current_orderby = ''; } // Not in the initial view and descending order. if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) { $current_order = 'desc'; } else { // The initial view is not always 'asc', we'll take care of this below. $current_order = 'asc'; } if ( ! empty( $columns['cb'] ) ) { static $cb_counter = 1; $columns['cb'] = ' '; ++$cb_counter; } foreach ( $columns as $column_key => $column_display_name ) { $class = array( 'manage-column', "column-$column_key" ); $aria_sort_attr = ''; $abbr_attr = ''; $order_text = ''; if ( in_array( $column_key, $hidden, true ) ) { $class[] = 'hidden'; } if ( 'cb' === $column_key ) { $class[] = 'check-column'; } elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ), true ) ) { $class[] = 'num'; } if ( isset( $sortable[ $column_key ] ) ) { $orderby = isset( $sortable[ $column_key ][0] ) ? $sortable[ $column_key ][0] : ''; $desc_first = isset( $sortable[ $column_key ][1] ) ? $sortable[ $column_key ][1] : false; $abbr = isset( $sortable[ $column_key ][2] ) ? $sortable[ $column_key ][2] : ''; $orderby_text = isset( $sortable[ $column_key ][3] ) ? $sortable[ $column_key ][3] : ''; $initial_order = isset( $sortable[ $column_key ][4] ) ? $sortable[ $column_key ][4] : ''; /* * We're in the initial view and there's no $_GET['orderby'] then check if the * initial sorting information is set in the sortable columns and use that. */ if ( '' === $current_orderby && $initial_order ) { // Use the initially sorted column $orderby as current orderby. $current_orderby = $orderby; // Use the initially sorted column asc/desc order as initial order. $current_order = $initial_order; } /* * True in the initial view when an initial orderby is set via get_sortable_columns() * and true in the sorted views when the actual $_GET['orderby'] is equal to $orderby. */ if ( $current_orderby === $orderby ) { // The sorted column. The `aria-sort` attribute must be set only on the sorted column. if ( 'asc' === $current_order ) { $order = 'desc'; $aria_sort_attr = ' aria-sort="ascending"'; } else { $order = 'asc'; $aria_sort_attr = ' aria-sort="descending"'; } $class[] = 'sorted'; $class[] = $current_order; } else { // The other sortable columns. $order = strtolower( $desc_first ); if ( ! in_array( $order, array( 'desc', 'asc' ), true ) ) { $order = $desc_first ? 'desc' : 'asc'; } $class[] = 'sortable'; $class[] = 'desc' === $order ? 'asc' : 'desc'; $asc_text = 'Sort ascending.' ; $desc_text = 'Sort descending.'; $order_text = 'asc' === $order ? $asc_text : $desc_text; } if ( '' !== $order_text ) { $order_text = ' ' . $order_text . ''; } // Print an 'abbr' attribute if a value is provided via get_sortable_columns(). $abbr_attr = $abbr ? ' abbr="' . esc_attr( $abbr ) . '"' : ''; $column_display_name = sprintf( '' . '%2$s' . '' . '' . '' . '' . '%3$s' . '', esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ), $column_display_name, $order_text ); } $tag = ( 'cb' === $column_key ) ? 'td' : 'th'; $scope = ( 'th' === $tag ) ? 'scope="col"' : ''; $id = $with_id ? "id='$column_key'" : ''; if ( ! empty( $class ) ) { $class = "class='" . implode( ' ', $class ) . "'"; } echo wp_kses_post("<$tag $scope $id $class $aria_sort_attr $abbr_attr>$column_display_name"); } } protected function get_table_classes() { $mode = get_user_setting( 'posts_list_mode', 'list' ); $mode_class = esc_attr( 'table-view-' . $mode ); return array( 'cx-table','widefat', 'striped', $this->_args['plural'] ); } } class CompressX_Log_List_V2 extends WP_List_Table { public $page_num; public $log_list; public function __construct( $args = array() ) { parent::__construct( array( 'plural' => 'log', 'screen' => 'log' ) ); } public function get_columns() { $columns = array(); $columns['cx_date'] = __('Date & Time','compressx'); $columns['cx_log_file_name'] =__( 'Log File Name', 'compressx' ); $columns['cx_size'] = __( 'Size', 'compressx' ); $columns['cx_log_actions'] = __( 'Actions', 'compressx' ); return $columns; } public function set_log_list($log_list, $page_num=1) { $this->log_list=$log_list; $this->page_num=$page_num; } public function get_pagenum() { if($this->page_num=='first') { $this->page_num=1; } else if($this->page_num=='last') { $this->page_num=$this->_pagination_args['total_pages']; } $pagenum = $this->page_num ? $this->page_num : 0; if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) { $pagenum = $this->_pagination_args['total_pages']; } return max( 1, $pagenum ); } public function prepare_items() { $columns = $this->get_columns(); $hidden = array(); $sortable = array(); $this->_column_headers = array($columns, $hidden, $sortable); $total_items =sizeof($this->log_list); $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => 30, ) ); } public function has_items() { return !empty($this->log_list); } public function _column_cx_date( $log ) { echo ''.esc_html($log['date']).''; } public function _column_cx_log_file_name( $log ) { echo ''.esc_html($log['file_name']).''; } public function _column_cx_size( $log ) { echo ''.esc_html(size_format($log['size'],2)).''; } public function _column_cx_log_actions( $log ) { ?> _display_rows( $this->log_list ); } private function _display_rows($log_list) { $page=$this->get_pagenum(); $page_log_list=array(); $count=0; while ( $count<$page ) { $page_log_list = array_splice( $log_list, 0, 30); $count++; } foreach ( $page_log_list as $log) { $this->single_row($log); } } public function single_row($log) { ?> single_row_columns( $log ); ?> _args['plural'] ); $css_type = 'padding:0 0 1em 0;'; } else if( 'bottom' === $which ) { $css_type = 'display: none;'; } $total_pages = $this->_pagination_args['total_pages']; if ( $total_pages >1) { ?>
to
extra_tablenav( $which ); $this->pagination( $which ); ?>
_pagination_args ) ) { return; } $total_items = isset( $this->_pagination_args['total_items'] ) ? (int) $this->_pagination_args['total_items'] : 0; $total_pages = isset( $this->_pagination_args['total_pages'] ) ? (int) $this->_pagination_args['total_pages'] : 0; $infinite_scroll = false; if ( isset( $this->_pagination_args['infinite_scroll'] ) ) { $infinite_scroll = $this->_pagination_args['infinite_scroll']; } if ( 'top' === $which && $total_pages > 1 ) { $this->screen->render_screen_reader_content( 'heading_pagination' ); } $output = '' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . ''; $current = (int) $this->get_pagenum(); if ( $current < 1 ) { $current = 1; } if ( $total_pages > 0 && $current > $total_pages ) { $current = $total_pages; } $page_links = array(); $total_pages_before = ''; $total_pages_after = ''; $disable_first = false; $disable_last = false; $disable_prev = false; $disable_next = false; if ( 1 === $current ) { $disable_first = true; $disable_prev = true; } if ( $total_pages === $current ) { $disable_last = true; $disable_next = true; } /* * First page. * Existing JS does not read value from .first-page. * It directly sends page = 'first'. */ if ( $disable_first ) { $page_links[] = ''; } else { $page_links[] = sprintf( "%s", esc_html__( 'First page' ), '«' ); } /* * Previous page. * Existing JS reads value and then uses page - 1. * Therefore value must be the current page, not the target page. */ if ( $disable_prev ) { $page_links[] = ''; } else { $page_links[] = sprintf( "%s", esc_attr( $current ), esc_html__( 'Previous page' ), '‹' ); } if ( 'bottom' === $which ) { $html_current_page = $current; $total_pages_before = '' . esc_html__( 'Current Page' ) . ''; } else { $html_current_page = sprintf( "%s", '', esc_attr( $current ), strlen( (string) $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' ), $html_current_page, $html_total_pages ) . $total_pages_after; /* * Next page. * Existing JS reads value and then uses page + 1. * Therefore value must be the current page, not the target page. */ if ( $disable_next ) { $page_links[] = ''; } else { $page_links[] = sprintf( "%s", esc_attr( $current ), esc_html__( 'Next page' ), '›' ); } /* * Last page. * Existing JS does not read value from .last-page. * It directly sends page = 'last'. */ if ( $disable_last ) { $page_links[] = ''; } else { $page_links[] = sprintf( "%s", esc_attr( $total_pages ), esc_html__( 'Last page' ), '»' ); } $pagination_links_class = 'pagination-links'; if ( ! empty( $infinite_scroll ) ) { $pagination_links_class .= ' hide-if-js'; } $output .= "\n" . implode( "\n", $page_links ) . ''; if ( $total_pages ) { $page_class = $total_pages < 2 ? ' one-page' : ''; } else { $page_class = ' no-pages'; } $this->_pagination = "
" . $output . '
'; echo $this->_pagination; } public function display() { $this->display_tablenav( 'top' ); $this->screen->render_screen_reader_content( 'heading_list' ); $total_pages = $this->_pagination_args['total_pages']; if ( $total_pages <= 1) { ?>
to
print_column_headers(); ?> display_rows_or_placeholder(); ?> print_column_headers(); ?>
display_tablenav( 'bottom' ); } public function print_column_headers( $with_id = true ) { list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); $current_url = remove_query_arg( 'paged', $current_url ); // When users click on a column header to sort by other columns. if ( isset( $_GET['orderby'] ) ) { $current_orderby = $_GET['orderby']; // In the initial view there's no orderby parameter. } else { $current_orderby = ''; } // Not in the initial view and descending order. if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) { $current_order = 'desc'; } else { // The initial view is not always 'asc', we'll take care of this below. $current_order = 'asc'; } if ( ! empty( $columns['cb'] ) ) { static $cb_counter = 1; $columns['cb'] = ' '; ++$cb_counter; } foreach ( $columns as $column_key => $column_display_name ) { $class = array( 'manage-column', "column-$column_key" ); $aria_sort_attr = ''; $abbr_attr = ''; $order_text = ''; if ( in_array( $column_key, $hidden, true ) ) { $class[] = 'hidden'; } if ( 'cb' === $column_key ) { $class[] = 'check-column'; } elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ), true ) ) { $class[] = 'num'; } if ( isset( $sortable[ $column_key ] ) ) { $orderby = isset( $sortable[ $column_key ][0] ) ? $sortable[ $column_key ][0] : ''; $desc_first = isset( $sortable[ $column_key ][1] ) ? $sortable[ $column_key ][1] : false; $abbr = isset( $sortable[ $column_key ][2] ) ? $sortable[ $column_key ][2] : ''; $orderby_text = isset( $sortable[ $column_key ][3] ) ? $sortable[ $column_key ][3] : ''; $initial_order = isset( $sortable[ $column_key ][4] ) ? $sortable[ $column_key ][4] : ''; /* * We're in the initial view and there's no $_GET['orderby'] then check if the * initial sorting information is set in the sortable columns and use that. */ if ( '' === $current_orderby && $initial_order ) { // Use the initially sorted column $orderby as current orderby. $current_orderby = $orderby; // Use the initially sorted column asc/desc order as initial order. $current_order = $initial_order; } /* * True in the initial view when an initial orderby is set via get_sortable_columns() * and true in the sorted views when the actual $_GET['orderby'] is equal to $orderby. */ if ( $current_orderby === $orderby ) { // The sorted column. The `aria-sort` attribute must be set only on the sorted column. if ( 'asc' === $current_order ) { $order = 'desc'; $aria_sort_attr = ' aria-sort="ascending"'; } else { $order = 'asc'; $aria_sort_attr = ' aria-sort="descending"'; } $class[] = 'sorted'; $class[] = $current_order; } else { // The other sortable columns. $order = strtolower( $desc_first ); if ( ! in_array( $order, array( 'desc', 'asc' ), true ) ) { $order = $desc_first ? 'desc' : 'asc'; } $class[] = 'sortable'; $class[] = 'desc' === $order ? 'asc' : 'desc'; /* translators: Hidden accessibility text. */ $asc_text = 'Sort ascending.'; /* translators: Hidden accessibility text. */ $desc_text = 'Sort descending.'; $order_text = 'asc' === $order ? $asc_text : $desc_text; } if ( '' !== $order_text ) { $order_text = ' ' . $order_text . ''; } // Print an 'abbr' attribute if a value is provided via get_sortable_columns(). $abbr_attr = $abbr ? ' abbr="' . esc_attr( $abbr ) . '"' : ''; $column_display_name = sprintf( '' . '%2$s' . '' . '' . '' . '' . '%3$s' . '', esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ), $column_display_name, $order_text ); } $tag = ( 'cb' === $column_key ) ? 'td' : 'th'; $scope = ( 'th' === $tag ) ? 'scope="col"' : ''; $id = $with_id ? "id='$column_key'" : ''; $class[] = 'compressx-v2-px-4'; $class[] = 'compressx-v2-py-2'; if ( ! empty( $class ) ) { $class = "class='" . implode( ' ', $class ) . "'"; } echo wp_kses_post("<$tag $scope $id $class $aria_sort_attr $abbr_attr>$column_display_name"); } } protected function get_table_classes() { $mode = get_user_setting( 'posts_list_mode', 'list' ); $mode_class = esc_attr( 'table-view-' . $mode ); return array( 'cx-table','widefat', 'striped','compressx-v2-text-sm', $this->_args['plural'] ); } }