NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


<?php
/*
Plugin Name: TradingView Alert System
Description: Receives TradingView webhooks and shows real-time alerts on trading page
Version: 1.0
Author: Your Team
*/

if (!defined('ABSPATH')) exit;

/**
* 1️⃣ REGISTER WEBHOOK ENDPOINT
* URL: /wp-json/tradingview/v1/alert
*/
add_action('rest_api_init', function () {
register_rest_route('tradingview/v1', '/alert', [
'methods' => 'POST',
'callback' => 'tv_receive_alert',
'permission_callback' => '__return_true',
]);
});

function tv_receive_alert(WP_REST_Request $request)
{
$data = json_decode($request->get_body(), true);

if (!$data) {
return new WP_REST_Response(['error' => 'Invalid JSON'], 400);
}

// 🔐 Secret validation
$SECRET = 'Joohirastogi@2229#';
if (!isset($data['secret']) || $data['secret'] !== $SECRET) {
return new WP_REST_Response(['error' => 'Unauthorized'], 401);
}

$alert = [
'id' => uniqid(),
'ticker' => sanitize_text_field($data['ticker'] ?? ''),
'price' => sanitize_text_field($data['price'] ?? ''),
'time' => current_time('H:i:s'),
];

// Store as QUEUE
// $alerts = get_option('tv_alert_queue', []);
// $alerts[] = $alert;
// update_option('tv_alert_queue', $alerts);

$alerts = get_option('tv_alert_queue', []);

if (!is_array($alerts)) {
$alerts = [];
}

$alerts[] = [
'id' => uniqid(),
'ticker' => sanitize_text_field($data['ticker'] ?? ''),
'price' => sanitize_text_field($data['price'] ?? ''),
'time' => current_time('H:i:s'),
];

// keep only last 100 alerts
$alerts = array_slice($alerts, -100);

update_option('tv_alert_queue', $alerts);

return new WP_REST_Response(['status' => 'alert received'], 200);
}

/**
* 2️⃣ FETCH ONLY NEW ALERTS (QUEUE CONSUME)
* URL: /wp-json/tradingview/v1/latest
*/
add_action('rest_api_init', function () {
register_rest_route('tradingview/v1', '/latest', [
'methods' => 'GET',
'callback' => 'tv_get_latest_alerts',
'permission_callback' => '__return_true',
]);
});





function tv_get_latest_alerts(WP_REST_Request $request)
{
$last_id = sanitize_text_field($request->get_param('last_id'));
$alerts = get_option('tv_alert_queue', []);

if (empty($alerts)) {
return new WP_REST_Response([
'has_alert' => false,
'alerts' => []
], 200);
}

// If no last_id → send all alerts (page refresh)
if (!$last_id) {
return new WP_REST_Response([
'has_alert' => true,
'alerts' => $alerts
], 200);
}

// Send only NEW alerts
$new_alerts = [];
$found = false;

foreach ($alerts as $alert) {
if ($found) {
$new_alerts[] = $alert;
}
if ($alert['id'] === $last_id) {
$found = true;
}
}

return new WP_REST_Response([
'has_alert' => !empty($new_alerts),
'alerts' => $new_alerts
], 200);
}

/**
* 3️⃣ LOAD JS FILE
*/
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script(
'tv-toast-js',
plugin_dir_url(__FILE__) . 'toast.js',
[],
time(),
true
);
});
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.