1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: define( 'SIDECAR_FILE', __FILE__ );
28: define( 'SIDECAR_DIR', dirname( __FILE__ ) );
29: define( 'SIDECAR_PATH', plugin_dir_path( __FILE__ ) );
30:
31: define( 'SIDECAR_VER', '0.5.1' );
32: define( 'SIDECAR_MIN_PHP', '5.2.4' );
33: define( 'SIDECAR_MIN_WP', '3.2' );
34:
35: 36: 37:
38: final class Sidecar {
39: 40: 41:
42: private static $_installed_dir = false;
43:
44: 45: 46:
47: private static $_this_url = false;
48:
49: 50: 51:
52: private static $_this_domain = false;
53:
54: 55: 56:
57: static function on_load() {
58: spl_autoload_register( array( __CLASS__, 'autoloader' ) );
59: }
60:
61: 62: 63:
64: static function autoloader( $class_name ) {
65: $suffix = preg_replace( '#^Sidecar_(.*)$#', '$1', $class_name );
66: $suffix = str_replace( '_', '-', strtolower( $suffix ) );
67: $filename = dirname( __FILE__ ) . "/classes/class-{$suffix}.php";
68: if ( is_file( $filename ) ) {
69: require( $filename );
70: }
71: }
72:
73: 74: 75: 76:
77: static function show_error( $message, $args ) {
78: $args = func_get_args();
79: echo '<div class="error"><p><strong>ERROR</strong>[Sidecar]: ' . call_user_func_array( 'sprintf', $args ) . '</p></div>';
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89: 90:
91: static function element_is( $array, $element, $value = true, $exactly = false ) {
92: return isset( $array[$element] ) && ( $exactly ? $value === $array[$element] : $value == $array[$element] );
93: }
94:
95: 96: 97: 98: 99:
100: static function this_domain() {
101: if ( ! self::$_this_domain ) {
102: $parts = explode( '/', site_url() );
103: self::$_this_domain = $parts[2];
104: }
105: return self::$_this_domain;
106: }
107:
108: 109: 110: 111: 112: 113: 114:
115: static function installed_dir() {
116: if ( ! self::$_installed_dir ) {
117: $site_url = site_url();
118: if ( 2 == substr_count( $site_url, '/' ) ) {
119: self::$_installed_dir = '/';
120: } else {
121: $regex = '^https?://' . preg_quote( self::this_domain() ) . '(/.*)/?$';
122: self::$_installed_dir = preg_replace( "#{$regex}#", '$1', site_url() );
123: }
124: }
125: return self::$_installed_dir;
126: }
127:
128: 129: 130: 131: 132:
133: static function this_url() {
134: if ( ! self::$_this_url ) {
135: $installed_dir = self::installed_dir();
136: $requested_path = substr( $_SERVER['REQUEST_URI'], strlen( $installed_dir ) );
137: self::$_this_url = site_url( $requested_path );
138: }
139: return self::$_this_url;
140: }
141: }
142: Sidecar::on_load();
143: