| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Template { |
| 10 |
|
| 11 |
public static function template_path() { |
| 12 |
return apply_filters( 'storeengine/template_path', 'storeengine/' ); |
| 13 |
} |
| 14 |
|
| 15 |
public static function plugin_path() { |
| 16 |
return apply_filters( 'storeengine/plugin_path', STOREENGINE_ROOT_DIR_PATH ); |
| 17 |
} |
| 18 |
|
| 19 |
public static function get_template_part( $slug, $name = '' ) { |
| 20 |
$template = false; |
| 21 |
if ( $name ) { |
| 22 |
$template = STOREENGINE_TEMPLATE_DEBUG_MODE ? '' : locate_template( |
| 23 |
array( |
| 24 |
"{$slug}-{$name}.php", |
| 25 |
self::template_path() . "{$slug}-{$name}.php", |
| 26 |
) |
| 27 |
); |
| 28 |
|
| 29 |
if ( ! $template ) { |
| 30 |
$fallback = self::plugin_path() . "/templates/{$slug}-{$name}.php"; |
| 31 |
$template = file_exists( $fallback ) ? $fallback : ''; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! $template ) { |
| 36 |
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/storeengine/slug.php. |
| 37 |
if ( STOREENGINE_TEMPLATE_DEBUG_MODE ) { |
| 38 |
$template = ''; |
| 39 |
} else { |
| 40 |
$template = locate_template( [ "{$slug}.php", self::template_path() . "{$slug}.php" ] ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Allow 3rd party plugins to filter template file from their plugin. |
| 45 |
$template = apply_filters( 'storeengine/get_template_part', $template, $slug, $name ); |
| 46 |
|
| 47 |
if ( $template ) { |
| 48 |
load_template( $template, false ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public static function locate_template( $template_name, $template_path = '', $default_path = '' ) { |
| 53 |
if ( ! $template_path ) { |
| 54 |
$template_path = self::template_path(); |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! $default_path ) { |
| 58 |
$default_path = self::plugin_path() . 'templates/'; |
| 59 |
} |
| 60 |
|
| 61 |
// Look within passed path within the theme - this is priority. |
| 62 |
if ( false !== strpos( $template_name, 'storeengine_product_category' ) || false !== strpos( $template_name, 'storeengine_product_tag' ) ) { |
| 63 |
$cs_template = str_replace( '_', '-', $template_name ); |
| 64 |
$template = locate_template( |
| 65 |
array( |
| 66 |
trailingslashit( $template_path ) . $cs_template, |
| 67 |
$cs_template, |
| 68 |
) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
if ( empty( $template ) ) { |
| 73 |
$template = locate_template( |
| 74 |
array( |
| 75 |
trailingslashit( $template_path ) . $template_name, |
| 76 |
$template_name, |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
// Get default template/. |
| 82 |
if ( ! $template || STOREENGINE_TEMPLATE_DEBUG_MODE ) { |
| 83 |
if ( empty( $cs_template ) ) { |
| 84 |
$template = $default_path . $template_name; |
| 85 |
} else { |
| 86 |
$template = $default_path . $cs_template; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Return what we found. |
| 91 |
return apply_filters( 'storeengine/locate_template', $template, $template_name, $template_path ); |
| 92 |
} |
| 93 |
|
| 94 |
public static function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
| 95 |
$template = self::locate_template( $template_name, $template_path, $default_path ); |
| 96 |
|
| 97 |
// Allow 3rd party plugin filter template file from their plugin. |
| 98 |
$filter_template = apply_filters( 'storeengine/get_template', $template, $template_name, $args, $template_path, $default_path ); |
| 99 |
|
| 100 |
if ( $filter_template !== $template ) { |
| 101 |
if ( ! file_exists( $filter_template ) ) { |
| 102 |
/* translators: %s template */ |
| 103 |
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( '%s does not exist.', 'storeengine' ), '<code>' . esc_html( $filter_template ) . '</code>' ), '1.0.0' ); |
| 104 |
|
| 105 |
return; |
| 106 |
} |
| 107 |
$template = $filter_template; |
| 108 |
} |
| 109 |
|
| 110 |
$action_args = array( |
| 111 |
'template_name' => $template_name, |
| 112 |
'template_path' => $template_path, |
| 113 |
'located' => $template, |
| 114 |
'args' => $args, |
| 115 |
); |
| 116 |
|
| 117 |
if ( ! empty( $args ) && is_array( $args ) ) { |
| 118 |
if ( isset( $args['action_args'] ) ) { |
| 119 |
_doing_it_wrong( |
| 120 |
__FUNCTION__, |
| 121 |
sprintf( |
| 122 |
// translators: %s. Class-Method name. |
| 123 |
esc_html__( 'The “action_args” can not be overwritten when calling %s.', 'storeengine' ), |
| 124 |
esc_html( __METHOD__ ) |
| 125 |
), |
| 126 |
'1.0.0' |
| 127 |
); |
| 128 |
unset( $args['action_args'] ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* This use of extract() cannot be removed. There are many possible ways that |
| 133 |
* templates could depend on variables that it creates existing, and no way to |
| 134 |
* detect and deprecate it. |
| 135 |
* |
| 136 |
* Passing the EXTR_SKIP flag is the safest option, ensuring globals and |
| 137 |
* function variables cannot be overwritten. |
| 138 |
* @see load_template() |
| 139 |
*/ |
| 140 |
extract( $args, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract |
| 141 |
} |
| 142 |
|
| 143 |
do_action( 'storeengine/before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] ); |
| 144 |
|
| 145 |
include $action_args['located']; |
| 146 |
|
| 147 |
do_action( 'storeengine/after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns rendered content for the template. |
| 152 |
* |
| 153 |
* @param string $template_name Template file name/path. |
| 154 |
* @param array $args Args to decode (will be available as variable inside the template). |
| 155 |
* @param string $template_path Template path (relative from default/root path). |
| 156 |
* @param string $default_path Template root path (default to plugins template directory). |
| 157 |
* |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
public static function get_template_content( string $template_name, array $args = [], string $template_path = '', string $default_path = '' ): string { |
| 161 |
ob_start(); |
| 162 |
self::get_template( $template_name, $args, $template_path, $default_path ); |
| 163 |
$rendered = (string) ob_get_clean(); |
| 164 |
|
| 165 |
return apply_filters( 'storeengine/template/get_content', $rendered, $template_name, $args, $template_path, $default_path ); |
| 166 |
} |
| 167 |
} |
| 168 |
|