@@ -68,123 +68,136 @@ public static function adminBarMenuHook( \WP_Admin_Bar $wp_admin_bar ): void {
6868    }
6969
7070    public  static  function  afterAdminBarRender (): void  {
71-         $ ajax_job_queue_url  = Controller::getAdminAjaxUrl ( 'job_queue '  );
72-         ?> 
73-     <script>
74-     var static_deploy_job_queue_url = "<?php  echo  esc_js ( $ ajax_job_queue_url  ); ?> ";
75-     var static_deploy_last_interval = 30000;
76-     var static_deploy_job_type_labels = {
77-         detect: "Detecting URLs",
78-         crawl: "Crawling Site",
79-         post_process: "Post-Processing",
80-         deploy: "Deploying",
81-         direct_deploy: "Deploying (Direct)",
82-         direct_deploy_post: "Deploying (Single Post)",
83-     };
84-     var static_deploy_idle = false;
85- 
86-     function static_deploy_update_status_button(text, bgcolor) {
87-         document.querySelectorAll(".static-deploy-deploy-status-container").forEach(el => {
88-             el.style.backgroundColor = bgcolor;
89-             el.style.borderRadius = "5px";
90-         });
91- 
92-         document.querySelectorAll(".static-deploy-deploy-status").forEach(el => {
93-             el.textContent = "Static Deploy: " + text;
94-         });
95-     }
71+         $ ajax_job_queue_url  = esc_js ( Controller::getAdminAjaxUrl ( 'job_queue '  ) );
72+ 
73+         $ script  = sprintf (
74+             '<script>  
75+ var static_deploy_job_queue_url = "%s"; 
76+ var static_deploy_last_interval = 30000; 
77+ var static_deploy_job_type_labels = { 
78+     detect: "Detecting URLs", 
79+     crawl: "Crawling Site", 
80+     post_process: "Post-Processing", 
81+     deploy: "Deploying", 
82+     direct_deploy: "Deploying (Direct)", 
83+     direct_deploy_post: "Deploying (Single Post)", 
84+ }; 
85+ var static_deploy_idle = false; 
86+ 
87+ function static_deploy_update_status_button(text, bgcolor) { 
88+     document.querySelectorAll(".static-deploy-deploy-status-container").forEach(el => { 
89+         el.style.backgroundColor = bgcolor; 
90+         el.style.borderRadius = "5px"; 
91+     }); 
92+ 
93+     document.querySelectorAll(".static-deploy-deploy-status").forEach(el => { 
94+         el.textContent = "Static Deploy: " + text; 
95+     }); 
96+ } 
9697
97-     function static_deploy_check_idle() {
98-         if ( static_deploy_idle && document.visibilityState == 'visible' ) {
99-             static_deploy_update_status_button('Checking status...', '');
100-             static_deploy_update_status();
101-         }
98+ function static_deploy_check_idle() { 
99+     if ( static_deploy_idle && document.visibilityState == "visible" ) { 
100+         static_deploy_update_status_button("Checking status...", ""); 
101+         static_deploy_update_status(); 
102102    } 
103+ } 
103104
104-     function static_deploy_process_job_queue_data(data) {
105-         static_deploy_last_interval = 30000;
106-         setTimeout(static_deploy_update_status, 30000);
105+ function static_deploy_process_job_queue_data(data) { 
106+     static_deploy_last_interval = 30000; 
107+     setTimeout(static_deploy_update_status, 30000); 
108+ 
109+     // Cache data to localStorage so we can show it immediately 
110+     // when page is loaded or refreshed 
111+     data.timestamp = Date.now(); 
112+     try { 
113+         localStorage.setItem("static_deploy_job_queue", JSON.stringify(data)); 
114+     } catch (e) { 
115+         console.warn("Could not write to localStorage:", e); 
116+     } 
107117
108-         // Cache data to localStorage so we can show it immediately
109-         // when page is loaded or refreshed
110-         data.timestamp = Date.now();
111-         try {
112-             localStorage.setItem('static_deploy_job_queue', JSON.stringify(data));
113-         } catch (e) {
114-             console.warn('Could not write to localStorage:', e);
115-         }
118+     let bgcolor = ""; 
119+     let text; 
116120
117-         let bgcolor = "";
118-         let text;
119- 
120-         if (data.jobs.length === 0) {
121-             if (data.job_count === 0) {
122-                 if (data.invalidations) {
123-                     text = "Refreshing CDN cache";
124-                 } else {
125-                     bgcolor = "green";
126-                     text = "Deployed";
127-                 }
121+     if (data.jobs.length === 0) { 
122+         if (data.job_count === 0) { 
123+             if (data.invalidations) { 
124+                 text = "Refreshing CDN cache"; 
128125            } else { 
129-                 text = "Queued";
126+                 bgcolor = "green"; 
127+                 text = "Deployed"; 
130128            } 
131129        } else { 
132-             let type = data.jobs[0].job_type;
133-             text = static_deploy_job_type_labels[type] || type;
130+             text = "Queued"; 
134131        } 
135- 
136-         static_deploy_update_status_button(text, bgcolor);
132+     } else { 
133+         let type = data.jobs[0].job_type; 
134+         text = static_deploy_job_type_labels[type] || type; 
137135    } 
138136
139-     function static_deploy_update_status() {
140-         if ( document.visibilityState != 'visible' ) {
141-             static_deploy_idle = true;
142-             static_deploy_last_interval = 30000;
143-             setTimeout(static_deploy_update_status, 30000);
144-             static_deploy_update_status_button("Idle", "");
145-             return;
146-         }
137+     static_deploy_update_status_button(text, bgcolor); 
138+ } 
147139
148-         static_deploy_idle = false;
149-         fetch(static_deploy_job_queue_url, {
150-             method: "GET",
151-         })
152-         .then(response => response.json())
153-         .then(static_deploy_process_job_queue_data)
154-         .catch(error => {
155-             console.error(error);
156-             static_deploy_last_interval *= 2;
157-             setTimeout(static_deploy_update_status, static_deploy_last_interval);
158-         });
140+ function static_deploy_update_status() { 
141+     if ( document.visibilityState != "visible" ) { 
142+         static_deploy_idle = true; 
143+         static_deploy_last_interval = 30000; 
144+         setTimeout(static_deploy_update_status, 30000); 
145+         static_deploy_update_status_button("Idle", ""); 
146+         return; 
159147    } 
160148
161-     function static_deploy_init() {
162-         // Apply cached data
163-         try {
164-             const cached = JSON.parse(localStorage.getItem('static_deploy_job_queue'));
165-             // Don't use cached data older than one week
166-             const millis_in_week = 7 * 86400 * 1000;
167-             const millis_since = Date.now() - cached.timestamp; // NaN if no cached data
168-             if (cached && millis_since < millis_in_week) {
169-                 static_deploy_process_job_queue_data(cached);
170-             }
171-         } catch (e) {
172-             console.warn('Could not read from localStorage:', e);
149+     static_deploy_idle = false; 
150+     fetch(static_deploy_job_queue_url, { 
151+         method: "GET", 
152+     }) 
153+     .then(response => response.json()) 
154+     .then(static_deploy_process_job_queue_data) 
155+     .catch(error => { 
156+         console.error(error); 
157+         static_deploy_last_interval *= 2; 
158+         setTimeout(static_deploy_update_status, static_deploy_last_interval); 
159+     }); 
160+ } 
161+ 
162+ function static_deploy_init() { 
163+     // Apply cached data 
164+     try { 
165+         const cached = JSON.parse(localStorage.getItem("static_deploy_job_queue")); 
166+         // Don \'t use cached data older than one week 
167+         const millis_in_week = 7 * 86400 * 1000; 
168+         const millis_since = Date.now() - cached.timestamp; // NaN if no cached data 
169+         if (cached && millis_since < millis_in_week) { 
170+             static_deploy_process_job_queue_data(cached); 
173171        } 
172+     } catch (e) { 
173+         console.warn("Could not read from localStorage:", e); 
174+     } 
174175
175-          document.querySelectorAll(".static-deploy-deploy-status-container").forEach(el => {
176-              el.style.visibility = "visible";
177-          });
176+     document.querySelectorAll(".static-deploy-deploy-status-container").forEach(el => { 
177+         el.style.visibility = "visible"; 
178+     }); 
178179
179-          static_deploy_update_status();
180-      }
180+     static_deploy_update_status(); 
181+ } 
181182
182-     window.onload = (event) => {
183-         setInterval(static_deploy_check_idle, 1000);
184-         setTimeout(static_deploy_init, 1);
185-     };
186-     </script>
187-         <?php 
183+ window.onload = (event) => { 
184+     setInterval(static_deploy_check_idle, 1000); 
185+     setTimeout(static_deploy_init, 1); 
186+ }; 
187+ </script> ' ,
188+             $ ajax_job_queue_url
189+         );
190+         wp_register_script (
191+             'static-deploy-admin-bar ' ,
192+             '' ,
193+             [],
194+             1 ,
195+             [
196+                 'in_footer '  => false ,
197+             ],
198+         );
199+         wp_enqueue_script ( 'static-deploy-admin-bar '  );
200+         wp_add_inline_script ( 'static-deploy-admin-bar ' , $ script  );
188201    }
189202
190203    public  static  function  ajaxJobQueue (): void  {
0 commit comments