| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our helper functions to use across the plugin. |
| 4 |
* |
| 5 |
* @package DebugQuickLook |
| 6 |
*/ |
| 7 |
|
| 8 |
// Call our namepsace. |
| 9 |
namespace DebugQuickLook\Helpers; |
| 10 |
|
| 11 |
// Set our alias items. |
| 12 |
use DebugQuickLook as Core; |
| 13 |
|
| 14 |
/** |
| 15 |
* Run a quick check to see if the debug log constant is set. |
| 16 |
* |
| 17 |
* @return boolean |
| 18 |
*/ |
| 19 |
function maybe_constant_set() { |
| 20 |
return defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ? true : false; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Build and return the data for our admin nodes. |
| 25 |
* |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
function get_admin_bar_nodes() { |
| 29 |
|
| 30 |
// Set the view args. |
| 31 |
$view_args = array( |
| 32 |
'id' => 'quick-look-view', |
| 33 |
'title' => __( 'View File', 'debug-quick-look' ), |
| 34 |
'href' => esc_url( build_quicklook_url( 'view' ) ), |
| 35 |
'position' => 0, |
| 36 |
'parent' => 'debug-quick-look', |
| 37 |
'meta' => array( |
| 38 |
'title' => __( 'View File', 'debug-quick-look' ), |
| 39 |
'target' => '_blank', |
| 40 |
'rel' => 'noopener', |
| 41 |
), |
| 42 |
); |
| 43 |
|
| 44 |
// Set the raw args. |
| 45 |
$raw_args = array( |
| 46 |
'id' => 'quick-look-raw', |
| 47 |
'title' => __( 'View File (Raw)', 'debug-quick-look' ), |
| 48 |
'href' => esc_url( build_quicklook_url( 'raw' ) ), |
| 49 |
'position' => 0, |
| 50 |
'parent' => 'debug-quick-look', |
| 51 |
'meta' => array( |
| 52 |
'title' => __( 'View File (Raw)', 'debug-quick-look' ), |
| 53 |
'target' => '_blank', |
| 54 |
'rel' => 'noopener', |
| 55 |
), |
| 56 |
); |
| 57 |
|
| 58 |
// Set the purge args. |
| 59 |
$purge_args = array( |
| 60 |
'id' => 'quick-look-purge', |
| 61 |
'title' => __( 'Purge File', 'debug-quick-look' ), |
| 62 |
'href' => esc_url( build_quicklook_url( 'purge' ) ), |
| 63 |
'position' => 0, |
| 64 |
'parent' => 'debug-quick-look', |
| 65 |
'meta' => array( |
| 66 |
'title' => __( 'Purge File', 'debug-quick-look' ), |
| 67 |
), |
| 68 |
); |
| 69 |
|
| 70 |
// Add the text node with our warning. |
| 71 |
$error_args = array( |
| 72 |
'id' => 'quick-look-error', |
| 73 |
'title' => __( 'The <span>WP_DEBUG_LOG</span> constant is not defined!', 'debug-quick-look' ), |
| 74 |
'position' => 0, |
| 75 |
'parent' => 'debug-quick-look', |
| 76 |
'meta' => array( |
| 77 |
'class' => 'debug-quick-look-missing', |
| 78 |
), |
| 79 |
); |
| 80 |
|
| 81 |
// Return the array of data. |
| 82 |
return array( 'view' => $view_args, 'raw' => $raw_args, 'purge' => $purge_args, 'error' => $error_args ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Build and return the single URL for a quick action. |
| 87 |
* |
| 88 |
* @param string $action The action being added. |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
function build_quicklook_url( $action = '' ) { |
| 93 |
|
| 94 |
// Set my nonce name and key. |
| 95 |
$nonce = 'debug_quicklook_' . sanitize_text_field( $action ) . '_action'; |
| 96 |
|
| 97 |
// Set up my args. |
| 98 |
$setup = array( |
| 99 |
'quicklook' => 1, |
| 100 |
'debug' => sanitize_text_field( $action ), |
| 101 |
'nonce' => wp_create_nonce( $nonce ), |
| 102 |
); |
| 103 |
|
| 104 |
// And return the URL. |
| 105 |
return add_query_arg( $setup, admin_url( '/' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Set each function we want to use during formatting. |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
function get_formatting_args() { |
| 114 |
|
| 115 |
// Set the args. |
| 116 |
$setup = array( |
| 117 |
'\wrap_dateblock' => 'native', |
| 118 |
'\wrap_stacktrace' => 'native', |
| 119 |
'\wrap_warning_types' => 'native', |
| 120 |
'\wrap_json_bits' => 'native', |
| 121 |
); |
| 122 |
|
| 123 |
// Return our args, filtered. |
| 124 |
return apply_filters( Core\HOOK_PREFIX . 'formatting_args', $setup ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get our debug log with the option to filter. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
function get_debug_file() { |
| 133 |
return apply_filters( Core\HOOK_PREFIX . 'debug_file', Core\DEBUG_FILE ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Run a quick check to see if the debug log file is empty. |
| 138 |
* |
| 139 |
* @return boolean |
| 140 |
*/ |
| 141 |
function check_debug_file() { |
| 142 |
|
| 143 |
// Set our file. |
| 144 |
$debug = get_debug_file(); |
| 145 |
|
| 146 |
// If no file exists at all, create an empty one. |
| 147 |
if ( false === file_exists( $debug ) ) { |
| 148 |
file_put_contents( $debug, '' ); |
| 149 |
} |
| 150 |
|
| 151 |
// If the file is empty, return that. |
| 152 |
return 0 === filesize( $debug ) ? false : true; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Purge the existing log file. |
| 157 |
* |
| 158 |
* @return boolean |
| 159 |
*/ |
| 160 |
function purge_debug_file() { |
| 161 |
|
| 162 |
// Purge the data file. |
| 163 |
file_put_contents( get_debug_file(), '' ); |
| 164 |
|
| 165 |
// And redirect with a query string. |
| 166 |
$direct = add_query_arg( array( 'quicklook' => 1, 'quickpurge' => 1 ), admin_url( '/' ) ); |
| 167 |
|
| 168 |
// Then redirect. |
| 169 |
wp_redirect( $direct ); |
| 170 |
exit; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Check the user capabilities at various points. |
| 175 |
* |
| 176 |
* @param string $location Where on the admin we're checking. |
| 177 |
* |
| 178 |
* @return boolean |
| 179 |
*/ |
| 180 |
function check_user_cap( $location = '' ) { |
| 181 |
|
| 182 |
// Set our role check. |
| 183 |
$setcap = apply_filters( Core\HOOK_PREFIX . 'user_cap', 'manage_options', $location ); |
| 184 |
|
| 185 |
// Return the result of checking. |
| 186 |
return current_user_can( $setcap ); |
| 187 |
} |
| 188 |
|