168 lines
4.9 KiB
PHP
168 lines
4.9 KiB
PHP
<?php
|
|
|
|
/* ----------------------------------------------------------------
|
|
##### PAGE D'OPTIONS DE GESTION DES RÉSEAUX SOCIAUX
|
|
| ------------------------------------------------------------------*/
|
|
|
|
// Ajouter la page d'options au menu d'administration
|
|
function carhop_add_options_page()
|
|
{
|
|
if (get_current_blog_id() !== 1) return;
|
|
|
|
add_options_page(
|
|
'Options de réseaux sociaux', // Titre de la page
|
|
'Réseaux sociaux', // Titre du menu
|
|
'manage_options', // Capacité requise
|
|
'carhop-options', // Slug de la page
|
|
'carhop_options_page_html' // Fonction de callback
|
|
);
|
|
}
|
|
add_action('admin_menu', 'carhop_add_options_page');
|
|
|
|
// Enregistrer les paramètres
|
|
function carhop_settings_init()
|
|
{
|
|
// Enregistrer un groupe de paramètres
|
|
register_setting('carhop_options', 'carhop_options');
|
|
|
|
// Ajouter une section pour les réseaux sociaux
|
|
add_settings_section(
|
|
'carhop_section_social',
|
|
'Réseaux sociaux',
|
|
'carhop_section_social_callback',
|
|
'carhop_options'
|
|
);
|
|
|
|
// Champs pour les réseaux sociaux
|
|
add_settings_field(
|
|
'facebook_url',
|
|
'URL Facebook',
|
|
'carhop_field_facebook_callback',
|
|
'carhop_options',
|
|
'carhop_section_social',
|
|
array('label_for' => 'facebook_url')
|
|
);
|
|
|
|
add_settings_field(
|
|
'youtube_url',
|
|
'URL YouTube',
|
|
'carhop_field_youtube_callback',
|
|
'carhop_options',
|
|
'carhop_section_social',
|
|
array('label_for' => 'youtube_url')
|
|
);
|
|
}
|
|
add_action('admin_init', 'carhop_settings_init');
|
|
|
|
|
|
// Callback pour la section réseaux sociaux
|
|
function carhop_section_social_callback()
|
|
{
|
|
echo '<p>Configurez les liens vers vos réseaux sociaux.</p>';
|
|
}
|
|
|
|
|
|
function carhop_field_facebook_callback($args)
|
|
{
|
|
$options = get_option('carhop_options');
|
|
$value = isset($options[$args['label_for']]) ? $options[$args['label_for']] : '';
|
|
?>
|
|
<input type="url"
|
|
id="<?php echo esc_attr($args['label_for']); ?>"
|
|
name="carhop_options[<?php echo esc_attr($args['label_for']); ?>]"
|
|
value="<?php echo esc_attr($value); ?>"
|
|
class="regular-text"
|
|
placeholder="https://facebook.com/votreprofil">
|
|
<?php
|
|
}
|
|
|
|
function carhop_field_youtube_callback($args)
|
|
{
|
|
$options = get_option('carhop_options');
|
|
$value = isset($options[$args['label_for']]) ? $options[$args['label_for']] : '';
|
|
?>
|
|
<input type="url"
|
|
id="<?php echo esc_attr($args['label_for']); ?>"
|
|
name="carhop_options[<?php echo esc_attr($args['label_for']); ?>]"
|
|
value="<?php echo esc_attr($value); ?>"
|
|
class="regular-text"
|
|
placeholder="https://youtube.com/votrechaine">
|
|
<?php
|
|
}
|
|
// Page HTML de l'interface d'options
|
|
function carhop_options_page_html()
|
|
{
|
|
// Vérifier les permissions
|
|
if (!current_user_can('manage_options')) {
|
|
return;
|
|
}
|
|
|
|
// Traitement des messages
|
|
if (isset($_GET['settings-updated'])) {
|
|
add_settings_error('carhop_messages', 'carhop_message', 'Paramètres sauvegardés', 'updated');
|
|
}
|
|
|
|
settings_errors('carhop_messages');
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
|
|
|
|
<form action="options.php" method="post">
|
|
<?php
|
|
settings_fields('carhop_options');
|
|
do_settings_sections('carhop_options');
|
|
submit_button('Sauvegarder les paramètres');
|
|
?>
|
|
</form>
|
|
|
|
<div style="margin-top: 20px; padding: 15px; background: #f1f1f1; border-radius: 5px;">
|
|
<h3>Comment utiliser ces paramètres :</h3>
|
|
<p><strong>Dans vos templates :</strong></p>
|
|
<pre><code>$options = get_option('carhop_options');
|
|
echo $options['facebook_url']; // URL Facebook
|
|
echo $options['youtube_url']; // URL YouTube</code></pre>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
// Fonctions utilitaires pour récupérer les options facilement
|
|
function get_carhop_network_links_option($option_name, $default = '')
|
|
{
|
|
$options = get_option('carhop_options');
|
|
return isset($options[$option_name]) ? $options[$option_name] : $default;
|
|
}
|
|
|
|
// Fonction pour afficher les liens sociaux
|
|
function carhop_display_social_links($class = 'social-links')
|
|
{
|
|
switch_to_blog(1);
|
|
$facebookUrl = get_carhop_network_links_option('facebook_url');
|
|
$youtubeUrl = get_carhop_network_links_option('youtube_url');
|
|
restore_current_blog();
|
|
|
|
ob_start();
|
|
?>
|
|
|
|
|
|
<ul class="social-networks-links">
|
|
<?php if (!empty($facebookUrl)) : ?>
|
|
<li>
|
|
<a class="social-link" title="<?php echo __('Facebook', "carhop") ?>" href="<?php echo $facebookUrl ?>" target="_blank">
|
|
<img class="social-icon" src="<?php echo get_template_directory_uri() . '/resources/img/icons/carhop-social-facebook.svg' ?>" alt="">
|
|
</a>
|
|
</li>
|
|
<?php endif; ?>
|
|
<?php if (!empty($youtubeUrl)) : ?>
|
|
<li>
|
|
<a class="social-link" title="<?php echo __('Youtube', "carhop") ?>" href="<?php echo $youtubeUrl ?>" target="_blank">
|
|
<img class="social-icon" src=" <?php echo get_template_directory_uri() . '/resources/img/icons/carhop-social-youtube.svg' ?>" alt="">
|
|
</a>
|
|
</li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
<?php
|
|
$output = ob_get_clean();
|
|
return $output;
|
|
}
|