| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeminiLabs\BlackBar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * @var Application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function __construct(Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $this->app = $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @action admin_enqueue_scripts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @action wp_enqueue_scripts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function enqueueAssets(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         wp_enqueue_script(Application::ID, $this->app->url('assets/main.js')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), ['dashicons']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @action admin_body_class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function filterBodyClasses($classes): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return trim((string) $classes.' '.Application::ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @action all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function initActions(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (!class_exists('Debug_Bar_Slow_Actions')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->app->actions->startTimer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @action all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function initConsole(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (Application::CONSOLE_HOOK !== func_get_arg(0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $args = array_pad(func_get_args(), 4, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $args = array_combine(['hook', 'message', 'errno', 'location'], $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $args = array_map('sanitize_textarea_field', $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $entry = array_pop($backtrace); // get the fourth backtrace entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (empty(trim($args['location'])) && array_key_exists('file', $entry)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $path = explode(ABSPATH, $entry['file']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $args['location'] = sprintf('%s:%s', array_pop($path), $entry['line']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->app->console->store($args['message'], $args['errno'], $args['location']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @action all | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public function initProfiler(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $hook = func_get_arg(0); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $name = func_num_args() > 1 ? func_get_arg(1) : 'Timer'; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $microtime = microtime(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         if ('timer:start' === $hook) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $this->app->profiler->start($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } elseif ('timer:stop' === $hook) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $this->app->profiler->stop($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         } elseif ('blackbar/profiler/noise' === $hook) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             $this->app->profiler->setNoise($microtime); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } elseif ('blackbar/profiler/start' === $hook) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             $this->app->profiler->setStart($microtime); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         } elseif ('blackbar/profiler/stop' === $hook) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             $this->app->profiler->setStop($microtime); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @action plugins_loaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function registerLanguages(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         load_plugin_textdomain(Application::ID, false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             plugin_basename($this->app->path()).'/languages/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @action admin_footer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @action wp_footer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function renderBar(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         do_action('blackbar/profiler/stop'); // stop profiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->app->render('debug-bar', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             'modules' => [ // order is intentional | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $this->app->console, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $this->app->profiler, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $this->app->queries, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $this->app->actions, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $this->app->templates, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $this->app->globals, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  |