<?php
/**
 * Example usage of OneSignal Push Notifications
 * 
 * This file shows how to use the push notification system
 * in your WordPress theme or other plugins.
 */

// Example 1: Send notification from CMS data (like your original implementation)
function send_push_from_cms_data() {
    try {
        $pushData = [
            'id' => 123,
            'title' => 'Nuovo articolo pubblicato',
            'content' => 'Leggi il nostro ultimo articolo su WordPress',
            'url' => 'https://example.com/articolo-123',
            'image' => 'https://example.com/image.jpg'
        ];
        
        \Blazemedia\OneSignal\Push\PushManager::sendNotificationToOneSignal($pushData);
        echo "Push notification inviata con successo!";
        
    } catch (\Exception $e) {
        echo "Errore: " . $e->getMessage();
    }
}

// Example 2: Send notification from WordPress post
function send_push_from_post($post_id) {
    try {
        $post = get_post($post_id);
        $pushManager = new \Blazemedia\OneSignal\Push\PushManager();
        $pushManager->sendPostNotification($post);
        
        echo "Push notification inviata per il post {$post_id}";
        
    } catch (\Exception $e) {
        echo "Errore: " . $e->getMessage();
    }
}

// Example 3: Send custom notification
function send_custom_push() {
    try {
        $pushManager = new \Blazemedia\OneSignal\Push\PushManager();
        $pushManager->sendCustomNotification(
            'Titolo della notifica',
            'Contenuto della notifica',
            'https://example.com/url',
            'https://example.com/image.jpg'
        );
        
        echo "Push notification personalizzata inviata!";
        
    } catch (\Exception $e) {
        echo "Errore: " . $e->getMessage();
    }
}

// Example 4: Hook to send push when post is published (alternative to auto push)
function send_push_on_post_publish($post_id, $post) {
    // Only for new posts, not updates
    if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
        return;
    }
    
    // Only for specific post types
    if ($post->post_type !== 'post') {
        return;
    }
    
    try {
        $pushManager = new \Blazemedia\OneSignal\Push\PushManager();
        $pushManager->sendPostNotification($post);
        
        // Log success
        error_log("OneSignal: Manual push sent for post {$post_id}");
        
    } catch (\Exception $e) {
        // Log error
        error_log("OneSignal: Manual push failed for post {$post_id}: " . $e->getMessage());
    }
}

// Uncomment to enable manual push on post publish
// add_action('publish_post', 'send_push_on_post_publish', 10, 2);

// Example 5: Send push from AJAX request
function send_push_ajax() {
    // Verify nonce for security
    if (!wp_verify_nonce($_POST['nonce'], 'onesignal_push_nonce')) {
        wp_die('Security check failed');
    }
    
    try {
        $pushManager = new \Blazemedia\OneSignal\Push\PushManager();
        $pushManager->sendCustomNotification(
            sanitize_text_field($_POST['title']),
            sanitize_textarea_field($_POST['content']),
            esc_url_raw($_POST['url']),
            esc_url_raw($_POST['image'])
        );
        
        wp_send_json_success('Push notification inviata con successo!');
        
    } catch (\Exception $e) {
        wp_send_json_error('Errore: ' . $e->getMessage());
    }
}

// Uncomment to enable AJAX endpoint
// add_action('wp_ajax_send_push_notification', 'send_push_ajax');
// add_action('wp_ajax_nopriv_send_push_notification', 'send_push_ajax'); 