11<?php
22
3- namespace StaticWeb ;
3+ namespace StaticDeploy ;
44
55use Aws \Exception \AwsException ;
66
77class AdminBar {
8-
9- public static function admin_bar_menu_hook ( \WP_Admin_Bar $ wp_admin_bar ) : void {
10- if ( ! defined ( 'WP2STATIC_PATH ' ) ) {
11- return ;
12- }
13-
14- $ deployment_url = \WP2Static \CoreOptions::getValue ( 'deploymentURL ' );
15-
16- $ title = '<div class="staticweb-deploy-status-container" style="border-radius: 5px; link-color: #fff;"><a style="color: white" href=" ' . $ deployment_url . '" target="_blank"><div class="staticweb-deploy-status" style="margin: 0 5px">WP2Static: Checking status...</div></a></div> ' ;
17-
18- $ group = array (
19- 'id ' => 'staticweb-group '
8+ public static function registerHooks (): void {
9+ add_action (
10+ 'admin_bar_menu ' ,
11+ [ self ::class, 'adminBarMenuHook ' ],
12+ 100
2013 );
21- $ wp_admin_bar ->add_group ( $ group );
22-
23- $ status = array (
24- 'id ' => 'staticweb-status ' ,
25- 'parent ' => 'staticweb-group ' ,
26- 'title ' => $ title
14+ add_action (
15+ 'wp_after_admin_bar_render ' ,
16+ [ self ::class, 'afterAdminBarRender ' ]
2717 );
28- $ wp_admin_bar ->add_node ( $ status );
29-
30- $ phpmyadmin = array (
31- 'id ' => 'staticweb-phpmyadmin ' ,
32- 'parent ' => 'staticweb-status ' ,
33- 'title ' => '<a href="/phpmyadmin/" target="_blank">phpMyAdmin</a> '
18+ $ hook_name = 'wp_ajax_ ' . Controller::getHookName ( 'job_queue ' );
19+ add_action (
20+ $ hook_name ,
21+ [ self ::class, 'ajaxJobQueue ' ]
3422 );
35- $ wp_admin_bar -> add_node ( $ phpmyadmin );
23+ }
3624
37- $ community = array (
38- 'id ' => 'staticweb-community ' ,
39- 'parent ' => 'staticweb-status ' ,
40- 'title ' => '<a href="https://staticword.press/c/staticweb-io-community/18" target="_blank">Community</a> '
41- );
42- $ wp_admin_bar ->add_node ( $ community );
25+ public static function adminBarMenuHook ( \WP_Admin_Bar $ wp_admin_bar ): void {
26+ $ deployment_url = Options::getValue ( 'deploymentURL ' );
4327
44- $ hosting = array (
45- 'id ' => 'staticweb-hosting ' ,
46- 'parent ' => 'staticweb-status ' ,
47- 'title ' => '<a href="https://staticweb.io/static-cloud-hosting/" target="_blank">Hosting</a> '
48- );
49- $ wp_admin_bar ->add_node ( $ hosting );
28+ $ title = '<div class="static-deploy-deploy-status-container" ' .
29+ 'style="border-radius: 5px; link-color: #fff;"> ' .
30+ '<a style="color: white" href=" ' . $ deployment_url .
31+ '" target="_blank"><div class="static-deploy-deploy-status" ' .
32+ 'style="margin: 0 5px">Static Deploy: Checking status...</div></a></div> ' ;
5033
51- $ news = array (
52- 'id ' => 'staticweb-news ' ,
53- 'parent ' => 'staticweb-status ' ,
54- 'title ' => '<a href="https://staticweb.io/news/" target="_blank">News</a> '
55- );
56- $ wp_admin_bar ->add_node ( $ news );
57-
58- $ staticweb_io = array (
59- 'id ' => 'staticweb-staticweb-io ' ,
60- 'parent ' => 'staticweb-status ' ,
61- 'title ' => '<a href="https://staticweb.io" target="_blank">StaticWeb.io</a> '
62- );
63- $ wp_admin_bar ->add_node ( $ staticweb_io );
34+ $ group = [
35+ 'id ' => 'static-deploy-group ' ,
36+ ];
37+ $ wp_admin_bar ->add_group ( $ group );
6438
65- $ wp2static = array (
66- 'id ' => 'staticweb-wp2static ' ,
67- 'parent ' => 'staticweb-status ' ,
68- 'title ' => '<a href="https://wp2static.com" target="_blank">WP2Static.com</a> '
69- );
70- $ wp_admin_bar ->add_node ( $ wp2static );
39+ $ node = [
40+ 'id ' => 'static-deploy-status ' ,
41+ 'parent ' => 'static-deploy-group ' ,
42+ 'title ' => $ title ,
43+ ];
44+ $ wp_admin_bar ->add_node ( $ node );
45+
46+ $ node = [
47+ 'id ' => 'static-deploy-github ' ,
48+ 'parent ' => 'static-deploy-status ' ,
49+ 'title ' => '<a href="https://github.com/staticweb-io/static-deploy" ' .
50+ ' target="_blank">GitHub</a> ' ,
51+ ];
52+ $ wp_admin_bar ->add_node ( $ node );
53+
54+ $ node = [
55+ 'id ' => 'static-deploy-news ' ,
56+ 'parent ' => 'static-deploy-status ' ,
57+ 'title ' => '<a href="https://staticweb.io/news/" target="_blank">News</a> ' ,
58+ ];
59+ $ wp_admin_bar ->add_node ( $ node );
60+
61+ $ node = [
62+ 'id ' => 'static-deploy-platform ' ,
63+ 'parent ' => 'static-deploy-status ' ,
64+ 'title ' => '<a href="https://staticweb.io/platform/" target="_blank">Platform</a> ' ,
65+ ];
66+ $ wp_admin_bar ->add_node ( $ node );
7167 }
7268
73- public static function after_admin_bar_render () : void {
74- ?>
69+ public static function afterAdminBarRender (): void {
70+ $ ajax_job_queue_url = Controller::getAdminAjaxUrl ( 'job_queue ' );
71+ ?>
7572 <script>
76- var staticweb_last_interval = 30000 ;
77- var staticweb_ajax_url = " <?php echo admin_url ( ' admin-ajax.php ' ); ?> " ;
78- var staticweb_job_type_labels = {
79- detect: "Detecting URLs",
80- crawl: "Crawling Site",
81- post_process: "Post-Processing",
82- deploy: "Deploying",
83- direct_deploy: "Deploying"
84- }
85- var staticweb_idle = false;
86-
87- function staticweb_update_status_button (text, bgcolor) {
88- document.querySelectorAll(".staticweb -deploy-status-container").forEach(el => {
73+ var static_deploy_job_queue_url = " <?php echo $ ajax_job_queue_url ; ?> " ;
74+ var static_deploy_last_interval = 30000 ;
75+ var static_deploy_job_type_labels = {
76+ detect: "Detecting URLs",
77+ crawl: "Crawling Site",
78+ post_process: "Post-Processing",
79+ deploy: "Deploying",
80+ direct_deploy: "Deploying (Direct) "
81+ };
82+ var static_deploy_idle = false;
83+
84+ function static_deploy_update_status_button (text, bgcolor) {
85+ document.querySelectorAll(".static-deploy -deploy-status-container").forEach(el => {
8986 el.style.backgroundColor = bgcolor;
9087 el.style.borderRadius = "5px";
9188 });
9289
93- document.querySelectorAll(".staticweb -deploy-status").forEach(el => {
94- el.textContent = "WP2Static : " + text;
90+ document.querySelectorAll(".static-deploy -deploy-status").forEach(el => {
91+ el.textContent = "Static Deploy : " + text;
9592 });
96- }
97-
98- function staticweb_check_idle () {
99- if ( staticweb_idle && document.visibilityState == 'visible' ) {
100- staticweb_update_status_button ('Checking status...', '');
101- staticweb_update_status ();
102- }
103- }
104-
105- function staticweb_update_status () {
106- if ( document.visibilityState != 'visible' ) {
107- staticweb_idle = true;
108- staticweb_last_interval = 30000;
109- setTimeout(staticweb_update_status , 30000);
110- staticweb_update_status_button ("Idle", "");
111- return;
112- }
113-
114- staticweb_idle = false;
115- fetch(staticweb_ajax_url + "?action=staticweb_job_queue" , {
93+ }
94+
95+ function static_deploy_check_idle () {
96+ if ( static_deploy_idle && document.visibilityState == 'visible' ) {
97+ static_deploy_update_status_button ('Checking status...', '');
98+ static_deploy_update_status ();
99+ }
100+ }
101+
102+ function static_deploy_update_status () {
103+ if ( document.visibilityState != 'visible' ) {
104+ static_deploy_idle = true;
105+ static_deploy_last_interval = 30000;
106+ setTimeout(static_deploy_update_status , 30000);
107+ static_deploy_update_status_button ("Idle", "");
108+ return;
109+ }
110+
111+ static_deploy_idle = false;
112+ fetch(static_deploy_job_queue_url , {
116113 method: "GET",
117114 })
118115 .then(response => response.json())
119116 .then(data => {
120- staticweb_last_interval = 30000;
121- setTimeout(staticweb_update_status , 30000);
117+ static_deploy_last_interval = 30000;
118+ setTimeout(static_deploy_update_status , 30000);
122119
123120 let bgcolor = "";
124121 let text;
@@ -135,93 +132,97 @@ function staticweb_update_status() {
135132 text = "Queued";
136133 }
137134 } else {
138- text = staticweb_job_type_labels [data.jobs[0].job_type];
135+ text = static_deploy_job_type_labels [data.jobs[0].job_type];
139136 }
140137
141- staticweb_update_status_button (text, bgcolor);
138+ static_deploy_update_status_button (text, bgcolor);
142139 })
143140 .catch(error => {
144141 console.error(error);
145- staticweb_last_interval *= 2;
146- setTimeout(staticweb_update_status, staticweb_last_interval );
142+ static_deploy_last_interval *= 2;
143+ setTimeout(static_deploy_update_status, static_deploy_last_interval );
147144 });
148- }
145+ }
149146
150- window.onload = (event) => {
151- setInterval(staticweb_check_idle , 1000);
152- setTimeout(staticweb_update_status , 100);
153- };
147+ window.onload = (event) => {
148+ setInterval(static_deploy_check_idle , 1000);
149+ setTimeout(static_deploy_update_status , 100);
150+ };
154151 </script>
155- <?php
156- }
152+ <?php
153+ }
157154
158- public static function ajax_staticweb_job_queue () : void {
159- $ job_count = \ WP2Static \ JobQueue::getWaitingJobs ();
160- $ jobs = self ::get_jobs_in_progress ();
161- $ invalidations = self ::list_invalidations_in_progress ();
162- if ($ invalidations
163- && array_key_exists ( 'Invalidations ' , $ invalidations )
155+ public static function ajaxJobQueue () : void {
156+ $ job_count = JobQueue::getWaitingJobs ();
157+ $ jobs = self ::getJobsInProgress ();
158+ $ invalidations = self ::listInvalidationsInProgress ();
159+ if ( $ invalidations
160+ && array_key_exists ( 'Invalidations ' , $ invalidations )
164161 && 0 < count ( $ invalidations ['Invalidations ' ] ) ) {
165- $ in_progress = true ;
166- } else {
167- $ in_progress = false ;
168- }
169- $ arr = ['invalidations ' => $ in_progress ,
162+ $ in_progress = true ;
163+ } else {
164+ $ in_progress = false ;
165+ }
166+ $ arr = [
167+ 'invalidations ' => $ in_progress ,
170168 'job_count ' => $ job_count ,
171- 'jobs ' => $ jobs ];
172- echo ( json_encode ( $ arr ) );
173- die ();
174- }
169+ 'jobs ' => $ jobs ,
170+ ];
171+ echo ( json_encode ( $ arr ) );
172+ die ();
173+ }
175174
176- public static function get_jobs_in_progress () : array {
177- global $ wpdb ;
178- $ jobs = [];
175+ public static function getJobsInProgress () : array {
176+ global $ wpdb ;
177+ $ jobs = [];
179178
180- $ table_name = $ wpdb -> prefix . ' wp2static_jobs ' ;
179+ $ table_name = JobQueue:: getTableName () ;
181180
182- $ jobs_in_progress = $ wpdb ->get_results (
183- "SELECT * FROM $ table_name
181+ $ jobs_in_progress = $ wpdb ->get_results (
182+ "SELECT * FROM $ table_name
184183 WHERE status = 'processing' "
185- );
184+ );
186185
187- return $ jobs_in_progress ;
188- }
186+ return $ jobs_in_progress ;
187+ }
189188
190- public static function list_invalidations ( int $ max_items = 5 ) {
191- $ cloudfront = \ WP2StaticS3 \Deployer::cloudfrontClient ();
192- $ distribution_id = \ WP2StaticS3 \Controller ::getValue ( 'cfDistributionID ' );
189+ public static function listInvalidations ( int $ max_items = 5 ) {
190+ $ cloudfront = S3 \Deployer::cloudfrontClient ();
191+ $ distribution_id = S3 \S3Options ::getValue ( 'distributionId ' );
193192
194- if ( ! $ distribution_id ) {
195- return ;
196- }
193+ if ( ! $ distribution_id ) {
194+ return ;
195+ }
197196
198- try {
199- return $ cloudfront ->listInvalidations (
200- ['DistributionId ' => $ distribution_id ,
201- 'MaxItems ' => "$ max_items " ] );
202- } catch ( AwsException $ e ) {
203- return $ e ;
197+ try {
198+ return $ cloudfront ->listInvalidations (
199+ [
200+ 'DistributionId ' => $ distribution_id ,
201+ 'MaxItems ' => "$ max_items " ,
202+ ]
203+ );
204+ } catch ( AwsException $ e ) {
205+ return $ e ;
206+ }
204207 }
205- }
206208
207- public static function list_invalidations_in_progress ( int $ max_items = 5 ) {
208- $ invalidations = self ::list_invalidations ( $ max_items );
209+ public static function listInvalidationsInProgress ( int $ max_items = 5 ) {
210+ $ invalidations = self ::listInvalidations ( $ max_items );
209211
210- if ( ! $ invalidations ) {
211- return ;
212- } else if ( is_a ( $ invalidations , 'Aws\Exception\AwsException ' ) ) {
213- return ['Exception ' => $ invalidations ];
214- } else {
215- $ inv_items = $ invalidations ['InvalidationList ' ]['Items ' ];
216-
217- $ arr = [];
218- foreach ( $ inv_items as $ inv ) {
219- if ( "InProgress " === $ inv ['Status ' ] ) {
220- array_push ( $ arr , $ inv );
212+ if ( ! $ invalidations ) {
213+ return ;
214+ } elseif ( is_a ( $ invalidations , 'Aws\Exception\AwsException ' ) ) {
215+ return [ 'Exception ' => $ invalidations ];
216+ } else {
217+ $ inv_items = $ invalidations ['InvalidationList ' ]['Items ' ];
218+
219+ $ arr = [];
220+ foreach ( $ inv_items as $ inv ) {
221+ if ( 'InProgress ' === $ inv ['Status ' ] ) {
222+ array_push ( $ arr , $ inv );
223+ }
221224 }
225+ return [ 'Invalidations ' => $ arr ];
222226 }
223- return ['Invalidations ' => $ arr ];
224227 }
225228}
226-
227- }
0 commit comments