.php */ class Template { const THEME_DIR = 'accordion-faq'; /** * Locate a template file, honoring theme overrides. * * @param string $name Template file name, e.g. "faq-item.php". * @return string Absolute path to the template. */ public static function locate( $name ) { $name = ltrim( $name, '/' ); /** * Filter the list of directories searched for template overrides. * * @param string[] $dirs Directories (with trailing slash), searched in order. * @param string $name Template name. */ $dirs = apply_filters( 'accordion_faq_template_directories', array( trailingslashit( get_stylesheet_directory() ) . self::THEME_DIR . '/', trailingslashit( get_template_directory() ) . self::THEME_DIR . '/', ), $name ); foreach ( $dirs as $dir ) { $candidate = $dir . $name; if ( is_readable( $candidate ) ) { return $candidate; } } return ACCORDION_FAQ_PATH . 'templates/' . $name; } /** * Render a template to a string with the given data in scope. * * @param string $name Template name. * @param array $data Variables extracted into template scope. * @return string */ public static function render( $name, array $data = array() ) { ob_start(); self::output( $name, $data ); return (string) ob_get_clean(); } /** * Directly output a template with the given data in scope. * * Templates escape every value inline (esc_html/esc_attr/…), so including * them prints already-escaped markup. Nesting via this method keeps the * escaping close to each value instead of echoing a pre-built HTML string. * * @param string $name Template name. * @param array $data Variables extracted into template scope. * @return void */ public static function output( $name, array $data = array() ) { $file = self::locate( $name ); if ( ! is_readable( $file ) ) { return; } // Expose data as named variables to the template. // phpcs:ignore WordPress.PHP.DontExtract.extract_extract -- controlled, plugin-defined keys only. extract( $data, EXTR_SKIP ); include $file; } }