| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Helper; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
|
| 8 |
class LogHelper { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public static function writeLog( $message, $type_log = 'error', $name = 'log' ) { |
| 13 |
|
| 14 |
global $wp_filesystem; |
| 15 |
|
| 16 |
if ( empty( $wp_filesystem ) ) { |
| 17 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 18 |
WP_Filesystem(); |
| 19 |
} |
| 20 |
|
| 21 |
if ( ! is_string( $message ) ) { |
| 22 |
$message = print_r( $message, true ); |
| 23 |
} |
| 24 |
|
| 25 |
$folder = YAYMAIL_PLUGIN_PATH . '/includes/Logs'; |
| 26 |
if ( ! file_exists( $folder ) ) { |
| 27 |
@mkdir( $folder, 0755 ); |
| 28 |
$wp_filesystem->chmod( $folder, 0755 ); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
$filename = $folder . DIRECTORY_SEPARATOR . $name . '.txt'; |
| 33 |
|
| 34 |
clearstatcache(); // Remove filesize cache |
| 35 |
|
| 36 |
$handle = $wp_filesystem->get_contents( $filename, 'a' ); |
| 37 |
if ( filesize( $filename ) == 0 ) { |
| 38 |
$wp_filesystem->put_contents( $handle, self::getSystemStats() ); |
| 39 |
} |
| 40 |
|
| 41 |
$wp_filesystem->put_contents( $handle, current_time( 'mysql' ) . ' [' . strtoupper( $type_log ) . '] ' . $message . PHP_EOL ); |
| 42 |
fclose( $handle ); |
| 43 |
} |
| 44 |
private static function getSystemStats() { |
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 48 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 49 |
} |
| 50 |
$server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( $_SERVER['SERVER_SOFTWARE'] ) : ''; |
| 51 |
$system_stats = '==== SYSTEM STATS ====' . PHP_EOL; |
| 52 |
$system_stats .= 'WordPress Version: ' . get_bloginfo( 'version' ) . PHP_EOL; |
| 53 |
$system_stats .= 'PHP Version: ' . phpversion() . PHP_EOL; |
| 54 |
$system_stats .= 'MySQL Version: ' . $wpdb->db_version() . PHP_EOL; |
| 55 |
$system_stats .= 'Website Name: ' . get_bloginfo() . PHP_EOL; |
| 56 |
$system_stats .= 'Theme: ' . wp_get_theme() . PHP_EOL; |
| 57 |
$system_stats .= 'WordPress URL: ' . site_url() . PHP_EOL; |
| 58 |
$system_stats .= 'Site URL: ' . home_url() . PHP_EOL; |
| 59 |
$system_stats .= 'Multisite: ' . ( is_multisite() ? 'yes' : 'no' ) . PHP_EOL; |
| 60 |
$system_stats .= 'PHP Extensions: ' . json_encode( get_loaded_extensions() ) . PHP_EOL; |
| 61 |
$system_stats .= 'Server Info: ' . $server_software . PHP_EOL; |
| 62 |
$system_stats .= 'WP Memory Limit: ' . WP_MEMORY_LIMIT . PHP_EOL; |
| 63 |
$system_stats .= 'WP Admin Memory Limit: ' . WP_MAX_MEMORY_LIMIT . PHP_EOL; |
| 64 |
$system_stats .= 'PHP Memory Limit: ' . ini_get( 'memory_limit' ) . PHP_EOL; |
| 65 |
$system_stats .= 'Max Execution Time: ' . ini_get( 'max_execution_time' ) . PHP_EOL; |
| 66 |
$system_stats .= 'Open BaseDir: ' . ini_get( 'open_basedir' ) . PHP_EOL; |
| 67 |
$system_stats .= 'WordPress Plugins: ' . json_encode( get_plugins() ) . PHP_EOL; |
| 68 |
$system_stats .= 'WordPress Active Plugins: ' . json_encode( get_site_option( 'active_plugins' ) ) . PHP_EOL; |
| 69 |
$system_stats .= '==== SYSTEM STATS ====' . PHP_EOL . PHP_EOL; |
| 70 |
return $system_stats; |
| 71 |
} |
| 72 |
public static function getMessageException( $ex, $ajax = false ) { |
| 73 |
$message = 'SYSTEM ERROR: ' . $ex->getCode() . ' : ' . $ex->getMessage(); |
| 74 |
$message .= PHP_EOL . $ex->getFile() . '(' . $ex->getLine() . ')'; |
| 75 |
$message .= PHP_EOL . $ex->getTraceAsString(); |
| 76 |
self::writeLog( $message ); |
| 77 |
if ( $ajax ) { |
| 78 |
wp_send_json_error( array( 'mess' => $message ) ); |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
// writeLog use show content when save email, save |
| 84 |
public static function writeLogContent( $content = '', $tailName = 'html' ) { |
| 85 |
global $wp_filesystem; |
| 86 |
|
| 87 |
if ( empty( $wp_filesystem ) ) { |
| 88 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 89 |
WP_Filesystem(); |
| 90 |
} |
| 91 |
|
| 92 |
$name = 'log-' . current_time( 'timestamp' ); |
| 93 |
$folder = YAYMAIL_PLUGIN_PATH . '/includes/Logs'; |
| 94 |
$filename = $folder . DIRECTORY_SEPARATOR . $name . '.' . $tailName; |
| 95 |
$handle = $wp_filesystem->get_contents( $filename, 'a' ); |
| 96 |
$wp_filesystem->put_contents( $handle, print_r( $content, true ) ); |
| 97 |
fclose( $handle ); |
| 98 |
} |
| 99 |
} |
| 100 |
|