PluginProbe
UpStream: a Project Management Plugin for WordPress / trunk
UpStream: a Project Management Plugin for WordPress vtrunk
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / class-upstream-autoloader.php

class-upstream-autoloader.php in UpStream: a Project Management Plugin for WordPress trunk, at includes/class-upstream-autoloader.php

112 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UpStream Autoloader.
4 *
5 * @package Upstream
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * UpStream Autoloader.
15 *
16 * @class UpStream_Autoloader
17 * @version 0.0.1
18 * @package UpStream/Classes
19 * @category Class
20 * @author UpStream
21 */
22 class UpStream_Autoloader {
23 /**
24 * Path to the includes directory.
25 *
26 * @var string
27 */
28 private $include_path = '';
29
30 /**
31 * The Constructor.
32 */
33 public function __construct() {
34 if ( function_exists( '__autoload' ) ) {
35 spl_autoload_register( '__autoload' );
36 }
37
38 spl_autoload_register( array( $this, 'autoload' ) );
39
40 $this->include_path = untrailingslashit( plugin_dir_path( UPSTREAM_PLUGIN_FILE ) ) . '/includes/';
41 }
42
43 /**
44 * Auto-load classes on demand to reduce memory consumption.
45 *
46 * @param string $class Class name.
47 */
48 public function autoload( $class ) {
49 $orig_class = $class;
50 $class = strtolower( $class );
51 $file = $this->get_file_name_from_class( $class );
52 $path = '';
53
54 if ( strpos( $class, 'upstream_options' ) === 0 ) {
55 $path = $this->include_path . 'admin/options/';
56 } elseif ( strpos( $class, 'upstream_metaboxes' ) === 0 ) {
57 $path = $this->include_path . 'admin/metaboxes/';
58 } elseif ( strpos( $class, 'upstream_model' ) === 0 ) {
59 $path = $this->include_path . 'model/';
60 }
61
62 // WCS class file name conversion.
63 if ( strpos( $class, 'upstream_' ) === 0 ) {
64 $file = str_replace( 'class-up-', 'class-upstream-', $file );
65 }
66
67 if ( empty( $path ) || ( ! $this->load_file( $path . $file ) && strpos( $class, 'upstream_' ) === 0 ) ) {
68 $this->load_file( $this->include_path . $file );
69 }
70 }
71
72 /**
73 * Take a class name and turn it into a file name.
74 *
75 * @param string $class Class name.
76 *
77 * @return string
78 */
79 private function get_file_name_from_class( $class ) {
80 $updated_classes = array(
81 'upstream_cache',
82 'upstream_client',
83 'upstream_import',
84 'upstream_project',
85 );
86
87 if ( ! in_array( $class, $updated_classes, true ) ) {
88 $class = str_replace( 'upstream', 'up', $class );
89 }
90
91 return 'class-' . str_replace( '_', '-', $class ) . '.php';
92 }
93
94 /**
95 * Include a class file.
96 *
97 * @param string $path Class file path.
98 *
99 * @return bool successful or not
100 */
101 private function load_file( $path ) {
102 if ( $path && is_readable( $path ) ) {
103 include_once $path;
104 return true;
105 }
106
107 return false;
108 }
109 }
110
111 new UpStream_Autoloader();
112