Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP CLI commands available on web-synthesis servers
<?php
function synth_clear_mem_check () {
delete_transient( 'synthesis_memory_check' );
}
add_action('update_option_active_plugins', 'synth_clear_mem_check');
// If we're running from WP CLI, add our CLI commands.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
class Synthesis_Ops_Commands extends WP_CLI_Command {
/**
* @synopsis <wordpress|genesis> <version>
* @subcommand wphash
*
*/
function wordpresshash( $args, $assoc_args ) {
$filecount = 0;
$hashes = "";
$version = $args[1];
$is_wordpress = false;
if ( $args[0] == "wordpress" ) {
WP_CLI::launch( "wget -c -q http://wordpress.org/wordpress-" . $version . ".zip -P /tmp/ || echo -n" );
$is_wordpress = true;
} elseif ( $args[0] == "genesis" ) {
WP_CLI::launch( "wget -c -q http://genesistheme.com/download/sdf98h9p08hsdf009jsdf/genesis." . $version . ".zip -P /tmp/ || echo -n" );
} else {
WP_CLI::warning( "command usage: wp synth-ops wphash <wordpress|genesis> <version>" );
exit();
}
$zip_file = ( $is_wordpress ) ? "/tmp/wordpress-" . $version . ".zip" : "/tmp/genesis." . $version . ".zip";
if ( ! file_exists( $zip_file ) ) {
WP_CLI::warning( "Wrong " . $args[0] . " version OR server not available at this moment." );
exit();
}
$zip = zip_open( $zip_file );
if ( is_resource( $zip ) ) {
while ( $zip_entry = zip_read( $zip ) ) {
zip_entry_open( $zip, $zip_entry, "r" );
$filename = null;
if ( substr( zip_entry_name( $zip_entry ), -1 ) == "/" )
continue;
if ( preg_match( "/(farbtastic|install)-rtl\.css/", zip_entry_name( $zip_entry ) ) )
continue;
if ( $is_wordpress && preg_match( "/(wp-admin|wp-includes)\//", zip_entry_name( $zip_entry ) ) ) {
$filename = preg_replace( "/^wordpress\//", "", zip_entry_name( $zip_entry ) );
} else if ( ! $is_wordpress ) {
$filename = zip_entry_name( $zip_entry );
}
if ( ! is_null($filename) ) {
$hashes .= serialize( $filename ) . serialize( md5( zip_entry_read( $zip_entry, zip_entry_filesize( $zip_entry ) ) ) );
$filecount++;
}
zip_entry_close( $zip_entry );
}
$hashes = "a:" . $filecount . ":{" . $hashes . "}";
$prefix = ( $is_wordpress ) ? "wp" : "genesis";
WP_CLI::launch( "chmod -R 644 " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/" );
WP_CLI::launch( "echo '" . $hashes . "' > " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/" . $prefix . $version . ".php" );
# uncomment to use the same owner than SYNTHESIS_CHILD_PLUGIN_DIR to the hashfiles directory
#WP_CLI::launch( "chown -R $(stat -c %U " . SYNTHESIS_CHILD_PLUGIN_DIR . "):$(stat -c %U " . SYNTHESIS_CHILD_PLUGIN_DIR . ") " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/" );
WP_CLI::launch( "chmod -R 400 " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/" );
WP_CLI::success( $args[0] . " " . $version . " hash file has been generated." );
}
zip_close( $zip );
}
/**
* @synopsis --url=<url>
* @subcommand nginx-hash
*
*/
/*
function nginxhash($args, $assoc_args) {
list( $urlx ) = $args;
$cache_path = '/var/lib/nginx/proxy_a/';
$url = parse_url($_POST['xurl']);
if(!$url) {
echo 'Invalid URL entered';
die();
}
$mobilescheme = 'mobile' . $url['scheme'] . 'GET' ;
$desktopscheme = '_not_mobile_' . $url['scheme'] . 'get' ;
$host = $url['host'];
$requesturi = $url['path'];
$mobilehash = md5($mobilescheme.'GET'.$host.$requesturi);
$desktophash = md5($desktopscheme.'GET'.$host.$requesturi);
WP_CLI::success( $cache_path . substr($mobilehash, -1) . '/' . substr($mobilehash,-3,2) . '/' . $mobilehash );
}
*/
/**
* @synopsis --version=<version>
* @subcommand update-genesis
*
*/
function updategenesis( $args, $assoc_args ) {
$version = $assoc_args['version'];
$genesis = "genesis." . $version . ".zip";
WP_CLI::launch( "wget -q http://genesistheme.com/download/sdf98h9p08hsdf009jsdf/" . $genesis . " -P /tmp/ || echo -n" );
if ( ! file_exists( "/tmp/" . $genesis ) ) {
WP_CLI::warning( "Wrong Genesis version OR server not available at this moment." );
exit();
}
if ( is_dir( get_theme_root() . "/genesis" ) ) {
WP_CLI::launch( "rm -rf " . get_theme_root() . "/genesis" );
}
WP_CLI::launch( "unzip /tmp/" . $genesis . " -d " . get_theme_root() . " > /dev/null 2>&1" );
WP_CLI::launch( "chown -R $(stat -c %U " . get_theme_root() . "):$(stat -c %U " . get_theme_root() . ") " . get_theme_root() . "/genesis" );
if ( is_dir( get_theme_root() . "/genesis" ) ) {
WP_CLI::success( "Genesis " . $version . " has been reinstalled." );
WP_CLI::run_command( array( 'synth-ops', 'verify-genesis' ) );
}
}
/**
* @synopsis [--check] [--default-settings]
* @subcommand total-cache
*
*/
function w3tc( $args, $assoc_args ) {
if ( ! file_exists( WP_CONTENT_DIR . "/plugins/w3-total-cache/" ) ) {
WP_CLI::warning( "W3 Total Cache plugin is not installed into plugins/w3-total-cache/. Skipping." );
exit();
}
if ( ! is_plugin_active( "w3-total-cache/w3-total-cache.php" ) ) {
WP_CLI::warning( "W3 Total Cache plugin is not enabled. Skipping." );
exit();
}
$w3tc_version = get_plugin_data( WP_CONTENT_DIR . "/plugins/w3-total-cache/w3-total-cache.php" );
$w3tc_version = $w3tc_version['Version'];
$w3tc_master = ( $w3tc_version >= "0.9.2.6" ) ? WP_CONTENT_DIR . "/w3tc-config/master.php" : WP_CONTENT_DIR . "/w3-total-cache-config.php";
if ( ! file_exists( $w3tc_master ) ) {
WP_CLI::warning( "W3 Total Cache config file can not be found. Skipping." );
exit();
}
if ( isset( $assoc_args['check'] ) ) {
$file = fopen( $w3tc_master, "r" );
while ( ! feof( $file ) ) {
$line = fgets( $file );
if ( preg_match( '/pgcache\.enabled\' => true/', $line ) ) { $pgcache = true; continue; }
if ( preg_match( '/pgcache\.engine\' => \'file\'/', $line ) ) { $pgcache_method = "disk: basic"; continue; }
if ( preg_match( '/pgcache\.engine\' => \'file_generic\'/', $line ) ) { $pgcache_method = "disk: enhanced"; continue; }
if ( preg_match( '/pgcache\.engine\' => \'apc\'/', $line ) ) { $pgcache_method = "opcode: apc"; continue; }
if ( preg_match( '/minify\.enabled\' => true/', $line ) ) { $minify = true; continue; }
if ( preg_match( '/dbcache\.enabled\' => true/', $line ) ) { $dbcache = true; continue; }
if ( preg_match( '/objectcache\.enabled\' => true/', $line ) ) { $objectcache = true; continue; }
if ( preg_match( '/objectcache\.engine\' => \'file\'/', $line ) ) { $objectcache_method = "disk"; continue; }
if ( preg_match( '/objectcache\.engine\' => \'apc\'/', $line ) ) { $objectcache_method = "apc"; continue; }
if ( preg_match( '/fragmentcache\.enabled\' => true/', $line ) ) { $fragmentcache = true; continue; }
if ( preg_match( '/fragmentcache\.engine\' => \'file\'/', $line ) ) { $fragmentcache_method = "disk"; continue; }
if ( preg_match( '/fragmentcache\.engine\' => \'apc\'/', $line ) ) { $fragmentcache_method = "apc"; continue; }
if ( preg_match( '/browsercache\.enabled\' => true/', $line ) ) { $browsercache = true; continue; }
if ( preg_match( '/cdn\.enabled\' => true/', $line ) ) { $cdn = true; continue; }
}
fclose( $file );
WP_CLI::line( "W3 Total Cache " . $w3tc_version );
WP_CLI::line( "" );
WP_CLI::line( "Page Cache: " . ( isset( $pgcache ) ? "enabled (" . $pgcache_method . ")" : "-" ) );
WP_CLI::line( "Minify: " . ( isset( $minify ) ? "enabled" : "-" ) );
WP_CLI::line( "Database Cache: " . ( isset( $dbcache ) ? "enabled" : "-" ) );
WP_CLI::line( "Object Cache: " . ( isset( $objectcache ) ? "enabled (" . $objectcache_method . ")" : "-" ) );
WP_CLI::line( "Fragment Cache: " . ( isset( $fragmentcache ) ? "enabled (" . $fragmentcache_method . ")" : "-" ) );
WP_CLI::line( "Browser Cache: " . ( isset( $browsercache ) ? "enabled" : "-" ) );
WP_CLI::line( "CDN: " . ( isset( $cdn ) ? "enabled" : "-" ) );
} else if ( isset( $assoc_args['default-settings'] ) ) {
$config = ( file_exists( "/var/run/apache2.pid" ) ) ? "master_apache" : "master_nginx";
if ( file_exists( "/tmp/" . $config ) )
WP_CLI::launch( "rm /tmp/" . $config );
$url = "http://tiagohillebrandt.eti.br/w3tc/" . $w3tc_version . "/" . $config;
WP_CLI::launch( "wget -q " . $url . " -P /tmp/ || echo -n" );
if ( ! file_exists( "/tmp/" . $config ) ) {
WP_CLI::warning( "W3TC version not compatible or server not available at this moment. Please import the settings manually." );
exit();
}
$backup_file = preg_replace( "/https?:\/\/(www.)?/", "", home_url() );
$backup_file = $backup_file . ".master.php";
$backup_file = preg_replace( "/\//", "_", $backup_file );
WP_CLI::launch( "mv " . $w3tc_master . " /root/" . $backup_file );
WP_CLI::line( "Current settings backup: /root/" . $backup_file );
WP_CLI::launch( "rm " . WP_CONTENT_DIR . "/advanced-cache.php 2> /dev/null || echo -n" );
WP_CLI::launch( "rm " . WP_CONTENT_DIR . "/db.php 2> /dev/null || echo -n" );
WP_CLI::launch( "rm " . WP_CONTENT_DIR . "/object-cache.php 2> /dev/null || echo -n" );
WP_CLI::launch( "rm -r " . WP_CONTENT_DIR . "/cache/* 2> /dev/null || echo -n" );
WP_CLI::launch( "rm -r " . WP_CONTENT_DIR . "/w3tc/* 2> /dev/null || echo -n" );
WP_CLI::launch( "mv /tmp/" . $config . " " . $w3tc_master );
WP_CLI::launch( "chown -R $(stat -c %U " . WP_CONTENT_DIR . "):$(stat -c %U " . WP_CONTENT_DIR . ") " . $w3tc_master );
WP_CLI::success( "W3TC recommended settings have been imported." );
}
}
/**
* @subcommand flush-transients
*
*/
function flushtransients( $args, $assoc_args ) {
global $wpdb;
$backup_file = preg_replace( "/https?:\/\/(www.)?/", "", home_url() );
$backup_file = $backup_file . ".options.sql";
$backup_file = preg_replace( "/\//", "_", $backup_file );
WP_CLI::launch( "mysqldump --add-drop-table -u " . DB_USER . " --password=" . DB_PASSWORD . " " . DB_NAME . " $wpdb->options > /root/" . $backup_file );
WP_CLI::line( "Options table backup: /root/" . $backup_file );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_trans%' OR option_name LIKE '_site_%';" );
WP_CLI::success( "Transients have been flushed." );
}
/**
* @synopsis [--loggly]
* @subcommand healthcheck
*
*/
function healthcheck( $args, $assoc_args ) {
list( $name ) = $args;
global $wpdb;
$networkstatus = is_multisite() ? "true" : "false";
$blog_url = get_bloginfo( 'url' );
$blog_version = get_bloginfo ( 'version' );
$domain_name = str_replace( 'http://', '', str_replace( 'https://', '', untrailingslashit( $blog_url ) ) );
$count = self::synth_get_plugin_count();
if ( false === ( $memusage = get_transient( 'synthesis_memory_check' ) ) )
$memusage = 0;
$orphan = "SELECT substring(option_name,1,6) as x,count(*) FROM $wpdb->options group by x HAVING x = '_trans' order by count(*) desc";
$result = $wpdb->get_var ( $orphan, 1, 0 );
$autoloaded_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->options WHERE autoload = 'yes';" );
$autoloaded_size = round( $wpdb->get_var( "SELECT SUM(LENGTH(option_value)) / POWER(1024,2) FROM $wpdb->options WHERE autoload = 'yes';" ), 2 );
$domain = explode( "/", preg_replace( "/^www\./", "", $domain_name ) );
$subsite = str_replace( $domain[0], "", preg_replace( "/^www\./", "", $domain_name ) );
$wpcron = "-";
if ( file_exists( "/var/log/nginx/". $domain[0] . ".access.log" ) )
$wpcron = sizeof( preg_grep( "/" . preg_replace( "/\//", "\\/", $subsite ) . "\/wp-cron\.php/", file( "/var/log/nginx/" . $domain[0] . ".access.log" ) ) );
if ( defined( "DISABLE_WP_CRON" ) )
if ( constant( "DISABLE_WP_CRON" ) )
$wpcron .= " (probably running via crontab)";
$healthdata = array( 'body' => json_encode( array( 'site' => $domain_name, 'wpversion' => $blog_version, 'memusage' => $memusage,
'transients' => $result, 'plugincount' => $count, 'networkstatus' => $networkstatus,
'autoloaded_count' => $autoloaded_count, 'autoloaded_size' => $autoloaded_size,
'wpcron' => $wpcron ) ),
'headers' => array( 'Content-Type' => 'application/json' )
);
if ( empty( $assoc_args['loggly'] ) ) {
WP_CLI::line( "Memory Usage: $memusage" );
WP_CLI::line( "WordPress Version: $blog_version" );
WP_CLI::line( "Transient Variables: $result" );
WP_CLI::line( "Active Plugins: $count" );
WP_CLI::line( "Autoloaded Options Count: $autoloaded_count" );
WP_CLI::line( "Autoloaded Options Size: $autoloaded_size MB" );
WP_CLI::line( "WordPress Cron Calls: $wpcron" );
WP_CLI::line( "Network Status: $networkstatus" );
} else {
wp_remote_post( 'https://logs-01.loggly.com/inputs/11bdfe58-13d0-479c-a81b-b5fa8ee1806b/tag/synthesis-inventory/', $healthdata );
WP_CLI::success( "Data sent to Loggly" );
}
}
private function synth_get_plugin_count() {
$count = count( get_option( 'active_plugins' ) );
if( is_multisite() )
$count += count( get_site_option( 'active_sitewide_plugins', array() ) );
return $count;
}
private function get_crc_dir($dir, $tag){
$files = scandir($dir);
array_shift($files);
array_shift($files);
$dir .= '/';
$ret = array();
foreach($files as $f){
if(is_dir($dir.$f))
$ret = array_merge($this->get_crc_dir($dir.$f, $tag.$f.'/'), $ret);
else
$ret["{$tag}{$f}"] = md5_file($dir.$f);
}
return $ret;
}
/**
* @subcommand verify-wordpress
*
*/
function verify($args, $assoc_args){
$dir = ABSPATH.'/wp-admin';
$ret = $this->get_crc_dir($dir, 'wp-admin/');
$dir = ABSPATH.'/wp-includes';
$ret = array_merge($ret, $this->get_crc_dir($dir, 'wp-includes/'));
$config_file = 'wp' . get_bloginfo('version') . '.php';
$conf_file_path = SYNTHESIS_CHILD_PLUGIN_DIR . 'hashfiles/' . $config_file;
$blog_url = get_bloginfo( 'url' );
$domain_name = str_replace( 'http://', '', str_replace( 'https://', '', untrailingslashit( $blog_url ) ) );
if ( file_exists( $conf_file_path ) ) {
$answers = unserialize( file_get_contents( $conf_file_path ) ) ;
} else {
WP_CLI::warning( "Unsupported WordPress Version" );
return;
}
$corrupt = array();
foreach($answers as $a => $c){
if($c != $ret[$a]) //it's bad
$corrupt[] = $a;
}
if ( empty( $assoc_args['loggly'] ) ) {
if(count($corrupt)){
foreach($corrupt as $f){
WP_CLI::warning( "File Mismatch: $f" );
}
}
} elseif ( isset( $assoc_args['loggly'] ) ) {
$wpverify = array( 'body' => json_encode( array( 'site' => $domain_name, 'verif-status' => "fail" ) ),
'headers' => array( 'Content-Type' => 'application/json' )
);
wp_remote_post( 'https://logs-01.loggly.com/inputs/11bdfe58-13d0-479c-a81b-b5fa8ee1806b/tag/wp-verify/', $wpverify );
WP_CLI::success( "Data sent to Loggly" ) ;
}
WP_CLI::success( "Wordpress Files Verified" );
}
/**
* @subcommand verify-genesis
*
*/
function verify_genesis(){
$genesis_dir = get_theme_root().'/genesis';
if(!file_exists($genesis_dir))
WP_CLI::error( "Genesis directory not found." );
$genesis_data = wp_get_theme('genesis');
$config_file = 'genesis' . $genesis_data->get( 'Version' ) . '.php';
$conf_file_path = SYNTHESIS_CHILD_PLUGIN_DIR . 'hashfiles/' . $config_file;
if ( file_exists( $conf_file_path ) ) {
$answers = unserialize( file_get_contents( $conf_file_path ) ) ;
} else {
WP_CLI::warning( "Unsupported WordPress Version" );
return;
}
$ret = $this->get_crc_dir($genesis_dir, 'genesis/');
foreach($answers as $a => $c){
if($c != $ret[$a]) //it's bad
$corrupt[] = $a;
}
if(count($corrupt)){
foreach($corrupt as $f){
WP_CLI::warning( "File Mismatch: $f" );
}
}
WP_CLI::success( "Framework Files Verified" );
}
}
WP_CLI::add_command( 'synth-ops', 'Synthesis_Ops_Commands' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.