| @@ -1,73 +1,35 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * Template loader for the WpStream plugin. | |
| 4 | + * Template loader for the WpStream plugin | |
| 5 | 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 | 6 | * @package wpstream-plugin |
| 12 | 7 | */ |
| 13 | 8 | |
| 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 | 9 | class WpStream_Template_Loader { |
| 24 | - /** | |
| 25 | - * Hook the template registration, resolution and script enqueue callbacks. | |
| 26 | - */ | |
| 27 | 10 | public function __construct() { |
| 28 | - // Offer the Dashboard page template in the editor (late priority to win over others). | |
| 29 | 11 | 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 | 12 | add_filter( 'template_include', array( $this, 'include_templates' ) ); |
| 32 | - // Enqueue dashboard scripts when the Dashboard template is in use. | |
| 33 | 13 | add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_dashboard_scripts' ) ); |
| 34 | 14 | } |
| 35 | 15 | |
| 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 | 16 | public function wpstream_add_page_templates( $templates ) { |
| 43 | - // Only expose the template when the companion theme is active. | |
| 44 | 17 | if ( get_template() === 'hello-wpstream' ) { |
| 45 | 18 | // Add the Dashboard template |
| 46 | 19 | $templates['wpstream-theme-dashboard.php'] = __('WpStream Dashboard Page', 'wpstream'); |
| 47 | 20 | } |
| 48 | 21 | |
| 49 | - // Return the (possibly extended) template list. | |
| 50 | 22 | return $templates; |
| 51 | 23 | } |
| 52 | 24 | |
| 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 | 25 | public function include_templates( $template ) { |
| 60 | - // Page template override: only for real pages under the companion theme. | |
| 61 | 26 | if ( is_page() && get_template() === 'hello-wpstream' ) { |
| 62 | - // Which page template was assigned to this page? | |
| 63 | 27 | $page_template = get_page_template_slug(); |
| 64 | 28 | |
| 65 | - // Route the Dashboard template to the plugin's own file. | |
| 66 | 29 | if ( 'wpstream-theme-dashboard.php' === $page_template ) { |
| 67 | 30 | $file = WPSTREAM_PLUGIN_PATH . 'hello-wpstream/page-templates/wpstream-theme-dashboard.php'; |
| 68 | 31 | |
| 69 | - // Use it only if the file actually exists. | |
| 70 | 32 | if ( file_exists( $file ) ) { |
| 71 | 33 | return $file; |
| 72 | 34 | } |
| 73 | 35 | } |
| @@ -74,12 +36,10 @@ | ||
| 74 | 36 | } |
| 75 | 37 | |
| 76 | 38 | // Single templates for custom post types |
| 77 | 39 | if ( get_template() === 'hello-wpstream' ) { |
| 78 | - // Base directory holding the plugin's single-CPT templates. | |
| 79 | 40 | $single_template_path = WPSTREAM_PLUGIN_PATH . 'hello-wpstream/single-templates/'; |
| 80 | 41 | |
| 81 | - // Free live-stream product single view. | |
| 82 | 42 | if ( is_singular( 'wpstream_product' ) ) { |
| 83 | 43 | $template_file = $single_template_path . 'single-wpstream_product.php'; |
| 84 | 44 | if ( file_exists( $template_file ) ) { |
| 85 | 45 | $template = $template_file; |
| @@ -85,9 +45,8 @@ | ||
| 85 | 45 | $template = $template_file; |
| 86 | 46 | } |
| 87 | 47 | } |
| 88 | 48 | |
| 89 | - // VOD product single view. | |
| 90 | 49 | if ( is_singular( 'wpstream_product_vod' ) ) { |
| 91 | 50 | $template_file = $single_template_path . 'single-wpstream_product_vod.php'; |
| 92 | 51 | if ( file_exists( $template_file ) ) { |
| 93 | 52 | $template = $template_file; |
| @@ -93,11 +52,10 @@ | ||
| 93 | 52 | $template = $template_file; |
| 94 | 53 | } |
| 95 | 54 | } |
| 96 | 55 | |
| 97 | - // Bundle single view. | |
| 98 | - if ( is_singular( 'wpstream_bundles' ) ) { | |
| 99 | - $template_file = $single_template_path . 'single-wpstream_bundles.php'; | |
| 56 | + if ( is_post_type_archive( 'wpstream_bundles' ) ) { | |
| 57 | + $template_file = $single_template_path . 'archive-wpstream_bundles.php'; | |
| 100 | 58 | if ( file_exists( $template_file ) ) { |
| 101 | 59 | $template = $template_file; |
| 102 | 60 | } |
| 103 | 61 | } |
| @@ -102,41 +60,30 @@ | ||
| 102 | 60 | } |
| 103 | 61 | } |
| 104 | 62 | } |
| 105 | 63 | |
| 106 | - // No override matched (or file missing): keep the original template. | |
| 107 | 64 | return $template; |
| 108 | 65 | } |
| 109 | 66 | |
| 110 | - /** | |
| 111 | - * Enqueue the dashboard JS (and its localized strings) on the Dashboard page. | |
| 112 | - */ | |
| 113 | 67 | public function maybe_enqueue_dashboard_scripts() { |
| 114 | - // Only run on the Dashboard template under the companion theme. | |
| 115 | 68 | 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 | 69 | $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 | 70 | wp_enqueue_script( 'jquery-ui-sortable' ); |
| 120 | 71 | |
| 121 | - // Register/enqueue the dashboard behaviour script. | |
| 122 | 72 | 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 | 73 | wp_localize_script( |
| 125 | 74 | 'wpstream_theme-dashboard-js', |
| 126 | 75 | 'wpstreamDashboardVars', |
| 127 | 76 | array( |
| 128 | 77 | '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' ), | |
| 78 | + 'saving' => esc_html__( 'Updating your details....', 'hello-wpstream' ), | |
| 79 | + 'saved' => esc_html__( 'The changes were saved', 'hello-wpstream' ), | |
| 80 | + 'notsaved' => esc_html__( 'Something did not not work. Please try again.', 'hello-wpstream' ), | |
| 81 | + 'createchannel' => esc_html__( 'We are creating the channel. The page will refresh after this is done.', 'hello-wpstream' ), | |
| 133 | 82 | ) |
| 134 | 83 | ); |
| 135 | 84 | |
| 136 | - // Cache-busting version for the chunked upload helper script. | |
| 137 | 85 | $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 | 86 | wp_enqueue_script( |
| 140 | 87 | 'ajax-upload', |
| 141 | 88 | WPSTREAM_PLUGIN_DIR_URL . 'hello-wpstream/js/ajax-upload.js', |
| 142 | 89 | array( |