|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\Logger; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\Logger\Log; |
|
6
|
|
|
|
|
7
|
|
|
final class Application |
|
8
|
|
|
{ |
|
9
|
|
|
const ID = 'logger'; |
|
10
|
|
|
|
|
11
|
|
|
public $file; |
|
12
|
|
|
public $languages; |
|
13
|
|
|
public $log; |
|
14
|
|
|
public $name; |
|
15
|
|
|
public $version; |
|
16
|
|
|
|
|
17
|
|
|
protected static $instance; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return static |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function load() |
|
23
|
|
|
{ |
|
24
|
|
|
if( empty( static::$instance )) { |
|
25
|
|
|
static::$instance = new static; |
|
26
|
|
|
} |
|
27
|
|
|
return static::$instance; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->file = trailingslashit( dirname( __DIR__ )).static::ID.'.php'; |
|
36
|
|
|
$this->log = new Log( $this->path( 'development.log' )); |
|
37
|
|
|
$plugin = get_file_data( $this->file, [ |
|
38
|
|
|
'languages' => 'Domain Path', |
|
39
|
|
|
'name' => 'Plugin Name', |
|
40
|
|
|
'version' => 'Version', |
|
41
|
|
|
], 'plugin' ); |
|
42
|
|
|
array_walk( $plugin, function( $value, $key ) { |
|
43
|
|
|
$this->$key = $value; |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function init() |
|
51
|
|
|
{ |
|
52
|
|
|
add_action( 'admin_init', [$this, 'clearLog'] ); |
|
53
|
|
|
add_action( 'plugins_loaded', [$this, 'registerLanguages'] ); |
|
54
|
|
|
add_action( 'admin_menu', [$this, 'registerMenu'] ); |
|
55
|
|
|
add_action( 'admin_menu', [$this, 'registerSettings'] ); |
|
56
|
|
|
add_filter( 'all', [$this, 'initLogger'] ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Usage: apply_filters( 'logger', $data, $optional_log_type ); |
|
61
|
|
|
* @return static |
|
62
|
|
|
*/ |
|
63
|
|
|
public function initLogger() |
|
64
|
|
|
{ |
|
65
|
|
|
$args = func_get_args() + ['', null, 'debug']; |
|
66
|
|
|
if( $args[0] !== static::ID || is_null( $args[1] ))return; |
|
67
|
|
|
return $this->log->log( $args[2], $args[1] ); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public function clearLog() |
|
74
|
|
|
{ |
|
75
|
|
|
$request = filter_input( INPUT_POST, static::ID, FILTER_DEFAULT , FILTER_REQUIRE_ARRAY ); |
|
76
|
|
|
if( empty( $request['action'] ) || $request['action'] != 'clear-log' )return; |
|
77
|
|
|
check_admin_referer( $request['action'] ); |
|
78
|
|
|
$this->log->clear(); |
|
79
|
|
|
add_action( 'admin_notices', [$this, 'renderNotice'] ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $file |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function path( $file = '' ) |
|
87
|
|
|
{ |
|
88
|
|
|
$basepath = trailingslashit( realpath( plugin_dir_path( $this->file ))); |
|
89
|
|
|
return $basepath.ltrim( trim( $file ), '/' ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function registerLanguages() |
|
96
|
|
|
{ |
|
97
|
|
|
load_plugin_textdomain( static::ID, '', |
|
98
|
|
|
trailingslashit( plugin_basename( $this->path() ).'/'.$this->languages ) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return void |
|
104
|
|
|
* @action admin_menu |
|
105
|
|
|
*/ |
|
106
|
|
|
public function registerMenu() |
|
107
|
|
|
{ |
|
108
|
|
|
add_submenu_page( |
|
109
|
|
|
'tools.php', |
|
110
|
|
|
__( 'Logger', 'logger' ), |
|
111
|
|
|
__( 'Logger', 'logger' ), |
|
112
|
|
|
'manage_options', |
|
113
|
|
|
static::ID, |
|
114
|
|
|
[$this, 'renderSettingsPage'] |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return void |
|
120
|
|
|
* @action admin_menu |
|
121
|
|
|
*/ |
|
122
|
|
|
public function registerSettings() |
|
123
|
|
|
{ |
|
124
|
|
|
register_setting( static::ID, static::ID ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $view |
|
129
|
|
|
* @return void|null |
|
130
|
|
|
*/ |
|
131
|
|
|
public function render( $view, array $data = [] ) |
|
132
|
|
|
{ |
|
133
|
|
|
if( !file_exists( $file = $this->path( 'views/'.$view.'.php' )))return; |
|
134
|
|
|
extract( $data ); |
|
135
|
|
|
include $file; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function renderNotice() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->render( 'notice', [ |
|
144
|
|
|
'notice' => __( 'The log was cleared.', 'logger' ), |
|
145
|
|
|
'status' => 'success', |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return void |
|
151
|
|
|
* @callback add_submenu_page |
|
152
|
|
|
*/ |
|
153
|
|
|
public function renderSettingsPage() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->render( 'settings', [ |
|
156
|
|
|
'id' => static::ID, |
|
157
|
|
|
'log' => $this->log, |
|
158
|
|
|
'settings' => $this->getSettings(), |
|
159
|
|
|
'title' => __( 'Logger', 'logger' ), |
|
160
|
|
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return object |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getSettings() |
|
167
|
|
|
{ |
|
168
|
|
|
$settings = get_option( static::ID, [] ); |
|
169
|
|
|
if( empty( $settings )) { |
|
170
|
|
|
update_option( static::ID, $settings = [ |
|
171
|
|
|
'enabled' => 0, |
|
172
|
|
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
return (object)$settings; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|