upload_path = $upload_path ?: wp_upload_dir()['basedir'] . '/ad-fuz/log/'; } /** * Upload Directory Path. * * @return string */ private function upload_dir_path(): string { return $this->upload_path; } private function __clone() { } /** * Get Instance. * * @return self */ public static function get_instance( $upload_path = null ) { if ( ! self::$instance ) { self::$instance = new self( $upload_path ); } return self::$instance; } /** * Initialize * * @param string $channel Channel. */ public function initialize( string $channel = 'default' ): File { $AdFuz = AdFuz(); $this->channel = $channel; $this->path(); $this->cleaning(); /** * TODO: remove default value of true in Production. */ $this->is_logging_enabled = $AdFuz->core['debug']['type']; return $this; } /** * File Path. */ public function path() { $upload_base_dir = $this->upload_dir_path(); if ( ! is_dir( $upload_base_dir ) ) { wp_mkdir_p( $upload_base_dir ); } $date = gmdate( 'Ymd' ); $full_path = $upload_base_dir . $this->channel . '__' . $date . '.log'; if ( ! file_exists( $full_path ) ) { include_once ABSPATH . 'wp-admin/includes/file.php'; \WP_Filesystem(); global $wp_filesystem; $wp_filesystem->put_contents( $full_path, '', FS_CHMOD_FILE ); } $this->path = $full_path; } /** * Cleaning Log Files. */ public function cleaning() { if ( $this->is_ready_to_clean() ) { $dir_path = $this->upload_dir_path(); $files = array_diff( scandir( $dir_path ), array( '.', '..' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_scandir foreach ( $files as $file ) { $split = preg_split( '/(__|\.)/', $file ); if ( count( $split ) === 3 ) { $current_date = gmdate( 'Ymd', strtotime( '- ' . self::CLEANING_DAYS . ' days' ) ); if ( (int) $split[1] < (int) $current_date ) { wp_delete_file( $dir_path . $file ); } } else { wp_delete_file( $dir_path . $file ); } } } } /** * Check if the cleaning days are greater to clean the files. * * @return bool */ private function is_ready_to_clean(): bool { $max_date = get_transient( 'adfuz_delete_log' ); if ( false === $max_date ) { $max_time = self::CLEANING_DAYS * 24 * 60 * 60; set_transient( 'adfuz_delete_log', true, $max_time ); return true; } return false; } /** * Set Time Zone * * @param string $time_zone Time Zone. */ public function set_time_zone( string $time_zone ) { if ( in_array( $time_zone, timezone_identifiers_list() ) ) { $this->time_zone = $time_zone; } } /** * Write Log. * * @param mixed $message message. * @param string $level Level. */ public function write_log( $message, string $level = self::DEBUG ) { if ( 'test' === $this->is_logging_enabled ) { $this->write_line( $message, $level ); } } /** * Write Line. * * @param mixed $message message. * @param string $level Level. */ protected function write_line( $message, string $level ) { switch ( $level ) { case self::INFO: $this->write_in_to_file( $message, self::INFO ); break; case self::WARNING: $this->write_in_to_file( $message, self::WARNING ); break; case self::ERROR: $this->write_in_to_file( $message, self::ERROR ); break; default: $this->write_in_to_file( $message ); break; } } /** * Write into file. * * @param mixed $message message. * @param string $level Level. */ protected function write_in_to_file( $message, string $level = self::DEBUG ) { $write = sprintf( " \n \n ================== %s (%s) START================== \n \n", $this->get_time(), $level ); // phpcs:ignore $write .= print_r( $message, 1 ); $write .= sprintf( " \n \n ================== %s (%s) END================== \n \n \n", $this->get_time(), $level ); // phpcs:ignore // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents file_put_contents( $this->path, $write, FILE_APPEND | LOCK_EX ); } /** * Get Time * * @return string */ public function get_time(): string { $date = new DateTime(); $date->setTimezone( new \DateTimeZone( $this->time_zone ) ); return $date->format( 'd/m/Y H:i:s' ); } }