Skip to content

Commit

Permalink
Merge pull request #49 from kopepasah/fix/33-phpcs-errors
Browse files Browse the repository at this point in the history
Fixes PHPCS Errors based on WPCS
  • Loading branch information
danielbachhuber authored Oct 30, 2019
2 parents 50e2d62 + daa72bf commit eea05b4
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 120 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"phpstan/phpstan-shim": "^0.11.12",
"phpunit/phpunit": "^7",
"szepeviktor/phpstan-wordpress": "dev-master",
"wp-coding-standards/wpcs": "^2.1"
"wp-coding-standards/wpcs": "^2.1",
"phpcompatibility/php-compatibility": "^9.3"
},
"autoload-dev": {
"psr-4": {
Expand Down
60 changes: 59 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 25 additions & 17 deletions inc/class-api.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* API Class
*
* @package Pantheon HUD
*/

namespace Pantheon\HUD;

Expand All @@ -14,8 +19,6 @@ class API {
*/
const API_URL_BASE = 'https://api.live.getpantheon.com:8443';

private static $endpoint_url = 'https://api.live.getpantheon.com:8443/sites/self/state';

/**
* Holds the domains data when present.
*
Expand Down Expand Up @@ -65,6 +68,7 @@ public function get_last_code_push_timestamp() {
/**
* Get the primary url for the environment
*
* @param string $env Environment to fetch the domains of.
* @return string
*/
public function get_primary_environment_url( $env ) {
Expand All @@ -82,8 +86,8 @@ public function get_primary_environment_url( $env ) {
* @return array
*/
public function get_environment_details() {
$env = ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ? $_ENV['PANTHEON_ENVIRONMENT'] : 'dev';
$details = array(
$env = ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ? $_ENV['PANTHEON_ENVIRONMENT'] : 'dev';
$details = array(
'web' => array(),
'database' => array(),
);
Expand All @@ -93,7 +97,7 @@ public function get_environment_details() {
}
$php_version = $this->get_php_version();
if ( $php_version ) {
$php_version = (string) $php_version;
$php_version = (string) $php_version;
$details['web']['php_version'] = 'PHP ' . $php_version;
}
if ( ! empty( $environment_settings['dbserver'] ) ) {
Expand Down Expand Up @@ -125,7 +129,7 @@ private function get_domains_data( $env ) {
return $this->domains_data[ $env ];
}
if ( ! empty( $env ) ) {
$url = sprintf( '%s/sites/self/environments/%s/domains', self::API_URL_BASE, $env );
$url = sprintf( '%s/sites/self/environments/%s/domains', self::API_URL_BASE, $env );
$this->domains_data[ $env ] = self::fetch_api_data( $url );
} else {
$this->domains_data[ $env ] = [];
Expand All @@ -143,7 +147,7 @@ private function get_environment_settings_data() {
return $this->environment_settings_data;
}
if ( ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
$url = sprintf( '%s/sites/self/environments/%s/settings', self::API_URL_BASE, $_ENV['PANTHEON_ENVIRONMENT'] );
$url = sprintf( '%s/sites/self/environments/%s/settings', self::API_URL_BASE, $_ENV['PANTHEON_ENVIRONMENT'] );
$this->environment_settings_data = self::fetch_api_data( $url );
} else {
$this->environment_settings_data = [];
Expand All @@ -154,31 +158,35 @@ private function get_environment_settings_data() {
/**
* Fetch data from a given Pantheon API URL.
*
* @param string $url URL from which to fetch data.
* @return array
*/
private function fetch_api_data( $url ) {

// Function internal to Pantheon infrastructure
// Function internal to Pantheon infrastructure.
$pem_file = apply_filters( 'pantheon_hud_pem_file', null );
if ( function_exists( 'pantheon_curl' ) ) {
/** @var string[] */
$bits = parse_url( $url );
$bits = wp_parse_url( $url );
$response = pantheon_curl( sprintf( '%s://%s%s', $bits['scheme'], $bits['host'], $bits['path'] ), null, $bits['port'] );
$body = ! empty( $response['body'] ) ? $response['body'] : '';
$body = ! empty( $response['body'] ) ? $response['body'] : '';
return json_decode( $body, true );
// for those developing locally who know what they're doing
} else if ( $pem_file || ( defined( 'PANTHEON_HUD_PHPUNIT_RUNNING' ) && PANTHEON_HUD_PHPUNIT_RUNNING ) ) {

// For those developing locally who know what they're doing.
} elseif ( $pem_file || ( defined( 'PANTHEON_HUD_PHPUNIT_RUNNING' ) && PANTHEON_HUD_PHPUNIT_RUNNING ) ) {
$require_curl = function() {
return array( 'curl' );
};
add_filter( 'http_api_transports', $require_curl );
$client_cert = function( $handle ) use ( $pem_file ) {
curl_setopt( $handle, CURLOPT_SSLCERT, $pem_file );
curl_setopt( $handle, CURLOPT_SSLCERT, $pem_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions
};
add_action( 'http_api_curl', $client_cert );
$response = wp_remote_get( $url, array(
'sslverify' => false, // yolo
) );
$response = wp_remote_get(
$url,
array(
'sslverify' => false, // yolo.
)
);
if ( is_wp_error( $response ) ) {
return array();
}
Expand Down
112 changes: 74 additions & 38 deletions inc/class-toolbar.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Toolbar Class
*
* @package Pantheon HUD
*/

namespace Pantheon\HUD;

Expand All @@ -8,63 +13,83 @@
*/
class Toolbar {

/**
* Class Instance.
*
* @var $instance
*/
private static $instance;

/**
* Singleton
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
self::$instance = new self();
self::$instance->setup_actions();
}
return self::$instance;
}

/**
* Setup Actions
*/
private function setup_actions() {
add_action( 'admin_bar_menu', array( $this, 'action_admin_bar_menu' ), 100 );
add_action( 'wp_enqueue_scripts', array( $this, 'add_admin_bar_inline_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_bar_inline_styles' ) );
}

/**
* Hook into Admin Bar
*
* @param object $wp_admin_bar The Admin Bar Object.
*/
public function action_admin_bar_menu( $wp_admin_bar ) {
$api = new API;
$name = $api->get_site_name();
$api = new API();
$name = $api->get_site_name();
$site_id = $api->get_site_id();
$env = $this->get_environment();
$title = '<img src="' . esc_url( plugins_url( 'assets/img/pantheon-fist-color.svg', PANTHEON_HUD_ROOT_FILE ) ) . '" width="32" height="32" />';
$bits = array();
$env = $this->get_environment();
$title = '<img src="' . esc_url( plugins_url( 'assets/img/pantheon-fist-color.svg', PANTHEON_HUD_ROOT_FILE ) ) . '" width="32" height="32" />';
$bits = array();
if ( $name ) {
$bits[] = $name;
}
$bits[] = $env;
$title .= ' ' . esc_html( strtolower( implode( ':', $bits ) ) );
$wp_admin_bar->add_node( array(
'id' => 'pantheon-hud',
'href' => false,
'title' => $title,
) );
$wp_admin_bar->add_node(
array(
'id' => 'pantheon-hud',
'href' => false,
'title' => $title,
)
);

$env_admins = '';
# TODO: List envs from API to include Multidev.
foreach( array( 'dev', 'test', 'live' ) as $e ) {
// TODO: List envs from API to include Multidev.
foreach ( array( 'dev', 'test', 'live' ) as $e ) {
$url = $api->get_primary_environment_url( $e );
if ( $url ) {
$env_admins .= '<a target="_blank" href="' . esc_url( rtrim( $url ) . '/wp-admin/' ) . '">' . esc_html( $e ) . '</a> | ';
}
}

if ( ! empty( $env_admins ) ) {
$wp_admin_bar->add_node( array(
'id' => 'pantheon-hud-wp-admin-links',
'parent' => 'pantheon-hud',
'href' => false,
'title' => '<em>wp-admin links</em><br />' . rtrim( $env_admins, ' |' ),
) );
$wp_admin_bar->add_node(
array(
'id' => 'pantheon-hud-wp-admin-links',
'parent' => 'pantheon-hud',
'href' => false,
'title' => '<em>wp-admin links</em><br />' . rtrim( $env_admins, ' |' ),
)
);
}

$environment_details = $api->get_environment_details();
if ( $environment_details ) {
$details_html = array();
if ( isset( $environment_details['web']['appserver_count'] ) ) {
$pluralize = $environment_details['web']['appserver_count'] > 1 ? 's' : '';
$pluralize = $environment_details['web']['appserver_count'] > 1 ? 's' : '';
$web_detail = $environment_details['web']['appserver_count'] . ' app container' . $pluralize;
if ( isset( $environment_details['web']['php_version'] ) ) {
$web_detail .= ' running ' . $environment_details['web']['php_version'];
Expand All @@ -81,34 +106,40 @@ public function action_admin_bar_menu( $wp_admin_bar ) {
}
if ( ! empty( $details_html ) ) {
$details_html = '<em>' . esc_html__( 'Environment Details', 'pantheon-hud' ) . '</em><br /> - ' . implode( '<br /> - ', $details_html );
$wp_admin_bar->add_node( array(
'id' => 'pantheon-hud-environment-details',
'parent' => 'pantheon-hud',
'title' => $details_html,
) );
$wp_admin_bar->add_node(
array(
'id' => 'pantheon-hud-environment-details',
'parent' => 'pantheon-hud',
'title' => $details_html,
)
);
}
}

if ( $name && $env ) {
$wp_cli_stub = sprintf( 'terminus wp %s.%s', $name, $env );
$wp_admin_bar->add_node( array(
'id' => 'pantheon-hud-wp-cli-stub',
'parent' => 'pantheon-hud',
'title' => '<em>' . esc_html__( 'WP-CLI via Terminus', 'pantheon-hud' ) . '</em><br /><input value="' . esc_attr( $wp_cli_stub ) . '">',
) );
$wp_admin_bar->add_node(
array(
'id' => 'pantheon-hud-wp-cli-stub',
'parent' => 'pantheon-hud',
'title' => '<em>' . esc_html__( 'WP-CLI via Terminus', 'pantheon-hud' ) . '</em><br /><input value="' . esc_attr( $wp_cli_stub ) . '">',
)
);
}

if ( $site_id && $env ) {
$dashboard_link = sprintf( 'https://dashboard.pantheon.io/sites/%s#%s/code', $site_id, $env );
$wp_admin_bar->add_node( array(
'id' => 'pantheon-hud-dashboard-link',
'parent' => 'pantheon-hud',
'href' => $dashboard_link,
'title' => esc_html__( 'Visit Pantheon Dashboard', 'pantheon-hud' ),
'meta' => array(
'target' => '_blank',
$wp_admin_bar->add_node(
array(
'id' => 'pantheon-hud-dashboard-link',
'parent' => 'pantheon-hud',
'href' => $dashboard_link,
'title' => esc_html__( 'Visit Pantheon Dashboard', 'pantheon-hud' ),
'meta' => array(
'target' => '_blank',
),
) );
)
);
}

}
Expand Down Expand Up @@ -164,6 +195,11 @@ public function add_admin_bar_inline_styles() {
wp_add_inline_style( 'admin-bar', str_replace( array( '<style>', '</style>' ), '', ob_get_clean() ) );
}

/**
* Get Pantheon Env.
*
* @return string Pantheon environment or 'local'.
*/
private function get_environment() {
return ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ? $_ENV['PANTHEON_ENVIRONMENT'] : 'local';
}
Expand Down
Loading

0 comments on commit eea05b4

Please sign in to comment.