Repository navigation
Expand file tree
/
Copy pathclass-connector-editor.php
More file actions
348 lines (300 loc) · 8.32 KB
/
Copy pathclass-connector-editor.php
File metadata and controls
348 lines (300 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* Connector for Editor
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - Connector_Editor
*/
class Connector_Editor extends Connector {
/**
* Connector slug
*/
public string $name = 'editor';
/**
* Actions registered for this connector
*/
public array $actions = array();
/**
* Actions registered for this connector
*
* @var array
*/
private $edited_file = array();
/**
* Register connector in the WP Frontend
*/
public bool $register_frontend = false;
/**
* Register all context hooks
*
* @return void
*/
public function register() {
parent::register();
add_action( 'wp_ajax_edit-theme-plugin-file', array( $this, 'get_edition_data' ), 1 );
}
/**
* Return translated connector label
*
* @return string Translated connector label
*/
public function get_label() {
return esc_html__( 'Editor', 'stream' );
}
/**
* Return translated action labels
*
* @return array Action label translations
*/
public function get_action_labels() {
return array(
'updated' => esc_html__( 'Updated', 'stream' ),
);
}
/**
* Return translated context labels
*
* @return array Context label translations
*/
public function get_context_labels() {
/**
* Filter available context labels for the Editor connector
*
* @param array $context_labels Array of context slugs and their translated labels
*/
return apply_filters(
'wp_stream_editor_context_labels',
array(
'themes' => esc_html__( 'Themes', 'stream' ),
'plugins' => esc_html__( 'Plugins', 'stream' ),
)
);
}
/**
* Get the context based on wp_redirect location
*
* @param string $location The URL of the redirect.
*
* @return string Context slug
*/
public function get_context( $location ) {
$context = null;
if ( false !== strpos( $location, 'theme-editor.php' ) ) {
$context = 'themes';
}
if ( false !== strpos( $location, 'plugin-editor.php' ) ) {
$context = 'plugins';
}
/**
* Filter available contexts for the Editor connector
*
* @param string $context Context slug
* @param string $location The URL of the redirect
*/
return apply_filters( 'wp_stream_editor_context', $context, $location );
}
/**
* Get the message format for file updates
*
* @return string Translated string
*/
public function get_message() {
/* translators: %1$s: a file name, %2$s: a theme / plugin name (e.g. "index.php", "Stream") */
return _x(
'"%1$s" in "%2$s" updated',
'1: File name, 2: Theme/plugin name',
'stream'
);
}
/**
* Add action links to Stream drop row in admin list screen
*
* @filter wp_stream_action_links_{connector}
*
* @param array $links Previous links registered.
* @param object $record Stream record.
*
* @return array Action links
*/
public function action_links( $links, $record ) {
if ( current_user_can( 'edit_theme_options' ) ) {
$file_name = $record->get_meta( 'file', true );
$file_path = $record->get_meta( 'file_path', true );
if ( ! empty( $file_name ) && ! empty( $file_path ) ) {
$theme_slug = $record->get_meta( 'theme_slug', true );
$plugin_slug = $record->get_meta( 'plugin_slug', true );
$theme_exists = ( ! empty( $theme_slug ) && file_exists( $file_path ) );
$plugin_exists = ( ! empty( $plugin_slug ) && file_exists( $file_path ) );
if ( $theme_exists ) {
$links[ esc_html__( 'Edit File', 'stream' ) ] = add_query_arg(
array(
'theme' => rawurlencode( $theme_slug ),
'file' => rawurlencode( $file_name ),
),
self_admin_url( 'theme-editor.php' )
);
$links[ esc_html__( 'Theme Details', 'stream' ) ] = add_query_arg(
array(
'theme' => rawurlencode( $theme_slug ),
),
self_admin_url( 'themes.php' )
);
}
if ( $plugin_exists ) {
$links[ esc_html__( 'Edit File', 'stream' ) ] = add_query_arg(
array(
'plugin' => rawurlencode( $plugin_slug ),
'file' => rawurlencode( str_ireplace( trailingslashit( WP_PLUGIN_DIR ), '', $file_path ) ),
),
self_admin_url( 'plugin-editor.php' )
);
}
}
}
return $links;
}
/**
* Retrieves data submitted on the screen, prepares it for the appropriate context type and logs the changes
*
* @action wp_ajax_edit-theme-plugin-file
*/
public function get_edition_data() {
if ( ! current_user_can( 'edit_theme_options' ) ) {
return;
}
$action = wp_stream_filter_input( INPUT_POST, 'action' );
$request_method = wp_stream_filter_input( INPUT_SERVER, 'REQUEST_METHOD' );
$theme_slug = wp_stream_filter_input( INPUT_POST, 'theme' );
$plugin_slug = wp_stream_filter_input( INPUT_POST, 'plugin' );
$relative_file = wp_stream_filter_input( INPUT_POST, 'file' );
if ( ! empty( $theme_slug ) && ! check_admin_referer( 'edit-theme_' . $theme_slug . '_' . $relative_file, 'nonce' ) ) {
return;
}
if ( ! empty( $plugin_slug ) && ! check_admin_referer( 'edit-plugin_' . $relative_file, 'nonce' ) ) {
return;
}
if ( ( isset( $request_method ) && 'POST' !== $request_method ) || ( 'edit-theme-plugin-file' !== $action ) ) {
return;
}
$location = null;
if ( $theme_slug ) {
$location = 'theme-editor.php';
$this->edited_file = $this->get_theme_data( $theme_slug );
}
if ( $plugin_slug ) {
$location = 'plugin-editor.php';
$this->edited_file = $this->get_plugin_data( $plugin_slug );
}
if ( ! $location ) {
return;
}
$this->log_changes( $location );
}
/**
* Retrieve theme data needed for the log message
*
* @param string $slug The theme slug (e.g. twentyfourteen).
*
* @return mixed $output Compacted variables
*/
public function get_theme_data( $slug ) {
$theme = wp_get_theme( $slug );
if ( ! $theme->exists() || ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) ) {
return false;
}
$allowed_files = $theme->get_files( 'php', 1 );
$style_files = $theme->get_files( 'css' );
$file = wp_stream_filter_input( INPUT_POST, 'file' );
$allowed_files['style.css'] = $style_files['style.css'];
if ( empty( $file ) ) {
$file_name = 'style.css';
$file_path = $allowed_files['style.css'];
} else {
$file_name = $file;
$file_path = sprintf( '%s/%s', $theme->get_stylesheet_directory(), $file_name );
}
$file_md5 = md5_file( $file_path );
$name = $theme->get( 'Name' );
$output = compact(
'file_name',
'file_path',
'file_md5',
'slug',
'name'
);
return $output;
}
/**
* Retrieve plugin data needed for the log message
*
* @param string $slug The plugin file base name (e.g. akismet/akismet.php).
* @return mixed $output Compacted variables.
*/
public function get_plugin_data( $slug ) {
$base = null;
$name = null;
$slug = current( explode( '/', $slug ) );
$file_name = wp_stream_filter_input( INPUT_POST, 'file' );
$file_path = WP_PLUGIN_DIR . '/' . $file_name;
$file_md5 = md5_file( $file_path );
$plugins = get_plugins();
foreach ( $plugins as $key => $plugin_data ) {
if ( 0 === strpos( $key, $slug ) ) {
$base = $key;
$name = $plugin_data['Name'];
break;
}
}
$file_name = str_ireplace( trailingslashit( $slug ), '', $file_name );
$slug = ! empty( $base ) ? $base : $slug;
$output = compact(
'file_name',
'file_path',
'file_md5',
'slug',
'name'
);
return $output;
}
/**
* Logs changes
*
* @param string $location Location.
*/
public function log_changes( string $location ): string {
if ( ! empty( $this->edited_file ) ) {
if ( md5_file( $this->edited_file['file_path'] ) === $this->edited_file['file_md5'] ) {
$context = $this->get_context( $location );
switch ( $context ) {
case 'themes':
$name_key = 'theme_name';
$slug_key = 'theme_slug';
break;
case 'plugins':
$name_key = 'plugin_name';
$slug_key = 'plugin_slug';
break;
default:
$name_key = 'name';
$slug_key = 'slug';
}
$this->log(
$this->get_message(),
array(
'file' => (string) $this->edited_file['file_name'],
$name_key => (string) $this->edited_file['name'],
$slug_key => (string) $this->edited_file['slug'],
'file_path' => (string) $this->edited_file['file_path'],
),
null,
$context,
'updated'
);
}
}
return $location;
}
}