PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / trunk
Starter Sites & Templates by Neve vtrunk
1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / includes / Logger.php
templates-patterns-collection / includes Last commit date
Importers 2 months ago Admin.php 1 week ago Editor.php 1 year ago Elementor.php 1 year ago License.php 1 year ago Logger.php 2 years ago Main.php 1 week ago Rest_Server.php 2 weeks ago Sites_Listing.php 2 weeks ago Starter_Ranking.php 1 week ago TI_Beaver.php 1 year ago WP_Cli.php 3 months ago White_Label_Config.php 3 years ago
Logger.php
249 lines
1 <?php
2 /**
3 * Onboarding Logger.
4 *
5 * @package templates-patterns-collection
6 */
7
8 namespace TIOB;
9
10 /**
11 * Class Logger
12 */
13 class Logger {
14
15 /**
16 * Emojis mapped for each case.
17 *
18 * @var array
19 */
20 private $icon_map = array(
21 'success' => 'S',
22 'warning' => 'W',
23 'progress' => 'P',
24 'error' => 'E',
25 'generic' => '-',
26 'info' => 'I',
27 );
28
29 /**
30 * Log file path.
31 *
32 * @var string
33 */
34 private $log_file_path;
35
36 /**
37 * Log file path url.
38 *
39 * @var string
40 */
41 private $log_file_path_url;
42
43 /**
44 * Log file name.
45 *
46 * @var string
47 */
48 private $log_file_name = 'ti-theme-onboarding.log';
49
50 /**
51 * Log transient name.
52 *
53 * @var string
54 */
55 public static $log_transient_name = 'ti_theme_onboarding';
56
57 /**
58 * The expiration time for the transient.
59 *
60 * @var float|int
61 */
62 private $log_transient_expiration = 2 * WEEK_IN_SECONDS;
63
64 /**
65 * @var string
66 */
67 private $log_string = '';
68
69 /**
70 * @var Logger
71 */
72 private static $_instance;
73
74 /**
75 * Logger constructor.
76 */
77 public function __construct() {
78 if ( ! defined( 'TI_OB_DEBUG_LOG' ) ) {
79 define( 'TI_OB_DEBUG_LOG', true );
80 }
81
82 if ( TI_OB_DEBUG_LOG !== true ) {
83 return;
84 }
85 require_once( ABSPATH . 'wp-admin/includes/file.php' ); // you have to load this file
86 add_action( 'shutdown', array( $this, 'log_to_transient' ) );
87 $this->set_log_path();
88 $this->migrate_from_file_to_transient();
89 $this->log_client_info();
90 }
91
92 /**
93 * Log info.
94 *
95 * @param string $label log label.
96 * @param string|int|null $value log value.
97 */
98 public function log_info( $label, $value = null ) {
99 if ( $value === null ) {
100 $this->log( "{$label}", 'info' );
101
102 return;
103 }
104 $this->log( "{$label} : {$value}", 'info' );
105 }
106
107 /**
108 * Log client info for debug purposes.
109 */
110 private function log_client_info() {
111 $this->log_info( "WordPress Instance Info:\n" );
112 $this->log_info( 'Home URL', home_url() );
113 $this->log_info( 'Site URL', site_url() );
114 $this->log_info( 'WordPress Version', get_bloginfo( 'version' ) );
115 $this->log_info( 'Onboarding Version', Main::VERSION );
116 $this->log_info( 'Multisite', is_multisite() ? 'Yes' : 'No' );
117 $this->log_info( 'Server Info', isset( $_SERVER['SERVER_SOFTWARE'] ) ? wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) : '' );
118 $this->log_info( 'PHP Version', phpversion() );
119 $this->log_info( 'HTTPS', is_ssl() ? 'Yes' : 'No' );
120 $this->log_info( 'PHP Max Execution Time', ini_get( 'max_execution_time' ) );
121 $this->log_info( 'PHP Max Input Vars', ini_get( 'max_input_vars' ) );
122 $this->log_info( 'Max Upload Size', wp_max_upload_size() );
123 $this->log_info( 'Plugins:' );
124 foreach ( $this->get_plugins() as $plugin ) {
125 $author = strip_tags( $plugin['Author'] );
126 $this->log_info( '[PLUGIN] ' . $plugin['Name'], 'v' . $plugin['Version'] . " ({$author}) " );
127 }
128 }
129
130 /**
131 * Get active plugins.
132 *
133 * @return array
134 */
135 private function get_plugins() {
136 require_once ABSPATH . 'wp-admin/includes/plugin.php';
137 $active_plugins = (array) get_option( 'active_plugins', array() );
138 if ( is_multisite() ) {
139 $network_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
140 $active_plugins = array_merge( $active_plugins, $network_activated_plugins );
141 }
142
143 $active_plugins_data = array();
144 foreach ( $active_plugins as $plugin ) {
145 $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
146 $active_plugins_data[] = $data;
147 }
148
149 return $active_plugins_data;
150 }
151
152 /**
153 * Returns the instance of the class.
154 *
155 * @return Logger
156 */
157 public static function get_instance() {
158 if ( null === self::$_instance ) {
159 self::$_instance = new self();
160 }
161
162 return self::$_instance;
163 }
164
165 /**
166 * Set the log path.
167 */
168 private function set_log_path() {
169 $wp_upload_dir = wp_upload_dir( null, false );
170 $this->log_file_path = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR;
171
172 $this->log_file_path_url = $wp_upload_dir['baseurl'] . DIRECTORY_SEPARATOR;
173 }
174
175 /**
176 * Log entry.
177 *
178 * @param string $message log message.
179 * @param string $type log type.
180 */
181 public function log( $message = 'No message provided.', $type = 'error' ) {
182 $log_entry = array(
183 'message' => $message,
184 'type' => array_key_exists( $type, $this->icon_map ) ? $this->icon_map[ $type ] : $this->icon_map['generic'],
185 'time' => gmdate( '[d/M/Y:H:i:s]' ),
186 );
187 $this->log_string .= $this->get_log_entry( $log_entry );
188 }
189
190 /**
191 * Log to file.
192 */
193 public function log_to_transient() {
194
195 $transient_data = get_transient( self::$log_transient_name );
196
197 if ( ! $transient_data ) {
198 $transient_data = '';
199 }
200
201 $transient_data .= $this->log_string;
202 set_transient( self::$log_transient_name, $transient_data, $this->log_transient_expiration );
203 }
204
205 /**
206 * Get the formatted log entry.
207 *
208 * @return string
209 */
210 private function get_log_entry( $log_entry ) {
211 if ( ! is_string( $log_entry['message'] ) ) {
212 $log_entry['message'] = json_encode( $log_entry['message'] );
213 }
214
215 return trim( preg_replace( '/\s\s+/', ' ', "{$log_entry['time']} ({$log_entry['type']}): {$log_entry['message']}" ) ) . PHP_EOL;
216 }
217
218 /**
219 * Get the log URL.
220 */
221 public function get_log_url() {
222 return $this->log_file_path_url . self::$log_transient_name;
223 }
224
225 /**
226 * Migrate from file to transient.
227 */
228 public function migrate_from_file_to_transient() {
229 $log_file = $this->log_file_path . $this->log_file_name;
230 global $wp_filesystem;
231 WP_Filesystem();
232
233 $has_file = file_exists( $log_file );
234
235 if ( ! $has_file ) {
236 return;
237 }
238
239 $content = $wp_filesystem->get_contents( $log_file );
240 $this->log_string .= $content;
241
242 $this->log_to_transient();
243
244 if ( is_writable( $log_file ) ) {
245 unlink( $log_file );
246 }
247 }
248 }
249