-
-
Save coulterpeterson/25ee1205e1f6e85591ed8490909e4207 to your computer and use it in GitHub Desktop.
Configurations for a Local WordPress site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
# Script Options | |
$shortopts = ""; | |
# $shortopts .= "p:"; // Required value | |
# $shortopts .= "v::"; // Optional value | |
$shortopts .= "i"; // Use http | |
$longopts = array( | |
# "optional::" // Optional value | |
# "required:", // Required value | |
# "novalue", // No value | |
"path:", // Path to local site | |
"prod-url:", // Url for remote site | |
"upload-dir:", // Path to uploads | |
"git-remote:", // Path to remote repository to use as origin | |
"insecure", // Use http instead of https | |
); | |
$options = getopt( $shortopts, $longopts ); | |
# var_dump($options); | |
# Set the path for the local site | |
if ( array_key_exists( 'path', $options ) ) { | |
# use the specified value | |
$path = $options['path']; | |
} else { | |
# try looking around in the cwd | |
if ( is_dir( 'conf' ) ) { | |
# if there is a conf dir, we're probably in the folder for the local site | |
$path = getcwd(); | |
} else if ( is_dir( '../../conf' ) ) { | |
# we're in the app/public dir, look up to get to conf | |
chdir('../..'); | |
$path = getcwd(); | |
} else { | |
# I don't know where I am | |
echo 'Please run this script in a Local site directory, or specify path with --path="path/to/site"' . PHP_EOL; | |
die(); | |
} | |
} | |
# Set paths for our conf files | |
$uploads_proxy_conf_path = $path . '/conf/nginx/includes/uploads-proxy.conf'; | |
$site_conf_hbs_path = $path . '/conf/nginx/site.conf.hbs'; | |
# Set up production URL | |
if ( array_key_exists( 'prod-url', $options ) ) { | |
# Use the provided url | |
$produrl = rtrim( $options['prod-url'], '/' ); | |
# Remove the protocol for the URL | |
$produrl = str_replace( 'https://', '', $produrl ); | |
$produrl = str_replace( 'http://', '', $produrl ); | |
} else { | |
echo 'Please specify a remote site URL for image proxy using --prod-url="siteurl.com"' . PHP_EOL; | |
die(); | |
} | |
# Set the protocol | |
if ( array_key_exists( 'insecure', $options ) || array_key_exists( 'i', $options ) ) { | |
$protocol = 'http://'; | |
} else { | |
$protocol = 'https://'; | |
} | |
# Set the upload directory | |
if ( array_key_exists( 'upload-dir', $options ) ) { | |
# user specified a custom upload directory | |
$uploaddir = $options['upload-dir']; | |
$uploaddir = ltrim( $uploaddir, '/' ); | |
$uploaddir = rtrim( $uploaddir, '/' ); | |
} else { | |
# Use the default WordPress upload directory | |
$uploaddir = 'wp-content/uploads'; | |
} | |
# Set up our string for the proxy config | |
$uploads_proxy_conf = sprintf( 'location ~ ^/%s/(.*) { | |
try_files $uri $uri/ @uploadsproxy; | |
} | |
location @uploadsproxy { | |
resolver 8.8.8.8; # Use Google for DNS. | |
resolver_timeout 60s; | |
proxy_http_version 1.1; | |
proxy_pass %s%s/%s/$1$is_args$args; | |
}', $uploaddir, $protocol, $produrl, $uploaddir ); | |
# Write the proxy config | |
if ( file_put_contents( $uploads_proxy_conf_path, $uploads_proxy_conf ) === false ) { | |
echo 'Writing uploads-proxy.conf failed' . PHP_EOL; | |
die(); | |
} else { | |
echo 'Wrote to ' . $uploads_proxy_conf_path . PHP_EOL; | |
} | |
# Set up our string for the site config file | |
$site_conf_hbs = ' # Proxy requests to the upload directory to the production site | |
# | |
include includes/uploads-proxy.conf; | |
# | |
# Static file rules'; | |
# Get the current contents of the file | |
$site_conf_hbs_content = file_get_contents( $site_conf_hbs_path ); | |
# This is ghetto. This directive needs to go in a certain place in the file | |
$site_conf_hbs = str_replace( '# Static file rules', $site_conf_hbs, $site_conf_hbs_content ); | |
# Write the site config | |
if ( file_put_contents( $site_conf_hbs_path, $site_conf_hbs ) === false ) { | |
echo 'Writing site.conf.hbs failed' . PHP_EOL; | |
die(); | |
} else { | |
echo 'Wrote to ' . $site_conf_hbs_path . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment