| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our actions functions. |
| 4 |
* |
| 5 |
* @package DebugQuickLook |
| 6 |
*/ |
| 7 |
|
| 8 |
// Call our namepsace. |
| 9 |
namespace DebugQuickLook\Actions; |
| 10 |
|
| 11 |
// Set our alias items. |
| 12 |
use DebugQuickLook as Core; |
| 13 |
use DebugQuickLook\Helpers as Helpers; |
| 14 |
use DebugQuickLook\Parser as Parser; |
| 15 |
use DebugQuickLook\Formatting as Formatting; |
| 16 |
|
| 17 |
/** |
| 18 |
* Start our engines. |
| 19 |
*/ |
| 20 |
add_action( 'admin_init', __NAMESPACE__ . '\run_quicklook_action' ); |
| 21 |
add_filter( 'removable_query_args', __NAMESPACE__ . '\add_removable_arg' ); |
| 22 |
add_action( 'admin_notices', __NAMESPACE__ . '\display_purge_result' ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Run the quicklook action if we've requested it. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
function run_quicklook_action() { |
| 30 |
|
| 31 |
// Run my cap check. |
| 32 |
$hascap = Helpers\check_user_cap( 'quicklook-action' ); |
| 33 |
|
| 34 |
// Bail without. |
| 35 |
if ( ! $hascap ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// Bail without the query strings or not on admin. |
| 40 |
if ( ! is_admin() || empty( $_GET['quicklook'] ) || empty( $_GET['debug'] ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Set our debug key. |
| 45 |
$setkey = sanitize_text_field( $_GET['debug'] ); |
| 46 |
|
| 47 |
// Check to see if our nonce was provided. |
| 48 |
if ( empty( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'], 'debug_quicklook_' . $setkey . '_action' ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// Include our action to run beforehand. |
| 53 |
do_action( Core\HOOK_PREFIX . 'before_action', $setkey ); |
| 54 |
|
| 55 |
// Switch through and return the item. |
| 56 |
switch ( esc_attr( $setkey ) ) { |
| 57 |
|
| 58 |
case 'view': |
| 59 |
Parser\run_parse(); |
| 60 |
break; |
| 61 |
|
| 62 |
case 'raw': |
| 63 |
Parser\run_parse( false ); |
| 64 |
break; |
| 65 |
|
| 66 |
case 'purge': |
| 67 |
Helpers\purge_debug_file(); |
| 68 |
break; |
| 69 |
|
| 70 |
// End all case breaks. |
| 71 |
} |
| 72 |
|
| 73 |
// Include our action to run afterwards. |
| 74 |
do_action( Core\HOOK_PREFIX . 'after_action', $setkey ); |
| 75 |
|
| 76 |
// And just be finished. |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Add our custom strings to the vars. |
| 82 |
* |
| 83 |
* @param array $args The existing array of args. |
| 84 |
* |
| 85 |
* @return array $args The modified array of args. |
| 86 |
*/ |
| 87 |
function add_removable_arg( $args ) { |
| 88 |
|
| 89 |
// Include my new args and return. |
| 90 |
return wp_parse_args( [ 'quicklook', 'quickpurge' ], $args ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Echo out the notification. |
| 95 |
* |
| 96 |
* @return HTML |
| 97 |
*/ |
| 98 |
function display_purge_result() { |
| 99 |
|
| 100 |
// Bail without the query strings or not on admin. |
| 101 |
if ( ! is_admin() || empty( $_GET['quicklook'] ) || empty( $_GET['quickpurge'] ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// Show the message. |
| 106 |
echo '<div class="notice notice-success is-dismissible">'; |
| 107 |
echo '<p>' . esc_html__( 'Success! Your debug file has been purged.', 'debug-quick-look' ) . '</p>'; |
| 108 |
echo '</div>'; |
| 109 |
} |
| 110 |
|