-
Notifications
You must be signed in to change notification settings - Fork 2
/
sidecar.php
executable file
·142 lines (131 loc) · 3.7 KB
/
sidecar.php
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
* Plugin Name: Sidecar for WordPress
* Plugin URI: http://github.com/newclarity/sidecar
* Description:
* Version: 0.5.1
* Author: NewClarity, MikeSchinkel
* Author URI: http://newclarity.net
* Text Domain: sidecar
* License: GPLv2
*
* Copyright 2012
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define( 'SIDECAR_FILE', __FILE__ );
define( 'SIDECAR_DIR', dirname( __FILE__ ) );
define( 'SIDECAR_PATH', plugin_dir_path( __FILE__ ) );
define( 'SIDECAR_VER', '0.5.1' );
define( 'SIDECAR_MIN_PHP', '5.2.4' );
define( 'SIDECAR_MIN_WP', '3.2' );
/**
*
*/
final class Sidecar {
/**
* @var string
*/
private static $_installed_dir = false;
/**
* @var string
*/
private static $_this_url = false;
/**
* @var string
*/
private static $_this_domain = false;
/**
*
*/
static function on_load() {
spl_autoload_register( array( __CLASS__, 'autoloader' ) );
}
/**
*
*/
static function autoloader( $class_name ) {
$suffix = preg_replace( '#^Sidecar_(.*)$#', '$1', $class_name );
$suffix = str_replace( '_', '-', strtolower( $suffix ) );
$filename = dirname( __FILE__ ) . "/classes/class-{$suffix}.php";
if ( is_file( $filename ) ) {
require( $filename );
}
}
/**
* @param string $message
* @param array $args
*/
static function show_error( $message, $args ) {
$args = func_get_args();
echo '<div class="error"><p><strong>ERROR</strong>[Sidecar]: ' . call_user_func_array( 'sprintf', $args ) . '</p></div>';
}
/**
* Tests an array element for value, first checking for isset().
*
* @param array $array
* @param string $element
* @param mixed $value
* @param bool $exactly
* @return bool
*/
static function element_is( $array, $element, $value = true, $exactly = false ) {
return isset( $array[$element] ) && ( $exactly ? $value === $array[$element] : $value == $array[$element] );
}
/**
* Returns the domain for this site.
*
* @return string
*/
static function this_domain() {
if ( ! self::$_this_domain ) {
$parts = explode( '/', site_url() );
self::$_this_domain = $parts[2];
}
return self::$_this_domain;
}
/**
* Returns the directory in which WordPress is installed, or '/' if root.
*
* Preceded with a '/' but no trailing '/'
*
* @return string
*/
static function installed_dir() {
if ( ! self::$_installed_dir ) {
$site_url = site_url();
if ( 2 == substr_count( $site_url, '/' ) ) {
self::$_installed_dir = '/';
} else {
$regex = '^https?://' . preg_quote( self::this_domain() ) . '(/.*)/?$';
self::$_installed_dir = preg_replace( "#{$regex}#", '$1', site_url() );
}
}
return self::$_installed_dir;
}
/**
* Returns the current URL.
*
* @return bool
*/
static function this_url() {
if ( ! self::$_this_url ) {
$installed_dir = self::installed_dir();
$requested_path = substr( $_SERVER['REQUEST_URI'], strlen( $installed_dir ) );
self::$_this_url = site_url( $requested_path );
}
return self::$_this_url;
}
}
Sidecar::on_load();