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

[TASK] Add redirect option, saves storage and ensures the latest vers… #14

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion class-404-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ function __construct() {
* Stream files from publicly registered IP address through PHP
*/
public function stream() {
// Redirect to original url?
// https://www.mijnpress.nl/2013/uploads-by-proxy-302-redirect-instead-of-force-download-to-local-directory/
if( defined('UBP_REDIRECT') && UBP_REDIRECT == TRUE )
{
$redirect_url = UBP_SITEURL.$this->get_remote_path();
if( defined( 'UBP_REPLACE' ) ) {
foreach( UBP_REPLACE as $search => $replace ) {
$redirect_url = str_replace( $search, $replace, $redirect_url );
}
}


if( !(defined('UBP_BYPASS_HEAD_CHECK') && UBP_BYPASS_HEAD_CHECK == TRUE) ) {
$this->response = wp_remote_head( $redirect_url );
if ( is_wp_error($this->response) || 200 != $this->response['response']['code'] ) {
// Comply with display_and_exit()
if(is_wp_error($this->response)) {
$this->response = array( 'headers' => array(), 'body' => $this->response->get_error_message() );
}
$this->display_and_exit( "Remote url not readable. Path: ".$this->get_remote_path() );
}
}
wp_redirect( $redirect_url, 302 ); // 302 as files _can_ change
exit;
}
// Falltrough to download file to local storage

require dirname(__FILE__).'/class-get-public-ip.php';

$ip = new UBP_Get_Public_IP( $this->get_domain() );
Expand All @@ -31,6 +58,7 @@ public function stream() {
// Route around local DNS by requesting by IP directly
$url = 'http://' . $this->get_auth() . $ip . $this->get_remote_path();


$this->response = wp_remote_get( $url, $args);

if ( !is_wp_error($this->response) && 200 == $this->response['response']['code'] ) {
Expand Down Expand Up @@ -195,4 +223,4 @@ public function get_remote_path() {
return $this->remote_path;
}

}
}
23 changes: 18 additions & 5 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
=== Uploads by Proxy ===
Contributors: pdclark
Contributors: pdclark, ramon fincken
Author URI: http://pdclark.com
Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion
Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion, DTAP, OTAP, 302, redirect
Requires at least: 3.1
Tested up to: 3.6
Stable tag: 1.1.2
Tested up to: 5.4.2
Stable tag: 1.1.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

For local development: Automatically load images from the production version of wp-content/uploads if they are missing locally.

== Description ==

This plugin is meant to be used by developers who work on sites in a local development environment before deploying changes to a production (live) server. It allows you skip downloading the contents of `wp-content/uploads` to your local WordPress install. Instead, images missing from the uploads directory are loaded from the production server as needed.
This plugin is meant to be used by developers who work on sites in a local development environment before deploying changes to a production (live) server. It allows you skip downloading the contents of `wp-content/uploads` to your local WordPress install. Instead, images missing from the uploads directory are loaded from the production server as needed. You have the option to download the files or to redirect to the original url.

= Setup =

Expand All @@ -22,6 +22,11 @@ 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!

If you are on a staging server you may also use this define to 302 redirect to the original URL instead of downloading. This saves storage and ensures the latest version of the uploaded media file.
define('UBP_REDIRECT', true);
define('UBP_REPLACE', [ '/app' => '/wp-content' ] ); // Enable vanilla wp-content instead of composer app directory



== Installation ==

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

== Changelog ==

= 1.1.4 =

* Add search replace

= 1.1.3 =

* Add support to 302 redirect to the original URL instead of downloading. Saves storage and ensures the latest version of the uploaded media file.

= 1.1.2 =

* Fix: Resolve a warning output in debug environments. (Static function not declared as static.)
Expand Down
6 changes: 3 additions & 3 deletions 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.3
*/

/**
Expand All @@ -24,8 +24,8 @@
*/
if ( !defined('UBP_IS_LOCAL') ) {
define('UBP_IS_LOCAL', (
( '127.0.0.1' == $_SERVER['SERVER_ADDR'] && '127.0.0.1' == $_SERVER['REMOTE_ADDR'] ) // IPv4
|| ( '::1' == $_SERVER['SERVER_ADDR'] && '::1' == $_SERVER['REMOTE_ADDR'] ) // IPv6
( isset( $_SERVER['SERVER_ADDR'] ) && '127.0.0.1' == $_SERVER['SERVER_ADDR'] && isset( $_SERVER['REMOTE_ADDR'] ) && '127.0.0.1' == $_SERVER['REMOTE_ADDR'] ) // IPv4
|| ( isset( $_SERVER['SERVER_ADDR'] ) && '::1' == $_SERVER['SERVER_ADDR'] && isset( $_SERVER['REMOTE_ADDR'] ) && '::1' == $_SERVER['REMOTE_ADDR'] ) // IPv6
) );
}

Expand Down