PluginProbe ʕ •ᴥ•ʔ
External Links – nofollow, noopener & new window / 2.66
External Links – nofollow, noopener & new window v2.66
2.66 0.31 0.32 0.33 0.34 0.35 1.01 1.02 1.03 1.10 1.20 1.21 1.30 1.31 1.40 1.41 1.50 1.51 1.52 1.53 1.54 1.55 1.56 1.60 1.61 1.62 1.70 1.80 1.81 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 2.3 2.32 2.35 2.40 2.42 2.43 2.45 2.46 2.47 2.48 2.50 2.51 2.55 2.56 2.57 2.58 2.59 2.60 2.61 2.62 2.63 2.64 2.65 trunk 0.10 0.11 0.12 0.20 0.21 0.30
wp-external-links / libs / wprun / class-wprun-autoloader.php
wp-external-links / libs / wprun Last commit date
class-wprun-autoloader.php 3 years ago class-wprun-base.php 1 year ago
class-wprun-autoloader.php
156 lines
1 <?php
2 /**
3 * Class WPRun_Autoloader_1x0x0
4 *
5 * @package WPRun
6 * @category WordPress Library
7 * @version 1.0.0
8
9 * @link https://www.webfactoryltd.com/
10 */
11 class WPRun_Autoloader_1x0x0
12 {
13
14 /**
15 * @var array
16 */
17 private $settings = array(
18 'file_name_prefix' => 'class-',
19 'replace_dashes_with' => '-',
20 );
21
22 /**
23 * @var array
24 */
25 private $paths = array();
26
27 /**
28 * Constructor
29 * @param array $settings Optional
30 */
31 final public function __construct( array $settings = array() )
32 {
33 $this->settings = wp_parse_args( $settings, $this->settings );
34
35 spl_autoload_register( array( $this, 'load_class' ) );
36 }
37
38 /**
39 * Add path to folder containing classes
40 * @param string $path
41 * @param boolean $include_subfolders Optional
42 * @return void
43 */
44 final public function add_path( $path, $include_subfolders = false )
45 {
46 $absolute_path = self::get_absolute_path( $path );
47
48 if ( ! is_dir( $absolute_path ) ) {
49 return;
50 }
51
52 if ( in_array( $absolute_path, $this->paths ) ) {
53 return;
54 }
55
56 $this->paths[] = $absolute_path;
57
58 // include subfolders
59 if ( true === $include_subfolders ) {
60 $entries = scandir( $absolute_path );
61
62 foreach ( $entries as $entry ) {
63 if ( '.' === $entry || '..' === $entry ) {
64 continue;
65 }
66
67 $item = $absolute_path . DIRECTORY_SEPARATOR . $entry;
68
69 if ( ! is_dir( $item ) ) {
70 continue;
71 }
72
73 $this->add_path( $item, true );
74 }
75 }
76 }
77
78 /**
79 * Get all paths
80 * @return array
81 */
82 final public function get_paths()
83 {
84 return $this->paths;
85 }
86
87 /**
88 * Loads a class file
89 * @param string $class_name
90 * @return void
91 */
92 public function load_class( $class_name )
93 {
94 // remove version postfix
95 $pure_class_name = preg_replace( '/_\d+x\d+x\d+/', '', $class_name );
96
97 $file_name = '';
98 $file_name .= $this->settings[ 'file_name_prefix' ];
99 $file_name .= str_replace( '_', $this->settings[ 'replace_dashes_with' ], $pure_class_name );
100 $file_name .= '.php';
101
102 $lower_file_name = strtolower( $file_name );
103
104 foreach ( $this->paths as $path ) {
105 $file_path = $path . DIRECTORY_SEPARATOR . $lower_file_name;
106
107 if ( file_exists( $file_path ) ) {
108 require_once $file_path;
109
110 // return if class is available else it was probably
111 // the wrong version (postfix) and should continue looking
112 if ( class_exists( $class_name ) ) {
113 return;
114 }
115 }
116 }
117 }
118
119 /**
120 * Convert to clean absolute path
121 * @param string $path
122 * @return string
123 */
124 final static protected function get_absolute_path( $path )
125 {
126 // convert to OS directory separator
127 $clean_path = str_replace( array( '/', '\\' ), DIRECTORY_SEPARATOR, $path );
128
129 $parts = array_filter( explode( DIRECTORY_SEPARATOR, $clean_path ), 'strlen' );
130
131 $absolutes = array();
132
133 foreach ( $parts as $part ) {
134 if ( '.' === $part ) {
135 continue;
136 }
137
138 if ( '..' === $part ) {
139 array_pop( $absolutes );
140 } else {
141 $absolutes[] = $part;
142 }
143 }
144
145 $absolute_path = implode( DIRECTORY_SEPARATOR, $absolutes );
146
147 // check if given path started with directory separator
148 if ( DIRECTORY_SEPARATOR === $clean_path[ 0 ] ) {
149 $absolute_path = DIRECTORY_SEPARATOR . $absolute_path;
150 }
151
152 return $absolute_path;
153 }
154
155 }
156