| 1 |
/* global metasyncConfigData, jQuery */ |
| 2 |
/** |
| 3 |
* MetaSync Config & Admin Bar Status Sync |
| 4 |
* |
| 5 |
* Extracted for Phase 5, #887 — Part B JS extraction. |
| 6 |
* Sets window.MetasyncConfig from localized data and keeps the admin-bar |
| 7 |
* sync status indicator in sync with the plugin page status. |
| 8 |
* |
| 9 |
* Localized data object: metasyncConfigData |
| 10 |
* - pluginName (string) — effective plugin display name |
| 11 |
* - ottoName (string) — whitelabel OTTO display name |
| 12 |
* |
| 13 |
* @since Phase 5 |
| 14 |
* @see wp_localize_script() call in class-metasync-admin.php |
| 15 |
*/ |
| 16 |
|
| 17 |
// Pass localized variables to global config |
| 18 |
window.MetasyncConfig = { |
| 19 |
pluginName: metasyncConfigData.pluginName, |
| 20 |
ottoName: metasyncConfigData.ottoName |
| 21 |
}; |
| 22 |
|
| 23 |
jQuery(document).ready(function($) { |
| 24 |
|
| 25 |
// Function to sync admin bar status |
| 26 |
function syncAdminBarStatus() { |
| 27 |
var pluginPageStatus = $('.metasync-integration-status .status-text').text(); |
| 28 |
var adminBarItem = $('#wp-admin-bar-searchatlas-status .ab-item'); |
| 29 |
var adminBarContainer = $('#wp-admin-bar-searchatlas-status'); |
| 30 |
var pluginName = window.MetasyncConfig.pluginName; |
| 31 |
|
| 32 |
if (pluginPageStatus && adminBarItem.length) { |
| 33 |
var allClasses = 'searchatlas-synced searchatlas-not-synced searchatlas-warning'; |
| 34 |
|
| 35 |
// Helper to update emoji in admin bar |
| 36 |
var updateAdminBarEmoji = function(targetEmoji, targetSvgCode) { |
| 37 |
var emojiImg = adminBarItem.find('img.emoji'); |
| 38 |
if (emojiImg.length > 0) { |
| 39 |
emojiImg.attr('alt', targetEmoji); |
| 40 |
var currentSrc = emojiImg.attr('src'); |
| 41 |
var updatedSrc = currentSrc.replace(/1f7e2\.svg|1f534\.svg|1f7e1\.svg/, targetSvgCode + '.svg'); |
| 42 |
emojiImg.attr('src', updatedSrc); |
| 43 |
} else { |
| 44 |
var newHtml = adminBarItem.html().replace(/\uD83D\uDFE2|\uD83D\uDD34|\uD83D\uDFE1/, targetEmoji); |
| 45 |
if (!newHtml.includes(targetEmoji) && newHtml.includes(pluginName)) { |
| 46 |
newHtml = newHtml.replace(pluginName, pluginName + ' ' + targetEmoji); |
| 47 |
} |
| 48 |
adminBarItem.html(newHtml); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
if (pluginPageStatus.includes('Synced') && !pluginPageStatus.includes('Not Synced')) { |
| 53 |
// Update admin bar to synced (GREEN) |
| 54 |
updateAdminBarEmoji('\uD83D\uDFE2', '1f7e2'); |
| 55 |
adminBarContainer.removeClass(allClasses).addClass('searchatlas-synced'); |
| 56 |
var syncTitle = pluginName + ' - Synced (Heartbeat API connectivity verified)'; |
| 57 |
adminBarContainer.attr('title', syncTitle); |
| 58 |
adminBarItem.attr('title', syncTitle); |
| 59 |
|
| 60 |
} else if (pluginPageStatus.includes('Warning')) { |
| 61 |
// Update admin bar to warning (YELLOW) |
| 62 |
updateAdminBarEmoji('\uD83D\uDFE1', '1f7e1'); |
| 63 |
adminBarContainer.removeClass(allClasses).addClass('searchatlas-warning'); |
| 64 |
var warnTitle = pluginName + ' - Connected but OTTO UUID is missing \u2014 deploys will not work. Please reconnect.'; |
| 65 |
adminBarContainer.attr('title', warnTitle); |
| 66 |
adminBarItem.attr('title', warnTitle); |
| 67 |
|
| 68 |
} else if (pluginPageStatus.includes('Not Synced')) { |
| 69 |
// Update admin bar to not synced (RED) |
| 70 |
updateAdminBarEmoji('\uD83D\uDD34', '1f534'); |
| 71 |
adminBarContainer.removeClass(allClasses).addClass('searchatlas-not-synced'); |
| 72 |
var notSyncTitle = pluginName + ' - Not Synced (Heartbeat API not responding or unreachable)'; |
| 73 |
adminBarContainer.attr('title', notSyncTitle); |
| 74 |
adminBarItem.attr('title', notSyncTitle); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Sync when tabs are switched (for General/Advanced tabs) |
| 80 |
$(document).on('click', 'a[href*="tab="]', function() { |
| 81 |
setTimeout(syncAdminBarStatus, 200); |
| 82 |
}); |
| 83 |
|
| 84 |
// Also check every 5 seconds to keep it in sync |
| 85 |
setInterval(syncAdminBarStatus, 5000); |
| 86 |
}); |
| 87 |
|