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