1+ <?php
2+ /*
3+ Plugin Name: Failover Status Monitor
4+ Description: Monitors failover status and provides notifications.
5+ */
6+
7+ // Prevent direct access
8+ defined ('ABSPATH ' ) or die ('No script kiddies please! ' );
9+
10+ // Add menu item under Dashboard
11+ function add_failover_status_menu () {
12+ add_menu_page (
13+ 'Failover Status ' ,
14+ 'Failover Status ' ,
15+ 'manage_options ' ,
16+ 'failover-status ' ,
17+ 'failover_status_page ' ,
18+ 'dashicons-networking ' ,
19+ 3
20+ );
21+ }
22+ add_action ('admin_menu ' , 'add_failover_status_menu ' );
23+
24+ // Enqueue the necessary scripts and styles for the color picker
25+ function enqueue_color_picker_scripts () {
26+ wp_enqueue_style ('wp-color-picker ' );
27+ wp_enqueue_script ('wp-color-picker ' );
28+ }
29+ add_action ('admin_enqueue_scripts ' , 'enqueue_color_picker_scripts ' );
30+
31+ // Failover Status page content
32+ function failover_status_page () {
33+ if (function_exists ('opcache_reset ' )) {
34+ opcache_reset ();
35+ }
36+
37+ $ current_ip = $ _SERVER ['SERVER_ADDR ' ];
38+ $ primary_failover_ip = get_option ('primary_failover_ip ' );
39+ $ is_failover_active = ($ current_ip !== $ primary_failover_ip );
40+ ?>
41+ <div class="wrap">
42+ <h2>Failover Status</h2>
43+
44+ <h3>Introduction</h3>
45+ <p>This is a plugin for monitoring failovers.</p>
46+
47+ <h3>Current Server</h3>
48+ <p>Hostname/IP: <?php echo gethostname () . '/ ' . $ current_ip ; ?> </p>
49+
50+ <h3>Status</h3>
51+ <p>
52+ <?php
53+ if ($ is_failover_active ) {
54+ echo '<span style="color: red; font-weight: bold;">Failover ACTIVE </span> <span class="dashicons dashicons-no-alt"></span> ' ;
55+ } else {
56+ echo '<span style="color: green; font-weight: bold;">Normal Operation </span> <span class="dashicons dashicons-yes"></span> ' ;
57+ }
58+ ?>
59+ </p>
60+
61+ <h3>Settings</h3>
62+ <form method="post" action="options.php">
63+ <?php
64+ settings_fields ('failover_settings ' );
65+ do_settings_sections ('failover-status ' );
66+ ?>
67+ <table class="form-table">
68+ <tr valign="top">
69+ <th scope="row">Primary Failover IP</th>
70+ <td>
71+ <input type="text" name="primary_failover_ip" value="<?php echo esc_attr (get_option ('primary_failover_ip ' )); ?> " />
72+ <input type="text" name="primary_server_name" placeholder="Primary Server Name" value="<?php echo esc_attr (get_option ('primary_server_name ' )); ?> " />
73+ </td>
74+ </tr>
75+ <tr valign="top">
76+ <th scope="row">Secondary Failover IP</th>
77+ <td>
78+ <input type="text" name="secondary_failover_ip" value="<?php echo esc_attr (get_option ('secondary_failover_ip ' )); ?> " />
79+ <input type="text" name="secondary_server_name" placeholder="Secondary Server Name" value="<?php echo esc_attr (get_option ('secondary_server_name ' )); ?> " />
80+ </td>
81+ </tr>
82+ <tr valign="top">
83+ <th scope="row">Failover Notification - Admin Bar</th>
84+ <td><input type="checkbox" name="enable_failover_admin_bar" value="1" <?php checked (get_option ('enable_failover_admin_bar ' ), 1 ); ?> /></td>
85+ </tr>
86+ <tr valign="top">
87+ <th scope="row">Failover Notification - Banner</th>
88+ <td><input type="checkbox" name="enable_failover_banner" value="1" <?php checked (get_option ('enable_failover_banner ' ), 1 ); ?> /></td>
89+ </tr>
90+ <tr valign="top">
91+ <th scope="row">Admin Bar Color (Failover)</th>
92+ <td><input id="color-picker" type="text" name="admin_bar_color" class="color-picker" value="<?php echo esc_attr (get_option ('admin_bar_color ' , '#9a6400 ' )); ?> " /></td>
93+ </tr>
94+ </table>
95+ <?php submit_button (); ?>
96+ </form>
97+ </div>
98+
99+ <script>
100+ jQuery(document).ready(function($) {
101+ $('.color-picker').wpColorPicker();
102+ });
103+ </script>
104+
105+ <?php
106+ }
107+
108+ // Register settings
109+ function register_failover_settings () {
110+ register_setting ('failover_settings ' , 'primary_failover_ip ' );
111+ register_setting ('failover_settings ' , 'secondary_failover_ip ' );
112+ register_setting ('failover_settings ' , 'enable_failover_admin_bar ' );
113+ register_setting ('failover_settings ' , 'enable_failover_banner ' );
114+ register_setting ('failover_settings ' , 'admin_bar_color ' );
115+ register_setting ('failover_settings ' , 'primary_server_name ' );
116+ register_setting ('failover_settings ' , 'secondary_server_name ' );
117+ }
118+ add_action ('admin_init ' , 'register_failover_settings ' );
119+
120+ // Add settings sections
121+ function add_failover_settings_sections () {
122+ add_settings_section (
123+ 'failover_settings_section ' ,
124+ 'Failover Settings ' ,
125+ 'failover_settings_section_callback ' ,
126+ 'failover-status '
127+ );
128+ }
129+ add_action ('admin_init ' , 'add_failover_settings_sections ' );
130+
131+ function failover_settings_section_callback () {
132+ echo '<p>Configure failover settings here.</p> ' ;
133+ }
134+
135+ // Failover notification logic
136+ function failover_notification () {
137+ $ current_ip = $ _SERVER ['SERVER_ADDR ' ];
138+ $ primary_failover_ip = get_option ('primary_failover_ip ' );
139+ $ admin_bar_color = get_option ('admin_bar_color ' , '#9a6400 ' ); // Default to #9a6400 if not set
140+
141+ if ($ current_ip !== $ primary_failover_ip ) {
142+ if (get_option ('enable_failover_admin_bar ' )) {
143+ // Admin bar color.
144+ function admin_bar_color () {
145+ global $ admin_bar_color ;
146+ echo '<style type="text/css">
147+ #wpadminbar { background-color: ' . $ admin_bar_color . ' !important; }
148+ </style> ' ;
149+ }
150+ add_action ('admin_head ' , 'admin_bar_color ' );
151+
152+ // Add "Failover ACTIVE" to admin bar
153+ function add_failover_admin_bar_node () {
154+ global $ wp_admin_bar ;
155+
156+ // Get the URL of the failover status page
157+ $ failover_page_url = admin_url ('admin.php?page=failover-status ' );
158+
159+ $ wp_admin_bar ->add_node (array (
160+ 'id ' => 'failover-active ' ,
161+ 'title ' => 'Failover ACTIVE ' ,
162+ 'href ' => $ failover_page_url ,
163+ 'meta ' => array (
164+ 'class ' => 'failover-active-node ' ,
165+ ),
166+ ));
167+ }
168+ add_action ('admin_bar_menu ' , 'add_failover_admin_bar_node ' , 999 );
169+
170+ // Style the "Failover ACTIVE" node
171+ function style_failover_admin_bar_node () {
172+ echo '<style type="text/css">
173+ #wpadminbar #wp-admin-bar-failover-active > .ab-item {
174+ color: white !important;
175+ background-color: red;
176+ font-weight: bold;
177+ }
178+ </style> ' ;
179+ }
180+ add_action ('admin_head ' , 'style_failover_admin_bar_node ' );
181+ }
182+
183+ if (get_option ('enable_failover_banner ' )) {
184+ // Display "Failover ACTIVE" as an admin notice
185+ function display_failover_admin_notice () {
186+ $ current_ip = $ _SERVER ['SERVER_ADDR ' ];
187+ // Get the URL of the failover status page
188+ $ failover_page_url = admin_url ('admin.php?page=failover-status ' );
189+ $ failover_server_name = ($ current_ip === get_option ('secondary_failover_ip ' ))
190+ ? get_option ('secondary_server_name ' , 'Secondary ' ) // Use saved name or default
191+ : get_option ('primary_server_name ' , 'Primary ' ); // Use saved name or default
192+
193+ echo '<div class="notice notice-error is-dismissible">
194+ <p><strong>Failover ACTIVE on ' . $ current_ip . '/ ' . $ failover_server_name . '</strong> - <a href=" ' . $ failover_page_url . '">View Failover Status</a></p>
195+ </div> ' ;
196+ }
197+ add_action ('admin_notices ' , 'display_failover_admin_notice ' );
198+ }
199+ }
200+ }
201+ add_action ('init ' , 'failover_notification ' );
0 commit comments