PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.7.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.7.2
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-log.php

class-convertkit-log.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 1.9.7.2, at includes/class-convertkit-log.php

150 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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