Last active
March 1, 2018 00:13
-
-
Save danielbachhuber/cb7d4bdcf1419c6964ab555c13983922 to your computer and use it in GitHub Desktop.
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
<?php | |
use WP_CLI\Utils; | |
class GutenSnaps { | |
/** | |
* Plugin to test. | |
* | |
* @var string | |
*/ | |
private $plugin; | |
/** | |
* Gutenberg URL to screenshot. | |
* | |
* @var string | |
*/ | |
private $gutenberg_url; | |
/** | |
* Classic editor URL to screenshot. | |
* | |
* @var string | |
*/ | |
private $classic_url; | |
/** | |
* URL for the Puppet Photo Booth installation. | |
* | |
* @var string | |
*/ | |
private $ppb_url; | |
/** | |
* Directory to put the screenshots into | |
* | |
* @var integer | |
*/ | |
private $screenshots_dir; | |
/** | |
* Start time for script execution. | |
* | |
* @var integer | |
*/ | |
private $start_time; | |
/** | |
* Create before/after snapshots of a given plugin in the editor. | |
* | |
* ## OPTIONS | |
* | |
* <plugin> | |
* : WordPress.org slug for the plugin to install/activate. | |
* | |
* [--post_id=<id>] | |
* : Post ID to test with. | |
* --- | |
* default: 1 | |
* --- | |
* | |
* [--ppb_url=<url>] | |
* : Url for the Puppet Photo Booth installation. | |
* --- | |
* default: http://localhost:8080 | |
* --- | |
* | |
* [--screenshots_dir=<dir>] | |
* : Directory to put the screenshots in. | |
* --- | |
* default: screenshots | |
* --- | |
*/ | |
public function __invoke( $args, $assoc_args ) { | |
list( $this->plugin ) = $args; | |
$this->start_time = microtime( true ); | |
$this->ppb_url = rtrim( $assoc_args['ppb_url'], '/' ); | |
$this->screenshots_dir = rtrim( $assoc_args['screenshots_dir'], '/' ); | |
if ( ! is_dir( $this->screenshots_dir ) ) { | |
wp_mkdir_p( $this->screenshots_dir ); | |
} | |
$this->gutenberg_url = add_query_arg( array( | |
'action' => 'edit', | |
'post' => $assoc_args['post_id'], | |
), admin_url( 'post.php' ) ); | |
$this->classic_url = add_query_arg( 'classic-editor', 1, $this->gutenberg_url ); | |
self::runcommand_silently( "plugin install {$this->plugin}" ); | |
self::take_screenshots( 'before' ); | |
self::runcommand_silently( "plugin activate {$this->plugin}" ); | |
$plugin_data = WP_CLI::runcommand( "plugin get {$this->plugin} --format=json", array( | |
'return' => true, | |
'parse' => 'json', | |
) ); | |
WP_CLI::log( "Installed and activated {$plugin_data['title']} ({$plugin_data['version']})" ); | |
WP_CLI::log( " -> [{$plugin_data['title']}](https://wordpress.org/plugins/{$this->plugin}/)" ); | |
self::take_screenshots( 'after' ); | |
self::runcommand_silently( "plugin deactivate {$this->plugin}" ); | |
self::runcommand_silently( "plugin delete {$this->plugin}" ); | |
self::calculate_difference( 'classic' ); | |
self::calculate_difference( 'gutenberg' ); | |
$total_time = round( microtime( true ) - $this->start_time, 3 ); | |
WP_CLI::success( "Gutensnaps complete ({$total_time}s)." ); | |
} | |
/** | |
* Run a command silently unless there's an error | |
* | |
* @param string $cmd Command to run. | |
*/ | |
private static function runcommand_silently( $cmd ) { | |
WP_CLI::log( 'Running: ' . $cmd ); | |
$ret = WP_CLI::runcommand( $cmd, array( | |
'return' => 'all', | |
'launch' => true, | |
'exit_error' => false, | |
) ); | |
if ( 0 !== $ret->return_code ) { | |
WP_CLI::log( $ret->stderr ); | |
exit( $ret->return_code ); | |
} | |
} | |
/** | |
* Take screenshots of the Gutenberg and classic editors | |
* | |
* @param string $when Whether this is 'before' or 'after'. | |
* @return array | |
*/ | |
private function take_screenshots( $when ) { | |
WP_CLI::log( "Taking {$when} screenshots..." ); | |
$urls = array( | |
'classic' => $this->classic_url, | |
'gutenberg' => $this->gutenberg_url, | |
); | |
$paths = array(); | |
foreach ( $urls as $type => $url ) { | |
$path = ABSPATH . "{$this->screenshots_dir}/{$this->plugin}-{$type}-{$when}.png"; | |
$snapshot_url = add_query_arg( array( | |
'url' => rawurlencode( $url ), | |
), $this->ppb_url . '/v1/fetch' ); | |
$response = Utils\http_request( 'GET', $snapshot_url, null, array(), array( | |
'filename' => $path, | |
) ); | |
if ( 200 !== $response->status_code ) { | |
WP_CLI::error( "Error fetching {$type} screenshot" ); | |
} | |
$paths[ $type ] = $path; | |
} | |
return $paths; | |
} | |
/** | |
* Compare differences between two screenshots | |
* | |
* @param string $type Type of screenshot comparison. | |
*/ | |
private function calculate_difference( $type ) { | |
WP_CLI::log( "Calculating {$type} difference..." ); | |
$before = home_url( "{$this->screenshots_dir}/{$this->plugin}-{$type}-before.png" ); | |
$after = home_url( "{$this->screenshots_dir}/{$this->plugin}-{$type}-after.png" ); | |
$path = ABSPATH . "{$this->screenshots_dir}/{$this->plugin}-{$type}-compare.png"; | |
$request_url = add_query_arg( array( | |
'imageA' => rawurlencode( $before ), | |
'imageB' => rawurlencode( $after ), | |
), $this->ppb_url . '/v1/compare' ); | |
$response = Utils\http_request( 'GET', $request_url, null, array(), array( | |
'filename' => $path, | |
) ); | |
if ( 200 !== $response->status_code ) { | |
WP_CLI::error( "Error fetching {$type} screenshot" ); | |
} | |
$percent_diff = round( $response->headers['X-PPB-Percent-Diff'], 3 ); | |
WP_CLI::log( " -> Percent difference: {$percent_diff}%" ); | |
} | |
} | |
WP_CLI::add_command( 'gutensnaps', 'GutenSnaps' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is cool