|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YIKES Inc. Easy Forms. |
|
4
|
|
|
* |
|
5
|
|
|
* @package YIKES\EasyForms |
|
6
|
|
|
* @author Freddie Mixell |
|
7
|
|
|
* @license GPL2 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace YIKES\EasyForms\View; |
|
11
|
|
|
|
|
12
|
|
|
use YIKES\EasyForms\Exception\InvalidURI; |
|
13
|
|
|
use YIKES\EasyForms\PluginHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class TemplatedView. |
|
17
|
|
|
* |
|
18
|
|
|
* Looks within the child theme and parent theme folders first for a view, |
|
19
|
|
|
* before defaulting to the plugin folder. |
|
20
|
|
|
* |
|
21
|
|
|
* @since %VERSION% |
|
22
|
|
|
* |
|
23
|
|
|
* @package YIKES\EasyForms |
|
24
|
|
|
* @author Freddie Mixell |
|
25
|
|
|
*/ |
|
26
|
|
|
class TemplatedView extends BaseView { |
|
27
|
|
|
|
|
28
|
|
|
use PluginHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Validate an URI. |
|
32
|
|
|
* |
|
33
|
|
|
* @since %VERSION% |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $uri URI to validate. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string Validated URI. |
|
38
|
|
|
* @throws InvalidURI If an invalid URI was passed into the View. |
|
39
|
|
|
*/ |
|
40
|
|
View Code Duplication |
protected function validate( $uri ) { |
|
|
|
|
|
|
41
|
|
|
$uri = $this->check_extension( $uri, static::VIEW_EXTENSION ); |
|
42
|
|
|
|
|
43
|
|
|
foreach ( $this->get_locations( $uri ) as $location ) { |
|
44
|
|
|
if ( is_readable( $location ) ) { |
|
45
|
|
|
return $location; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ( ! is_readable( $uri ) ) { |
|
50
|
|
|
throw InvalidURI::from_uri( $uri ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $uri; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the possible locations for the view. |
|
58
|
|
|
* |
|
59
|
|
|
* @since %VERSION% |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $uri URI of the view to get the locations for. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array Array of possible locations. |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function get_locations( $uri ) { |
|
66
|
|
|
/** |
|
67
|
|
|
* Filter the available locations for view templates to be found. |
|
68
|
|
|
* |
|
69
|
|
|
* Locations will be tried in the order provided by the array, |
|
70
|
|
|
* so locations with higher priority should be first. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $locations |
|
73
|
|
|
*/ |
|
74
|
|
|
$locations = (array) apply_filters( 'lpf_templated_view_locations', [ |
|
75
|
|
|
trailingslashit( get_stylesheet_directory() ) . "lpf/{$uri}", |
|
76
|
|
|
trailingslashit( get_template_directory() ) . "lpf/{$uri}", |
|
77
|
|
|
] ); |
|
78
|
|
|
|
|
79
|
|
|
// Ensure the plugin folder is always available. |
|
80
|
|
|
$locations[] = trailingslashit( $this->get_root_dir() ) . $uri; |
|
81
|
|
|
|
|
82
|
|
|
return $locations; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.