> ', $log_file_destination = null, $clear_log = false, $no_print_r = false ) { if ( ! self::isLoggerEnabled() ) { return; } if ( ! $log_file_destination ) { return; } $dirname = dirname( $log_file_destination ); // try to make directory if not exists if ( ! self::resolveDir( $dirname ) ) { return; } self::resolveDenyHtaccess( $dirname ); if ( $clear_log ) { file_put_contents( $log_file_destination, '' ); } if ( self::isLoggerTimeInMS() ) { $time_mark = '[' . self::getMicroTime() . '] '; } else { $time_mark = '[' . gmdate( 'Y-m-d H:i:s' ) . '] '; } if ( $no_print_r ) { if ( is_numeric( $var ) || is_string( $var ) || is_bool( $var ) ) { self::writeToFile( $time_mark . $desc . ' ' . $var . PHP_EOL, $log_file_destination ); } elseif ( is_object( $var ) ) { self::writeToFile( $time_mark . $desc . ' Object:' . get_class( $var ) . PHP_EOL, $log_file_destination ); } elseif ( is_array( $var ) ) { self::writeToFile( $time_mark . $desc . ' Array' . PHP_EOL, $log_file_destination ); } elseif ( is_resource( $var ) ) { self::writeToFile( $time_mark . $desc . ' Resource' . PHP_EOL, $log_file_destination ); } else { self::writeToFile( $time_mark . $desc . ' Something unprintable' . PHP_EOL, $log_file_destination ); } } else { self::writeToFile( $time_mark . $desc . ' ' . print_r( $var, true ) . PHP_EOL, $log_file_destination ); } } public static function log( $var, $desc = ' ', $name = 'default', $separate = false, $no_print_r = false ) { if ( ! self::isLoggerEnabled() ) { return; } $date = $separate ? gmdate( 'Y-m-d_H' ) : gmdate( 'Y-m-d' ); $dir = self::getDir(); $log_file_destination = trailingslashit( $dir ) . $name . '_' . $date . '.log'; self::_log( $var, $desc, $log_file_destination, false, $no_print_r ); } // ============================================================================== public static function setConfig( array $config ) { self::$config = $config; } public static function getConfig() { return self::$config; } public static function isLoggerEnabled() { $enabled_by_config = isset( self::$config['enabled'] ) && self::$config['enabled']; $enabled_by_constant = defined( 'ADSCALE_LOGGER_ENABLED' ) && ADSCALE_LOGGER_ENABLED; return $enabled_by_constant || $enabled_by_config; } public static function isLoggerTimeInMS() { return isset( self::$config['time_in_ms'] ) ? self::$config['time_in_ms'] : true; } public static function getDir() { return isset( self::$config['dir'] ) ? self::$config['dir'] : get_stylesheet_directory(); } public static function isNeedDenyHtaccess() { return isset( self::$config['need_deny_htaccess'] ) ? self::$config['need_deny_htaccess'] : false; } public static function getMicroTime() { $t = microtime( true ); $micro = sprintf( '%06d', ( $t - floor( $t ) ) * 1000000 ); $d = null; try { $d = new \DateTime( gmdate( 'Y-m-d H:i:s.' . $micro, $t ) ); } catch ( \Exception $e ) { return ''; } return $d ? $d->format( 'Y-m-d H:i:s.u' ) : ''; //note "u" is microseconds (1 seconds = 1000000 µs). } public static function resolveDir( $dirname ) { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } if ( $wp_filesystem->is_dir( $dirname ) ) { return true; } return (bool) $wp_filesystem->mkdir( $dirname, FS_CHMOD_DIR ); } public static function resolveDenyHtaccess( $dirname ) { if ( ! self::isNeedDenyHtaccess() ) { return null; } global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } if ( ! $wp_filesystem->is_dir( $dirname ) ) { return null; } // Define `.htaccess` path $htaccess_path = trailingslashit( $dirname ) . '.htaccess'; // Check if the `.htaccess` file already exists if ( $wp_filesystem->exists( $htaccess_path ) ) { return null; } // Define content without using HEREDOC $content = "order deny,allow" . PHP_EOL; $content .= "deny from all" . PHP_EOL; // Write content to file return (bool) $wp_filesystem->put_contents( $htaccess_path, $content, FS_CHMOD_FILE ); } }