PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / foopluginbase / classes / class-foo-plugin-file-locator.php
foogallery / includes / foopluginbase / classes Last commit date
class-foo-friendly-dates.php 2 months ago class-foo-plugin-base.php 2 months ago class-foo-plugin-file-locator.php 2 months ago class-foo-plugin-metabox-sanity.php 2 months ago class-foo-plugin-options.php 2 months ago class-foo-plugin-settings.php 2 months ago class-foo-plugin-textdomain.php 2 months ago index.php 2 months ago
class-foo-plugin-file-locator.php
171 lines
1 <?php
2 /**
3 * File Locator for Plugins.
4 * Based on ideas found in Gamajo-Template-Loader by Gary Jones http://github.com/GaryJones/Gamajo-Template-Loader
5 *
6 * @package Foo_Plugin_Base
7 * @author Brad Vincent
8 * @link https://github.com/fooplugins/Foo_Plugin_Base
9 * @copyright 2014 Brad Vincent
10 * @license GPL-2.0+
11 * @version 1.0.0
12 */
13
14 if ( !class_exists( 'Foo_Plugin_File_Locator_v1' ) ) {
15
16 /**
17 * File loader.
18 *
19 * Create a new class that extends this one and override the properties.
20 */
21 class Foo_Plugin_File_Locator_v1 {
22
23 /**
24 * Prefix for filter names.
25 *
26 * @since 1.0.0
27 *
28 * @type string
29 */
30 protected $plugin_slug;
31
32 /**
33 * Directory name where custom files for this plugin should be found in the theme.
34 *
35 * @since 1.0.0
36 *
37 * @type string
38 */
39 protected $theme_file_directory; //will use the plugin_slug if not set
40
41 /**
42 * Reference to the plugin root file.
43 *
44 * @since 1.0.0
45 *
46 * @type string
47 */
48 protected $plugin_file; // usually you would just pass in __FILE__
49
50 /**
51 * Directory name where files are found in this plugin.
52 *
53 * Can either be a defined constant, or a relative reference from where the subclass lives.
54 *
55 * @since 1.1.0
56 *
57 * @type string
58 */
59 protected $plugin_file_directory; // or includes/templates, etc.
60
61 protected $extra_locations = array();
62
63 public function __construct($plugin_slug, $plugin_file, $plugin_file_directory = '', $theme_file_directory = '') {
64 $this->plugin_file = $plugin_file;
65 $this->plugin_slug = $plugin_slug;
66
67 //if we did not pass in a theme directory, then default it to the plugin slug
68 if ( empty($theme_file_directory) ) {
69 $theme_file_directory = $plugin_slug;
70 }
71 $this->theme_file_directory = $theme_file_directory;
72 $this->plugin_file_directory = $plugin_file_directory;
73 }
74
75 /**
76 * Locates a file
77 *
78 * @since 1.0.0
79 *
80 * @param string $filename
81 *
82 * @return string
83 */
84 public function locate_file($filename) {
85
86 //allow the filename to be overridden
87 $filename = apply_filters( $this->plugin_slug . '_locate_file_filename', $filename );
88
89 // No file found yet
90 $located = false;
91
92 $possible_locations = $this->get_locations();
93
94 // try locating the file by looping through the file paths
95 foreach ( $possible_locations as $location ) {
96 $path = trailingslashit( $location['path'] );
97 if ( file_exists( $path . $filename ) ) {
98 $located = array(
99 'path' => $path . $filename,
100 'url' => trailingslashit( $location['url'] ) . $filename
101 );
102 break;
103 }
104 }
105
106 if ( $located ) {
107 do_action( $this->plugin_slug . '_located_file', $filename, $located );
108 } else {
109 do_action( $this->plugin_slug . '_not_located_file', $filename );
110 }
111
112 return $located;
113 }
114
115 /**
116 * Return a list of paths to check for file locations.
117 *
118 * Default is to check in a child theme (if relevant) before a parent theme, so that themes which inherit from a
119 * parent theme can just overload one file. If the file is not found in either of those, it looks in the
120 * theme-compat folder last.
121 *
122 * @since 1.0.0
123 *
124 * @return mixed|void
125 */
126 protected function get_locations() {
127 $theme_directory = trailingslashit( $this->theme_file_directory );
128
129 $locations = array(
130 10 => array(
131 'path' => trailingslashit( get_template_directory() ) . $theme_directory,
132 'url' => trailingslashit( get_template_directory_uri() ) . $theme_directory
133 ),
134 1000 => array(
135 'path' => trailingslashit( plugin_dir_path( $this->plugin_file ) ) . $this->plugin_file_directory,
136 'url' => trailingslashit( plugin_dir_url( $this->plugin_file ) ) . $this->plugin_file_directory
137 )
138 );
139
140 // Only add this conditionally, so non-child themes don't redundantly check active theme twice.
141 if ( is_child_theme() ) {
142 $locations[1] = array(
143 'path' => trailingslashit( get_stylesheet_directory() ) . $theme_directory,
144 'url' => trailingslashit( get_stylesheet_directory_uri() ) . $theme_directory
145 );
146 }
147
148 //add any extra locations that may have been added
149 $locations = $locations + $this->extra_locations;
150
151 /**
152 * Allow ordered list of template paths to be amended.
153 *
154 * @since 1.0.0
155 *
156 * @param array $var Default is directory in child theme at index 1, parent theme at 10, and plugin at 100.
157 */
158 $locations = apply_filters( $this->plugin_slug . '_file_locator_pickup_locations', $locations );
159
160 // sort the file paths based on priority
161 ksort( $locations, SORT_NUMERIC );
162
163 return $locations;
164 }
165
166 public function add_location( $position, $location ) {
167 $this->extra_locations[$position] = $location;
168 }
169 }
170 }
171