Add eg-sharebar.php v5.6.1 (production)
This commit is contained in:
parent
bdfe2bcbee
commit
52bb3ac3ab
|
|
@ -0,0 +1,268 @@
|
|||
<?php
|
||||
/*
|
||||
Plugin Name: EG Sharebar
|
||||
Description: Barra social personalizzata richiamabile via funzione PHP.
|
||||
Author: Emanuele Gori
|
||||
Version: 5.6.1
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
// ✅ FLAG DEBUG: Cambia a true per attivare i log in console
|
||||
define('EG_SHAREBAR_DEBUG', false);
|
||||
|
||||
function eg_sharebar_render() {
|
||||
$titolo = get_the_title();
|
||||
$url = get_permalink();
|
||||
|
||||
$default_instance = 'mastodon.uno';
|
||||
|
||||
// Firma personalizzata per ogni social
|
||||
$signature_masto = ' via @emanuelegori@emanuelegori.uno';
|
||||
$signature_x = ' via @40annibuttati';
|
||||
$signature_bluesky = ' via @emanuelegori@emanuelegori.uno';
|
||||
$signature_telegram = ' via emanuelegori.uno';
|
||||
|
||||
$premessa = 'Mi è piaciuto ';
|
||||
|
||||
$share_text_mastodon = $premessa . $titolo . ' ' . $url . $signature_masto;
|
||||
$share_text_x = $premessa . $titolo . ' ' . $url . $signature_x;
|
||||
$share_text_bluesky = $premessa . $titolo . ' ' . $url . $signature_bluesky;
|
||||
$share_text_telegram = $premessa . $titolo . ' ' . $url . $signature_telegram;
|
||||
|
||||
$instances = [
|
||||
'mastodon.uno' => 'mastodon.uno',
|
||||
'sociale.network' => 'sociale.network',
|
||||
'mastodon.social' => 'mastodon.social',
|
||||
'mastodon.art' => 'mastodon.art',
|
||||
'livellosecreto.it' => 'livellosecreto.it',
|
||||
];
|
||||
|
||||
$icons_url = plugins_url('social-icons/', __FILE__);
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<div class="eg-condividi-label" style="font-weight:600; margin-top:32px; margin-bottom:12px; font-size:1.1em; color:#222;">
|
||||
Ti è piaciuto l'articolo? Condividilo !!
|
||||
</div>
|
||||
<div class="eg-sharebar"
|
||||
data-default-instance="<?php echo esc_attr($default_instance); ?>"
|
||||
data-share-text="<?php echo esc_attr($share_text_mastodon); ?>"
|
||||
style="display:flex; gap:14px; flex-wrap:wrap; justify-content:center;">
|
||||
<div class="eg-masto-col" style="display:flex; flex-direction:column; align-items:center; margin-right:12px;">
|
||||
<a id="eg-masto-btn" class="share-btn mastodon" href="<?php echo esc_url("https://{$default_instance}/share?text=" . urlencode($share_text_mastodon)); ?>"
|
||||
target="_blank" rel="noopener" aria-label="Condividi su Mastodon" data-tooltip="Condividi su Mastodon">
|
||||
<img class="icon" src="<?php echo esc_url($icons_url . 'mastodon.svg'); ?>" alt="Mastodon" />
|
||||
<span id="eg-masto-label">Mastodon</span>
|
||||
</a>
|
||||
<select id="eg-masto-select" class="masto-select" style="margin-top:4px; margin-bottom:4px; background:rgba(99,100,255,0.07); border:1.5px dashed #6364ff77; border-radius:8px; color:#6364ff;" aria-label="Seleziona istanza Mastodon">
|
||||
<?php foreach ($instances as $urlist => $label): ?>
|
||||
<option value="<?php echo esc_attr($urlist); ?>"><?php echo esc_html($label); ?></option>
|
||||
<?php endforeach; ?>
|
||||
<option value="other">Altra istanza...</option>
|
||||
</select>
|
||||
<input id="eg-masto-custom" class="masto-input" type="text" placeholder="esc per uscire"
|
||||
style="width:100%; display:none; margin-bottom:7px; padding:7px 12px; border-radius:8px; border:1.5px solid #6364ff33; font-size:1.1em; text-align:center;" aria-label="Inserisci istanza personalizzata"/>
|
||||
</div>
|
||||
<a class="share-btn x" href="<?php echo esc_url('https://x.com/intent/tweet?text=' . urlencode($share_text_x)); ?>" target="_blank" rel="noopener" aria-label="Condividi su X " data-tooltip="Condividi su X">
|
||||
<img class="icon" src="<?php echo esc_url($icons_url . 'x.svg'); ?>" alt="X" />
|
||||
X
|
||||
</a>
|
||||
<a class="share-btn bluesky" href="<?php echo esc_url('https://bsky.app/intent/compose?text=' . urlencode($share_text_bluesky)); ?>" target="_blank" rel="noopener" aria-label="Condividi su Bluesky" data-tooltip="Condividi su Bluesky">
|
||||
<img class="icon" src="<?php echo esc_url($icons_url . 'bluesky.svg'); ?>" alt="Bluesky" />
|
||||
Bluesky
|
||||
</a>
|
||||
<a class="share-btn telegram" href="<?php echo esc_url('https://t.me/share/url?url=' . urlencode($url) . '&text=' . urlencode($titolo . $signature_telegram)); ?>" target="_blank" rel="noopener" aria-label="Condividi su Telegram" data-tooltip="Condividi su Telegram">
|
||||
<img class="icon" src="<?php echo esc_url($icons_url . 'telegram.svg'); ?>" alt="Telegram" />
|
||||
Telegram
|
||||
</a>
|
||||
<button class="share-btn link" onclick="egCopyLink('<?php echo esc_js($url); ?>')" aria-label="Copia link" data-tooltip="Copia link">
|
||||
<img class="icon" src="<?php echo esc_url($icons_url . 'link.svg'); ?>" alt="Link" />
|
||||
Copia Link
|
||||
</button>
|
||||
</div>
|
||||
<?php
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
function eg_sharebar() { echo eg_sharebar_render(); }
|
||||
add_shortcode('eg_sharebar', 'eg_sharebar_render');
|
||||
|
||||
// Carica CSS
|
||||
add_action('wp_enqueue_scripts', function() {
|
||||
if (is_singular()) {
|
||||
wp_register_style('eg-sharebar-css', false);
|
||||
wp_enqueue_style('eg-sharebar-css');
|
||||
$css_file = plugin_dir_path(__FILE__).'sharebar-style.css';
|
||||
if (file_exists($css_file)) {
|
||||
wp_add_inline_style('eg-sharebar-css', file_get_contents($css_file));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Carica JavaScript GLOBALMENTE nel footer
|
||||
add_action('wp_footer', function() {
|
||||
if (!is_singular()) return;
|
||||
|
||||
$debug = EG_SHAREBAR_DEBUG ? 'true' : 'false';
|
||||
?>
|
||||
<script>
|
||||
window.egInitSharebar = function() {
|
||||
var DEBUG = <?php echo $debug; ?>;
|
||||
function log() { if (DEBUG) console.log.apply(console, arguments); }
|
||||
function warn() { if (DEBUG) console.warn.apply(console, arguments); }
|
||||
function error() { if (DEBUG) console.error.apply(console, arguments); }
|
||||
|
||||
log('🔧 egInitSharebar: inizializzazione');
|
||||
|
||||
function egSetInstance(val) {
|
||||
try {
|
||||
localStorage.setItem("egMastoInstance", val);
|
||||
log('💾 localStorage salvato:', val);
|
||||
} catch(e){ error('❌ Errore localStorage:', e); }
|
||||
}
|
||||
|
||||
function egGetInstance() {
|
||||
try {
|
||||
var val = localStorage.getItem("egMastoInstance");
|
||||
log('📖 localStorage letto:', val);
|
||||
return val;
|
||||
} catch(e) {
|
||||
error('❌ Errore lettura localStorage:', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const sharebarContainer = document.querySelector('.eg-sharebar');
|
||||
if (!sharebarContainer) {
|
||||
warn('⚠️ Container .eg-sharebar non trovato');
|
||||
return;
|
||||
}
|
||||
|
||||
const mastoSelect = document.getElementById("eg-masto-select");
|
||||
const mastoCustom = document.getElementById("eg-masto-custom");
|
||||
const mastoBtn = document.getElementById("eg-masto-btn");
|
||||
const defaultInstance = sharebarContainer.getAttribute('data-default-instance') || 'mastodon.uno';
|
||||
const shareTextMasto = sharebarContainer.getAttribute('data-share-text') || '';
|
||||
|
||||
if (!mastoSelect || !mastoCustom || !mastoBtn) {
|
||||
error('❌ EG Sharebar: elementi non trovati!');
|
||||
return;
|
||||
}
|
||||
|
||||
let currentInstance = egGetInstance() || defaultInstance;
|
||||
let customOption = null;
|
||||
|
||||
function updateMastoBtn() {
|
||||
var newHref = "https://" + currentInstance + "/share?text=" + encodeURIComponent(shareTextMasto);
|
||||
mastoBtn.href = newHref;
|
||||
log('🔗 Href aggiornato:', newHref);
|
||||
}
|
||||
|
||||
function setSelectValue(val) {
|
||||
let found = false;
|
||||
for (let i = 0; i < mastoSelect.options.length; i++) {
|
||||
if (mastoSelect.options[i].value === val) {
|
||||
mastoSelect.selectedIndex = i;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
addOrUpdateCustomOption(val);
|
||||
mastoSelect.value = val;
|
||||
}
|
||||
}
|
||||
|
||||
function addOrUpdateCustomOption(val) {
|
||||
if (customOption) {
|
||||
customOption.value = val;
|
||||
customOption.text = val;
|
||||
} else {
|
||||
customOption = document.createElement("option");
|
||||
customOption.value = val;
|
||||
customOption.text = val;
|
||||
let otherOption = mastoSelect.querySelector('option[value="other"]');
|
||||
mastoSelect.insertBefore(customOption, otherOption);
|
||||
}
|
||||
}
|
||||
|
||||
mastoSelect.addEventListener('change', function() {
|
||||
log('🔄 Select cambiato:', this.value);
|
||||
|
||||
if (this.value === "other") {
|
||||
mastoCustom.value = "";
|
||||
mastoCustom.style.display = "block";
|
||||
mastoCustom.focus();
|
||||
mastoSelect.style.display = "none";
|
||||
} else {
|
||||
currentInstance = this.value;
|
||||
egSetInstance(currentInstance);
|
||||
updateMastoBtn();
|
||||
}
|
||||
});
|
||||
|
||||
mastoCustom.addEventListener('keydown', function(e) {
|
||||
if (e.key === "Enter") {
|
||||
let val = mastoCustom.value.trim();
|
||||
|
||||
if (!val.match(/^[a-z0-9.-]+\.[a-z]+$/i)) {
|
||||
alert("Inserisci un dominio valido (es. mastodon.social)");
|
||||
return;
|
||||
}
|
||||
currentInstance = val;
|
||||
egSetInstance(val);
|
||||
updateMastoBtn();
|
||||
mastoCustom.style.display = "none";
|
||||
mastoSelect.style.display = "block";
|
||||
addOrUpdateCustomOption(val);
|
||||
setSelectValue(val);
|
||||
} else if (e.key === "Escape") {
|
||||
mastoCustom.style.display = "none";
|
||||
mastoSelect.style.display = "block";
|
||||
setSelectValue(currentInstance);
|
||||
}
|
||||
});
|
||||
|
||||
mastoCustom.addEventListener('blur', function() {
|
||||
setTimeout(function() {
|
||||
if (mastoCustom.style.display === "block") {
|
||||
mastoCustom.style.display = "none";
|
||||
mastoSelect.style.display = "block";
|
||||
setSelectValue(currentInstance);
|
||||
}
|
||||
}, 200);
|
||||
});
|
||||
|
||||
setSelectValue(currentInstance);
|
||||
updateMastoBtn();
|
||||
|
||||
// Gestione tooltip
|
||||
const shareButtons = document.querySelectorAll('.eg-sharebar .share-btn');
|
||||
shareButtons.forEach(function(btn) {
|
||||
btn.addEventListener('click', function() { this.blur(); });
|
||||
btn.addEventListener('touchend', function() { this.blur(); });
|
||||
});
|
||||
|
||||
log('✅ EG Sharebar inizializzata');
|
||||
};
|
||||
|
||||
window.egCopyLink = function(link) {
|
||||
navigator.clipboard.writeText(link);
|
||||
alert("Link copiato!");
|
||||
};
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
if (document.querySelector('.eg-sharebar')) {
|
||||
window.egInitSharebar();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (document.querySelector('.eg-sharebar')) {
|
||||
window.egInitSharebar();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<?php
|
||||
}, 98);
|
||||
Loading…
Reference in New Issue