1
|
|
|
<?php |
|
|
|
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // security check, don't load file outside WP |
3
|
|
|
/** |
4
|
|
|
* Loading all class files within the Sensei/includes directory |
5
|
|
|
* |
6
|
|
|
* The auto loader class listens for calls to classes within Sensei and loads |
7
|
|
|
* the file containing the class. |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* @since 1.9.0 |
11
|
|
|
*/ |
12
|
|
|
class Sensei_Autoloader { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var path to the includes directory within Sensei. |
16
|
|
|
*/ |
17
|
|
|
private $include_path = 'includes'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array $class_file_map. List of classes mapped to their files |
21
|
|
|
*/ |
22
|
|
|
private $class_file_map = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
* @since 1.9.0 |
27
|
|
|
*/ |
28
|
|
|
public function __construct(){ |
29
|
|
|
|
30
|
|
|
// make sure we do not override an existing autoload function |
31
|
|
|
if( function_exists('__autoload') ){ |
32
|
|
|
spl_autoload_register( '__autoload' ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// setup a relative path for the current autoload instance |
36
|
|
|
$this->include_path = trailingslashit( untrailingslashit( dirname( __FILE__ ) ) ); |
37
|
|
|
|
38
|
|
|
//setup the class file map |
39
|
|
|
$this->initialize_class_file_map(); |
40
|
|
|
|
41
|
|
|
// add Sensei custom auto loader |
42
|
|
|
spl_autoload_register( array( $this, 'autoload' ) ); |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generate a list of Sensei class and map them the their respective |
48
|
|
|
* files within the includes directory |
49
|
|
|
* |
50
|
|
|
* @since 1.9.0 |
51
|
|
|
*/ |
52
|
|
|
public function initialize_class_file_map(){ |
53
|
|
|
|
54
|
|
|
$this->class_file_map = array( |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Main Sensei class |
58
|
|
|
*/ |
59
|
|
|
'Sensei_Main' => 'class-sensei.php', |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Admin |
63
|
|
|
*/ |
64
|
|
|
'Sensei_Welcome' => 'admin/class-sensei-welcome.php' , |
65
|
|
|
'Sensei_Learner_Management' => 'admin/class-sensei-learner-management.php' , |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Shortcodes |
69
|
|
|
*/ |
70
|
|
|
'Sensei_Shortcode_Loader' => 'shortcodes/class-sensei-shortcode-loader.php', |
71
|
|
|
'Sensei_Shortcode_Interface' => 'shortcodes/interface-sensei-shortcode.php', |
72
|
|
|
'Sensei_Shortcode_Featured_Courses' => 'shortcodes/class-sensei-shortcode-featured-courses.php', |
73
|
|
|
'Sensei_Shortcode_User_Courses' => 'shortcodes/class-sensei-shortcode-user-courses.php', |
74
|
|
|
'Sensei_Shortcode_Courses' => 'shortcodes/class-sensei-shortcode-courses.php', |
75
|
|
|
'Sensei_Shortcode_Teachers' => 'shortcodes/class-sensei-shortcode-teachers.php', |
76
|
|
|
'Sensei_Shortcode_User_Messages' => 'shortcodes/class-sensei-shortcode-user-messages.php', |
77
|
|
|
'Sensei_Shortcode_Course_Page' => 'shortcodes/class-sensei-shortcode-course-page.php', |
78
|
|
|
'Sensei_Shortcode_Lesson_Page' => 'shortcodes/class-sensei-shortcode-lesson-page.php', |
79
|
|
|
'Sensei_Shortcode_Course_Categories' => 'shortcodes/class-sensei-shortcode-course-categories.php', |
80
|
|
|
'Sensei_Shortcode_Unpurchased_Courses' => 'shortcodes/class-sensei-shortcode-unpurchased-courses.php', |
81
|
|
|
'Sensei_Legacy_Shortcodes' => 'shortcodes/class-sensei-legacy-shortcodes.php', |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Built in theme integration support |
85
|
|
|
*/ |
86
|
|
|
'Sensei_Theme_Integration_Loader' => 'theme-integrations/theme-integration-loader.php', |
87
|
|
|
'Sensei__S' => 'theme-integrations/_s.php', |
88
|
|
|
'Sensei_Twentyeleven' => 'theme-integrations/twentyeleven.php', |
89
|
|
|
'Sensei_Twentytwelve' => 'theme-integrations/twentytwelve.php', |
90
|
|
|
'Sensei_Twentythirteen' => 'theme-integrations/Twentythirteen.php', |
91
|
|
|
'Sensei_Twentyfourteen' => 'theme-integrations/Twentyfourteen.php', |
92
|
|
|
'Sensei_Twentyfifteen' => 'theme-integrations/Twentyfifteen.php', |
93
|
|
|
'Sensei_Twentysixteen' => 'theme-integrations/Twentysixteen.php', |
94
|
|
|
'Sensei_Storefront' => 'theme-integrations/Storefront.php', |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* WooCommerce |
98
|
|
|
*/ |
99
|
|
|
'Sensei_WC' => 'class-sensei-wc.php', |
100
|
|
|
|
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Autoload all sensei files as the class names are used. |
106
|
|
|
*/ |
107
|
|
|
public function autoload( $class ){ |
108
|
|
|
|
109
|
|
|
// only handle classes with the word `sensei` in it |
110
|
|
|
if( ! is_numeric( strpos ( strtolower( $class ), 'sensei') ) ){ |
111
|
|
|
|
112
|
|
|
return; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// exit if we didn't provide mapping for this class |
117
|
|
|
if( isset( $this->class_file_map[ $class ] ) ){ |
118
|
|
|
|
119
|
|
|
$file_location = $this->include_path . $this->class_file_map[ $class ]; |
120
|
|
|
require_once( $file_location); |
121
|
|
|
return; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// check for file in the main includes directory |
126
|
|
|
$class_file_path = $this->include_path . 'class-'.str_replace( '_','-', strtolower( $class ) ) . '.php'; |
127
|
|
|
if( file_exists( $class_file_path ) ){ |
128
|
|
|
|
129
|
|
|
require_once( $class_file_path ); |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// lastly check legacy types |
134
|
|
|
$stripped_woothemes_from_class = str_replace( 'woothemes_','', strtolower( $class ) ); // remove woothemes |
135
|
|
|
$legacy_class_file_path = $this->include_path . 'class-'.str_replace( '_','-', strtolower( $stripped_woothemes_from_class ) ) . '.php'; |
136
|
|
|
if( file_exists( $legacy_class_file_path ) ){ |
137
|
|
|
|
138
|
|
|
require_once( $legacy_class_file_path ); |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return; |
143
|
|
|
|
144
|
|
|
}// end autoload |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
new Sensei_Autoloader(); |
148
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.