|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace StaticWeb; |
| 4 | + |
| 5 | +use Aws\Exception\AwsException; |
| 6 | + |
| 7 | +class 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' |
| 20 | + ); |
| 21 | + $wp_admin_bar->add_group( $group ); |
| 22 | + |
| 23 | + $status = array( |
| 24 | + 'id' => 'staticweb-status', |
| 25 | + 'parent' => 'staticweb-group', |
| 26 | + 'title' => $title |
| 27 | + ); |
| 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>' |
| 34 | + ); |
| 35 | + $wp_admin_bar->add_node( $phpmyadmin ); |
| 36 | + |
| 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 ); |
| 43 | + |
| 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 ); |
| 50 | + |
| 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 ); |
| 64 | + |
| 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 ); |
| 71 | + } |
| 72 | + |
| 73 | + public static function after_admin_bar_render() : void { |
| 74 | +?> |
| 75 | + <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 => { |
| 89 | + el.style.backgroundColor = bgcolor; |
| 90 | + el.style.borderRadius = "5px"; |
| 91 | + }); |
| 92 | + |
| 93 | + document.querySelectorAll(".staticweb-deploy-status").forEach(el => { |
| 94 | + el.textContent = "WP2Static: " + text; |
| 95 | + }); |
| 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", { |
| 116 | + method: "GET", |
| 117 | + }) |
| 118 | + .then(response => response.json()) |
| 119 | + .then(data => { |
| 120 | + staticweb_last_interval = 30000; |
| 121 | + setTimeout(staticweb_update_status, 30000); |
| 122 | + |
| 123 | + let bgcolor = ""; |
| 124 | + let text; |
| 125 | + |
| 126 | + if (data.jobs.length === 0) { |
| 127 | + if (data.job_count === 0) { |
| 128 | + if (data.invalidations) { |
| 129 | + text = "Refreshing CDN cache"; |
| 130 | + } else { |
| 131 | + bgcolor = "green"; |
| 132 | + text = "Deployed"; |
| 133 | + } |
| 134 | + } else { |
| 135 | + text = "Queued"; |
| 136 | + } |
| 137 | + } else { |
| 138 | + text = staticweb_job_type_labels[data.jobs[0].job_type]; |
| 139 | + } |
| 140 | + |
| 141 | + staticweb_update_status_button(text, bgcolor); |
| 142 | + }) |
| 143 | + .catch(error => { |
| 144 | + console.error(error); |
| 145 | + staticweb_last_interval *= 2; |
| 146 | + setTimeout(staticweb_update_status, staticweb_last_interval); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + window.onload = (event) => { |
| 151 | + setInterval(staticweb_check_idle, 1000); |
| 152 | + setTimeout(staticweb_update_status, 100); |
| 153 | + }; |
| 154 | + </script> |
| 155 | +<?php |
| 156 | +} |
| 157 | + |
| 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) |
| 164 | + && 0 < count( $invalidations['Invalidations'] ) ) { |
| 165 | + $in_progress = true; |
| 166 | + } else { |
| 167 | + $in_progress = false; |
| 168 | + } |
| 169 | + $arr = ['invalidations' => $in_progress, |
| 170 | + 'job_count' => $job_count, |
| 171 | + 'jobs' => $jobs]; |
| 172 | + echo( json_encode ( $arr ) ); |
| 173 | + die(); |
| 174 | +} |
| 175 | + |
| 176 | +public static function get_jobs_in_progress() : array { |
| 177 | + global $wpdb; |
| 178 | + $jobs = []; |
| 179 | + |
| 180 | + $table_name = $wpdb->prefix . 'wp2static_jobs'; |
| 181 | + |
| 182 | + $jobs_in_progress = $wpdb->get_results( |
| 183 | + "SELECT * FROM $table_name |
| 184 | + WHERE status = 'processing'" |
| 185 | + ); |
| 186 | + |
| 187 | + return $jobs_in_progress; |
| 188 | +} |
| 189 | + |
| 190 | +public static function list_invalidations( int $max_items = 5) { |
| 191 | + $cloudfront = \WP2StaticS3\Deployer::cloudfrontClient(); |
| 192 | + $distribution_id = \WP2StaticS3\Controller::getValue( 'cfDistributionID' ); |
| 193 | + |
| 194 | + if ( ! $distribution_id ) { |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + try { |
| 199 | + return $cloudfront->listInvalidations( |
| 200 | + ['DistributionId' => $distribution_id, |
| 201 | + 'MaxItems' => "$max_items"] ); |
| 202 | + } catch ( AwsException $e ) { |
| 203 | + return $e; |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +public static function list_invalidations_in_progress( int $max_items = 5) { |
| 208 | + $invalidations = self::list_invalidations( $max_items ); |
| 209 | + |
| 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 ); |
| 221 | + } |
| 222 | + } |
| 223 | + return ['Invalidations' => $arr]; |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +} |
0 commit comments