292 lines
9.4 KiB
PHP
292 lines
9.4 KiB
PHP
<?php
|
|
|
|
// Exit if accessed directly
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
// Charger les styles du parent et de l'enfant
|
|
if (!function_exists('child_theme_styles')) {
|
|
function child_theme_styles() {
|
|
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
|
|
wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', ['parent-style']);
|
|
}
|
|
}
|
|
add_action('wp_enqueue_scripts', 'child_theme_styles');
|
|
|
|
// Ajouter les champs ACF à l'API REST pour les pages
|
|
add_action('rest_api_init', function () {
|
|
register_rest_field(
|
|
'page',
|
|
'acf',
|
|
[
|
|
'get_callback' => function ($object) {
|
|
return get_fields($object['id']);
|
|
},
|
|
'schema' => null,
|
|
]
|
|
);
|
|
});
|
|
|
|
// Gestion des menus ACF et sous-menus
|
|
function register_acf_dashboard_menus() {
|
|
// Menu principal : Page d'accueil
|
|
add_menu_page(
|
|
'Page d\'accueil', // Titre de la page principale
|
|
'Page d\'accueil', // Texte du menu
|
|
'manage_options', // Capacité requise
|
|
'acf-homepage', // Slug du menu principal
|
|
'acf_manager_main_page', // Fonction de rappel pour la page principale
|
|
'dashicons-admin-home', // Icône du menu
|
|
2 // Position dans le menu
|
|
);
|
|
|
|
// Sous-menus pour Page d'accueil
|
|
add_submenu_page(
|
|
'acf-homepage', // Slug du menu parent
|
|
'Gérer Section Hero', // Titre de la page
|
|
'Section Hero', // Texte du sous-menu
|
|
'manage_options', // Capacité requise
|
|
'acf-homepage-hero', // Slug du sous-menu
|
|
'acf_manager_section_hero' // Fonction de rappel
|
|
);
|
|
|
|
add_submenu_page(
|
|
'acf-homepage',
|
|
'Gérer Section Expertise',
|
|
'Section Expertise',
|
|
'manage_options',
|
|
'acf-homepage-expertise',
|
|
'acf_manager_section_expertise'
|
|
);
|
|
|
|
add_submenu_page(
|
|
'acf-homepage',
|
|
'Gérer Section Construction',
|
|
'Section Construction',
|
|
'manage_options',
|
|
'acf-homepage-construction',
|
|
'acf_manager_section_construction'
|
|
);
|
|
|
|
// Menu principal : Page Contact
|
|
add_menu_page(
|
|
'Page Contact', // Titre de la page principale
|
|
'Page Contact', // Texte du menu
|
|
'manage_options', // Capacité requise
|
|
'acf-contact', // Slug du menu principal
|
|
'acf_manager_contact_main', // Fonction de rappel pour la page principale
|
|
'dashicons-email', // Icône du menu
|
|
3 // Position dans le menu
|
|
);
|
|
|
|
// Sous-menus pour Page Contact
|
|
add_submenu_page(
|
|
'acf-contact',
|
|
'Gérer Section Formulaire',
|
|
'Section FAQ',
|
|
'manage_options',
|
|
'acf-contact-form',
|
|
'acf_manager_contact_form'
|
|
);
|
|
|
|
// add_submenu_page(
|
|
// 'acf-contact',
|
|
// 'Gérer Section Coordonnées',
|
|
// 'Section Coordonnées',
|
|
// 'manage_options',
|
|
// 'acf-contact-coordinates',
|
|
// 'acf_manager_contact_coordinates'
|
|
// );
|
|
}
|
|
add_action('admin_menu', 'register_acf_dashboard_menus');
|
|
|
|
// Fonction de rappel pour les pages principales
|
|
function acf_manager_main_page() {
|
|
echo '<div class="wrap"><h1>Bienvenue dans le Gestionnaire de la Page d\'accueil</h1><p>Choisissez une section pour gérer ses champs.</p></div>';
|
|
}
|
|
|
|
function acf_manager_contact_main() {
|
|
echo '<div class="wrap"><h1>Bienvenue dans le Gestionnaire de la Page Contact</h1><p>Choisissez une section pour gérer ses champs.</p></div>';
|
|
}
|
|
|
|
// Fonctions de rappel pour les sous-menus
|
|
function acf_manager_section_hero() {
|
|
render_acf_form('group_6774f26930ebd', '13'); // Remplacez par vos IDs ACF et Post ID
|
|
}
|
|
|
|
function acf_manager_section_expertise() {
|
|
render_acf_form('group_6779c41fcdc6d', '13');
|
|
}
|
|
|
|
function acf_manager_section_construction() {
|
|
render_acf_form('group_677d3294c3dfa', '13');
|
|
}
|
|
|
|
function acf_manager_contact_form() {
|
|
render_acf_form('group_677e8bf907253', '116'); // Remplacez par vos IDs ACF et Post ID
|
|
}
|
|
|
|
// function acf_manager_contact_coordinates() {
|
|
// render_acf_form('group_contact_coordinates', '116'); // Remplacez par vos IDs ACF et Post ID
|
|
// }
|
|
|
|
// Fonction générique pour afficher les formulaires ACF
|
|
function render_acf_form($field_group, $post_id) {
|
|
?>
|
|
<div class="wrap">
|
|
<h1>Gestion des Champs</h1>
|
|
<?php
|
|
if (function_exists('acf_form')) {
|
|
acf_form_head();
|
|
acf_form(array(
|
|
'post_id' => $post_id, // ID de la page ou de l'article
|
|
'field_groups' => array($field_group), // ID du groupe ACF
|
|
'form' => true,
|
|
'return' => add_query_arg('updated', 'true', wp_get_referer()),
|
|
'submit_value' => __('Enregistrer les modifications', 'acf'),
|
|
));
|
|
} else {
|
|
echo '<p>Le plugin ACF n\'est pas activé ou disponible.</p>';
|
|
}
|
|
?>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
register_rest_field(
|
|
'page',
|
|
'acf',
|
|
[
|
|
'get_callback' => function ($object) {
|
|
return get_fields($object['id']);
|
|
},
|
|
'schema' => null,
|
|
]
|
|
);
|
|
|
|
function ajouter_opengraph_meta_api($data, $post, $context) {
|
|
if ($context !== 'view' || !$post) {
|
|
return $data;
|
|
}
|
|
|
|
// Récupérer les métadonnées de Rank Math
|
|
$og_title = get_post_meta($post->ID, 'rank_math_title', true);
|
|
$og_description = get_post_meta($post->ID, 'rank_math_description', true);
|
|
$og_image = get_post_meta($post->ID, 'rank_math_facebook_image', true);
|
|
|
|
// Ajouter les métadonnées au champ API
|
|
$data->data['opengraph'] = [
|
|
'title' => $og_title ?: $post->post_title,
|
|
'description' => $og_description ?: wp_trim_words($post->post_content, 20),
|
|
'image' => $og_image ? wp_get_attachment_url($og_image) : null,
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
add_filter('rest_prepare_post', 'ajouter_opengraph_meta_api', 10, 3);
|
|
|
|
// ✅ Active les erreurs PHP pour le debug (désactive après les tests)
|
|
define('WP_DEBUG', true);
|
|
define('WP_DEBUG_LOG', true);
|
|
define('WP_DEBUG_DISPLAY', false);
|
|
@ini_set('log_errors', 1);
|
|
@ini_set('display_errors', 0);
|
|
|
|
// ✅ Charger la librairie JWT
|
|
use Firebase\JWT\JWT;
|
|
use Firebase\JWT\Key;
|
|
|
|
// ✅ Configurer le secret JWT dans `wp-config.php`
|
|
define('JWT_AUTH_SECRET_KEY', 'change_this_secret_key'); // Change ce secret !
|
|
|
|
// ✅ Fonction pour générer un token JWT
|
|
function custom_generate_jwt_token($user_id) {
|
|
$issuedAt = time();
|
|
$expiration = $issuedAt + (60 * 60 * 24); // Expire en 24h
|
|
|
|
$payload = [
|
|
'iss' => get_bloginfo('url'),
|
|
'iat' => $issuedAt,
|
|
'exp' => $expiration,
|
|
'user_id' => $user_id
|
|
];
|
|
|
|
return JWT::encode($payload, JWT_AUTH_SECRET_KEY, 'HS256');
|
|
}
|
|
|
|
// ✅ Fonction pour vérifier un token JWT
|
|
function custom_authenticate_with_jwt($token) {
|
|
try {
|
|
$decoded = JWT::decode($token, new Key(JWT_AUTH_SECRET_KEY, 'HS256'));
|
|
return get_user_by('ID', $decoded->user_id);
|
|
} catch (Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// ✅ Route API REST pour la connexion
|
|
function custom_authenticate_user(WP_REST_Request $request) {
|
|
$parameters = $request->get_json_params();
|
|
$username = sanitize_text_field($parameters['username']);
|
|
$password = sanitize_text_field($parameters['password']);
|
|
|
|
$user = wp_authenticate($username, $password);
|
|
|
|
if (is_wp_error($user)) {
|
|
return new WP_Error('authentication_failed', 'Identifiants incorrects.', ['status' => 401]);
|
|
}
|
|
|
|
$token = custom_generate_jwt_token($user->ID);
|
|
|
|
return [
|
|
'user_id' => $user->ID,
|
|
'token' => $token,
|
|
'email' => $user->user_email,
|
|
'role' => $user->roles
|
|
];
|
|
}
|
|
|
|
// ✅ Enregistrement des routes API
|
|
add_action('rest_api_init', function () {
|
|
register_rest_route('custom/v1', '/login', [
|
|
'methods' => 'POST',
|
|
'callback' => 'custom_authenticate_user',
|
|
'permission_callback' => '__return_true',
|
|
]);
|
|
});
|
|
|
|
// ✅ Autoriser les uploads et publications via l'API REST
|
|
function allow_admin_upload_and_post($allcaps, $cap, $args) {
|
|
if (in_array($cap[0], ['upload_files', 'edit_posts', 'publish_posts'])) {
|
|
$allcaps[$cap[0]] = true;
|
|
}
|
|
return $allcaps;
|
|
}
|
|
add_filter('map_meta_cap', 'allow_admin_upload_and_post', 10, 3);
|
|
|
|
// ✅ Autoriser CORS (utile pour React)
|
|
function custom_cors_headers() {
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
|
|
header("Access-Control-Allow-Headers: Content-Type, Authorization");
|
|
}
|
|
add_action('init', 'custom_cors_headers');
|
|
|
|
// 🔹 Autoriser la modification des champs ACF via l'API REST
|
|
add_action('rest_api_init', function () {
|
|
register_rest_field(
|
|
'page', // Type de post
|
|
'acf', // Champ ACF
|
|
[
|
|
'get_callback' => function ($object) {
|
|
return get_fields($object['id']); // ✅ Vérifie que l'ID est correct
|
|
},
|
|
'update_callback' => function ($value, $object) {
|
|
foreach ($value as $field_key => $field_value) {
|
|
update_field($field_key, $field_value, $object->ID); // ✅ Correction ici
|
|
}
|
|
return true;
|
|
},
|
|
'schema' => null,
|
|
]
|
|
);
|
|
});
|