<?php
/**
 * Log
 *
 * Add logs for plugin actions
 */

namespace WPrequal;

if ( ! defined( 'ABSPATH' ) ) {
	wp_die( 'Access denied!!' );
}

class Log {

	/**
	 * Write the log to the file.
	 *
	 * @param $key
	 * @param $data
	 */
	public static function write( $key, $data ) {

		if ( Settings::is_checked( 'wprequal_allow_logging' ) ) {

			$msg        = self::message( $data );
			$error_file = WPREQUAL_ERROR_LOG_DIR . "{$key}.log";

			// Create the WPrequal log file dir if we do not already have one.
			if ( ! file_exists( WPREQUAL_ERROR_LOG_DIR ) ) {
				wp_mkdir_p( WPREQUAL_ERROR_LOG_DIR );
			}

			if ( ! file_exists( $error_file ) ) {
				file_put_contents( $error_file, '' );
			}

			error_log( $msg, 3, $error_file );

		}

	}

	/**
	 * Message for  the log.
	 *
	 * @param $data
	 *
	 * @return string
	 */
	public static function message( $data ) {

		ob_start();

		$date = gmdate( "F j, Y, g:i a" );

		printf( "[%s] - ", esc_html( $date ) );

		if ( is_array( $data ) || is_object( $data ) ) {
			print_r( $data );
		} else {
			echo esc_html( $data );
		}

		echo "\n";
		echo '__________________________________________________________________________';
		echo "\n";

		return ob_get_clean();

	}

}