Make WordPress Core

Ticket #36335: classes-real.php

File classes-real.php, 8.7 KB (added by rmccue, 8 years ago)

Proposed actual code for the autoloaders

Line 
1<?php
2
3class WP_Autoload_Prefixed {
4        protected $prefix;
5        protected $path;
6        public function __construct( $prefix, $path ) {
7                $this->prefix = strtolower( $prefix );
8                $this->path = trailingslashit( $path );
9        }
10        public function load_class( $class ) {
11                $class = strtolower( $class );
12                if ( strpos( $class, $this->prefix ) !== 0 ) {
13                        return;
14                }
15
16                $filename = 'class-' . str_replace( '_', '-', $class ) . '.php';
17                include $this->path . $filename;
18        }
19}
20class WP_Autoload_PSR0 {
21        protected $prefix;
22        protected $path;
23        public function __construct( $prefix, $path ) {
24                $this->prefix = $prefix;
25                $this->path = trailingslashit( $path );
26        }
27        public function load_class( $class ) {
28                if ( strpos( $class, $this->prefix ) !== 0 ) {
29                        return;
30                }
31
32                $parts = explode( '_', substr( $class, strlen( $this->prefix ) ) );
33                $filename = implode( '/', $parts ) . '.php';
34                include $this->path . $filename;
35        }
36}
37class WP_Autoload_ClassMap {
38        protected $map;
39        public function __construct( $map ) {
40                $this->map = $map;
41        }
42        public function load_class( $class ) {
43                if ( ! isset( $this->map[ $class ] ) ) {
44                        return;
45                }
46
47                require $this->map[ $class ];
48        }
49}
50
51// Legacy
52$legacy_map = new WP_Autoload_ClassMap([
53        'WP' => 'wp-includes/class-wp.php',
54        'wpdb' => 'wp-includes/wp-db.php',
55        'WP_Date_Query' => 'wp-includes/date.php',
56        'WP_Locale' => 'wp-includes/locale.php',
57        'WP_oEmbed' => 'wp-includes/class-oembed.php',
58        'WP_Query' => 'wp-includes/query.php',
59        'WP_User_Meta_Session_Tokens' => 'wp-includes/session.php',
60
61        // Back-compat
62        'WP_HTTP_Fsockopen' => 'wp-includes/class-wp-http-streams.php',
63
64        // Old Customizer
65        'WP_Customize_Control' => 'wp-includes/class-wp-customize-control.php',
66        'WP_Customize_Panel' => 'wp-includes/class-wp-customize-panel.php',
67        'WP_Customize_Section' => 'wp-includes/class-wp-customize-section.php',
68        'WP_Customize_Setting' => 'wp-includes/class-wp-customize-setting.php',
69
70        // Dependencies (uses dot instead of dash)
71        'WP_Dependencies' => 'wp-includes/class.wp-dependencies.php',
72        '_WP_Dependency' => 'wp-includes/class.wp-dependencies.php',
73        'WP_Scripts' => 'wp-includes/class.wp-scripts.php',
74        'WP_Styles' => 'wp-includes/class.wp-styles.php',
75
76        // Widgets
77        'WP_Widget_Factory' => 'wp-includes/class-wp-widget-factory.php',
78
79        // Walkers
80        'Walker' => 'wp-includes/class-wp-walker.php',
81        'Walker_CategoryDropdown' => 'wp-includes/class-walker-category-dropdown.php',
82        'Walker_PageDropdown' => 'wp-includes/class-walker-page-dropdown.php',
83
84        // Diff
85        'WP_Text_Diff_Renderer_Table' => 'wp-includes/wp-diff.php',
86        'WP_Text_Diff_Renderer_inline' => 'wp-includes/wp-diff.php',
87]);
88spl_autoload_register( array( $legacy_map, 'load_class' ) );
89
90// Admin-only
91$admin_map = new WP_Autoload_ClassMap([
92        // List tables
93        'WP_Comments_List_Table'       => 'wp-admin/includes/class-wp-comments-list-table.php',
94        'WP_Links_List_Table'          => 'wp-admin/includes/class-wp-links-list-table.php',
95        'WP_List_Table'                => 'wp-admin/includes/class-wp-list-table.php',
96        'WP_Media_List_Table'          => 'wp-admin/includes/class-wp-media-list-table.php',
97        'WP_MS_Sites_List_Table'       => 'wp-admin/includes/class-wp-ms-sites-list-table.php',
98        'WP_MS_Themes_List_Table'      => 'wp-admin/includes/class-wp-ms-themes-list-table.php',
99        'WP_MS_Users_List_Table'       => 'wp-admin/includes/class-wp-ms-users-list-table.php',
100        'WP_Plugin_Install_List_Table' => 'wp-admin/includes/class-wp-plugin-install-list-table.php',
101        'WP_Plugins_List_Table'        => 'wp-admin/includes/class-wp-plugins-list-table.php',
102        'WP_Post_Comments_List_Table'  => 'wp-admin/includes/class-wp-post-comments-list-table.php',
103        'WP_Posts_List_Table'          => 'wp-admin/includes/class-wp-posts-list-table.php',
104        'WP_Terms_List_Table'          => 'wp-admin/includes/class-wp-terms-list-table.php',
105        'WP_Theme_Install_List_Table'  => 'wp-admin/includes/class-wp-theme-install-list-table.php',
106        'WP_Themes_List_Table'         => 'wp-admin/includes/class-wp-themes-list-table.php',
107        'WP_Users_List_Table'          => 'wp-admin/includes/class-wp-users-list-table.php',
108        '_WP_List_Table'               => 'wp-admin/includes/list-table.php',
109        '_WP_List_Table_Compat'        => 'wp-admin/includes/list-table.php',
110
111        // Skins
112        'WP_Upgrader_Skin'            => 'wp-admin/includes/class-wp-upgrader-skins.php',
113        'Plugin_Upgrader_Skin'        => 'wp-admin/includes/class-wp-upgrader-skins.php',
114        'Bulk_Upgrader_Skin'          => 'wp-admin/includes/class-wp-upgrader-skins.php',
115        'Bulk_Plugin_Upgrader_Skin'   => 'wp-admin/includes/class-wp-upgrader-skins.php',
116        'Bulk_Theme_Upgrader_Skin'    => 'wp-admin/includes/class-wp-upgrader-skins.php',
117        'Plugin_Installer_Skin'       => 'wp-admin/includes/class-wp-upgrader-skins.php',
118        'Theme_Installer_Skin'        => 'wp-admin/includes/class-wp-upgrader-skins.php',
119        'Theme_Upgrader_Skin'         => 'wp-admin/includes/class-wp-upgrader-skins.php',
120        'Language_Pack_Upgrader_Skin' => 'wp-admin/includes/class-wp-upgrader-skins.php',
121        'Automatic_Upgrader_Skin'     => 'wp-admin/includes/class-wp-upgrader-skins.php',
122
123        // Upgraders
124        'WP_Upgrader'            => 'wp-admin/includes/class-wp-upgrader.php',
125        'Plugin_Upgrader'        => 'wp-admin/includes/class-wp-upgrader.php',
126        'Theme_Upgrader'         => 'wp-admin/includes/class-wp-upgrader.php',
127        'Language_Pack_Upgrader' => 'wp-admin/includes/class-wp-upgrader.php',
128        'Core_Upgrader'          => 'wp-admin/includes/class-wp-upgrader.php',
129        'File_Upload_Upgrader'   => 'wp-admin/includes/class-wp-upgrader.php',
130        'WP_Automatic_Updater'   => 'wp-admin/includes/class-wp-upgrader.php',
131
132        // Admin walkers
133        'Walker_Category_Checklist' => 'wp-admin/includes/class-walker-category-checklist.php',
134        'Walker_Nav_Menu_Checklist' => 'wp-admin/includes/class-walker-nav-menu-checklist.php',
135        'Walker_Nav_Menu_Edit'      => 'wp-admin/includes/class-walker-nav-menu-edit.php',
136
137        // Others!
138        'Custom_Background'   => 'wp-admin/custom-background.php',
139        'Custom_Image_Header' => 'wp-admin/custom-header.php',
140        'WP_Importer'         => 'wp-admin/includes/class-wp-importer.php',
141        'WP_Press_This'       => 'wp-admin/includes/class-wp-press-this.php',
142        'WP_Site_Icon'        => 'wp-admin/includes/class-wp-site-icon.php',
143]);
144spl_autoload_register( array( $admin_map, 'load_class' ) );
145
146// Externals
147$external_map = new WP_Autoload_ClassMap([
148        // AtomLib
149        'AtomFeed' => 'wp-includes/atomlib.php',
150        'AtomEntry' => 'wp-includes/atomlib.php',
151        'AtomParser' => 'wp-includes/atomlib.php',
152
153        // FTP handling
154        'ftp' => 'wp-admin/includes/class-ftp.php',
155        'ftp_base' => 'wp-admin/includes/class-ftp.php',
156
157        // IXR
158        'IXR_Value' => 'wp-includes/class-IXR.php',
159        'IXR_Date' => 'wp-includes/class-IXR.php',
160
161        // Magpie
162        'MagpieRSS' => 'wp-includes/rss.php',
163        'RSSCache' => 'wp-includes/rss.php',
164
165        // ID3
166        'getid3_asf' => 'wp-includes/ID3/module.audio-video.asf.php',
167        'getid3_flv' => 'wp-includes/ID3/module.audio-video.flv.php',
168        'AMFStream' => 'wp-includes/ID3/module.audio-video.flv.php',
169        'AMFReader' => 'wp-includes/ID3/module.audio-video.flv.php',
170        'AVCSequenceParameterSetReader' => 'wp-includes/ID3/module.audio-video.flv.php',
171        'getid3_riff' => 'wp-includes/ID3/module.audio-video.riff.php',
172
173        // phpass
174        'PasswordHash' => 'wp-includes/class-phpass.php',
175
176        // POMO
177        'Translation_Entry' => 'wp-includes/pomo/entry.php',
178        'MO' => 'wp-includes/pomo/mo.php',
179        'PO' => 'wp-includes/pomo/po.php',
180        'POMO_Reader' => 'wp-includes/pomo/streams.php',
181        'POMO_FileReader' => 'wp-includes/pomo/streams.php',
182        'POMO_StringReader' => 'wp-includes/pomo/streams.php',
183        'POMO_CachedFileReader' => 'wp-includes/pomo/streams.php',
184        'POMO_CachedIntFileReader' => 'wp-includes/pomo/streams.php',
185        'Translations' => 'wp-includes/pomo/translations.php',
186        'Gettext_Translations' => 'wp-includes/pomo/translations.php',
187        'NOOP_Translations' => 'wp-includes/pomo/translations.php',
188
189        // POP3
190        'POP3' => 'wp-includes/class-pop3.php',
191
192        // Text_Diff
193        'Text_Diff' => 'wp-includes/Text/Diff.php',
194        'Text_MappedDiff' => 'wp-includes/Text/Diff.php',
195        'Text_Diff_Op' => 'wp-includes/Text/Diff.php',
196        'Text_Diff_Op_copy' => 'wp-includes/Text/Diff.php',
197        'Text_Diff_Op_change' => 'wp-includes/Text/Diff.php',
198        'Text_Diff_Op_delete' => 'wp-includes/Text/Diff.php',
199        'Text_Diff_Op_add' => 'wp-includes/Text/Diff.php',
200]);
201spl_autoload_register( array( $external_map, 'load_class' ) );
202
203$diff = new WP_Autoload_PSR0( 'Text_Diff_', 'wp-includes/Text/Diff/' );
204spl_autoload_register( array( $diff, 'load_class' ) );
205
206$prefixed = [
207        // Subdirs of includes
208        'wp_customize_' => 'wp-includes/customize/',
209        'wp_rest_' => 'wp-includes/rest-api/',
210        'wp_widget_' => 'wp-includes/widgets/',
211
212        // Admin-specific
213        'wp_filesystem_' => 'wp-admin/includes/',
214
215        // Catch-all
216        'wp_' => 'wp-includes/',
217        'walker_' => 'wp-includes/',
218];
219foreach ( $prefixed as $prefix => $path ) {
220        $loader = new WP_Autoload_Prefixed( $prefix, $path );
221        spl_autoload_register( array( $loader, 'load_class' ) );
222}