-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinpsyde-plugin-templating-utils.php
More file actions
218 lines (181 loc) · 5.45 KB
/
inpsyde-plugin-templating-utils.php
File metadata and controls
218 lines (181 loc) · 5.45 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the plugin-templating-utils package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Inpsyde;
if ( function_exists( __NAMESPACE__ . '\\plugin_file_base_dir' ) ) {
return;
}
/**
* Given a file a file inside a plugin directory, no matter how deep in the directory three, returns the absolute path
* to root directory of the plugin.
*
* @param string $plugin_file
*
* @return string
*
* @since 0.1.0
*/
function plugin_file_base_dir( $plugin_file ) {
global $wp_plugin_paths;
$plugin_dir = untrailingslashit( wp_normalize_path( WP_PLUGIN_DIR ) );
$plugin_file = wp_normalize_path( $plugin_file );
foreach ( $wp_plugin_paths as $dir => $real_dir ) {
if ( strpos( $plugin_file, $real_dir ) === 0 ) {
return $dir;
}
}
if ( ! preg_match( '#^' . preg_quote( $plugin_dir, '#' ) . '/([^/]+)/.+?\.php$#', $plugin_file, $matches ) ) {
return '';
}
return $plugin_dir . '/'. $matches[ 1 ];
}
/**
* Look for a template from a plugin directory.
* The plugin folder where to search is obtained from first argument, that can be a file in the target plugin directory,
* no matter how deep in the directory three.
*
* @param string $plugin_file
* @param string $slug
* @param null $name
* @param array $args
*
* @return string
*
* @since 0.2.0
*/
function find_plugin_template_part( $plugin_file, $slug, $name = null, array $args = [] ) {
$base = plugin_file_base_dir( $plugin_file );
if ( ! $base ) {
return '';
}
$templates = [];
$name = (string) $name;
$name and $templates[] = "{$slug}-{$name}.php";
$templates[] = "{$slug}.php";
$templates = apply_filters( "plugin_template_part_templates", $templates, $base, $slug, $name, $plugin_file, $args );
$base = trailingslashit( $base );
foreach ( (array) $templates as $template ) {
if ( is_string( $template ) && file_exists( $base . $template ) ) {
return $base . $template;
}
}
return '';
}
/**
* Similar to `get_template_part()` loads a template from a plugin directory.
* The plugin folder where to search is obtained from first argument, that can be a file in the target plugin directory,
* no matter how deep in the directory three.
*
* @param string $plugin_file
* @param string $slug
* @param null $name
* @param array $args
*
* @return bool
*
* @since 0.1.0
*/
function plugin_template_part( $plugin_file, $slug, $name = null, array $args = [] ) {
$template = find_plugin_template_part( $plugin_file, $slug, $name, $args );
if ( $template ) {
/** @noinspection PhpIncludeInspection */
include $template;
return TRUE;
}
return FALSE;
}
/**
* Similar to `get_theme_file_path()` returns the path of a file inside a plugin directory.
* The target plugin is obtained from first argument, that can be a file in the target plugin directory,
* no matter how deep in the directory three.
*
* @param string $plugin_file
* @param string $file
*
* @return string
*
* @since 0.1.0
*/
function plugin_file_path( $plugin_file, $file ) {
$base = plugin_file_base_dir( $plugin_file );
if ( ! $base ) {
return '';
}
$base = trailingslashit( $base );
$file = (string) apply_filters( 'plugin_file_path', $base . $file, $file, $base );
return file_exists( $file ) ? $file : '';
}
/**
* Similar to `get_theme_file_uri()` returns the URL of a file inside a plugin directory.
* The target plugin is obtained from first argument, that can be a file in the target plugin directory,
* no matter how deep in the directory three.
*
* @param string $plugin_file
* @param string $file
*
* @return string
*
* @since 0.1.0
*/
function plugin_file_uri( $plugin_file, $file ) {
$base = plugin_file_base_dir( $plugin_file );
$base and $base = trailingslashit( $base );
if ( ! $base || ! file_exists( $base . $file ) ) {
return '';
}
return (string) apply_filters( 'plugin_file_uri', plugins_url( $file, $base . 'plugin.php' ), $file, $base );
}
/**
* Like `plugin_template_part()`, but fallbacks to theme (or child theme) if file is not found in plugin.
*
* @param string $plugin_file
* @param string $slug
* @param string|null $name
* @param string|null $theme_slug
* @param array $args
*
* @since 0.1.0
*/
function plugin_template_part_fallback( $plugin_file, $slug, $name = null, $theme_slug = null, array $args = [] ) {
if ( ! plugin_template_part( $plugin_file, $slug, $name, $args ) ) {
get_template_part( $theme_slug ? : $slug, $name, $args );
}
}
/**
* Like `plugin_file_path()`, but fallbacks to theme (or child theme) if file is not found in plugin.
*
* @param string $plugin_file
* @param string $file
* @param string|null $theme_file
*
* @return string
*
* @since 0.1.0
*/
function plugin_file_path_fallback( $plugin_file, $file, $theme_file = null ) {
$path = plugin_file_path( $plugin_file, $file );
$path or $path = get_theme_file_path( $theme_file ? : $file );
return $path;
}
/**
* Like `plugin_file_uri()`, but fallbacks to theme (or child theme) if file is not found in plugin.
*
* @param string $plugin_file
* @param string $file
* @param string|null $theme_file
*
* @return string
*
* @since 0.1.0
*/
function plugin_file_uri_fallback( $plugin_file, $file, $theme_file = null ) {
$url = plugin_file_uri( $plugin_file, $file );
$url or $url = get_theme_file_uri( $theme_file ? : $file );
return $url;
}