| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Log class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read and write to the ConvertKit log file. |
| 11 |
* |
| 12 |
* @since 1.9.6 |
| 13 |
*/ |
| 14 |
class ConvertKit_Log { |
| 15 |
|
| 16 |
/** |
| 17 |
* The path and filename of the log file. |
| 18 |
* |
| 19 |
* @since 1.9.6 |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $log_file; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. Defines the log file location. |
| 27 |
* |
| 28 |
* @since 1.9.6 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
|
| 32 |
// Define location of log file. |
| 33 |
$this->log_file = trailingslashit( CONVERTKIT_PLUGIN_PATH ) . 'log.txt'; |
| 34 |
|
| 35 |
// Initialize WP_Filesystem. |
| 36 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 37 |
WP_Filesystem(); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns the path and filename of the log file. |
| 43 |
* |
| 44 |
* @since 1.9.6 |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_filename() { |
| 49 |
|
| 50 |
return $this->log_file; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Whether the log file exists. |
| 56 |
* |
| 57 |
* @since 1.9.6 |
| 58 |
* |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
public function exists() { |
| 62 |
|
| 63 |
return file_exists( $this->get_filename() ); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Adds an entry to the log file. |
| 69 |
* |
| 70 |
* @since 1.9.6 |
| 71 |
* |
| 72 |
* @param string $entry Log Line Entry. |
| 73 |
*/ |
| 74 |
public function add( $entry ) { |
| 75 |
|
| 76 |
// Initialize WordPress file system. |
| 77 |
global $wp_filesystem; |
| 78 |
|
| 79 |
// Prefix the entry with a date and time. |
| 80 |
$entry = '(' . gmdate( 'Y-m-d H:i:s' ) . ') ' . $entry . "\n"; |
| 81 |
|
| 82 |
// Get any existing log file contents. |
| 83 |
$contents = $wp_filesystem->get_contents( $this->get_filename() ); |
| 84 |
|
| 85 |
// Append entry. |
| 86 |
$contents .= $entry; |
| 87 |
|
| 88 |
// Write contents. |
| 89 |
$wp_filesystem->put_contents( $this->get_filename(), $contents ); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Reads the given number of lines from the log file. |
| 95 |
* |
| 96 |
* @since 1.9.6 |
| 97 |
* |
| 98 |
* @param int $number_of_lines Number of Lines. |
| 99 |
* @return string Log file data |
| 100 |
*/ |
| 101 |
public function read( $number_of_lines = 500 ) { |
| 102 |
|
| 103 |
// Initialize WordPress file system. |
| 104 |
global $wp_filesystem; |
| 105 |
|
| 106 |
// Bail if the log file does not exist. |
| 107 |
if ( ! $this->exists() ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
// Open log file. |
| 112 |
$log = $wp_filesystem->get_contents_array( $this->get_filename() ); // phpcs:ignore |
| 113 |
|
| 114 |
// Bail if the log file is empty. |
| 115 |
if ( ! is_array( $log ) || ! count( $log ) ) { |
| 116 |
return ''; |
| 117 |
} |
| 118 |
|
| 119 |
// Return a limited number of log lines for output. |
| 120 |
return implode( '', array_slice( $log, 0, $number_of_lines ) ); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Clears the log file without deleting the log file. |
| 126 |
* |
| 127 |
* @since 1.9.6 |
| 128 |
*/ |
| 129 |
public function clear() { |
| 130 |
|
| 131 |
// Initialize WordPress file system. |
| 132 |
global $wp_filesystem; |
| 133 |
|
| 134 |
$wp_filesystem->put_contents( $this->get_filename(), '' ); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Deletes the log file. |
| 140 |
* |
| 141 |
* @since 1.9.6 |
| 142 |
*/ |
| 143 |
public function delete() { |
| 144 |
|
| 145 |
unlink( $this->get_filename() ); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|