Templates.php
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Templates
*
* @since 4.0.17
*
* @package AIOSEO\Plugin\Common\Utils
*/
class Templates {
/**
* This plugin absolute path.
*
* @since 4.0.17
*
* @var string
*/
protected $pluginPath = AIOSEO_DIR;
/**
* Paths were our template files are located.
*
* @since 4.0.17
*
* @var string Array of paths.
*/
protected $paths = [
'app/Common/Views'
];
/**
*
* The theme folder.
*
* @since 4.0.17
*
* @var string
*/
private $themeTemplatePath = 'aioseo/';
/**
*
* A theme subfolder.
*
* @since 4.0.17
*
* @var string
*/
protected $themeTemplateSubpath = '';
/**
* Locate a template file in the theme or our plugin paths.
*
* @since 4.0.17
*
* @param string $templateName The template name.
* @return string The template absolute path.
*/
public function locateTemplate( $templateName ) {
// Try to find template file in the theme.
$template = locate_template(
[
trailingslashit( $this->getThemeTemplatePath() ) . trailingslashit( $this->getThemeTemplateSubpath() ) . $templateName
]
);
if ( ! $template ) {
// Try paths, in order.
foreach ( $this->paths as $path ) {
$template = trailingslashit( $this->addPluginPath( $path ) ) . $templateName;
if ( aioseo()->core->fs->exists( $template ) ) {
break;
}
}
}
return apply_filters( 'aioseo_locate_template', $template, $templateName );
}
/**
* Includes a template if the file exists.
*
* @param string $templateName The template path/name.php to be included.
* @param null $data Data passed down to the template.
* @return void
*/
public function getTemplate( $templateName, $data = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$template = $this->locateTemplate( $templateName );
if ( ! empty( $template ) and aioseo()->core->fs->exists( $template ) ) {
include $template;
}
}
/**
* Add this plugin path when trying the paths.
*
* @since 4.0.17
*
* @param string $path A path.
* @return string A path with the plugin absolute path.
*/
protected function addPluginPath( $path ) {
return trailingslashit( $this->pluginPath ) . $path;
}
/**
* Returns the theme folder for templates.
*
* @since 4.0.17
*
* @return string The theme folder for templates.
*/
public function getThemeTemplatePath() {
return apply_filters( 'aioseo_template_path', $this->themeTemplatePath );
}
/**
*
* Returns the theme subfolder for templates.
*
* @since 4.0.17
*
* @return string The theme subfolder for templates.
*/
public function getThemeTemplateSubpath() {
return apply_filters( 'aioseo_template_subpath', $this->themeTemplateSubpath );
}
}