Skip to content

Instantly share code, notes, and snippets.

@enejb
Created October 30, 2012 16:40
Show Gist options
  • Save enejb/3981405 to your computer and use it in GitHub Desktop.
Save enejb/3981405 to your computer and use it in GitHub Desktop.
WordPress Plugin that we use to generate a host file so that we can update our host file
<?php
/*
Plugin Name: Get Sites On Verf
Plugin URI:
Description: used to generate a host file so that we can update our local host file.
Author: Enej
Version: 0.5
Author URI:
*/
add_action( 'network_admin_menu','eb_get_verf_sites' );
function eb_get_verf_sites(){
add_submenu_page('sites.php', 'Generate remote host', 'Generate host file', 'manage_sites', 'eb-get-sites', 'eb_get_verf_sites_page');
// add_menu_page( 'sites.php' ,'List Sites', 'manage_sites', 'eb-get-sites', 'eb_get_sites_page' );
// ( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position )
}
function eb_get_verf_sites_page(){
global $wpdb;
$upload_dir = wp_upload_dir();
$where = $upload_dir['basedir'];
eb_generate_host_file( $where );
if( file_exists($where.'/hosts.txt' ) ): ?>
<p>You can download the hosts file here. <br .>
<?php echo $upload_dir['baseurl'].'/hosts.txt'; ?></p>
<?php endif; ?>
<h2>Generate a host file with all the domain</h2>
<form action="" method="post">
<label for="ip">Enter the IP Address</label><br />
<input type="text" name="IP" id="ip" value="<?php echo esc_attr(get_option('verf_ip') ); ?>" /><br />
<p class="small">The IP Address will prefixed before every domain, the IP Address is eather the current server of the load balancer.</p>
<?php wp_nonce_field(); ?>
<input name="save" type="submit" class="button-primary" value="Generate the Host File" />
</form>
<p>
<em>List of current domains:</em>
<pre>
<?php eb_get_all_sites(); ?>
</pre>
</p>
<?php
}
function eb_security_check(){
if (empty($_POST)) return false;
check_admin_referer();
}
function eb_generate_host_file( $where_to_save = null){
if (empty($_POST)) return false;
check_admin_referer();
$form_fields = array ('save'); // this is a list of the form field contents I want passed along between page views
$method = ''; // Normally you leave this an empty string and it figures it out by itself, but you can override the filesystem method here
// check to see if we are trying to save a file
if (isset($_POST['save'])) {
// okay, let's see about getting credentials
// $url = wp_nonce_url('admin.php?page=eb-get-sites');
$IP = ( filter_var( $_POST['IP'], FILTER_VALIDATE_IP) ? $_POST['IP'] : "");
ob_start();
eb_get_all_sites($IP);
$file_content = ob_get_contents();
ob_end_clean();
file_put_contents( $where_to_save.'/hosts.txt', $file_content );
update_option('verf_ip', $IP);
/*
if (false === ($creds = request_filesystem_credentials($url, $method, false, false, $form_fields) ) ) {
// if we get here, then we don't have credentials yet,
// but have just produced a form for the user to fill in,
// so stop processing for now
return false; // stop the normal page form from displaying
}
// now we have some credentials, try to get the wp_filesystem running
if ( ! WP_Filesystem($creds) ) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials($url, $method, true, false, $form_fields);
return false;
}
// get the upload directory and make a test.txt file
$filename = trailingslashit( $where_to_save ).'hosts.txt';
// get the file contents
// by this point, the $wp_filesystem global should be working, so let's use it to create a file
global $wp_filesystem;
if ( ! $wp_filesystem->put_contents( $filename, $file_content, FS_CHMOD_FILE) ) {
return false;
}
*/
}
return true;
}
function eb_get_all_sites($prefix = null ){
global $wpdb;
$wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
$domain_mapped = $wpdb->get_results( "SELECT blog_id, domain FROM {$wpdb->dmtable} ORDER BY blog_id" );
foreach($domain_mapped as $site):
$url = $site->domain;
$url = ( '/' == substr($url,-1 ) ? substr( $url,0 ,-1 ) : $url);
echo $prefix." ".trim($url)."\n";
endforeach;
unset($url);
$regular_domains = $wpdb->get_results( "SELECT blog_id, domain, path FROM {$wpdb->blogs} ORDER BY blog_id" );
foreach($regular_domains as $site):
$url = $site->domain.$site->path;
$url = ( '/' == substr($url,-1 ) ? substr( $url,0 ,-1 ) : $url);
echo $prefix." ".trim($url)."\n";
endforeach;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment