display_name] with theme entries overriding plugin entries by slug //built-in slugs render in the order below; anything else follows alphabetically by name function AdvancedSteamWidget_get_templates() { $template_order = array('profile', 'profile-small', 'profile-games', 'games', 'grid', 'full'); $templates = array(); $paths = array( ADVANCEDSTEAMWIDGET_PLUGIN_DIR . '/templates', get_stylesheet_directory() . '/advanced-steam-widget', ); //scan plugin first, then theme so theme entries win the merge foreach ($paths as $path) { if (!is_dir($path)) continue; foreach (glob($path . '/*.php') as $file) { $slug = basename($file, '.php'); $header = get_file_data($file, array('name' => 'Template Name')); $templates[$slug] = !empty($header['name']) ? $header['name'] : ucwords(str_replace('-', ' ', $slug)); } } //split into built-in (in hardcoded order) and the rest (alphabetical by name) $ordered = array(); foreach ($template_order as $slug) { if (isset($templates[$slug])) { $ordered[$slug] = $templates[$slug]; unset($templates[$slug]); } } uasort($templates, 'strcasecmp'); return $ordered + $templates; } //resolves a template slug to a file path //theme dir wins over plugin dir; returns empty string if slug doesn't resolve anywhere function AdvancedSteamWidget_locate_template($slug) { $theme_path = get_stylesheet_directory() . '/advanced-steam-widget/' . $slug . '.php'; if (file_exists($theme_path)) return $theme_path; $plugin_path = ADVANCEDSTEAMWIDGET_PLUGIN_DIR . '/templates/' . $slug . '.php'; if (file_exists($plugin_path)) return $plugin_path; return ''; } //reads the "Template Style" header from a template file and enqueues the resolved CSS //$template_file must be the absolute path returned by AdvancedSteamWidget_locate_template() //no-op if the header is missing or the referenced CSS file doesn't exist //shared by the render path (called from inside each template) and the editor preload (admin.php) function AdvancedSteamWidget_enqueue_template_style($template_file) { //parse the "Template Style: " header - single lightweight file read $header = get_file_data($template_file, array('style' => 'Template Style')); if (empty($header['style'])) return; //CSS path is always relative to the template file itself $rel = ltrim($header['style'], '/'); $css_path = dirname($template_file) . '/' . $rel; if (!file_exists($css_path)) return; //build a public URL for whichever known root the CSS lives under (plugin or active theme) $src = ''; $plugin_dir = wp_normalize_path(ADVANCEDSTEAMWIDGET_PLUGIN_DIR); $theme_dir = wp_normalize_path(get_stylesheet_directory()); $norm_css = wp_normalize_path($css_path); if (strpos($norm_css, $plugin_dir . '/') === 0) { $src = plugins_url(substr($norm_css, strlen($plugin_dir) + 1), ADVANCEDSTEAMWIDGET_PLUGIN_FILE); } elseif (strpos($norm_css, $theme_dir . '/') === 0) { $src = get_stylesheet_directory_uri() . '/' . substr($norm_css, strlen($theme_dir) + 1); } else return; //handle keyed off template slug so editor preload and render-time enqueue collapse to one style tag $slug = basename($template_file, '.php'); wp_enqueue_style('advanced-steam-widget-' . $slug, $src, array(), filemtime($css_path)); } //renders a template with $steam and $args in scope //falls back to games if slug doesn't resolve; templates handle their own CSS function AdvancedSteamWidget_render_template($slug, $steam, $args) { $file = AdvancedSteamWidget_locate_template($slug); if (!$file) { $file = AdvancedSteamWidget_locate_template('games'); if (!$file) { trigger_error('Advanced Steam Widget: template files missing. Could not render widget.', E_USER_WARNING); return; } } include $file; }