Completed
Push — master ( 80c0ca...82d23b )
by Dwain
04:32
created

Sensei_Autoloader::initialize_class_file_map()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 51
rs 9.411
cc 1
eloc 27
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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