| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Template loader for the WpStream plugin. |
| 5 |
* |
| 6 |
* When the bundled "hello-wpstream" theme is active, this class: |
| 7 |
* - registers a selectable "WpStream Dashboard" page template, |
| 8 |
* - swaps in the plugin's own page/single templates via `template_include`, and |
| 9 |
* - enqueues the dashboard's JavaScript (and localized strings) on that page. |
| 10 |
* |
| 11 |
* @package wpstream-plugin |
| 12 |
*/ |
| 13 |
|
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Registers and resolves the plugin's front-end templates for the companion theme. |
| 22 |
*/ |
| 23 |
class WpStream_Template_Loader { |
| 24 |
/** |
| 25 |
* Hook the template registration, resolution and script enqueue callbacks. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
// Offer the Dashboard page template in the editor (late priority to win over others). |
| 29 |
add_filter( 'theme_page_templates', array( $this, 'wpstream_add_page_templates' ), 100 ); // Higher priority |
| 30 |
// Override which template file WordPress loads for our pages/CPTs. |
| 31 |
add_filter( 'template_include', array( $this, 'include_templates' ) ); |
| 32 |
// Enqueue dashboard scripts when the Dashboard template is in use. |
| 33 |
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_dashboard_scripts' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Add the Dashboard page template to the theme's list of selectable templates. |
| 38 |
* |
| 39 |
* @param array $templates Existing page templates (slug => label). |
| 40 |
* @return array Templates, with the Dashboard entry added under hello-wpstream. |
| 41 |
*/ |
| 42 |
public function wpstream_add_page_templates( $templates ) { |
| 43 |
// Only expose the template when the companion theme is active. |
| 44 |
if ( get_template() === 'hello-wpstream' ) { |
| 45 |
// Add the Dashboard template |
| 46 |
$templates['wpstream-theme-dashboard.php'] = __('WpStream Dashboard Page', 'wpstream'); |
| 47 |
} |
| 48 |
|
| 49 |
// Return the (possibly extended) template list. |
| 50 |
return $templates; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Resolve the actual template file to load for our pages and custom post types. |
| 55 |
* |
| 56 |
* @param string $template Template path WordPress would otherwise use. |
| 57 |
* @return string Plugin-provided template when one applies, else the original. |
| 58 |
*/ |
| 59 |
public function include_templates( $template ) { |
| 60 |
// Page template override: only for real pages under the companion theme. |
| 61 |
if ( is_page() && get_template() === 'hello-wpstream' ) { |
| 62 |
// Which page template was assigned to this page? |
| 63 |
$page_template = get_page_template_slug(); |
| 64 |
|
| 65 |
// Route the Dashboard template to the plugin's own file. |
| 66 |
if ( 'wpstream-theme-dashboard.php' === $page_template ) { |
| 67 |
$file = WPSTREAM_PLUGIN_PATH . 'hello-wpstream/page-templates/wpstream-theme-dashboard.php'; |
| 68 |
|
| 69 |
// Use it only if the file actually exists. |
| 70 |
if ( file_exists( $file ) ) { |
| 71 |
return $file; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Single templates for custom post types |
| 77 |
if ( get_template() === 'hello-wpstream' ) { |
| 78 |
// Base directory holding the plugin's single-CPT templates. |
| 79 |
$single_template_path = WPSTREAM_PLUGIN_PATH . 'hello-wpstream/single-templates/'; |
| 80 |
|
| 81 |
// Free live-stream product single view. |
| 82 |
if ( is_singular( 'wpstream_product' ) ) { |
| 83 |
$template_file = $single_template_path . 'single-wpstream_product.php'; |
| 84 |
if ( file_exists( $template_file ) ) { |
| 85 |
$template = $template_file; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// VOD product single view. |
| 90 |
if ( is_singular( 'wpstream_product_vod' ) ) { |
| 91 |
$template_file = $single_template_path . 'single-wpstream_product_vod.php'; |
| 92 |
if ( file_exists( $template_file ) ) { |
| 93 |
$template = $template_file; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Bundle single view. |
| 98 |
if ( is_singular( 'wpstream_bundles' ) ) { |
| 99 |
$template_file = $single_template_path . 'single-wpstream_bundles.php'; |
| 100 |
if ( file_exists( $template_file ) ) { |
| 101 |
$template = $template_file; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// No override matched (or file missing): keep the original template. |
| 107 |
return $template; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Enqueue the dashboard JS (and its localized strings) on the Dashboard page. |
| 112 |
*/ |
| 113 |
public function maybe_enqueue_dashboard_scripts() { |
| 114 |
// Only run on the Dashboard template under the companion theme. |
| 115 |
if ( 'wpstream-theme-dashboard.php' === get_page_template_slug() && get_template() === 'hello-wpstream' ) { |
| 116 |
// Cache-busting version derived from the JS file's modified time. |
| 117 |
$modified_theme_js = gmdate( 'YmdHi', filemtime( WPSTREAM_PLUGIN_PATH . 'hello-wpstream/js/theme-dashboard.js' ) ); |
| 118 |
// The dashboard reorders items via jQuery UI sortable. |
| 119 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 120 |
|
| 121 |
// Register/enqueue the dashboard behaviour script. |
| 122 |
wp_enqueue_script( 'wpstream_theme-dashboard-js', WPSTREAM_PLUGIN_DIR_URL . 'hello-wpstream/js/theme-dashboard.js', array( 'jquery' ), $modified_theme_js, true ); |
| 123 |
// Expose admin URL and translated status strings to that script. |
| 124 |
wp_localize_script( |
| 125 |
'wpstream_theme-dashboard-js', |
| 126 |
'wpstreamDashboardVars', |
| 127 |
array( |
| 128 |
'admin_url' => get_admin_url(), |
| 129 |
'saving' => esc_html__( 'Updating your details....', 'wpstream' ), |
| 130 |
'saved' => esc_html__( 'The changes were saved', 'wpstream' ), |
| 131 |
'notsaved' => esc_html__( 'Something did not not work. Please try again.', 'wpstream' ), |
| 132 |
'createchannel' => esc_html__( 'We are creating the channel. The page will refresh after this is done.', 'wpstream' ), |
| 133 |
) |
| 134 |
); |
| 135 |
|
| 136 |
// Cache-busting version for the chunked upload helper script. |
| 137 |
$modificated_ajax_upload_js = gmdate( 'YmdHi', filemtime( WPSTREAM_PLUGIN_PATH . 'hello-wpstream/js/ajax-upload.js' ) ); |
| 138 |
// Enqueue the AJAX/plupload uploader used by the dashboard. |
| 139 |
wp_enqueue_script( |
| 140 |
'ajax-upload', |
| 141 |
WPSTREAM_PLUGIN_DIR_URL . 'hello-wpstream/js/ajax-upload.js', |
| 142 |
array( |
| 143 |
'jquery', |
| 144 |
'plupload-handlers', |
| 145 |
), |
| 146 |
$modificated_ajax_upload_js, |
| 147 |
true |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
} |