admin-bar-notice.js
1 year ago
class-admin-bar-notice.php
6 months ago
class-admin-sidebar-link.php
4 months ago
scan.php
7 months ago
admin-bar-notice.js
53 lines
| 1 | ( function ( localized ) { |
| 2 | function ready( fn ) { |
| 3 | if ( document.readyState !== 'loading' ) { |
| 4 | fn(); |
| 5 | } else { |
| 6 | document.addEventListener( 'DOMContentLoaded', fn ); |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | function fetch_scan_treats_and_add_link() { |
| 11 | var xhrRequest = new XMLHttpRequest(); |
| 12 | xhrRequest.open( 'GET', localized.scan_endpoint, true ); |
| 13 | xhrRequest.onload = function () { |
| 14 | if ( this.status === 200 ) { |
| 15 | // Success! |
| 16 | var body = JSON.parse( this.response ); |
| 17 | if ( body && body.data ) { |
| 18 | var apiResponse = JSON.parse( body.data ); |
| 19 | var numberOfThreats = |
| 20 | apiResponse.threats && apiResponse.threats.length ? apiResponse.threats.length : 0; |
| 21 | update_threats_link( numberOfThreats ); |
| 22 | } else { |
| 23 | update_threats_link( 0 ); |
| 24 | } |
| 25 | } else { |
| 26 | update_threats_link( 0 ); |
| 27 | } |
| 28 | }; |
| 29 | xhrRequest.setRequestHeader( 'X-WP-Nonce', localized.nonce ); |
| 30 | xhrRequest.send(); |
| 31 | } |
| 32 | |
| 33 | ready( function () { |
| 34 | fetch_scan_treats_and_add_link(); |
| 35 | } ); |
| 36 | |
| 37 | function update_threats_link( numberOfThreats ) { |
| 38 | var element = document.getElementById( 'wp-admin-bar-jetpack-scan-notice' ); |
| 39 | if ( ! element ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if ( ! numberOfThreats ) { |
| 44 | element.parentNode.removeChild( element ); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | var textLabel = numberOfThreats === 1 ? localized.singular : localized.multiple; |
| 49 | element.innerHTML = |
| 50 | '<a href="' + localized.scan_dashboard_url + '" class="ab-item">' + textLabel + '</a>'; |
| 51 | } |
| 52 | } )( window.Jetpack_Scan ); |
| 53 |