Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginnings of multisite support #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
61 changes: 54 additions & 7 deletions class-404-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public function download() {

$u = wp_upload_dir();

$basedir = str_replace( $this->uploads_basedir(), '', $u['basedir'] );
$abspath = $basedir . $this->get_local_path();
$remove = str_replace( get_option( 'siteurl' ), '', $u['baseurl'] );
$basedir = str_replace( $remove, '', $u['basedir'] );
$abspath = $basedir . $this->get_dated_path();
$dir = dirname( $abspath );

if ( !is_dir( $dir ) && !wp_mkdir_p( $dir ) ) {
Expand All @@ -55,7 +56,9 @@ public function download() {
$saved_image = $wp_filesystem->put_contents( $abspath, $this->response['body'], FS_CHMOD_FILE ); // predefined mode settings for WP files

if ( $saved_image ) {
wp_redirect( get_site_url( get_current_blog_id(), $this->get_local_path() ) );
$path = $this->get_ms_adjusted_path();
$redirect = get_site_url( get_current_blog_id(), $path );
wp_redirect( $redirect );
exit;
}else {
$this->display_and_exit( "Please check permissions. Could not write image $dir" );
Expand Down Expand Up @@ -84,6 +87,7 @@ public function display_and_exit( $message=false ) {
*/
public function allow_path() {
$path = $this->get_remote_path();

if ( empty( $path ) ) { return false; }

$allowed_paths = array(
Expand Down Expand Up @@ -120,12 +124,31 @@ public function get_siteurl() {
$url = str_replace( array( 'http://', 'https://' ), '', UBP_LIVE_DOMAIN );
$url = 'http://' . $url;

}else if ( defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) {

}else if ( is_multisite() && ( $siteurl = get_option( '_ubp_site_url' ) ) ) {

$url = parse_url( $siteurl );
$url = 'http://' . $url['host'] . @$url['path'];

}else if ( is_multisite() && defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) {


$details = get_blog_details();
$url = parse_url( UBP_SITEURL );
$ms_url = '';
if ( SUBDOMAIN_INSTALL ) {
$parts = explode( '.', $details->domain );
$ms_url = $parts[0] . '.' . $url['host'];
} else {
$ms_url = $url['host'] . $details->path;
}
$url = 'http://' . $ms_url . @$url['path'];

} else if ( defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) {

$url = parse_url( UBP_SITEURL );
$url = 'http://' . $url['host'] . @$url['path'];

}else if ( !is_multisite() ) {
} else {
// Nothing set... Get original siteurl from database

remove_filter( 'option_siteurl', '_config_wp_siteurl' );
Expand All @@ -147,6 +170,27 @@ public function get_domain() {
return $this->domain;
}

public function get_dated_path() {
$path = $this->get_local_path();

if ( function_exists( 'is_multisite' ) && is_multisite() && strpos( $path, '/files/' ) === 0 ) {
$path = str_replace( '/files', '', $path );
}

return $path;
}

public function get_ms_adjusted_path() {
$path = $this->get_local_path();

//switch to the "new" multisite files location
if ( function_exists( 'is_multisite' ) && is_multisite() && strpos( $path, '/files/' ) === 0 ) {
$path = 'wp-content/uploads/sites/' . get_current_blog_id() . str_replace( '/files', '', $path );
}

return $path;
}

public function get_auth() {
if ( !isset( $this->auth ) ) {
$user = parse_url( $this->get_siteurl(), PHP_URL_USER );
Expand All @@ -170,8 +214,10 @@ public function get_local_path() {
// If local install is in a subdirectory, modify path to request from WordPress root
$local_wordpress_path = parse_url( get_site_url(), PHP_URL_PATH ) . '/';
$requested_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );

if (substr($requested_path, 0, strlen($local_wordpress_path)) == $local_wordpress_path) {
$requested_path = substr($requested_path, strlen($local_wordpress_path)-1, strlen($requested_path));
//$requested_path = substr($requested_path, strlen($local_wordpress_path), strlen($requested_path));
$requested_path = substr($requested_path, strlen($local_wordpress_path)-1, strlen($requested_path));
}

$this->local_path = $requested_path;
Expand All @@ -186,6 +232,7 @@ public function get_remote_path() {

// If remote install is in a subdirectory, prepend the remote path
$remote_path = parse_url( $this->get_siteurl(), PHP_URL_PATH );

if ( !empty( $remote_path ) ) {
$this->remote_path = $remote_path . $this->get_local_path();
}else {
Expand Down
4 changes: 2 additions & 2 deletions class-get-public-ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function __toString() {
public function get_ip( $url, $args=array() ) {
$defaults = array(
'method' => 'GET',
'referer'=> $domain,
'referer'=> $this->domain,
'body' => '',
'index' => 0,
);
Expand All @@ -58,7 +58,7 @@ public function get_ip( $url, $args=array() ) {
);

$response = wp_remote_get( $url, $query_args );

if ( ! is_wp_error( $response ) ) {
$body = strip_tags($response['body']);

Expand Down
31 changes: 29 additions & 2 deletions class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ static public function init_404_template( $template ) {
}

static public function requirements_check() {
add_action( 'admin_init', 'UBP_Helpers::require_no_multisite', 11 );
//add_action( 'admin_init', 'UBP_Helpers::require_no_multisite', 11 );
add_action( 'admin_notices', 'UBP_Helpers::request_uploads_writable' );
add_action( 'admin_footer', 'UBP_Helpers::request_permalinks_enabled' );
}

/**
* Require single-site install before activating.
*/
static public function require_no_multisite() {
static public function require_no_multisite() {
if ( function_exists( 'is_multisite' ) && !is_multisite() ) { return true; }

if ( is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX) ) {
Expand Down Expand Up @@ -64,4 +64,31 @@ static public function request_uploads_writable() {
return false;
}

static public function print_multisite_setting( $id ) {
switch_to_blog( $id );
$ubp_site_url = get_option( '_ubp_site_url' );
restore_current_blog();
?>
<tr class="form-field">
<th scope="row">UBP Site URL</th>
<td><input class="all-options" name="option[_ubp_site_url]" type="text" id="ubp_site_url" value="<?php echo $ubp_site_url ?>" size="40"></td>
</tr>
<?php
}

static public function ubp_extra_paths( $allowed_paths ) {
$details = get_blog_details();
$allowed_paths[] = $details->path . 'files/';
$allowed_paths[] = $details->path . 'wp-content/uploads/';
return $allowed_paths;
}

static function stop_ms_files_rewriting() {
$path = ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id();
define( 'BLOGUPLOADDIR', $path );
}

static function pre_site_option_ms_files_rewriting( $option ) {
return 0; //return 0, not FALSE
}
}
28 changes: 27 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=== Uploads by Proxy ===
Contributors: pdclark
Contributors: pdclark, jrfoell
Author URI: http://pdclark.com
Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion
Requires at least: 3.1
Expand All @@ -22,6 +22,28 @@ In most cases, you should be able to activate the plugin and go. If the plugin d

If you are on a staging server (not a local development environment) you may need to force the plugin to run with `define( 'UBP_IS_LOCAL', true );` in `wp-config.php`. Do not set this on a live site!

= Multi-Site =

Multi-site support is available in WordPress 3.6+ but it requires additional configuration.

In wp-config.php, in addition to the 'UBP_SITEURL' define, also add this:

define('UPLOADBLOGSDIR', 'wp-content/uploads/sites');

In your .htaccess, comment out the "ms-files" line if present. It will look something like one of these two lines:

#RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
#RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

Also in your .htaccess, replace the following 'Normal' rewrite rules with the UBP Multisite rule below:

# Normal
#RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
# UBP Multisite
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]

This last .htaccess change is needed due to a bug that either manifests itself in Apache or in the default distributed WordPress Multi-Site .htaccess (https://core.trac.wordpress.org/ticket/20746)


== Installation ==

Expand Down Expand Up @@ -82,6 +104,10 @@ function ubp_ip_url( $url, $domain ) {

== Changelog ==

= 1.1.3 =

Multi-Site support (requires extra manual configuration)

= 1.1.2 =

* Fix: Resolve a warning output in debug environments. (Static function not declared as static.)
Expand Down
9 changes: 8 additions & 1 deletion uploads-by-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Author: Paul Clark
Author URI: http://pdclark.com
Description: Load images from production site if missing in development environment. Activate by using either <code>define('WP_SITEURL', 'http://development-domain.com');</code> or <code>define('UBP_SITEURL', 'http://live-domain.com/wordpress');</code> in wp-config.php.
Version: 1.1.2
Version: 1.1.3ms
*/

/**
Expand Down Expand Up @@ -54,3 +54,10 @@
add_action( 'admin_init', 'UBP_Helpers::requirements_check' );
add_filter( '404_template', 'UBP_Helpers::init_404_template' );
}

if ( function_exists( 'is_multisite' ) && is_multisite() ) {
add_action( 'wpmueditblogaction', 'UBP_Helpers::print_multisite_setting' );
add_filter( 'ubp_allowed_paths', 'UBP_Helpers::ubp_extra_paths' );
add_action( 'init','UBP_Helpers::stop_ms_files_rewriting' );
add_filter( 'pre_site_option_ms_files_rewriting', 'UBP_Helpers::pre_site_option_ms_files_rewriting' );
}