<?php
/*** Conditionally Display Dashboard ***/
// Check if the current user is an admin or the author of the current post
$is_admin_or_author = current_user_can('administrator') || get_post_field('post_author', get_the_ID()) == get_current_user_id();
// Now, you need to output JavaScript based on the condition
if ($is_admin_or_author) {
// The user is an admin or the author; output JavaScript to remove the .campaign-display elements
echo <<<HTML
<script>
(function() {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) {
// Check if the added node matches the .campaign-display selector
if (node.matches('.campaign-display')) {
node.parentNode.removeChild(node);
}
// Also check for any child nodes that match the selector
node.querySelectorAll('.campaign-display').forEach(function(childNode) {
childNode.parentNode.removeChild(childNode);
});
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Optional: Disconnect the observer after a certain condition to stop watching for changes
setTimeout(function() {
observer.disconnect();
}, 10000); // Adjust the time as needed
})();
</script>
HTML;
} else {
// For non-admins and non-authors, you can keep the previous logic or add any other required logic here.
echo <<<HTML
<script>
(function() {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) {
// Check if the added node matches the .campaign-dashboard selector
if (node.matches('.campaign-dashboard')) {
node.parentNode.removeChild(node);
}
// Also check for any child nodes that match the selector
node.querySelectorAll('.campaign-dashboard').forEach(function(childNode) {
childNode.parentNode.removeChild(childNode);
});
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Optional: Disconnect the observer after a certain condition to stop watching for changes
setTimeout(function() {
observer.disconnect();
}, 10000); // Adjust the time as needed
})();
</script>
HTML;
}
/*** Campaigns View Counter ***/
if ( get_post_type() === 'campaigns' ) {
// Update view count within the lock duration
$meta_values = get_post_meta( get_the_ID(), "campaign-views", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'campaign-views', $v2);
}
?>