PluginProbe
WP Debugging / 2.12.6
WP Debugging v2.12.6
2.12.6 2.12.5 trunk 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.11.10 2.11.11 2.11.12 2.11.13 2.11.14 2.11.15 2.11.16 2.11.17 2.11.18 2.11.19 2.11.2 2.11.20 2.11.21 2.11.22 2.11.23 2.11.24 2.11.3 All 59 releases
wp-debugging / vendor / norcross / debug-quick-look / includes / helpers.php

helpers.php in WP Debugging 2.12.6, at vendor/norcross/debug-quick-look/includes/helpers.php

199 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = [
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' => [
38 'title' => __( 'View File', 'debug-quick-look' ),
39 'target' => '_blank',
40 'rel' => 'noopener',
41 ],
42 ];
43
44 // Set the raw args.
45 $raw_args = [
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' => [
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 = [
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' => [
66 'title' => __( 'Purge File', 'debug-quick-look' ),
67 ],
68 ];
69
70 // Add the text node with our warning.
71 $error_args = [
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' => [
77 'class' => 'debug-quick-look-missing',
78 ],
79 ];
80
81 // Return the array of data.
82 return [
83 'view' => $view_args,
84 'raw' => $raw_args,
85 'purge' => $purge_args,
86 'error' => $error_args,
87 ];
88 }
89
90 /**
91 * Build and return the single URL for a quick action.
92 *
93 * @param string $action The action being added.
94 *
95 * @return string
96 */
97 function build_quicklook_url( $action = '' ) {
98
99 // Set my nonce name and key.
100 $nonce = 'debug_quicklook_' . sanitize_text_field( $action ) . '_action';
101
102 // Set up my args.
103 $setup = [
104 'quicklook' => 1,
105 'debug' => sanitize_text_field( $action ),
106 'nonce' => wp_create_nonce( $nonce ),
107 ];
108
109 // And return the URL.
110 return add_query_arg( $setup, admin_url( '/' ) );
111 }
112
113 /**
114 * Set each function we want to use during formatting.
115 *
116 * @return array
117 */
118 function get_formatting_args() {
119
120 // Set the args.
121 $setup = [
122 '\wrap_dateblock' => 'native',
123 '\wrap_stacktrace' => 'native',
124 '\wrap_warning_types' => 'native',
125 '\wrap_json_bits' => 'native',
126 ];
127
128 // Return our args, filtered.
129 return apply_filters( Core\HOOK_PREFIX . 'formatting_args', $setup );
130 }
131
132 /**
133 * Get our debug log with the option to filter.
134 *
135 * @return string
136 */
137 function get_debug_file() {
138 return apply_filters( Core\HOOK_PREFIX . 'debug_file', Core\DEBUG_FILE );
139 }
140
141 /**
142 * Run a quick check to see if the debug log file is empty.
143 *
144 * @return boolean
145 */
146 function check_debug_file() {
147
148 // Set our file.
149 $debug = get_debug_file();
150
151 // If no file exists at all, create an empty one.
152 if ( false === file_exists( $debug ) ) {
153 file_put_contents( $debug, '' );
154 }
155
156 // If the file is empty, return that.
157 return 0 === filesize( $debug ) ? false : true;
158 }
159
160 /**
161 * Purge the existing log file.
162 *
163 * @return boolean
164 */
165 function purge_debug_file() {
166
167 // Purge the data file.
168 file_put_contents( get_debug_file(), '' );
169
170 // And redirect with a query string.
171 $direct = add_query_arg(
172 [
173 'quicklook' => 1,
174 'quickpurge' => 1,
175 ],
176 admin_url( '/' )
177 );
178
179 // Then redirect.
180 wp_redirect( $direct );
181 exit;
182 }
183
184 /**
185 * Check the user capabilities at various points.
186 *
187 * @param string $location Where on the admin we're checking.
188 *
189 * @return boolean
190 */
191 function check_user_cap( $location = '' ) {
192
193 // Set our role check.
194 $setcap = apply_filters( Core\HOOK_PREFIX . 'user_cap', 'manage_options', $location );
195
196 // Return the result of checking.
197 return current_user_can( $setcap );
198 }
199