1212class Plugin {
1313 private $ _settings = array ();
1414 private $ _defaults = array (
15- 'pr ' => 'prettyprint ' ,
15+ 'pr ' => 'kint ' ,
1616 'active ' => false ,
1717 'admin ' => false ,
1818 'frontend ' => false ,
@@ -55,12 +55,12 @@ class Plugin {
5555 private $ _wp_version_real ;
5656 private $ _cp_version ;
5757 private $ _cp_version_real ;
58+ private $ _rest_request = false ;
5859
5960 public function __construct () {
6061
6162 }
6263
63- /** @return \Dev4Press\Plugin\DebugPress\Main\Plugin */
6464 public static function instance () : Plugin {
6565 static $ instance = null ;
6666
@@ -73,9 +73,25 @@ public static function instance() : Plugin {
7373 }
7474
7575 private function run () {
76+ add_action ( 'rest_api_init ' , array ( $ this , 'rest_api ' ) );
7677 add_action ( 'plugins_loaded ' , array ( $ this , 'plugins_loaded ' ), 0 );
7778 add_action ( 'plugins_loaded ' , array ( $ this , 'activation ' ), DEBUGPRESS_ACTIVATION_PRIORITY );
7879 add_action ( 'init ' , array ( $ this , 'init ' ) );
80+
81+ if ( is_admin () ) {
82+ add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'loader ' ) );
83+ } else {
84+ add_action ( 'wp ' , array ( $ this , 'loader ' ) );
85+ add_action ( 'login_init ' , array ( $ this , 'loader ' ) );
86+ }
87+ }
88+
89+ public function rest_api () {
90+ $ this ->_rest_request = defined ( 'REST_REQUEST ' ) && REST_REQUEST ;
91+ }
92+
93+ public function is_rest_request () : bool {
94+ return $ this ->_rest_request ;
7995 }
8096
8197 public function wp_version () : string {
@@ -126,49 +142,21 @@ public function activation() {
126142 debugpress_tracker ();
127143 }
128144
129- private function load_printer ( $ name = '' ) {
130- if ( ! function_exists ( 'debugpress_r ' ) ) {
131- $ name = empty ( $ name ) ? $ this ->get ( 'pr ' ) : 'prettyprint ' ;
132- $ path = DEBUGPRESS_PLUGIN_PATH . 'core/printer/ ' . $ name . '/load.php ' ;
133-
134- if ( file_exists ( $ path ) ) {
135- require_once ( $ path );
136- } else {
137- $ this ->load_printer ( 'prettyprint ' );
138- }
139- }
140- }
141-
142- private function define_constants () {
143- if ( $ this ->get ( 'auto_wpdebug ' ) && ! defined ( 'WP_DEBUG ' ) ) {
144- define ( 'WP_DEBUG ' , true );
145- }
146-
147- if ( $ this ->get ( 'auto_savequeries ' ) && ! defined ( 'SAVEQUERIES ' ) ) {
148- define ( 'SAVEQUERIES ' , true );
149- }
150-
151- define ( 'DEBUGPRESS_IS_DEBUG ' , defined ( 'WP_DEBUG ' ) && WP_DEBUG );
152- define ( 'DEBUGPRESS_IS_DEBUG_LOG ' , DEBUGPRESS_IS_DEBUG && defined ( 'WP_DEBUG_LOG ' ) && WP_DEBUG_LOG );
153-
154- $ version = str_replace ( '. ' , '' , phpversion () );
155- $ version = intval ( substr ( $ version , 0 , 2 ) );
156-
157- define ( 'DEBUGPRESS_PHP_VERSION ' , $ version );
158- }
159-
160145 public function init () {
161146 wp_register_style ( 'animated-popup ' , DEBUGPRESS_PLUGIN_URL . 'popup/popup.min.css ' , array (), $ this ->_animated_popup_version );
147+ wp_register_style ( 'debugpress-print ' , DEBUGPRESS_PLUGIN_URL . 'css/prettyprint ' . ( DEBUGPRESS_IS_DEBUG ? '' : '.min ' ) . '.css ' , array (), DEBUGPRESS_VERSION );
162148 wp_register_style ( 'debugpress ' , DEBUGPRESS_PLUGIN_URL . 'css/debugpress ' . ( DEBUGPRESS_IS_DEBUG ? '' : '.min ' ) . '.css ' , array ( 'animated-popup ' ), DEBUGPRESS_VERSION );
163149 wp_register_script ( 'animated-popup ' , DEBUGPRESS_PLUGIN_URL . 'popup/popup.min.js ' , array ( 'jquery ' ), $ this ->_animated_popup_version , true );
164150 wp_register_script ( 'debugpress ' , DEBUGPRESS_PLUGIN_URL . 'js/debugpress ' . ( DEBUGPRESS_IS_DEBUG ? '' : '.min ' ) . '.js ' , array ( 'animated-popup ' ), DEBUGPRESS_VERSION , true );
165151
166- if ( ! DEBUGPRESS_IS_AJAX && ! DEBUGPRESS_IS_CRON && $ this ->_allowed ) {
167- $ allowed = is_admin () ? $ this ->get ( 'admin ' ) : $ this ->get ( 'frontend ' );
152+ if ( is_admin () ? $ this ->get ( 'admin ' ) : $ this ->get ( 'frontend ' ) ) {
153+ Loader::instance ();
154+ }
155+ }
168156
169- if ( $ allowed ) {
170- Loader:: instance ();
171- }
157+ public function loader ( ) {
158+ if ( ! $ this -> is_rest_request () && ! DEBUGPRESS_IS_AJAX && ! DEBUGPRESS_IS_CRON && $ this -> _allowed ) {
159+ Loader:: instance ()-> run ();
172160 }
173161 }
174162
@@ -215,32 +203,6 @@ public function process_settings( $input ) : array {
215203 return $ settings ;
216204 }
217205
218- private function is_user_allowed () : bool {
219- if ( is_super_admin () ) {
220- return $ this ->get ( 'for_super_admin ' );
221- } else if ( is_user_logged_in () ) {
222- $ allowed = $ this ->get ( 'for_roles ' );
223-
224- if ( $ allowed === true || is_null ( $ allowed ) ) {
225- return true ;
226- } else if ( is_array ( $ allowed ) && empty ( $ allowed ) ) {
227- return false ;
228- } else if ( is_array ( $ allowed ) && ! empty ( $ allowed ) ) {
229- global $ current_user ;
230-
231- if ( is_array ( $ current_user ->roles ) ) {
232- $ matched = array_intersect ( $ current_user ->roles , $ allowed );
233-
234- return ! empty ( $ matched );
235- }
236- }
237- } else {
238- return $ this ->get ( 'for_visitor ' );
239- }
240-
241- return false ;
242- }
243-
244206 public function environment () : array {
245207 $ env = array ();
246208
@@ -292,4 +254,74 @@ public function build_stats( $key = 'wp_footer' ) : string {
292254
293255 return $ gd ;
294256 }
257+
258+ public function enqueue_print_style () {
259+ wp_enqueue_style ( 'debugpress-print ' );
260+ }
261+
262+ private function is_user_allowed () : bool {
263+ if ( is_super_admin () ) {
264+ return $ this ->get ( 'for_super_admin ' );
265+ } else if ( is_user_logged_in () ) {
266+ $ allowed = $ this ->get ( 'for_roles ' );
267+
268+ if ( $ allowed === true || is_null ( $ allowed ) ) {
269+ return true ;
270+ } else if ( is_array ( $ allowed ) && empty ( $ allowed ) ) {
271+ return false ;
272+ } else if ( is_array ( $ allowed ) && ! empty ( $ allowed ) ) {
273+ global $ current_user ;
274+
275+ if ( is_array ( $ current_user ->roles ) ) {
276+ $ matched = array_intersect ( $ current_user ->roles , $ allowed );
277+
278+ return ! empty ( $ matched );
279+ }
280+ }
281+ } else {
282+ return $ this ->get ( 'for_visitor ' );
283+ }
284+
285+ return false ;
286+ }
287+
288+ private function load_printer ( $ name = '' ) {
289+ if ( ! function_exists ( 'debugpress_r ' ) ) {
290+ $ name = empty ( $ name ) ? $ this ->get ( 'pr ' ) : 'prettyprint ' ;
291+ $ path = DEBUGPRESS_PLUGIN_PATH . 'core/printer/ ' . $ name . '/load.php ' ;
292+
293+ if ( $ name == 'prettyprint ' ) {
294+ if ( is_admin () ) {
295+ add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'enqueue_print_style ' ) );
296+ } else {
297+ add_action ( 'wp_enqueue_scripts ' , array ( $ this , 'enqueue_print_style ' ) );
298+ add_action ( 'login_enqueue_scripts ' , array ( $ this , 'enqueue_print_style ' ) );
299+ }
300+ }
301+
302+ if ( file_exists ( $ path ) ) {
303+ require_once ( $ path );
304+ } else {
305+ $ this ->load_printer ( 'prettyprint ' );
306+ }
307+ }
308+ }
309+
310+ private function define_constants () {
311+ if ( $ this ->get ( 'auto_wpdebug ' ) && ! defined ( 'WP_DEBUG ' ) ) {
312+ define ( 'WP_DEBUG ' , true );
313+ }
314+
315+ if ( $ this ->get ( 'auto_savequeries ' ) && ! defined ( 'SAVEQUERIES ' ) ) {
316+ define ( 'SAVEQUERIES ' , true );
317+ }
318+
319+ define ( 'DEBUGPRESS_IS_DEBUG ' , defined ( 'WP_DEBUG ' ) && WP_DEBUG );
320+ define ( 'DEBUGPRESS_IS_DEBUG_LOG ' , DEBUGPRESS_IS_DEBUG && defined ( 'WP_DEBUG_LOG ' ) && WP_DEBUG_LOG );
321+
322+ $ version = str_replace ( '. ' , '' , phpversion () );
323+ $ version = intval ( substr ( $ version , 0 , 2 ) );
324+
325+ define ( 'DEBUGPRESS_PHP_VERSION ' , $ version );
326+ }
295327}
0 commit comments