117 lines
4.1 KiB
PHP
117 lines
4.1 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Fediverso Box
|
|
Description: Mostra un box per seguire l'autore nel Fediverso.
|
|
Version: 2.1
|
|
Author: Emanuele Gori
|
|
Text Domain: fediverso-box
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
// FLAG DEBUG: Cambia a true per attivare i log in console
|
|
define('FEDIVERSO_BOX_DEBUG', false);
|
|
|
|
// Carica solo il CSS
|
|
add_action('wp_enqueue_scripts', function() {
|
|
if (is_singular()) {
|
|
wp_enqueue_style('fediverso-box-style', plugin_dir_url(__FILE__) . 'fediverso-style.css');
|
|
}
|
|
});
|
|
|
|
// Funzione richiamabile sia da shortcode che da PHP
|
|
if (!function_exists('fediverso_box')) {
|
|
function fediverso_box($atts = array()) {
|
|
$author_handle = 'emanuelegori@emanuelegori.uno';
|
|
|
|
ob_start();
|
|
?>
|
|
<div class="mastodon-box" data-author-handle="<?php echo esc_attr($author_handle); ?>">
|
|
<p>
|
|
🇮🇹 <?php echo esc_html__('Ricevi i prossimi articoli direttamente nel tuo feed (i Seguiti) del Fediverso.', 'fediverso-box'); ?>
|
|
(<a href="https://fediverso.info/" target="_blank"><?php echo esc_html__('Non conosci il Fediverso?', 'fediverso-box'); ?></a>)<br>
|
|
</p>
|
|
<form id="fediFollowForm" autocomplete="off">
|
|
<input type="text" id="fediUserInput" placeholder="Istanza es. mastodon.uno">
|
|
<button type="submit" id="fediFollowBtn">Segui</button>
|
|
</form>
|
|
<small>
|
|
<?php echo esc_html__('Non hai un account?', 'fediverso-box'); ?>
|
|
<a href="https://mastodon.uno/auth/sign_up" target="_blank"><?php echo esc_html__('Crea un profilo gratuito', 'fediverso-box'); ?></a>
|
|
</small>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
}
|
|
|
|
// Shortcode
|
|
add_shortcode('fediverso_box', function($atts) {
|
|
return fediverso_box($atts);
|
|
});
|
|
|
|
// Carica JavaScript GLOBALMENTE nel footer
|
|
add_action('wp_footer', function() {
|
|
if (!is_singular()) return;
|
|
|
|
$debug = FEDIVERSO_BOX_DEBUG ? 'true' : 'false';
|
|
?>
|
|
<script>
|
|
window.initFediversoBox = function() {
|
|
var DEBUG = <?php echo $debug; ?>;
|
|
function log() { if (DEBUG) console.log.apply(console, arguments); }
|
|
function warn() { if (DEBUG) console.warn.apply(console, arguments); }
|
|
|
|
log('🔧 initFediversoBox: inizializzazione');
|
|
|
|
var form = document.getElementById('fediFollowForm');
|
|
var input = document.getElementById('fediUserInput');
|
|
|
|
if (!form || !input) {
|
|
warn('Fediverso Box: elementi non trovati');
|
|
return;
|
|
}
|
|
|
|
// Rimuovi eventuali listener precedenti
|
|
var newForm = form.cloneNode(true);
|
|
form.parentNode.replaceChild(newForm, form);
|
|
form = newForm;
|
|
input = document.getElementById('fediUserInput');
|
|
|
|
form.onsubmit = function(e) {
|
|
e.preventDefault();
|
|
|
|
var userInput = input.value.trim().toLowerCase();
|
|
|
|
// Permetti solo il dominio dell'istanza
|
|
var match = userInput.match(/^[a-z0-9.-]+\.[a-z]{2,}$/i);
|
|
if (!match) {
|
|
alert('Inserisci solo il dominio della tua istanza Mastodon (es: mastodon.uno)');
|
|
return false;
|
|
}
|
|
|
|
var authorHandle = document.querySelector('.mastodon-box').getAttribute('data-author-handle') || 'emanuelegori@emanuelegori.uno';
|
|
var url = 'https://' + userInput + '/authorize_interaction?uri=' + encodeURIComponent(authorHandle);
|
|
|
|
log('🚀 Apertura URL:', url);
|
|
window.open(url, '_blank');
|
|
return false;
|
|
};
|
|
|
|
log('Fediverso Box inizializzata');
|
|
};
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
if (document.querySelector('.mastodon-box')) {
|
|
window.initFediversoBox();
|
|
}
|
|
});
|
|
} else {
|
|
if (document.querySelector('.mastodon-box')) {
|
|
window.initFediversoBox();
|
|
}
|
|
}
|
|
</script>
|
|
<?php
|
|
}, 97);
|