-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-racemanager.php
More file actions
267 lines (220 loc) · 10.4 KB
/
wp-racemanager.php
File metadata and controls
267 lines (220 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Plugin Name: WP RaceManager
* Description: Provides REST API endpoints for RotorHazard: download pilot registrations, upload race results. The "Races" menu item will be populated with the latest races. For more information, see the plugin settings.
* Version: 1.0
* Author: Peter Simandl
* Text Domain: wp-racemanager
*/
// Define the namespace
namespace RaceManager; // Use your preferred namespace if you have one.
//if (!defined('ABSPATH')) exit; // Exit if accessed directly
define( 'WP_RACEMANAGER_VERSION', '1.0.0' );
define( 'WP_RACEMANAGER_DIR', plugin_dir_path( __FILE__ ) );
define( 'WP_RACEMANAGER_URL', plugin_dir_url( __FILE__ ) );
//define( 'WP_RACEMANAGER_ASSETS', WP_RACEMANAGER_URL . 'assets/build/' );
// Include required files
// Activation hook to create the database table
function rm_activate() {
// Ensure Contact Form 7 is active.
if ( ! class_exists( 'WPCF7_ContactForm' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die(
__( 'Contact Form 7 Plugin not installed or active', 'wp-racemanager' ),
__( 'Plugin Activation Error', 'wp-racemanager' ),
array( 'back_link' => true )
);
}
// Create the database table for PWA subscriptions
require_once plugin_dir_path(__FILE__) . 'includes/pwa-subscription-handler.php';
\RaceManager\PWA_Subscription_Handler::create_db_table();
// Create Registration Table
require_once plugin_dir_path(__FILE__) . 'includes/admin-registrations.php';
rm_create_registration_table();
create_event_registration_cf7_form();
// Create the service worker and manifest files
require_once plugin_dir_path(__FILE__) . 'includes/pwa-handler.php';
rm_create_file_from_template('template-pwa-sw.js', ABSPATH);
rm_create_file_from_template('template-manifest.json', ABSPATH);
}
register_activation_hook(
__FILE__,
__NAMESPACE__ . '\\rm_activate' // "RaceManager\\rm_activate"
);
final class WP_RaceManager {
/**
* Store a static instance of the plugin class.
*
* @var WP_RaceManager|null
*/
private static $instance = null;
public $pwa_subscription_handler;
public $live_race_in_progress = false;
/**
* Instantiate or retrieve the existing instance of this class (Singleton).
*
* @return WP_RaceManager
*/
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*
* We keep it private or protected if we want to force
* usage of WP_RaceManager::instance() for the singleton pattern.
*/
private function __construct() {
// The following is a minimal approach.
//add_action( 'plugins_loaded', [ $this, 'maybe_init_rest_handlers' ] );
//require_once WP_RACEMANAGER_DIR . 'vendor/autoload.php'; // if you’re using Composer
// First load helper functions or implement them here
// Init global variables
add_action( 'init', [ $this, 'is_a_race_live' ] ); // Check if a race has been updated in the last two hours
// Load the REST API handling
//require_once __DIR__ . '/../../../../vendor/autoload.php'; // Relative path to the vendor directory (currently in root of httpdocs)
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); // only called on REST API requests
add_action( 'template_redirect', [ $this, 'handle_live_pages' ], 2 ); // called on every page load
/* require_once plugin_dir_path( __FILE__ ) . 'includes/pwa-subscription-handler.php';
$this->pwa_subscription_handler = new PWA_Subscription_Handler();
require_once plugin_dir_path( __FILE__ ) . 'includes/race-data-functions.php';
include_once plugin_dir_path(__FILE__) . 'includes/rest-handler.php'; */
// END of REST API handling
// TODO: Load only on admin pages
include_once plugin_dir_path(__FILE__) . 'includes/settings-handler.php';
// active on all pages
include_once plugin_dir_path(__FILE__) . 'includes/db-handler.php';
include_once plugin_dir_path(__FILE__) . 'includes/ajax-subscription-handler.php'; // Handles all subscription-related AJAX requests for the RaceManager plugin.
// active on every page
require_once plugin_dir_path(__FILE__) . 'includes/seo-handler.php'; // SEO functions
require_once plugin_dir_path(__FILE__) . 'includes/main-navigation-handler.php'; // filter function for main navigation to indicate live race in progress
require_once plugin_dir_path(__FILE__) . 'includes/block-loader.php';
include_once plugin_dir_path(__FILE__) . 'includes/cpt-handler.php'; //
include_once plugin_dir_path(__FILE__) . 'includes/race-archive.php'; // Race archive page (ordering by event start date)
require_once plugin_dir_path(__FILE__) . 'includes/admin-registrations.php'; // Admin functions for registrations
include_once plugin_dir_path(__FILE__) . 'includes/sc-cf7-event-dropdown.php'; // SC for Contact Form 7
include_once plugin_dir_path(__FILE__) . 'includes/cpt-meta-handler.php'; // cpt admin functions
include_once plugin_dir_path(__FILE__) . 'includes/block-modifiers.php'; // filter modifiers for default wp blocks
include_once plugin_dir_path(__FILE__) . 'includes/sc-gallery.php';
include_once plugin_dir_path(__FILE__) . 'includes/sc-rm_viewer.php'; // SC for heat viewer
include_once plugin_dir_path(__FILE__) . 'includes/sc-race-log.php'; // SC for race log
include_once plugin_dir_path(__FILE__) . 'includes/sc-rm_registered.php'; // SC for registered pilots
//include_once plugin_dir_path(__FILE__) . 'includes/sc-rm_cards.php'; // SC for Shortcode
//include_once plugin_dir_path(__FILE__) . 'includes/sc-rm_tabs.php'; // SC for Shortcode
// TODO: Include only on live pages request.
}
/* public static function activate() {
require_once plugin_dir_path(__FILE__) . 'includes/pwa-subscription-handler.php';
\RaceManager\PWA_Subscription_Handler::create_db_table();
// ...
}
register_activation_hook(__FILE__, [ 'RaceManager\WP_RaceManager', 'activate' ]); */
/**
* Check if we need to load the PWA subscription code,
* and then load it if necessary.
*/
public function register_rest_routes() {
// Load helper functions for RH JSON data
require_once plugin_dir_path( __FILE__ ) . 'includes/race-data-functions.php';
// Load instantiate PWA_Subscription_Handler.
require_once plugin_dir_path( __FILE__ ) . 'includes/pwa-subscription-handler.php';
// PWA class registers its rest routes in the constructor
$this->pwa_subscription_handler = new PWA_Subscription_Handler();
// Handle pilot download and results upload
require_once plugin_dir_path(__FILE__) . 'includes/rest-handler.php';
rm_register_rest_routes_rh();
}
public function handle_live_pages() {
if ( $this->is_live_page() ) {
include_once plugin_dir_path(__FILE__) . 'includes/livepage-handler.php';
rm_start_session();
rm_rewrite_live_urls();
include_once plugin_dir_path(__FILE__) . 'includes/pwa-handler.php';
rm_load_live_resources();
}
}
/**
* Detect whether this request is a WP REST API request for our "racemanager" namespace.
*/
private function is_racemanager_rest_api_request() {
return true;
// Make sure it's a REST request at all.
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
return false;
}
// Ensure rest_route param is present.
if ( empty( $_GET['rest_route'] ) ) {
return false;
}
// Check if it starts with "racemanager/v1" (adjust to your chosen namespace/version).
//return ( 0 === strpos( ltrim( $_GET['rest_route'], '/' ), 'rm/v1' ) );
return true;
}
public static function is_live_page() {
// Only proceed on page requests.
if ( ! is_page() ) {
return false;
}
// Get the current page ID.
$page_id = get_the_ID();
// Retrieve the stored Live Races page ID.
$rm_live_page_id = get_option('rm_live_page_id');
if ( ! $rm_live_page_id ) {
return false;
}
// Check if the current page is the Live Races page.
if ( $page_id == $rm_live_page_id ) {
return true;
}
// Check if the Live Races page is one of the ancestors of the current page.
if ( in_array( $rm_live_page_id, get_post_ancestors( $page_id ) ) ) {
return true;
}
return false;
}
public function is_a_race_live() {
//
// Generate the datetime string for two hours ago
$two_hours_ago = date( 'Y-m-d H:i:s', strtotime( '-2 hours', current_time( 'timestamp' ) ) );
$args = array(
'post_type' => 'race',
'posts_per_page' => 1, // Limit to one result
'fields' => 'ids', // Only retrieve IDs for efficiency
'meta_query' => array(
array(
'key' => '_race_last_upload',
'value' => $two_hours_ago,
'compare' => '>',
'type' => 'DATETIME'
),
),
);
$query = new \WP_Query( $args );
$this->live_race_in_progress = $query->have_posts();
if($this->live_race_in_progress) {
add_filter( 'render_block', 'rm_indicate_live_race', 10, 2 );
}
//return $query->have_posts();
}
public static function write_log($log) {
if (true === WP_DEBUG) {
if (is_array($log) || is_object($log)) {
error_log(print_r($log, true));
} else {
error_log($log);
}
}
}
}
/**
* Launch the plugin.
*/
function racemanager_run() {
return WP_RaceManager::instance();
}
racemanager_run(); // Instead of hooking, we can just run it directly here.
// Optionally use the plugins_loaded or init hook to delay the launch.
//add_action( 'plugins_loaded', 'RaceManager\racemanager_run' );