?php /** * Plugin Name: OneSignal - Push auto 3h (articles random) * Description: Envoie automatiquement un article aléatoire toutes les 3 heures via OneSignal (WordPress Cron). * Version: 1.2.0 */ if (!defined('ABSPATH')) exit; /** * ✅ SÉCURITÉ (recommandé) * Mets tes clés dans wp-config.php (au-dessus de "That's all"): * * define('OS_APP_ID', '963494fe-ec09-4a2b-acb8-2757383ffdb6'); * define('OS_API_KEY', 'ZTM3YWFiOWEtZGFlNC00YTQxLTkyNTktNzI1MWFiMGEzMmY1'); // ⚠️ ne la publie jamais */ // Si tu préfères (moins sécurisé) mettre ici, décommente et remplis : // define('OS_APP_ID', '963494fe-ec09-4a2b-acb8-2757383ffdb6'); // define('OS_API_KEY', 'ZTM3YWFiOWEtZGFlNC00YTQxLTkyNTktNzI1MWFiMGEzMmY1'); if (!defined('OS_APP_ID') || !defined('OS_API_KEY')) { // Clés absentes => on ne fait rien return; } /** Intervalle Cron: toutes les 3 heures */ add_filter('cron_schedules', function ($schedules) { $schedules['every_3_hours'] = [ 'interval' => 3 * 60 * 60, 'display' => 'Toutes les 3 heures' ]; return $schedules; }); /** À l’activation : planifier l’événement */ register_activation_hook(__FILE__, function () { if (!wp_next_scheduled('os_random_push_event')) { // Démarre dans 2 minutes pour éviter les soucis de cache / activation wp_schedule_event(time() + 120, 'every_3_hours', 'os_random_push_event'); } }); /** À la désactivation : supprimer l’événement */ register_deactivation_hook(__FILE__, function () { wp_clear_scheduled_hook('os_random_push_event'); }); /** L’événement Cron appelle cette fonction */ add_action('os_random_push_event', 'os_send_random_push'); /** * Envoie un push OneSignal avec un article aléatoire */ function os_send_random_push() { // ✅ Optionnel : limiter les envois à une plage horaire (ex: 07h → 23h) // Décommente si tu veux : /* $hour = (int) current_time('H'); // heure WordPress (timezone du site) if ($hour < 7 || $hour > 23) return; */ // ✅ Récupérer 1 article aléatoire publié $posts = get_posts([ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'rand', 'no_found_rows' => true, 'fields' => 'ids', ]); if (empty($posts)) return; $post_id = (int) $posts[0]; // ✅ Optionnel : éviter d’envoyer le même article 2 fois d’affilée // (si le random retombe sur le même) $last_id = (int) get_option('os_last_sent_post_id', 0); if ($last_id === $post_id) { // on retente une fois $posts_retry = get_posts([ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'rand', 'no_found_rows' => true, 'fields' => 'ids', ]); if (!empty($posts_retry)) { $post_id = (int) $posts_retry[0]; } } $title = wp_strip_all_tags(get_the_title($post_id)); $url = get_permalink($post_id); // ✅ Ton message (modifiable) $message = 'Découvre cet article 👇'; $payload = [ 'app_id' => OS_APP_ID, 'included_segments' => ['Subscribed Users'], 'headings' => ['fr' => $title], 'contents' => ['fr' => $message], 'url' => $url, ]; $response = wp_remote_post('https://onesignal.com/api/v1/notifications', [ 'headers' => [ 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Basic ' . OS_API_KEY ], 'body' => wp_json_encode($payload), 'timeout' => 15 ]); // Si succès, on mémorise l’article envoyé if (!is_wp_error($response)) { $code = wp_remote_retrieve_response_code($response); if ((int)$code >= 200 && (int)$code < 300) { update_option('os_last_sent_post_id', $post_id, false); } } }