PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / trunk
WpStream – Live Streaming, Video on Demand, Pay Per View vtrunk
4.14.0 4.13.2 4.13.1 4.13 4.12.5 4.12.4 4.12.3 4.12.2 4.12.1 4.12 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5 4.5.1 4.5.11 4.5.11.1 4.5.11.2 4.5.11.4 4.5.11.5 4.5.11.6 4.5.12 All 180 releases
wpstream / wpstream.php

wpstream.php in WpStream – Live Streaming, Video on Demand, Pay Per View trunk, at wpstream.php

620 lines 23.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WpStream - Live Streaming, Video on Demand, Pay Per View
4 * Plugin URI: http://wpstream.net
5 * Description: WpStream is a platform that allows you to live stream, create Video-on-Demand, and offer Pay-Per-View videos. We provide an affordable and user-friendly way for businesses, non-profits, and public institutions to broadcast their content and monetize their work.
6 * Version: 4.14.0
7 * Author: wpstream
8 * Author URI: http://wpstream.net
9 * Text Domain: wpstream
10 * Domain Path: /languages/
11 * License: GPLv2 or later
12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
13 * Requires at least: 5.8
14 * Requires PHP: 7.1
15 */
16
17 /**
18 * Main plugin bootstrap file for WpStream.
19 *
20 * WordPress reads the header comment above to list the plugin. This file then
21 * defines the plugin's global constants (version, remote API/player/click host
22 * URLs, path helpers), wires activation/deactivation hooks, schedules a daily
23 * log cleanup cron event, loads the core plugin class plus the Elementor
24 * integration, conditionally loads the hello-wpstream theme
25 * framework, registers the custom broadcaster-page rewrite endpoint, and finally
26 * instantiates and runs the Wpstream class.
27 *
28 * @package Wpstream
29 */
30
31 // If this file is called directly, abort.
32 // WPINC is defined by WordPress core; its absence means direct access.
33 if ( ! defined( 'WPINC' ) ) {
34 die;
35 }
36 // Current plugin version (kept in sync with the header "Version" field above).
37 define('WPSTREAM_PLUGIN_VERSION', '4.14.0');
38 // Base host for WpStream account/club links.
39 define('WPSTREAM_CLUBLINK', 'wpstream.net');
40 // Scheme used when building club links.
41 define('WPSTREAM_CLUBLINKSSL', 'https');
42 // URL to the site's plugins directory.
43 define('WPSTREAM_PLUGIN_URL', plugins_url() );
44 // URL to this plugin's own directory.
45 define('WPSTREAM_PLUGIN_DIR_URL', plugin_dir_url(__FILE__) );
46 // Filesystem path to this plugin's directory.
47 define('WPSTREAM_PLUGIN_PATH', plugin_dir_path(__FILE__) );
48 // Plugin basename (folder/file) used for hook identification.
49 define('WPSTREAM_PLUGIN_BASE', plugin_basename(__FILE__) );
50 // Remote WpStream API endpoint; guarded so it can be overridden before load.
51 if( !defined('WPSTREAM_API' ) ) {
52 // Default API host.
53 define('WPSTREAM_API', 'https://baker.wpstream.net');
54 }
55 // Legacy REST API host (server-id lookups); guarded so it can be overridden.
56 if( !defined('WPSTREAM_REST_API' ) ) {
57 // Default legacy REST host.
58 define('WPSTREAM_REST_API', 'https://rest-baker.wpstream.net');
59 }
60 // Telemetry/click-tracking host; guarded so it can be overridden.
61 if ( !defined( 'WPSTREAM_CLICK' ) ) {
62 // Default click-tracking host.
63 define( 'WPSTREAM_CLICK', 'https://click.wpstream.net' );
64 }
65 // Player host; guarded so it can be overridden.
66 if ( !defined( 'WPSTREAM_PLAYER') ) {
67 // Default player host.
68 define ( 'WPSTREAM_PLAYER', 'https://player.wpstream.net' );
69 }
70 // Prefix for live player URLs; guarded so it can be overridden.
71 if ( !defined( 'LIVE_PLAYER_URL_PREFIX' ) ) {
72 // Default live player URL prefix.
73 define('LIVE_PLAYER_URL_PREFIX', 'https://player.wpstream.net/player/live');
74 }
75
76
77 // Default network timeout (seconds) for remote API requests.
78 define('WPSTREAM_TIMEOUT_CONST', 20); // in seconds
79
80 // Register the classmap autoloader before anything references a plugin class,
81 // so activation hooks, cron callbacks, and the bootstrap below all resolve.
82 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wpstream-autoloader.php';
83 Wpstream_Autoloader::register();
84
85 /**
86 * Declare WooCommerce High-Performance Order Storage (HPOS) compatibility.
87 *
88 * All order access in the plugin goes through wc_get_orders() /
89 * wc_customer_bought_product(), which are HPOS-safe, so the plugin can be
90 * declared compatible with custom order tables. WooCommerce requires this
91 * declaration to run inside the before_woocommerce_init hook.
92 *
93 * @return void
94 */
95 function wpstream_declare_hpos_compatibility() {
96 // Guard: FeaturesUtil only exists on WooCommerce 6.9+.
97 if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
98 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
99 }
100 }
101 // WooCommerce fires this hook early on init; the declaration must happen there.
102 add_action( 'before_woocommerce_init', 'wpstream_declare_hpos_compatibility' );
103
104 /**
105 * Cron callback that purges expired WpStream log entries.
106 *
107 * @return void
108 */
109 function wpstream_cleanup_logs_handler() {
110 // Instantiate the logger.
111 $logger = new WpStream_Logger();
112 // Delete log entries older than the retention window.
113 $logger->clear_old_logs();
114 }
115 // Run the cleanup handler whenever the scheduled 'wpstream_log_cleanup' event fires.
116 add_action('wpstream_log_cleanup', 'wpstream_cleanup_logs_handler');
117
118 /**
119 * The code that runs during plugin activation.
120 * This action is documented in includes/class-wpstream-activator.php
121 */
122 function activate_wpstream() {
123 // Load the activator class on demand.
124 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wpstream-activator.php';
125 // Run the activation routine (creates DB tables, options, etc.).
126 Wpstream_Activator::activate();
127
128 // Schedule the daily log-cleanup cron event if it isn't already scheduled.
129 if( !wp_next_scheduled( 'wpstream_log_cleanup' ) ) {
130 // Register a daily recurring event starting now.
131 wp_schedule_event( time(), 'daily', 'wpstream_log_cleanup' );
132 }
133 }
134
135 /**
136 * The code that runs during plugin deactivation.
137 * This action is documented in includes/class-wpstream-deactivator.php
138 */
139 function deactivate_wpstream() {
140 // Load the deactivator class on demand.
141 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wpstream-deactivator.php';
142 // Run the deactivation routine.
143 Wpstream_Deactivator::deactivate();
144
145 // Remove the scheduled daily log-cleanup cron event.
146 wp_clear_scheduled_hook( 'wpstream_log_cleanup' );
147 }
148
149 // Register the activation/deactivation callbacks with WordPress.
150 register_activation_hook( __FILE__, 'activate_wpstream' );
151 register_deactivation_hook( __FILE__, 'deactivate_wpstream' );
152
153 /**
154 * One-time cleanup of leftovers from the removed streamify HLS proxy.
155 *
156 * Older plugin versions shipped a /wpstreamify/ proxy that scheduled a
157 * recurring cron event, registered a rewrite rule, and kept a cache directory
158 * under uploads. Sites upgrading past its removal still carry all three, so
159 * this clears them once and records a marker option so later loads skip it.
160 *
161 * @return void
162 */
163 function wpstream_cleanup_streamify_leftovers() {
164 // Already cleaned on this site — nothing to do.
165 if ( get_option( 'wpstream_streamify_removed' ) ) {
166 return;
167 }
168
169 // Remove the recurring proxy-cache cleanup event the old code scheduled.
170 wp_clear_scheduled_hook( 'wpstreamify_cleanup_event' );
171
172 // Regenerate the saved rewrite rules so the stale /wpstreamify/ route
173 // (whose query var no longer exists) drops out of them.
174 flush_rewrite_rules();
175
176 // Delete the orphaned proxy cache directory and its segment files.
177 $wpstream_streamify_cache_dir = WP_CONTENT_DIR . '/uploads/wpstreamify_cache/';
178 if ( is_dir( $wpstream_streamify_cache_dir ) ) {
179 $wpstream_streamify_files = glob( $wpstream_streamify_cache_dir . '*' );
180 foreach ( is_array( $wpstream_streamify_files ) ? $wpstream_streamify_files : array() as $wpstream_streamify_file ) {
181 if ( is_file( $wpstream_streamify_file ) ) {
182 unlink( $wpstream_streamify_file );
183 }
184 }
185 rmdir( $wpstream_streamify_cache_dir );
186 }
187
188 // Mark the cleanup done; autoloaded because it is checked on every load.
189 update_option( 'wpstream_streamify_removed', 1 );
190 }
191 // Run late on init so every remaining rewrite registration precedes the flush.
192 add_action( 'init', 'wpstream_cleanup_streamify_leftovers', 99 );
193
194 /**
195 * One-time rename of the VOD list Elementor widget key in stored page data.
196 *
197 * Older plugin versions registered the widget under 'class Wpstream_Media_List_Vod'
198 * (a stray "class " prefix left in get_name()). The name is now the clean
199 * 'Wpstream_Media_List_Vod', so any Elementor page saved with the old key would
200 * render the widget as "missing". This rewrites the stored widgetType in every
201 * _elementor_data row (pages and revisions alike), clears Elementor's generated
202 * CSS so the wrapper class regenerates, and records a marker option so later
203 * loads skip it.
204 *
205 * @return void
206 */
207 function wpstream_fix_vod_widget_name() {
208 // Already migrated on this site — nothing to do.
209 if ( get_option( 'wpstream_vod_widget_name_fixed' ) ) {
210 return;
211 }
212
213 global $wpdb;
214
215 // Rewrite the exact quoted JSON pair so user-authored content can never match.
216 $wpdb->query(
217 "UPDATE {$wpdb->postmeta}
218 SET meta_value = REPLACE( meta_value,
219 '\"widgetType\":\"class Wpstream_Media_List_Vod\"',
220 '\"widgetType\":\"Wpstream_Media_List_Vod\"' )
221 WHERE meta_key = '_elementor_data'
222 AND meta_value LIKE '%class Wpstream_Media_List_Vod%'"
223 );
224
225 // Elementor caches per-post CSS containing the old wrapper class; regenerate it.
226 if ( class_exists( '\\Elementor\\Plugin' ) ) {
227 \Elementor\Plugin::instance()->files_manager->clear_cache();
228 }
229
230 // Mark the migration done; autoloaded because it is checked on every load.
231 update_option( 'wpstream_vod_widget_name_fixed', 1 );
232 }
233 // Run on init so Elementor (loaded on plugins_loaded) is available for the cache clear.
234 add_action( 'init', 'wpstream_fix_vod_widget_name', 99 );
235
236 /**
237 * Wrapper function for wpstream_dashboard_save_channel_data to be called from theme
238 * This provides backward compatibility for the theme
239 */
240 function wpstream_dashboard_save_channel_data_plugin() {
241 // Reach the global plugin instance created at the bottom of this file.
242 global $wpstream_plugin;
243 // Only delegate if the plugin and its AJAX handler are available.
244 if (isset($wpstream_plugin) && isset($wpstream_plugin->wpstream_ajax)) {
245 // Forward the call to the real AJAX save-channel handler.
246 $wpstream_plugin->wpstream_ajax->wpstream_dashboard_save_channel_data();
247 }
248 }
249
250 /**
251 * The core plugin class that is used to define internationalization,
252 * admin-specific hooks, and public-facing site hooks.
253 */
254 // Load the core plugin class (defines i18n, admin, and public hooks).
255 require plugin_dir_path( __FILE__ ) . 'includes/class-wpstream.php';
256 // Migrate legacy option, transient and user-meta names once per site.
257 require plugin_dir_path( __FILE__ ) . 'includes/class-wpstream-storage-migration.php';
258 // Elementor requirement gate + widget registrar (autoloaded; checks run on plugins_loaded).
259 new Wpstream_Elementor_Widgets();
260 // Load third-party integrations bootstrap.
261 require plugin_dir_path( __FILE__ ) . 'integrations/integrations.php';
262
263 /**
264 * Load the bundled hello-wpstream theme framework files.
265 *
266 * Hooked to after_setup_theme so the theme's helper functions, post types,
267 * WooCommerce glue, widgets, and (when Elementor is active) Elementor pieces are
268 * available. Missing files are logged rather than fatal.
269 *
270 * @return void
271 */
272 function wpstream_load_theme_functionality() {
273 // Absolute path to the bundled hello-wpstream framework directory.
274 define ('WPSTREAM_FRAMEWORK_BASE', plugin_dir_path(__FILE__) . 'hello-wpstream' );
275
276 // Framework files to load, relative to WPSTREAM_FRAMEWORK_BASE.
277 $core_files = array(
278 '/framework/metaboxes.php',
279 '/framework/query-functions.php',
280 '/framework/wpstream-video-functions.php',
281 '/framework/ajax-functions.php',
282 '/framework/dashboard-functions.php',
283 '/framework/wpstream-help-functions.php',
284 '/framework/video-pages-functions.php',
285 '/framework/comments-functions.php',
286 '/framework/gallery-functions.php',
287 '/framework/shortcodes-functions.php',
288 '/framework/post-types/main.php',
289 '/framework/woocommerce-functions.php',
290 '/framework/ajax-upload.php',
291 '/framework/email-functions.php',
292 '/framework/widgets/class-wpstream-widget-manager.php',
293 '/framework/classes/class-wpstream-login-register.php',
294 '/framework/classes/class-wpstream_theme-social-login.php',
295 );
296
297 // Load core files
298 // Include each framework file, or log it if the file is missing.
299 foreach ( $core_files as $file ) {
300 // Build the absolute path to this framework file.
301 $file_path = WPSTREAM_FRAMEWORK_BASE . $file;
302 // Include it if present; otherwise record the missing file.
303 if ( file_exists( $file_path ) ) {
304 // File exists: load it once.
305 require_once $file_path;
306 } else {
307 // File missing: log an error but keep loading the rest.
308 error_log( 'Missing required file: ' . $file_path );
309 }
310 }
311
312 // Load Elementor integration if the plugin is active
313 // ELEMENTOR_VERSION is only defined when Elementor is loaded.
314 if ( defined( 'ELEMENTOR_VERSION' ) ) {
315
316 // Additional framework files needed only for the Elementor integration.
317 $elementor_files = array(
318 '/elementor/functions/blog_functions.php',
319 '/elementor/wpstream-elementor.php'
320 );
321
322 // Include each Elementor file, or log it if missing.
323 foreach ( $elementor_files as $file ) {
324 // Build the absolute path to this Elementor file.
325 $file_path = WPSTREAM_FRAMEWORK_BASE . $file;
326 // Include it if present; otherwise record the missing file.
327 if ( file_exists( $file_path ) ) {
328 // File exists: load it once.
329 require_once $file_path;
330 } else {
331 // File missing: log an error but keep loading the rest.
332 error_log( 'Missing required file: ' . $file_path );
333 }
334 }
335 }
336 }
337
338 // Load the theme framework after the active theme is set up.
339 add_action( 'after_setup_theme', 'wpstream_load_theme_functionality' );
340
341 /**
342 * Load js for players
343 *
344 * @return void
345 */
346 // Guard the definition so the theme can define its own version without collision.
347 if ( ! function_exists( 'wpstream_enqueue_player_assets' ) ) {
348 /**
349 * Enqueue the full player stack (scripts + styles) at a player render site.
350 *
351 * Every handle is registered (not enqueued) on wp_enqueue_scripts, so nothing
352 * loads on pages without a player; render sites call this the moment player
353 * markup is emitted. Scripts print in the footer; the two stylesheets print
354 * late as well, which is fine because the player element is built by JS.
355 * videojs-logo / quality-selector / quality-levels arrive as dependencies
356 * of wpstream-player.
357 *
358 * @return void
359 */
360 function wpstream_enqueue_player_assets() {
361 // Video.js core.
362 wp_enqueue_script( 'video.min' );
363 // WpStream player wrapper script (drags logo/quality deps with it).
364 wp_enqueue_script( 'wpstream-player' );
365 // Base Video.js CSS + the WpStream player skin.
366 wp_enqueue_style( 'video-js.min' );
367 wp_enqueue_style( 'videojs-wpstream-player' );
368 }
369 }
370
371 if ( ! function_exists( 'wpstream_get_product_type_name' ) ) {
372 /**
373 * Safely read a product's first product_type term name.
374 *
375 * wp_get_post_terms() returns a WP_Error when the taxonomy is unavailable
376 * and an empty array when the product carries no term; naive
377 * $term_list[0]->name reads crash on both under PHP 8. Callers that need the
378 * display label use this accessor; branching logic uses the slug accessor below.
379 *
380 * @param int $product_id Product/post id.
381 * @return string The first term name, or '' when none can be read.
382 */
383 function wpstream_get_product_type_name( $product_id ) {
384 $term_list = wp_get_post_terms( intval( $product_id ), 'product_type' );
385
386 // WP_Error (missing taxonomy) or no assigned term: report "no type".
387 if ( is_wp_error( $term_list ) || empty( $term_list[0]->name ) ) {
388 return '';
389 }
390
391 return $term_list[0]->name;
392 }
393 }
394
395 if ( ! function_exists( 'wpstream_get_product_type_slug' ) ) {
396 /**
397 * Return the stable WooCommerce product-type identifier for a post.
398 *
399 * Product-type names are editable display labels, while the slug is the
400 * identifier used by WooCommerce and WpStream branching logic.
401 *
402 * @param int $product_id Product/post id.
403 * @return string The first term slug, or '' when none can be read.
404 */
405 function wpstream_get_product_type_slug( $product_id ) {
406 $term_list = wp_get_post_terms( intval( $product_id ), 'product_type' );
407
408 if ( is_wp_error( $term_list ) || empty( $term_list[0]->slug ) ) {
409 return '';
410 }
411
412 return $term_list[0]->slug;
413 }
414 }
415
416 if ( ! function_exists( 'wpstream_load_player_js_on_demand' ) ) {
417 /**
418 * Enqueue player scripts only when unit cards are configured to use video.
419 *
420 * @return void
421 */
422 function wpstream_load_player_js_on_demand()
423 {
424 // Read the customizer setting that toggles video previews on unit cards.
425 $wpstream_unit_card_use_video = get_theme_mod('wpstream_unit_card_use_video');
426 // Only enqueue the player assets when that setting is enabled.
427 if ($wpstream_unit_card_use_video) {
428 // Full player stack (scripts + styles).
429 wpstream_enqueue_player_assets();
430 }
431
432 }
433 }
434
435 /**
436 * Begins execution of the plugin.
437 *
438 * Since everything within the plugin is registered via hooks,
439 * then kicking off the plugin from this point in the file does
440 * not affect the page life cycle.
441 *
442 * @since 3.0.1
443 */
444
445 // Expose the plugin instance globally so themes/helpers can reach it.
446 global $wpstream_plugin;
447 // Build the core plugin object.
448 $wpstream_plugin = new Wpstream();
449 // Register all of the plugin's hooks and start it running.
450 $wpstream_plugin->run();
451
452 // After any core/plugin/theme update completes, run our upgrade cleanup.
453 add_action( 'upgrader_process_complete', 'wpstream_my_upgrade_function',10, 2);
454
455 /**
456 * Clear cached API token data after this plugin is updated.
457 *
458 * @param WP_Upgrader $upgrader_object The upgrader instance (unused).
459 * @param array $options Details about what was upgraded.
460 * @return void
461 */
462 function wpstream_my_upgrade_function( $upgrader_object, $options ) {
463
464
465 // This plugin's basename, used to detect if it was among the updated plugins.
466 $current_plugin_path_name = plugin_basename( __FILE__ );
467
468 // Only act on plugin update operations.
469 if ($options['action'] == 'update' && $options['type'] == 'plugin' ) {
470 // Guard against a missing/non-array options payload.
471 if(is_array($options)):
472 // Inspect every plugin that was updated in this batch.
473 foreach($options['plugins'] as $each_plugin) {
474 // Match against this plugin.
475 if ($each_plugin==$current_plugin_path_name) {
476 // Drop the cached API token transient.
477 delete_transient('wpstream_token_api');
478 // Reset the stored token expiry timestamp.
479 update_option('wpstream_token_expire',0);
480 // Blank out the stored current token.
481 update_option('wpstream_current_token',' ');
482
483 }
484 }
485 endif;
486 }
487
488
489 }
490
491
492
493 // Front-end SEO metadata (Open Graph, Twitter cards, structured data). The file
494 // registers its own wp_head hook when loaded — see Wpstream_Seo::init().
495 require plugin_dir_path( __FILE__ ) . 'includes/class-wpstream-seo.php';
496
497
498 /*
499 *
500 * Check integrations
501 *
502 */
503 // Only wire the One Click Demo Import (OCDI) hooks when the hello-wpstream theme is active.
504 if ( get_template() === 'hello-wpstream' ) {
505 // Load the theme-import helper functions referenced by the filters below.
506 require_once plugin_dir_path( __FILE__ ) . 'integrations/hello-wpstream/theme-import.php';
507 // Register the set of demo import files offered by OCDI.
508 add_filter( 'pt-ocdi/import_files', 'wpstream_ocdi_import_files' );
509 // Run post-import setup (menus, pages, options) after a demo import.
510 add_action( 'pt-ocdi/after_import', 'wpstream_ocdi_after_import_setup' );
511 // Customize the intro text shown on the OCDI import screen.
512 add_filter( 'pt-ocdi/plugin_intro_text', 'wpstream_ocdi_plugin_intro_text' );
513 // Send telemetry when a demo content import is attempted.
514 add_action( 'ocdi/before_content_import', 'wpstream_track_ocdi_import_attempt', 10, 1 );
515 }
516
517
518 // Once all plugins are loaded, evaluate which integrations should run.
519 add_action( 'plugins_loaded', 'wpstream_check_integrations' );
520
521
522 /*
523 *
524 * Redirect on plugin activation
525 *
526 */
527
528 // Queue activation intent, then consume it safely on the next admin request.
529 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wpstream-activation-redirect.php';
530
531
532
533 /*
534 *
535 * remove the style selectWoo for the theme
536 *
537 */
538 // The existence of wpstream_get_author_id signals the wpstream theme is active.
539 if ( function_exists( 'wpstream_get_author_id' ) ) {
540 // only if wpstream-theme is activated
541
542 remove_filter( 'pre_user_description', 'wp_filter_kses' ); // Removes the filter that applies KSES filtering for user descriptions
543 remove_filter( 'term_description', 'wp_kses_data' ); // Removes the filter that applies KSES data filtering for term descriptions
544
545 // Define the Select2 dequeue callback only once (guard against redefinition).
546 if ( ! function_exists( 'wsis_dequeue_stylesandscripts_select2' ) ) {
547 // Run the dequeue late (priority 100) so it overrides earlier enqueues.
548 add_action( 'wp_enqueue_scripts', 'wsis_dequeue_stylesandscripts_select2', 100 );
549 /**
550 * Remove CSS and/or JS for Select2 used by WooCommerce
551 */
552 function wsis_dequeue_stylesandscripts_select2() {
553 // Only relevant when WooCommerce is present.
554 if ( class_exists( 'woocommerce' ) ) {
555 // Remove WooCommerce's selectWoo stylesheet.
556 wp_dequeue_style( 'selectWoo' );
557 wp_deregister_style( 'selectWoo' );
558
559 // Remove WooCommerce's selectWoo script.
560 wp_dequeue_script( 'selectWoo' );
561 wp_deregister_script( 'selectWoo' );
562 }
563 }
564 }
565 }
566
567 /**
568 * Register broadcaster page endpoint
569 */
570 function wpstream_register_broadcaster_endpoint() {
571 // Register the query var that flags a broadcaster page request (0 or 1).
572 add_rewrite_tag('%broadcaster_page%', '([0-1]{1})');
573 // Register the numeric channel-id query var.
574 add_rewrite_tag('%channel_id%', '([0-9]+)');
575
576 // Current stored rewrite rules, used to detect if our rule already exists.
577 $rewrite_rules = get_option('rewrite_rules');
578 // Pretty-URL pattern: /broadcaster-page/{id}/
579 $rule_pattern = 'broadcaster-page/([0-9]+)/?$';
580 // Internal target that maps the captured id onto our query vars.
581 $rule_target = 'index.php?broadcaster_page=1&channel_id=$matches[1]';
582
583 // Add the rewrite rule at the top so it takes precedence.
584 add_rewrite_rule($rule_pattern, $rule_target, 'top');
585 // Flush rules only when they are missing our pattern (avoids flushing every request).
586 if ( !is_array($rewrite_rules) || !key_exists( $rule_pattern, $rewrite_rules ) ) {
587 // Rebuild the rewrite rules so the new endpoint works immediately.
588 flush_rewrite_rules();
589 }
590 }
591 // Register the broadcaster endpoint on every init.
592 add_action('init', 'wpstream_register_broadcaster_endpoint');
593
594 /**
595 * Load broadcaster template when the custom endpoint is accessed
596 */
597 function wpstream_load_broadcaster_template($template) {
598 // When the broadcaster_page query var is set, serve our custom template.
599 if (get_query_var('broadcaster_page') == 1) {
600 // Return the path to our broadcaster template
601 return plugin_dir_path(__FILE__) . 'templates/broadcaster-template.php';
602 }
603 // Otherwise fall back to the theme's normal template.
604 return $template;
605 }
606 // Intercept template selection to swap in the broadcaster template when needed.
607 add_filter('template_include', 'wpstream_load_broadcaster_template');
608
609 /**
610 * Flush rewrite rules on plugin activation to ensure our endpoint works
611 */
612 function wpstream_flush_rewrite_rules() {
613 // Register the endpoint's rewrite tags/rules first.
614 wpstream_register_broadcaster_endpoint();
615 // Then flush so the new rules are active immediately after activation.
616 flush_rewrite_rules();
617 }
618 // Ensure rewrite rules are flushed when the plugin is activated.
619 register_activation_hook(__FILE__, 'wpstream_flush_rewrite_rules');
620