      1: <?php
      2: /*
      3:  * Plugin Name: Sidecar for WordPress
      4:  * Plugin URI: https://github.com/newclarity/sidecar
      5:  * Description:
      6:  * Version: 0.5.1
      7:  * Author: NewClarity, MikeSchinkel
      8:  * Author URI: https://newclarity.net
      9:  * Text Domain: sidecar
     10:  * License: GPLv2
     11:  *
     12:  *  Copyright 2012
     13:  *
     14:  *  This program is free software; you can redistribute it and/or modify
     15:  *  it under the terms of the GNU General Public License, version 2, as
     16:  *  published by the Free Software Foundation.
     17:  *
     18:  *  This program is distributed in the hope that it will be useful,
     19:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     20:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     21:  *  GNU General Public License for more details.
     22:  *
     23:  *  You should have received a copy of the GNU General Public License
     24:  *  along with this program; if not, write to the Free Software
     25:  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     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:    * @var string
     41:    */
     42:   private static $_installed_dir = false;
     43:
     44:   /**
     45:    * @var string
     46:    */
     47:   private static $_this_url = false;
     48:
     49:   /**
     50:    * @var string
     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:    * @param string $message
     75:    * @param array $args
     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:    * Tests an array element for value, first checking for isset().
     84:    *
     85:    * @param array $array
     86:    * @param string $element
     87:    * @param mixed $value
     88:    * @param bool $exactly
     89:    * @return bool
     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:      * Returns the domain for this site.
     97:    *
     98:      * @return string
     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:      * Returns the directory in which WordPress is installed, or '/' if root.
    110:    *
    111:    * Preceded with a '/' but no trailing '/'
    112:      *
    113:      * @return string
    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:    * Returns the current URL.
    130:    *
    131:    * @return bool
    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: