*/
declare(strict_types=1);
namespace WSAL\Writers;
use WSAL\Helpers\Settings_Helper;
use WSAL\Extensions\Views\Reports;
use WSAL\Helpers\DateTime_Formatter_Helper;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( '\WSAL\Writers\HTML_Writer' ) ) {
/**
* Provides logging functionality for the comments.
*
* @since 5.0.0
*/
class HTML_Writer {
/**
* Holds the full file name for the HTML file
*
* @var string
*
* @since 5.0.0
*/
private static $file_name = '';
/**
* Holds the columns for the HTML file
*
* @var array
*
* @since 5.0.0
*/
private static $columns_header = array();
/**
* Writes the HTML file to the specified location.
*
* @param int $step - Current step.
* @param array $data - If local variable is not set, data array could be passed to that method.
* @param array $report_filters - Report criteria filters array.
*
* @return void
*
* @since 5.0.0
*/
public static function write_html( int $step, array &$data = array(), array $report_filters = array() ) {
if ( 1 === $step ) {
$output = fopen( self::get_file(), 'w' );
} else {
$output = fopen( self::get_file(), 'a+' );
}
if ( 1 === $step ) {
self::prepare_header( '', $output, $report_filters );
}
if ( ! empty( $data ) ) {
foreach ( $data as $i => $alert ) {
$date = ( isset( $alert['created_on'] ) ) ? $alert['created_on'] : '';
$r = '
';
$processed_row_data = array();
foreach ( self::$columns_header as $key => $label ) {
$value = array_key_exists( $key, $alert ) ? $alert[ $key ] : '';
if ( 'timestamp' === $key ) {
$value = $date;
}
$processed_row_data[ $key ] = $value;
}
foreach ( $processed_row_data as $key => $label ) {
$cell_styling = 'padding: 16px 7px;';
if ( 'alert_id' === $key ) {
$cell_styling .= ' text-align: center; font-weight: 700;';
} elseif ( 'severity' === $key ) {
$cell_styling = '" class="tooltip" title="' . $label;
$label = '';
} elseif ( 'user_displayname' === $key ) {
$cell_styling .= ' min-width: 100px;';
} elseif ( 'message' === $key ) {
$cell_styling .= ' min-width: 400px; word-break: break-all; line-height: 1.5;';
}
$r .= '| ' . $label . ' | ';
}
$r .= '
';
fwrite( $output, $r );
}
}
fclose( $output );
}
/**
* Sets the writer file name.
*
* @param string $file - The name of the writer file to be used.
*
* @return void
*
* @since 5.0.0
*/
public static function set_file( string $file ) {
self::$file_name = $file;
}
/**
* Returns the writer file name.
*
* @return string
*
* @since 5.0.0
*/
public static function get_file(): string {
return (string) self::$file_name;
}
/**
* Sets the header_columns property to be used in HTML generation
*
* @param array $columns - The column names.
*
* @return void
*
* @since 5.0.0
*/
public static function set_header_columns( array $columns ) {
self::$columns_header = $columns;
}
/**
* Set the file footer - closes the HTML output.
*
* @return void
*
* @since 5.0.0
*/
public static function set_footer() {
$output = fopen( self::get_file(), 'a+' );
fwrite( $output, ' WP Activity Log is supported and developed by Melapress.