-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.php
451 lines (385 loc) · 12 KB
/
debug.php
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
/**
* GatherContent Plugin
*
* @package GatherContent Plugin
*/
namespace GatherContent\Importer;
use GatherContent\Importer\Settings\Form_Section;
use GatherContent\Importer\Admin\Admin;
/**
* GatherContent Plugin Debug
*
* @since 3.0.1
*/
class Debug extends Base {
/**
* A flag to check if we are in debug mode.
*
* @var boolean
*/
protected static $debug_mode = false;
/**
* GatherContent\Importer\Admin\Admin instance.
*
* @var GatherContent\Importer\Admin\Admin
*/
protected $admin;
/**
* The GC log file name.
*
* @var string
*/
protected static $log_file = 'gathercontent-debug.log';
/**
* The path to the GC log file.
*
* @var string
*/
protected static $log_path = '';
/**
* The query string used to trigger debug mode.
* The query string value must be in the current date in the 'm-d-Y' format.
* e.g. ?gathercontent_debug_mode=11-17-2017
*
* @since 3.1.7
*
* @var string
*/
protected static $query_string = 'gathercontent_debug_mode';
/**
* Constructor. Sets the asset_suffix var.
*
* @since 3.0.1
*/
public function __construct( Admin $admin ) {
$this->admin = $admin;
self::$log_path = WP_CONTENT_DIR . '/' . self::$log_file;
if ( $debug_mode_enabled = get_option( self::$query_string ) ) {
if ( time() > $debug_mode_enabled ) {
delete_option( self::$query_string );
} else {
self::$debug_mode = true;
}
} else {
// Check if constant is set.
self::$debug_mode = self::has_debug_constant();
}
}
/**
* Initiate admin hooks
*
* @return void
* @since 3.0.1
*
*/
public function init_hooks() {
if ( is_admin() && isset( $_GET[ self::$query_string ] ) ) {
$enabled = self::toggle_debug_mode( (bool) $_GET[ self::$query_string ] );
unset( $_GET[ self::$query_string ] );
add_action( 'all_admin_notices', array(
$this,
$enabled ? 'debug_enabled_notice' : 'debug_disabled_notice'
) );
}
if ( ! self::$debug_mode ) {
return;
}
add_filter( "sanitize_option_{$this->admin->option_name}", array( $this, 'do_debug_options_actions' ), 5 );
add_action( 'admin_init', array( $this, 'add_debug_fields' ), 50 );
add_action( 'cwby_sync_items_result', array( $this, 'log_sync_results' ), 10, 2 );
}
/**
* Hooked to `cwby_sync_items_result`, logs results to the debug mode log.
*
* @param mixed $maybe_error Result of sync.
* @param object $sync The GatherContent\Importer\Sync\Pull or GatherContent\Importer\Sync\Push object.
*
* @return void
* @since 3.0.1
*
*/
public function log_sync_results( $maybe_error, $sync ) {
self::debug_log( $maybe_error, $sync->direction . ' items result' );
}
/**
* Outputs admin notice that the debug mode has been disabled.
*
* @return void
* @since 3.0.1
*
*/
public function debug_disabled_notice() {
echo '<div id="message" class="updated"><p>' . esc_html__( 'Content Workflow Debug Mode: Disabled', 'content-workflow-by-bynder' ) . '</p></div>';
}
/**
* Outputs admin notice that the debug mode has been enabled.
*
* @return void
* @since 3.0.1
*
*/
public function debug_enabled_notice() {
echo '<div id="message" class="updated"><p>' . esc_html__( 'Content Workflow Debug Mode: Enabled', 'content-workflow-by-bynder' ) . '</p></div>';
}
/**
* Adds debug fields to the GatherConent API connection settings page.
*
* @since 3.0.1
*/
public function add_debug_fields() {
$section = new Form_Section(
'debug',
__( 'Debug Mode', 'content-workflow-by-bynder' ),
sprintf( __( 'Debug file location: %s', 'content-workflow-by-bynder' ), '<code>wp-content/' . self::$log_file . '</code>' ),
Admin::SLUG
);
$section->add_field(
'log_importer_requests',
__( 'Log Importer Requests?', 'content-workflow-by-bynder' ),
array( $this, 'debug_checkbox_field_cb' )
);
$section->add_field(
'review_stuck_status',
__( 'Review stuck sync statuses?', 'content-workflow-by-bynder' ),
array( $this, 'debug_checkbox_field_cb' )
);
$section->add_field(
'delete_stuck_status',
__( 'Delete stuck sync statuses?', 'content-workflow-by-bynder' ),
array( $this, 'debug_checkbox_field_cb' )
);
$section->add_field(
'view_gc_log_file',
__( 'View contents of the Content Workflow debug log file?', 'content-workflow-by-bynder' ),
array( $this, 'debug_checkbox_field_cb' )
);
$section->add_field(
'delete_gc_log_file',
__( 'Delete Content Workflow debug log file?', 'content-workflow-by-bynder' ),
array( $this, 'debug_checkbox_field_cb' )
);
$section->add_field(
'disable_debug_mode',
__( 'Disable Debug Mode?', 'content-workflow-by-bynder' ),
array( $this, 'debug_checkbox_field_cb' )
);
}
/**
* The Debug mode checkbox toggle field callback.
*
* @param GatherContent\Importer\Settings\Form_Section $field Form_Section object.
*
* @return void
* @since 3.0.1
*
*/
public function debug_checkbox_field_cb( $field ) {
$id = $field->param( 'id' );
$args = array(
'id' => $id,
'name' => $this->admin->option_name . '[debug][' . $id . ']',
'type' => 'checkbox',
'class' => '',
'value' => 1,
);
if ( $this->admin->get_setting( $id ) ) {
$args['checked'] = 'checked';
}
$this->view( 'input', $args );
}
/**
* Handles the actions associated with the debug checkbox toggle fields.
*
* @param array $settings Array of settings.
*
* @return void
* @since 3.0.1
*
*/
public function do_debug_options_actions( $settings ) {
if ( empty( $settings['debug']['log_importer_requests'] ) ) {
$settings['log_importer_requests'] = false;
unset( $settings['debug'] );
return $settings;
}
if ( empty( $settings['debug'] ) ) {
return $settings;
}
$orig_settings = $settings;
$settings = wp_parse_args(
$settings['debug'],
array(
'log_importer_requests' => false,
'review_stuck_status' => false,
'delete_stuck_status' => false,
'view_gc_log_file' => false,
'delete_gc_log_file' => false,
'disable_debug_mode' => false,
)
);
$back_url = isset( $_SERVER['HTTP_REFERER'] ) ? sanitize_text_field( $_SERVER['HTTP_REFERER'] ) : '';
$back_button = $back_url ? '<p><a href="' . $back_url . '">' . __( 'Go Back', 'content-workflow-by-bynder' ) . '</a></p>' : '';
if ( $settings['review_stuck_status'] || $settings['delete_stuck_status'] ) {
return $this->handle_stuck_statuses( $settings, $back_button );
} elseif ( $settings['delete_gc_log_file'] ) {
return $this->delete_cwby_log_file( $back_button );
} elseif ( $settings['view_gc_log_file'] ) {
return $this->view_cwby_log_file( $back_button );
} elseif ( $settings['disable_debug_mode'] ) {
wp_safe_redirect( add_query_arg( self::$query_string, 0, $back_url ) );
exit;
} elseif ( $settings['log_importer_requests'] ) {
$orig_settings['log_importer_requests'] = true;
unset( $orig_settings['debug'] );
return $orig_settings;
}
wp_die( '<xmp>' . __LINE__ . ') $settings: ' . esc_html( print_r( $settings, true ) ) . '</xmp>' . wp_kses_post( $back_button ), esc_html__( 'Debug Mode', 'content-workflow-by-bynder' ) );
}
/**
* Handles the actions associated with the stuck statuses checkboxes.
*
* @param array $settings Array of settings
* @param string $back_button The back button markup.
*
* @return void
* @since 3.0.1
*
*/
public function handle_stuck_statuses( $settings, $back_button ) {
global $wpdb;
/**
* Ignoring the prepared rule as it doesn't work with a table name as a parameter.
* The table name is a WordPress core table and is not user input.
*/
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$options = $wpdb->get_results( "SELECT `option_name` FROM `$wpdb->options` WHERE `option_name` LIKE ('gc_pull_item_%') OR `option_name` LIKE ('gc_push_item_%');" );
if ( ! empty( $options ) ) {
foreach ( $options as $key => $option ) {
$options[ $key ] = array(
'name' => $option->option_name,
'value' => get_option( $option->option_name ),
);
}
} else {
wp_die( esc_html__( 'There are no stuck statuses.', 'content-workflow-by-bynder' ) . wp_kses_post( $back_button ), esc_html__( 'Debug Mode', 'content-workflow-by-bynder' ) );
}
if ( $settings['delete_stuck_status'] ) {
foreach ( $options as $key => $option ) {
$options[ $key ]['deleted'] = delete_option( $option['name'] );
}
}
wp_die( '<xmp>' . __LINE__ . ') $options: ' . esc_html( print_r( $options, true ) ) . '</xmp>' . wp_kses_post( $back_button ), esc_html__( 'Debug Mode', 'content-workflow-by-bynder' ) );
}
/**
* Handles the deleting the debug mode log.
*
* @param string $back_button The back button markup.
*
* @return void
* @since 3.0.1
*
*/
public function delete_cwby_log_file( $back_button ) {
if ( wp_delete_file( self::$log_path ) ) {
wp_die( esc_html__( 'Content Workflow log file deleted.', 'content-workflow-by-bynder' ) . wp_kses_post( $back_button ), esc_html__( 'Debug Mode', 'content-workflow-by-bynder' ) );
}
wp_die( esc_html__( 'Failed to delete Content Workflow log file.', 'content-workflow-by-bynder' ) . wp_kses_post( $back_button ), esc_html__( 'Debug Mode', 'content-workflow-by-bynder' ) );
}
/**
* Handles the view for the debug mode log.
*
* @param string $back_button The back button markup.
*
* @return void
* @since 3.0.1
*
*/
public function view_cwby_log_file( $back_button ) {
$log_contents = file_exists( self::$log_path ) ? wp_remote_get( self::$log_path ) : '';
if ( ! $log_contents ) {
wp_die( esc_html__( 'Content Workflow log file is empty.', 'content-workflow-by-bynder' ) . wp_kses_post( $back_button ), esc_html__( 'Debug Mode', 'content-workflow-by-bynder' ) );
}
die( '<html><body>' . wp_kses_post( $back_button ) . '<pre><textarea style="width:100%;height:100%;min-height:1000px;font-size:14px;font-family:monospace;padding:.5em;">' . esc_textarea( print_r( $log_contents, true ) ) . '</textarea></pre></body></html>' );
}
/**
* Check if GATHERCONTENT_DEBUG_MODE constant is set.
*
* @return string
* @since 3.0.2
*
*/
public static function has_debug_constant() {
return defined( 'GATHERCONTENT_DEBUG_MODE' ) && GATHERCONTENT_DEBUG_MODE;
}
/**
* Check if SCRIPT_DEBUG is enabled.
*
* @return string
* @since 3.0.1
*
*/
public static function debug_mode() {
return self::$debug_mode;
}
/**
* Enable/disable the Debug Mode.
*
* @param bool $debug_enabled Enable/Disable
*
* @return bool Whether it has been enabled.
* @since 3.0.1
*
*/
public static function toggle_debug_mode( $debug_enabled ) {
$changed = false;
if ( ! $debug_enabled ) {
delete_option( self::$query_string );
$changed = ! empty( self::$debug_mode );
} elseif ( gmdate( 'm-d-Y' ) === $debug_enabled ) {
update_option( self::$query_string, time() + DAY_IN_SECONDS );
$changed = empty( self::$debug_mode );
} else {
$debug_enabled = self::$debug_mode;
}
self::$debug_mode = $debug_enabled || self::has_debug_constant();
if ( $changed ) {
$status = self::$debug_mode
? esc_html__( 'Enabled', 'content-workflow-by-bynder' )
: esc_html__( 'Disabled', 'content-workflow-by-bynder' );
self::_debug_log( sprintf( esc_html__( 'Content Workflow Debug Mode: %s', 'content-workflow-by-bynder' ), $status ) );
}
return self::$debug_mode;
}
/**
* Write a message to the log if debug enabled.
*
* @param string $message Message to write to log file.
* @param string $title Describes what is being logged.
*
* @return void
* @since 3.0.1
*
*/
public static function debug_log( $message = '', $title = '' ) {
if ( self::$debug_mode ) {
self::_debug_log( $message, $title );
}
}
/**
* Write a message to the log.
*
* @param string $message Message to write to log file.
*
* @return void
* @since 3.0.1
*
*/
protected static function _debug_log( $message = '', $title = '' ) {
$message = print_r( $message, 1 );
if ( $title ) {
$message = print_r( $title, 1 ) . ': ' . $message;
}
error_log( gmdate( 'Y-m-d H:i:s' ) . ': ' . $message . "\r\n", 3, self::$log_path );
}
}